build.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // const typescript = require('@rollup/plugin-typescript');
  2. const typescript = require('rollup-plugin-typescript2');
  3. const replace = require('@rollup/plugin-replace');
  4. const rollup = require('rollup');
  5. const path = require('path');
  6. const processs = require('process');
  7. const chalk = require('chalk');
  8. const progress = require('./progress');
  9. const UglifyJS = require('uglify-js');
  10. const fs = require('fs');
  11. function current() {
  12. return (new Date()).toLocaleString();
  13. }
  14. function createInputOption(env, isWatch) {
  15. return {
  16. input: path.resolve(__dirname, '../index.ts'),
  17. plugins: [
  18. typescript({
  19. clean: !isWatch,
  20. tsconfigOverride: {
  21. compilerOptions: {
  22. // Rollup don't use CommonJS by default.
  23. module: 'ES2015',
  24. sourceMap: true,
  25. // Use the esm d.ts
  26. declaration: false
  27. }
  28. }
  29. }),
  30. replace({
  31. preventAssignment: true,
  32. 'process.env.NODE_ENV': JSON.stringify(env)
  33. }),
  34. progress({
  35. scope: {
  36. total: 0
  37. }
  38. })
  39. ]
  40. };
  41. }
  42. const outputOption = {
  43. format: 'umd',
  44. file: path.resolve(__dirname, '../dist/zrender.js'),
  45. sourcemap: true,
  46. name: 'zrender'
  47. };
  48. function minify(outPath) {
  49. const code = fs.readFileSync(outPath, 'utf-8');
  50. const uglifyResult = UglifyJS.minify(code);
  51. if (uglifyResult.error) {
  52. throw new Error(uglifyResult.error);
  53. }
  54. fs.writeFileSync(outPath, uglifyResult.code, 'utf-8');
  55. }
  56. if (processs.argv.includes('--watch')) {
  57. const watcher = rollup.watch({
  58. ...createInputOption('development', true),
  59. output: [outputOption],
  60. watch: {
  61. clearScreen: true
  62. }
  63. });
  64. watcher.on('event', event => {
  65. switch(event.code) {
  66. // case 'START':
  67. // console.log(chalk.green('Begin to watch'));
  68. // break;
  69. case 'BUNDLE_START':
  70. console.log(
  71. chalk.gray(current()),
  72. chalk.blue('File changed. Begin to bundle')
  73. );
  74. break;
  75. case 'BUNDLE_END':
  76. console.log(
  77. chalk.gray(current()),
  78. chalk.green('Finished bundle')
  79. );
  80. break;
  81. case 'ERROR':
  82. console.log(
  83. chalk.gray(current()),
  84. chalk.red(event.error)
  85. );
  86. break;
  87. }
  88. });
  89. }
  90. else {
  91. // Unminified
  92. rollup.rollup({
  93. ...createInputOption('development', false)
  94. }).then(bundle => {
  95. bundle.write(outputOption)
  96. .then(() => {
  97. // Minified
  98. if (process.argv.indexOf('--minify') >= 0) {
  99. rollup.rollup({
  100. ...createInputOption('production', false)
  101. }).then(bundle => {
  102. const file = outputOption.file.replace(/.js$/, '.min.js');
  103. bundle.write(Object.assign(outputOption, {
  104. file,
  105. sourcemap: false
  106. })).then(function () {
  107. minify(file);
  108. });
  109. });
  110. }
  111. });
  112. });
  113. }