This is an automated email from the ASF dual-hosted git repository. wangzx pushed a commit to branch gh-pages in repository https://gitbox.apache.org/repos/asf/echarts-examples.git
The following commit(s) were added to refs/heads/gh-pages by this push: new a3cf0a37 optimize sandbox script import a3cf0a37 is described below commit a3cf0a378d7079179adb744db1ebb5b437aeb921 Author: plainheart <y...@all-my-life.cn> AuthorDate: Tue May 3 01:18:02 2022 +0800 optimize sandbox script import --- build/minify-loader.js | 13 +++++++++++++ build/webpack.config.js | 23 ++++++++++++++++++++++ src/editor/sandbox/handleLoop.js | 2 +- src/editor/sandbox/index.js | 41 +++++++++++++++++++++------------------- src/editor/sandbox/setup.js | 2 +- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/build/minify-loader.js b/build/minify-loader.js new file mode 100644 index 00000000..4a87f7ae --- /dev/null +++ b/build/minify-loader.js @@ -0,0 +1,13 @@ +const { minify } = require('terser'); + +module.exports = function minifyLoader(source, map, meta) { + if (this.mode !== 'production') { + return source; + } + const callback = this.async(); + /** @type {import('terser').MinifyOptions} */ + const options = this.getOptions(); + minify(source, options) + .then((result) => callback(null, result.code, result.map, meta)) + .catch(callback); +}; diff --git a/build/webpack.config.js b/build/webpack.config.js index 97d692ef..04811450 100644 --- a/build/webpack.config.js +++ b/build/webpack.config.js @@ -24,6 +24,9 @@ module.exports = [ }, { test: /\.js$/, + resourceQuery: { + not: [/raw-pure/] + }, use: ['babel-loader'], exclude: /node_modules/ }, @@ -59,6 +62,26 @@ module.exports = [ } } ] + }, + { + resourceQuery: /raw-pure/, + type: 'asset/source' + }, + { + resourceQuery: /raw-minify/, + type: 'asset/source', + use: [ + { + loader: path.resolve(__dirname, './minify-loader.js'), + /** @type {import('terser').MinifyOptions} */ + options: { + compress: { + pure_funcs: ['console.debug', 'console.log'] + } + } + } + ], + enforce: 'post' } ] }, diff --git a/src/editor/sandbox/handleLoop.js b/src/editor/sandbox/handleLoop.js index 0843016c..cd615758 100644 --- a/src/editor/sandbox/handleLoop.js +++ b/src/editor/sandbox/handleLoop.js @@ -4,7 +4,7 @@ * * @param {string} code the source code */ -export default function handleLoop(code) { +function handleLoop(code) { let AST; try { AST = acorn.parse(code, { diff --git a/src/editor/sandbox/index.js b/src/editor/sandbox/index.js index 3fb16e51..b71ece69 100644 --- a/src/editor/sandbox/index.js +++ b/src/editor/sandbox/index.js @@ -1,9 +1,24 @@ import srcdoc from './srcdoc.html'; -import handleLoop from './handleLoop'; -import setup from './setup'; -import loopController from 'raw-loader!./loopController'; -import showDebugDirtyRect from 'raw-loader!../../dep/showDebugDirtyRect'; -import estraverse from '!!raw-loader!./estraverse.browser'; +import estraverse from './estraverse.browser?raw-pure'; +import loopController from './loopController?raw-minify'; +import handleLoop from './handleLoop?raw-minify'; +import showDebugDirtyRect from '../../dep/showDebugDirtyRect?raw-minify'; +import setup from './setup?raw-minify'; + +function prepareSetupScript(isShared) { + const isProd = process.env.NODE_ENV === 'production'; + return [ + estraverse, + loopController, + [ + '(()=>{', + handleLoop, + showDebugDirtyRect, + `(${setup})(${isShared})`, + '})()' + ].join(isProd ? '' : '\n\n') + ].map((content) => ({ content })); +} export function createSandbox( container, @@ -15,20 +30,8 @@ export function createSandbox( onOptionUpdated, onCSSParsed ) { - scripts = (scripts && scripts.slice()) || []; - scripts.push( - { content: estraverse }, - { content: loopController }, - { - // TODO optimize - content: ` - (function(){ - var handleLoop = ${handleLoop}; - ${showDebugDirtyRect} - ;(${setup})(${isShared}) - })() - ` - } + scripts = ((scripts && scripts.slice()) || []).concat( + prepareSetupScript(isShared) ); const sandbox = document.createElement('iframe'); diff --git a/src/editor/sandbox/setup.js b/src/editor/sandbox/setup.js index 926a99f3..099091d7 100644 --- a/src/editor/sandbox/setup.js +++ b/src/editor/sandbox/setup.js @@ -1,4 +1,4 @@ -export default function setup(isShared) { +function setup(isShared) { const sendMessage = (payload) => parent.postMessage(payload, '*'); const chartStyleEl = document.head.querySelector('#chart-styles'); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org For additional commands, e-mail: commits-h...@echarts.apache.org