You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2020/07/28 06:46:43 UTC

[incubator-echarts] branch next-npm-env updated: chore(release): transform __DEV__ after all bundled.

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch next-npm-env
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git


The following commit(s) were added to refs/heads/next-npm-env by this push:
     new e882d9a  chore(release): transform __DEV__ after all bundled.
e882d9a is described below

commit e882d9a6a8a2749a183b6d0766f2860ee993e9bb
Author: pissang <bm...@gmail.com>
AuthorDate: Tue Jul 28 14:46:07 2020 +0800

    chore(release): transform __DEV__ after all bundled.
---
 build/build.js         | 49 ++++++++++++++++++++++++-------------------------
 build/config.js        | 10 +---------
 build/transform-dev.js |  1 +
 test/lib/config.js     |  2 +-
 4 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/build/build.js b/build/build.js
index 34e1e55..2e9f61b 100755
--- a/build/build.js
+++ b/build/build.js
@@ -26,10 +26,9 @@ const chalk = require('chalk');
 const rollup = require('rollup');
 const ecLangPlugin = require('./ec-lang-rollup-plugin');
 const prePublish = require('./pre-publish');
-const recheckDEV = require('./transform-dev').recheckDEV;
+const transformDEV = require('./transform-dev');
 const UglifyJS = require("uglify-js");
 const preamble = require('./preamble');
