From a921ad031599ffc1993e86567a2bfd049e4ce17d Mon Sep 17 00:00:00 2001 From: sushuang Date: Sun, 5 Nov 2017 18:37:57 +0800 Subject: [PATCH] print bundle version to aid debug. --- build/build.js | 3 ++- build/config.js | 12 +++++++-- test/lib/config.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/build/build.js b/build/build.js index b09389969..20842df87 100755 --- a/build/build.js +++ b/build/build.js @@ -95,7 +95,8 @@ function run() { input: commander.input, output: commander.output, sourcemap: commander.sourcemap, - format: commander.format || 'umd' + format: commander.format || 'umd', + addBundleVersion: isWatch }; validateIO(opt.input, opt.output); diff --git a/build/config.js b/build/config.js index bce4ca5cf..f4437d79b 100644 --- a/build/config.js +++ b/build/config.js @@ -12,8 +12,9 @@ function getPathBasedOnECharts(path) { /** * @param {boolean} [min=false] * @param {string} [lang=null] null/undefined/'' or 'en' or 'fi' or a file path + * @param {boolean} [addBundleVersion=false] */ -function getPlugins(min, lang) { +function getPlugins(min, lang, addBundleVersion) { let plugins = [ ecDevPlugin() ]; @@ -26,6 +27,12 @@ function getPlugins(min, lang) { nodeResolvePlugin() ); + addBundleVersion && plugins.push({ + outro: function () { + return 'exports.bundleVersion = "' + (+new Date()) + '";'; + } + }); + min && plugins.push(uglifyPlugin({ compress: { // Eliminate __DEV__ code. @@ -51,6 +58,7 @@ function getPlugins(min, lang) { * @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 {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. */ exports.createECharts = function (opt) { opt = opt || {}; @@ -78,7 +86,7 @@ exports.createECharts = function (opt) { } return { - plugins: getPlugins(opt.min, opt.lang), + plugins: getPlugins(opt.min, opt.lang, opt.addBundleVersion), input: input, legacy: true, // Support IE8- output: { diff --git a/test/lib/config.js b/test/lib/config.js index 0de2d9170..1a46468fc 100644 --- a/test/lib/config.js +++ b/test/lib/config.js @@ -45,4 +45,68 @@ window.__ECHARTS__DEFAULT__RENDERER__ = matchResult[1]; } + // Mount bundle version print. + if (typeof require !== 'undefined') { + var originalRequire = require; + window.require = function (deps, cb) { + var newCb = function () { + if (deps && deps instanceof Array) { + printBundleVersion(deps, [].slice.call(arguments)); + } + cb && cb.apply(this, arguments); + }; + return originalRequire.call(this, deps, newCb); + }; + } + + function printBundleVersion(bundleIds, bundles) { + var content = []; + for (var i = 0; i < bundleIds.length; i++) { + var bundle = bundles[i]; + var bundleVersion = bundle && bundle.bundleVersion; + if (bundleVersion) { + var date = new Date(+bundleVersion); + // Check whether timestamp. + if (!isNaN(+date)) { + bundleVersion = date.getHours() + ':' + date.getMinutes() + ': ' + + '' + + date.getSeconds() + '.' + date.getMilliseconds() + + ''; + } + else { + bundleVersion = encodeHTML(bundleVersion); + } + content.push(encodeHTML(bundleIds[i]) + '.js: ' + bundleVersion); + } + } + + var domId = 'ec-test-bundle-version'; + var dom = document.getElementById(domId); + if (!dom) { + dom = document.createElement('div'); + dom.setAttribute('id', domId); + dom.style.cssText = [ + 'background: rgb(52,56,64)', + 'color: rgb(215,215,215)', + 'position: fixed', + 'right: 0', + 'top: 0', + 'font-size: 10px', + 'padding: 1px 2px 1px 2px', + 'border-bottom-left-radius: 3px' + ].join(';'); + document.body.appendChild(dom); + } + dom.innerHTML = content.join(''); + } + + function encodeHTML(source) { + return String(source) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + } + })(); \ No newline at end of file -- GitLab