Gruntfile.js 3.2 KB

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