-const path = require('path');
 
 async function run() {
 
@@ -57,8 +56,6 @@ async function run() {
                 + '\n' + descIndent + '# Build all to `dist` folder.',
             egIndent + 'node build/build.js --prepublish'
                 + '\n' + descIndent + '# Only prepublish.',
-            egIndent + 'node build/build.js --removedev'
-                + '\n' + descIndent + '# Remove __DEV__ code. If --min, __DEV__ always be removed.',
             egIndent + 'node build/build.js --type ""'
                 + '\n' + descIndent + '# Only generate `dist/echarts.js`.',
             egIndent + 'node build/build.js --type common --min'
@@ -86,10 +83,6 @@ async function run() {
             'Build all for release'
         )
         .option(
-            '--removedev',
-            'Transform __DEV__ code into process.env="production".'
-        )
-        .option(
             '--min',
             'Whether to compress the output file, and remove error-log-print code.'
         )
@@ -132,7 +125,6 @@ async function run() {
         output: commander.output,
         format: commander.format,
         sourcemap: commander.sourcemap,
-        removeDev: commander.removedev || commander.min,
         addBundleVersion: isWatch,
         // Force to disable cache in release build.
         // TODO npm run build also disable cache?
@@ -153,15 +145,12 @@ async function run() {
             config.createBMap(),
             config.createDataTool()
         ];
-        await build(cfgs, opt.min);
+        await build(cfgs, opt.min, opt.sourcemap);
     }
     else {
         const cfg = config.createECharts(opt);
-        await build([cfg], opt.min);
-
-        if (opt.removeDev) {
-            checkBundleCode(cfg);
-        }
+        await build([cfg], opt.min, opt.sourcemap);
+        checkBundleCode(cfg);
     }
 }
 
@@ -171,7 +160,7 @@ function checkBundleCode(cfg) {
     if (!code) {
         throw new Error(`${cfg.output.file} is empty`);
     }
-    recheckDEV(code);
+    transformDEV.recheckDEV(code);
     console.log(chalk.green.dim('Check code: correct.'));
 }
 
@@ -211,7 +200,7 @@ function validateLang(lang, output) {
  *      ...
  *  ]
  */
-async function build(configs, min) {
+async function build(configs, min, sourcemap) {
 
     // ensureZRenderCode.prepare();
 
@@ -230,6 +219,13 @@ async function build(configs, min) {
         console.timeEnd('rollup build');
 
         await bundle.write(singleConfig.output);
+        const sourceCode = fs.readFileSync(singleConfig.output.file, 'utf-8');
+        // Convert __DEV__ to true;
+        const transformResult = transformDEV.transform(sourceCode, sourcemap, 'true');
+        fs.writeFileSync(singleConfig.output.file, transformResult.code, 'utf-8');
+        if (transformResult.map) {
+            fs.writeFileSync(singleConfig.output.file + '.map', JSON.stringify(transformResult.map), 'utf-8');
+        }
 
         console.log(
             chalk.green.dim('Created '),
@@ -247,16 +243,19 @@ async function build(configs, min) {
                 chalk.cyan.dim(' ...')
             )
             console.time('Minify');
-            const code = fs.readFileSync(singleConfig.output.file, 'utf-8');
-            const result = UglifyJS.minify(code, {
-                output: {
-                    preamble: preamble.js
+            const uglifyResult = UglifyJS.minify(
+                // Convert __DEV__ to false and let uglify remove the dead code;
+                transformDEV.transform(sourceCode, false, 'false').code,
+                {
+                    output: {
+                        preamble: preamble.js
+                    }
                 }
-            });
-            if (result.error) {
-                throw new Error(result.error);
+            );
+            if (uglifyResult.error) {
+                throw new Error(uglifyResult.error);
             }
-            fs.writeFileSync(fileMinPath, result.code, 'utf-8');
+            fs.writeFileSync(fileMinPath, uglifyResult.code, 'utf-8');
 
             console.timeEnd('Minify');
             console.log(
diff --git a/build/config.js b/build/config.js
index 0a70548..5ec1858 100644
--- a/build/config.js
+++ b/build/config.js
@@ -25,10 +25,9 @@ const ecDir = nodePath.resolve(__dirname, '..');
 const typescriptPlugin = require('rollup-plugin-typescript2');
 const fs = require('fs');
 const progress = require('./progress');
-const transformDEV = require('./transform-dev');
 
 function preparePlugins(
-    {lang, sourcemap, removeDev, addBundleVersion, totalFiles, clean},
+    {lang, sourcemap, addBundleVersion, totalFiles, clean},
     {include, exclude}
 ) {
     assert(include);
@@ -72,12 +71,6 @@ function preparePlugins(
         })
     ];
 
-    plugins.push({
-        transform: function (sourceCode) {
-            return transformDEV.transform(sourceCode, sourcemap, removeDev ? 'false' : 'true')
-        }
-    });
-
     lang && plugins.push(
         ecLangPlugin({lang})
     );
@@ -98,7 +91,6 @@ function preparePlugins(
  * @param {string} [opt.input=undefined] If set, `opt.output` is required too, and `opt.type` is ignored.
  * @param {string} [opt.output=undefined] If set, `opt.input` is required too, and `opt.type` is ignored.
  * @param {boolean} [opt.sourcemap] If set, `opt.input` is required too, and `opt.type` is ignored.
- * @param {boolean} [opt.removeDev]
  * @param {string} [opt.format='umd'] If set, `opt.input` is required too, and `opt.type` is ignored.
  * @param {boolean} [opt.addBundleVersion=false] Only for debug in watch, prompt that the two build is different.
  * @param {Object} [opt.totalFiles] Total files to bundle
diff --git a/build/transform-dev.js b/build/transform-dev.js
index 6d67bd0..85d7a4b 100644
--- a/build/transform-dev.js
+++ b/build/transform-dev.js
@@ -42,6 +42,7 @@ module.exports.transform = function (sourceCode, sourcemap, expr) {
         plugins: [ [transformDEVPlugin, {
             expr: expr || 'process.env.NODE_ENV !== \'production\''
         }] ],
+        compact: false,
         sourceMaps: sourcemap
     });
 
diff --git a/test/lib/config.js b/test/lib/config.js
index c980f4e..a49cf1e 100644
--- a/test/lib/config.js
+++ b/test/lib/config.js
@@ -51,7 +51,7 @@
         }
     }
     if (!ecDistPath) {
-        ecDistPath = 'dist/echarts';
+        ecDistPath = 'dist/echarts.min';
     }
 
     if (typeof require !== 'undefined') {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org