bundle.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 outputBundle = outputFile + '.bundle.js';
  25. fs.writeFileSync(outputBundle, buf);
  26. var uglifyOpts = {
  27. warnings: true,
  28. compress: {},
  29. output: {
  30. preamble: '/* ' + json.name + ' ' + json.version + ': ' + json.description + ' */'
  31. }
  32. };
  33. if (compress) {
  34. var compressOpts = compress.split(',');
  35. for (var i=0; i<compressOpts.length; ++i) {
  36. var pair = compressOpts[i].split('=');
  37. uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] != 'false';
  38. }
  39. }
  40. if (standalone) uglifyOpts.outSourceMap = json.name + '.min.js.map';
  41. var result = uglify.minify(outputBundle, uglifyOpts);
  42. fs.writeFileSync(outputFile + '.min.js', result.code);
  43. if (result.map) fs.writeFileSync(outputFile + '.min.js.map', result.map);
  44. if (!standalone) fs.unlinkSync(outputBundle);
  45. });