| 1 | var Browserify = require('browserify');
|
|---|
| 2 | var bresolve = require('browser-resolve');
|
|---|
| 3 | patchResolve();
|
|---|
| 4 |
|
|---|
| 5 | module.exports = function (grunt) {
|
|---|
| 6 | grunt.initConfig({
|
|---|
| 7 | pkg: grunt.file.readJSON('package.json'),
|
|---|
| 8 | outputFolder: ".",
|
|---|
| 9 |
|
|---|
| 10 | browserify: {
|
|---|
| 11 | main: {
|
|---|
| 12 | src: ['index.js'],
|
|---|
| 13 | dest: '<%= outputFolder %>/<%= pkg.name %>.js',
|
|---|
| 14 | options: {
|
|---|
| 15 | browserifyOptions: { standalone: '<%= pkg.name %>' },
|
|---|
| 16 | banner: '/*! <%= pkg.name %> <%= pkg.version %> */\n',
|
|---|
| 17 | alias: {
|
|---|
| 18 | "jsonpath": "./index.js"
|
|---|
| 19 | },
|
|---|
| 20 | require: [
|
|---|
| 21 | /**
|
|---|
| 22 | * When running in Node, we require('./aesprim') and that module takes care of monkey-patching esprima
|
|---|
| 23 | * using resolve, path finding, etc...
|
|---|
| 24 | * Anyways, Browserify doesn't support "resolve", so we need to trick the module. We'll actually be
|
|---|
| 25 | * returning a verbatim, non-modified "esprima" when the code runs require('./aesprim').
|
|---|
| 26 | * That is ok because we will modify the "esprima" source code right after the bundle process, via
|
|---|
| 27 | * the postBundleCB callback.
|
|---|
| 28 | */
|
|---|
| 29 | ["esprima", {expose: "./aesprim"}]
|
|---|
| 30 | ],
|
|---|
| 31 | ignore: [
|
|---|
| 32 | 'file',
|
|---|
| 33 | 'system',
|
|---|
| 34 | 'source-map',
|
|---|
| 35 | 'estraverse',
|
|---|
| 36 | 'escodegen',
|
|---|
| 37 | 'underscore',
|
|---|
| 38 | 'reflect',
|
|---|
| 39 | 'JSONSelect',
|
|---|
| 40 | './lib/aesprim.js'
|
|---|
| 41 | //'assert' //can't remove because of lib/index.js,
|
|---|
| 42 | ],
|
|---|
| 43 | postBundleCB: function(err, src, next) {
|
|---|
| 44 | /**
|
|---|
| 45 | * This is ugly, but we need to make "esprima" understand '@' as a valid character.
|
|---|
| 46 | * It's either this or bundle a copy of the library with those few bytes of changes.
|
|---|
| 47 | */
|
|---|
| 48 | src = src.toString("utf8").replace(/(function isIdentifierStart\(ch\) {\s+return)/m, '$1 (ch == 0x40) || ');
|
|---|
| 49 | next(err, new Buffer(src, "utf8"));
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | },
|
|---|
| 54 |
|
|---|
| 55 | uglify: {
|
|---|
| 56 | options: {
|
|---|
| 57 | banner: '/*! <%= pkg.name %> <%= pkg.version %> */\n'
|
|---|
| 58 | },
|
|---|
| 59 | build: {
|
|---|
| 60 | src: '<%= outputFolder %>/<%= pkg.name %>.js',
|
|---|
| 61 | dest: '<%= outputFolder %>/<%= pkg.name %>.min.js'
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | });
|
|---|
| 66 |
|
|---|
| 67 | grunt.loadNpmTasks('grunt-browserify');
|
|---|
| 68 | grunt.loadNpmTasks('grunt-contrib-uglify')
|
|---|
| 69 | grunt.registerTask('default', ['browserify', 'uglify']);
|
|---|
| 70 |
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | function patchResolve() {
|
|---|
| 74 | var _createDeps = Browserify.prototype._createDeps;
|
|---|
| 75 | Browserify.prototype._createDeps = function() {
|
|---|
| 76 | var returnValue = _createDeps.apply(this, arguments);
|
|---|
| 77 | this._bresolve = function(id, opts, cb) {
|
|---|
| 78 | opts.browser = 'alias';
|
|---|
| 79 | return bresolve(id, opts, cb);
|
|---|
| 80 | };
|
|---|
| 81 | return returnValue;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|