optimize.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var UglifyJS = require('uglify-js');
  2. var fs = require('fs');
  3. var etpl = require('etpl');
  4. var argv = require('optimist').argv;
  5. etpl.config({
  6. commandOpen: '/**',
  7. commandClose: '*/'
  8. });
  9. var mode = argv.m || 'all';
  10. var configPath = mode === 'all' ? 'config/echarts.js' : 'config/echarts.' + mode + '.js';
  11. var outPath = mode === 'all' ? '../dist/echarts.js' : '../dist/echarts.' + mode + '.js';
  12. var config = eval('(' + fs.readFileSync(configPath, 'utf-8') + ')');
  13. var mainCode = fs.readFileSync(outPath, 'utf-8');
  14. var startCode = fs.readFileSync('wrap/start.js', 'utf-8');
  15. var nutCode = fs.readFileSync('wrap/nut.js', 'utf-8');
  16. var endCode = fs.readFileSync('wrap/end.js', 'utf-8');
  17. endCode = etpl.compile(endCode)({
  18. parts: config.include
  19. });
  20. // FIXME
  21. var sourceCode = [startCode, nutCode, require('./mangleString')(mainCode), endCode].join('\n');
  22. var ast = UglifyJS.parse(sourceCode);
  23. /* jshint camelcase: false */
  24. // compressor needs figure_out_scope too
  25. ast.figure_out_scope();
  26. ast = ast.transform(UglifyJS.Compressor( {} ));
  27. // need to figure out scope again so mangler works optimally
  28. ast.figure_out_scope();
  29. ast.compute_char_frequency();
  30. ast.mangle_names();
  31. fs.writeFileSync(outPath, [startCode, nutCode, mainCode, endCode].join('\n'), 'utf-8');
  32. fs.writeFileSync(outPath.replace('.js', '.min.js'), ast.print_to_string(), 'utf-8');