bundle.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var fs = require('fs')
  3. , path = require('path')
  4. , browserify = require('browserify')
  5. , uglify = require('uglify-js');
  6. var pkg = process.argv[2]
  7. , standalone = process.argv[3]
  8. , compress = process.argv[4];
  9. var packageDir = path.join(__dirname, '..');
  10. if (pkg != '.') packageDir = path.join(packageDir, 'node_modules', pkg);
  11. var json = require(path.join(packageDir, 'package.json'));
  12. var distDir = path.join(__dirname, '..', 'dist');
  13. if (!fs.existsSync(distDir)) fs.mkdirSync(distDir);
  14. var bOpts = {};
  15. if (standalone) bOpts.standalone = standalone;
  16. browserify(bOpts)
  17. .require(path.join(packageDir, json.main), {expose: json.name})
  18. .bundle(function (err, buf) {
  19. if (err) {
  20. console.error('browserify error:', err);
  21. process.exit(1);
  22. }
  23. var outputFile = path.join(distDir, json.name);
  24. var uglifyOpts = {
  25. warnings: true,
  26. compress: {},
  27. output: {
  28. preamble: '/* ' + json.name + ' ' + json.version + ': ' + json.description + ' */'
  29. }
  30. };
  31. if (compress) {
  32. var compressOpts = compress.split(',');
  33. for (var i=0, il = compressOpts.length; i<il; ++i) {
  34. var pair = compressOpts[i].split('=');
  35. uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] != 'false';
  36. }
  37. }
  38. if (standalone) {
  39. uglifyOpts.sourceMap = {
  40. filename: json.name + '.min.js',
  41. url: json.name + '.min.js.map'
  42. };
  43. }
  44. var result = uglify.minify(buf.toString(), uglifyOpts);
  45. fs.writeFileSync(outputFile + '.min.js', result.code);
  46. if (result.map) fs.writeFileSync(outputFile + '.min.js.map', result.map);
  47. if (standalone) fs.writeFileSync(outputFile + '.bundle.js', buf);
  48. if (result.warnings) {
  49. for (var j=0, jl = result.warnings.length; j<jl; ++j)
  50. console.warn('UglifyJS warning:', result.warnings[j]);
  51. }
  52. });