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 05:43:10 UTC

[incubator-echarts] branch next-npm-env updated: chore(release): remove __DEV__ in distribution file

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 c94865f  chore(release): remove __DEV__ in distribution file
c94865f is described below

commit c94865f2750f2810f31f30cca303edae380bcc19
Author: pissang <bm...@gmail.com>
AuthorDate: Tue Jul 28 13:42:49 2020 +0800

    chore(release): remove __DEV__ in distribution file
    
    Will keep dev log in file not minified.
---
 build/build.js                                     |  9 ++--
 build/config.js                                    | 10 +++--
 build/transform-dev-rollup-plugin.js               | 48 ----------------------
 build/transform-dev.js                             | 38 ++++++++++-------
 src/config.ts                                      | 41 ------------------
 .../global.d.ts                                    | 18 +-------
 6 files changed, 34 insertions(+), 130 deletions(-)

diff --git a/build/build.js b/build/build.js
index 646bda2..34e1e55 100755
--- a/build/build.js
+++ b/build/build.js
@@ -20,7 +20,6 @@
 */
 
 const fs = require('fs');
-const nodePath = require('path');
 const config = require('./config.js');
 const commander = require('commander');
 const chalk = require('chalk');
@@ -30,6 +29,7 @@ const prePublish = require('./pre-publish');
 const recheckDEV = require('./transform-dev').recheckDEV;
 const UglifyJS = require("uglify-js");
 const preamble = require('./preamble');
+const path = require('path');
 
 async function run() {
 
@@ -165,12 +165,11 @@ async function run() {
     }
 }
 
-function checkBundleCode() {
-    const fullBundleDistPath = path.join(__dirname, '../dist/echarts.min.js');
+function checkBundleCode(cfg) {
     // Make sure __DEV__ is eliminated.
-    let code = fs.readFileSync(fullBundleDistPath, {encoding: 'utf-8'});
+    let code = fs.readFileSync(cfg.output.file, {encoding: 'utf-8'});
     if (!code) {
-        throw new Error(`${fullBundleDistPath} is empty`);
+        throw new Error(`${cfg.output.file} is empty`);
     }
     recheckDEV(code);
     console.log(chalk.green.dim('Check code: correct.'));
diff --git a/build/config.js b/build/config.js
index d58ebbb..0a70548 100644
--- a/build/config.js
+++ b/build/config.js
@@ -19,13 +19,13 @@
 
 const assert = require('assert');
 const nodeResolvePlugin = require('rollup-plugin-node-resolve');
-const ecTransformDevPlugin = require('./transform-dev-rollup-plugin');
 const ecLangPlugin = require('./ec-lang-rollup-plugin');
 const nodePath = require('path');
 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},
@@ -72,9 +72,11 @@ function preparePlugins(
         })
     ];
 
