You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

преди 4 години
  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. });