optimize.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var UglifyJS = require('uglify-js');
  20. var fs = require('fs');
  21. var etpl = require('etpl');
  22. var argv = require('optimist').argv;
  23. etpl.config({
  24. commandOpen: '/**',
  25. commandClose: '*/'
  26. });
  27. var mode = argv.m || 'all';
  28. var configPath = mode === 'all' ? 'config/echarts.js' : 'config/echarts.' + mode + '.js';
  29. var outPath = mode === 'all' ? '../dist/echarts.js' : '../dist/echarts.' + mode + '.js';
  30. var config = eval('(' + fs.readFileSync(configPath, 'utf-8') + ')');
  31. var mainCode = fs.readFileSync(outPath, 'utf-8');
  32. var startCode = fs.readFileSync('wrap/start.js', 'utf-8');
  33. var nutCode = fs.readFileSync('wrap/nut.js', 'utf-8');
  34. var endCode = fs.readFileSync('wrap/end.js', 'utf-8');
  35. endCode = etpl.compile(endCode)({
  36. parts: config.include
  37. });
  38. // FIXME
  39. var sourceCode = [startCode, nutCode, require('./mangleString')(mainCode), endCode].join('\n');
  40. var ast = UglifyJS.parse(sourceCode);
  41. /* jshint camelcase: false */
  42. // compressor needs figure_out_scope too
  43. ast.figure_out_scope();
  44. ast = ast.transform(UglifyJS.Compressor( {} ));
  45. // need to figure out scope again so mangler works optimally
  46. ast.figure_out_scope();
  47. ast.compute_char_frequency();
  48. ast.mangle_names();
  49. fs.writeFileSync(outPath, [startCode, nutCode, mainCode, endCode].join('\n'), 'utf-8');
  50. fs.writeFileSync(outPath.replace('.js', '.min.js'), ast.print_to_string(), 'utf-8');