-    removeDev && plugins.push(
-        ecTransformDevPlugin({sourcemap})
-    );
+    plugins.push({
+        transform: function (sourceCode) {
+            return transformDEV.transform(sourceCode, sourcemap, removeDev ? 'false' : 'true')
+        }
+    });
 
     lang && plugins.push(
         ecLangPlugin({lang})
diff --git a/build/transform-dev-rollup-plugin.js b/build/transform-dev-rollup-plugin.js
deleted file mode 100644
index da9e5ac..0000000
--- a/build/transform-dev-rollup-plugin.js
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements.  See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership.  The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License.  You may obtain a copy of the License at
-*
-*   http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing,
-* software distributed under the License is distributed on an
-* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-* KIND, either express or implied.  See the License for the
-* specific language governing permissions and limitations
-* under the License.
-*/
-
-/**
- * Transform the code of `if (__DEV__) { ... }`. to `if (process.env.NODE_ENV !== 'production') { ... }`
- *
- * Usage:
- *
- * import ecTransformDevPlugin from 'echats/build/rollup-plugin-ec-transform-dev';
- * let rollupConfig = {
- *     plugins: [
- *         ecTransformDevPlugin(),
- *         ...
- *     ]
- * };
- */
-
-const removeDEV = require('./transform-dev');
-
-/**
- * @param {Object} [opt]
- * @param {boolean} [opt.sourcemap]
- */
-module.exports = function ({sourcemap} = {}) {
-
-    return {
-        transform: function (sourceCode) {
-            return removeDEV.transform(sourceCode, sourcemap);
-        }
-    };
-};
-
diff --git a/build/transform-dev.js b/build/transform-dev.js
index 37aacab..6d67bd0 100644
--- a/build/transform-dev.js
+++ b/build/transform-dev.js
@@ -18,16 +18,30 @@
 */
 
 const babel = require('@babel/core');
-const removeDEVBabelPlugin = require('./transform-dev-babel-plugin');
+const parser = require('@babel/parser');
 
-/**
- * @param {string} sourceCode
- * @param {boolean} sourcemap
- * @return {Object} {code: string, map: string}
- */
-module.exports.transform = function (sourceCode, sourcemap) {
+function transformDEVPlugin ({types, template}) {
+    return {
+        visitor: {
+            Identifier: {
+                enter(path, state) {
+                    if (path.isIdentifier({ name: '__DEV__' }) && path.scope.hasGlobal('__DEV__')) {
+                        path.replaceWith(
+                            parser.parseExpression(state.opts.expr)
+                        );
+                    }
+                }
+            }
+        }
+    };
+};
+
+
+module.exports.transform = function (sourceCode, sourcemap, expr) {
     let {code, map} = babel.transformSync(sourceCode, {
-        plugins: [removeDEVBabelPlugin],
+        plugins: [ [transformDEVPlugin, {
+            expr: expr || 'process.env.NODE_ENV !== \'production\''
+        }] ],
         sourceMaps: sourcemap
     });
 
@@ -39,11 +53,5 @@ module.exports.transform = function (sourceCode, sourcemap) {
  * @throws {Error} If check failed.
  */
 module.exports.recheckDEV = function (code) {
-    let result = code.match(/.if\s*\([^()]*__DEV__/);
-    if (result
-        && result[0].indexOf('`if') < 0
-        && result[0].indexOf('if (typeof __DEV__') < 0
-    ) {
-        throw new Error('__DEV__ is not removed.');
-    }
+    return code.indexOf('process.env.NODE_ENV') >= 0;
 };
diff --git a/src/config.ts b/src/config.ts
deleted file mode 100644
index 7c7549a..0000000
--- a/src/config.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements.  See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership.  The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License.  You may obtain a copy of the License at
-*
-*   http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing,
-* software distributed under the License is distributed on an
-* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-* KIND, either express or implied.  See the License for the
-* specific language governing permissions and limitations
-* under the License.
-*/
-
-// (1) The code `if (__DEV__) ...` can be removed by build tool.
-// (2) If intend to use `__DEV__`, this module should be imported. Use a global
-// variable `__DEV__` may cause that miss the declaration (see #6535), or the
-// declaration is behind of the using position (for example in `Model.extent`,
-// And tools like rollup can not analysis the dependency if not import).
-
-let dev;
-
-// In browser
-if (typeof window !== 'undefined') {
-    dev = (window as any).__DEV__;
-}
-// In node
-else if (typeof global !== 'undefined') {
-    dev = (global as any).__DEV__;
-}
-
-if (typeof dev === 'undefined') {
-    dev = true;
-}
-
-declare const __DEV__: boolean;
\ No newline at end of file
diff --git a/build/transform-dev-babel-plugin.js b/src/global.d.ts
similarity index 61%
rename from build/transform-dev-babel-plugin.js
rename to src/global.d.ts
index 18318f2..a66cb70 100644
--- a/build/transform-dev-babel-plugin.js
+++ b/src/global.d.ts
@@ -17,20 +17,4 @@
 * under the License.
 */
 
-const parser = require('@babel/parser');
-
-const replacedExpr = parser.parseExpression('process.env.NODE_ENV !== \'production\'');
-
-module.exports = function ({types, template}, options) {
-    return {
-        visitor: {
-            Identifier: {
-                enter(path) {
-                    if (path.isIdentifier({ name: '__DEV__' }) && path.scope.hasGlobal('__DEV__')) {
-                        path.replaceWith(replacedExpr);
-                    }
-                }
-            }
-        }
-    };
-};
\ No newline at end of file
+declare const __DEV__: boolean;


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