build.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env node
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. const fs = require('fs');
  21. const config = require('./config.js');
  22. const commander = require('commander');
  23. const chalk = require('chalk');
  24. const rollup = require('rollup');
  25. const prePublish = require('./pre-publish');
  26. const transformDEV = require('./transform-dev');
  27. const preamble = require('./preamble');
  28. async function run() {
  29. /**
  30. * Tips for `commander`:
  31. * (1) If arg xxx not specified, `commander.xxx` is undefined.
  32. * Otherwise:
  33. * If '-x, --xxx', `commander.xxx` can only be true/false, even if '--xxx yyy' input.
  34. * If '-x, --xxx <some>', the 'some' string is required, or otherwise error will be thrown.
  35. * If '-x, --xxx [some]', the 'some' string is optional, that is, `commander.xxx` can be boolean or string.
  36. * (2) `node ./build/build.js --help` will print helper info and exit.
  37. */
  38. let descIndent = ' ';
  39. let egIndent = ' ';
  40. commander
  41. .usage('[options]')
  42. .description([
  43. 'Build echarts and generate result files in directory `echarts/dist`.',
  44. '',
  45. ' For example:',
  46. '',
  47. egIndent + 'node build/build.js --prepublish'
  48. + '\n' + descIndent + '# Only prepublish.',
  49. egIndent + 'node build/build.js --type ""'
  50. + '\n' + descIndent + '# Only generate `dist/echarts.js`.',
  51. egIndent + 'node build/build.js --type common --min'
  52. + '\n' + descIndent + '# Only generate `dist/echarts.common.min.js`.',
  53. egIndent + 'node build/build.js --type simple --min'
  54. + '\n' + descIndent + '# Only generate `dist/echarts-en.simple.min.js`.',
  55. ].join('\n'))
  56. .option(
  57. '--prepublish',
  58. 'Build all for release'
  59. )
  60. .option(
  61. '--min',
  62. 'Whether to compress the output file, and remove error-log-print code.'
  63. )
  64. .option(
  65. '--type <type name>', [
  66. 'Can be "simple" or "common" or "all" (default). Or can be simple,common,all to build multiple. For example,',
  67. descIndent + '`--type ""` or `--type "common"`.'
  68. ].join('\n'))
  69. .option(
  70. '--format <format>',
  71. 'The format of output bundle. Can be "umd", "amd", "iife", "cjs", "esm".'
  72. )
  73. .parse(process.argv);
  74. let isPrePublish = !!commander.prepublish;
  75. let buildType = commander.type || 'all';
  76. let opt = {
  77. min: commander.min,
  78. format: commander.format || 'umd'
  79. };
  80. validateIO(opt.input, opt.output);
  81. if (isPrePublish) {
  82. await prePublish();
  83. }
  84. else if (buildType === 'extension') {
  85. const cfgs = [
  86. config.createBMap(opt),
  87. config.createDataTool(opt)
  88. ];
  89. await build(cfgs);
  90. }
  91. else if (buildType === 'myTransform') {
  92. const cfgs = [
  93. config.createMyTransform(opt)
  94. ];
  95. await build(cfgs);
  96. }
  97. else {
  98. const types = buildType.split(',').map(a => a.trim());
  99. const cfgs = types.map(type =>
  100. config.createECharts({
  101. ...opt,
  102. type
  103. })
  104. );
  105. await build(cfgs);
  106. }
  107. }
  108. function checkBundleCode(cfg) {
  109. // Make sure process.env.NODE_ENV is eliminated.
  110. for (let output of cfg.output) {
  111. let code = fs.readFileSync(output.file, {encoding: 'utf-8'});
  112. if (!code) {
  113. throw new Error(`${output.file} is empty`);
  114. }
  115. transformDEV.recheckDEV(code);
  116. console.log(chalk.green.dim('Check code: correct.'));
  117. }
  118. }
  119. function validateIO(input, output) {
  120. if ((input != null && output == null)
  121. || (input == null && output != null)
  122. ) {
  123. throw new Error('`input` and `output` must be both set.');
  124. }
  125. }
  126. /**
  127. * @param {Array.<Object>} configs A list of rollup configs:
  128. * See: <https://rollupjs.org/#big-list-of-options>
  129. * For example:
  130. * [
  131. * {
  132. * ...inputOptions,
  133. * output: [outputOptions],
  134. * },
  135. * ...
  136. * ]
  137. */
  138. async function build(configs) {
  139. console.log(chalk.yellow(`
  140. NOTICE: If you are using 'npm run build'. Run 'npm run prepublish' before build !!!
  141. `));
  142. console.log(chalk.yellow(`
  143. NOTICE: If you are using syslink on zrender. Run 'npm run prepublish' in zrender first !!
  144. `));
  145. for (let singleConfig of configs) {
  146. console.log(
  147. chalk.cyan.dim('\Bundling '),
  148. chalk.cyan(singleConfig.input)
  149. );
  150. console.time('rollup build');
  151. const bundle = await rollup.rollup(singleConfig);
  152. for (let output of singleConfig.output) {
  153. console.log(
  154. chalk.green.dim('Created '),
  155. chalk.green(output.file),
  156. chalk.green.dim(' successfully.')
  157. );
  158. await bundle.write(output);
  159. };
  160. console.timeEnd('rollup build');
  161. checkBundleCode(singleConfig);
  162. }
  163. }
  164. async function main() {
  165. try {
  166. await run();
  167. }
  168. catch (err) {
  169. console.log(chalk.red('BUILD ERROR!'));
  170. // rollup parse error.
  171. if (err) {
  172. if (err.loc) {
  173. console.warn(chalk.red(`${err.loc.file} (${err.loc.line}:${err.loc.column})`));
  174. console.warn(chalk.red(err.message));
  175. }
  176. if (err.frame) {
  177. console.warn(chalk.red(err.frame));
  178. }
  179. console.log(chalk.red(err ? err.stack : err));
  180. err.id != null && console.warn(chalk.red(`id: ${err.id}`));
  181. err.hook != null && console.warn(chalk.red(`hook: ${err.hook}`));
  182. err.code != null && console.warn(chalk.red(`code: ${err.code}`));
  183. err.plugin != null && console.warn(chalk.red(`plugin: ${err.plugin}`));
  184. }
  185. // console.log(err);
  186. }
  187. }
  188. main();