From ec03c9d604aff2a34f4cec9dd16ec14e033c4c19 Mon Sep 17 00:00:00 2001 From: sushuang Date: Fri, 3 Nov 2017 04:19:36 +0800 Subject: [PATCH] Enhance build tool. --- build/build.js | 69 +++++++++++++++++++++++++++++++++++++++---------- build/config.js | 58 +++++++++++++++++++++++++++++++---------- 2 files changed, 99 insertions(+), 28 deletions(-) diff --git a/build/build.js b/build/build.js index 3b0857f45..6713fe96d 100755 --- a/build/build.js +++ b/build/build.js @@ -20,7 +20,19 @@ function run() { commander .usage('[options]') - .description('Build echarts and generate result files in directory `echarts/dist`.') + .description([ + 'Build echarts and generate result files in directory `echarts/dist`.', + '', + ' For example:', + '', + ' node build/build.js --release # Build all to `dist` folder.', + ' node build/build.js --type "" # Only generate `dist/echarts.js`.', + ' node build/build.js --type common --min # Only generate `dist/echarts.common.min.js`.', + ' node build/build.js --type simple --min --lang en # Only generate `dist/echarts-en.simple.min.js`.', + ' node build/build.js --min --lang en -i "/my/index.js" -o "/my/ec.js" ' + + '# Take `/my/index.js` as input and generate a bundle `/my/ec.js`, ' + + 'which is in EN language and has been minified.', + ].join('\n')) .option( '-w, --watch', 'Watch modifications of files and auto-compile to dist file (e.g., `echarts/dist/echarts.js`).' @@ -31,6 +43,10 @@ function run() { + 'A langXX.js file is required in directory `echarts`. ' + 'e.g., `--lang en`, where a file `langEN.js` is required.' ) + .option( + '--release', + 'Build all for release' + ) .option( '--min', 'Whether to compress the output file.' @@ -40,20 +56,45 @@ function run() { 'Can be "simple" or "common" or "" (default). ' + 'e.g., `--type ""` or `--type "common"`.' ) + .option( + '--sourcemap', + 'Whether output sourcemap.' + ) + .option( + '--format ', + 'The format of output bundle. Can be "umd", "amd", "iife", "cjs", "es".' + ) + .option( + '-i, --input ', + 'If input file path is specified, output file path must be specified too.' + ) + .option( + '-o, --output ', + 'If output file path is specified, input file path must be specified too.' + ) .parse(process.argv); - let opt = {}; let isWatch = !!commander.watch; - opt.lang = commander.lang || null; - opt.min = !!commander.min; - opt.type = commander.type || ''; - let buildAll = commander.watch == null - && commander.lang == null - && commander.min == null - && commander.type == null; + let isRelease = !!commander.release; + + let opt = { + lang: commander.lang || null, + min: !!commander.min, + type: commander.type || '', + input: commander.input, + output: commander.output, + sourcemap: !!commander.sourcemap, + format: commander.format || 'umd' + }; + + if ((opt.input != null && opt.output == null) + || (opt.input == null && opt.output != null) + ) { + throw new Error('`input` and `output` must be both set.'); + } // Clear `echarts/dist` - if (buildAll) { + if (isRelease) { fsExtra.removeSync(getPath('./dist')); } @@ -63,10 +104,7 @@ function run() { watch(config.createECharts(opt)); } else { - if (!buildAll) { - configs = [config.createECharts(opt)]; - } - else { + if (isRelease) { configs = []; [ @@ -87,6 +125,9 @@ function run() { config.createDataTool(true) ); } + else { + configs = [config.createECharts(opt)]; + } build(configs); diff --git a/build/config.js b/build/config.js index 6c63090c4..263b7c3ca 100644 --- a/build/config.js +++ b/build/config.js @@ -1,10 +1,10 @@ +/* global process */ const nodeResolvePlugin = require('rollup-plugin-node-resolve'); const uglifyPlugin = require('rollup-plugin-uglify'); const {dirname, resolve} = require('path'); -// Based on echarts/ -function getPath(relativePath) { - return resolve(__dirname, '../', relativePath); +function getPathBasedOnECharts(path) { + return resolve(__dirname, '../', path); } /** @@ -46,26 +46,56 @@ function getPlugins(min, lang) { * @param {string} [opt.type=''] '' or 'simple' or 'common' * @param {boolean} [opt.min=false] * @param {string} [opt.lang=undefined] null/undefined/'' or 'en' or 'fi' or ... + * @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 {string} [opt.format='umd'] If set, `opt.input` is required too, and `opt.type` is ignored. */ exports.createECharts = function (opt) { opt = opt || {}; let postfixType = opt.type ? '.' + opt.type : ''; let postfixMin = opt.min ? '.min' : ''; let postfixLang = opt.lang ? '-' + opt.lang.toLowerCase() : ''; + let isCustom; + let input = opt.input; + let output = opt.output; + let sourcemap = opt.sourcemap; + let format = opt.format || 'umd'; + + if (input != null || output != null) { + if (input == null || output == null) { + throw new Error('`input` and `output` must be both set.'); + } + isCustom = true; + // Based on process.cwd(); + input = resolve(input); + output = resolve(output); + } + else { + input = getPathBasedOnECharts(`./index${postfixType}.js`); + output = getPathBasedOnECharts(`dist/echarts${postfixLang}${postfixType}${postfixMin}.js`); + if (sourcemap == null) { + sourcemap = !opt.min && !postfixType; + } + } return { plugins: getPlugins(opt.min, opt.lang), - input: getPath(`./index${postfixType}.js`), + input: input, legacy: true, // Support IE8- output: { name: 'echarts', - format: 'umd', - sourcemap: !opt.min && !postfixType, + format: format, + sourcemap: sourcemap, legacy: true, // Must be declared both in inputOptions and outputOptions. - file: getPath(`dist/echarts${postfixLang}${postfixType}${postfixMin}.js`) + file: output }, watch: { - include: [getPath('./src/**'), getPath('./index*.js'), getPath('../zrender/src/**')] + include: [ + getPathBasedOnECharts('./src/**'), + getPathBasedOnECharts('./index*.js'), + getPathBasedOnECharts('../zrender/src/**') + ] } }; }; @@ -78,7 +108,7 @@ exports.createBMap = function (min) { return { plugins: getPlugins(min), - input: getPath(`./extension/bmap/bmap.js`), + input: getPathBasedOnECharts(`./extension/bmap/bmap.js`), legacy: true, // Support IE8- external: ['echarts'], output: { @@ -90,10 +120,10 @@ exports.createBMap = function (min) { // For UMD `global.echarts` echarts: 'echarts' }, - file: getPath(`dist/extension/bmap${postfix}.js`) + file: getPathBasedOnECharts(`dist/extension/bmap${postfix}.js`) }, watch: { - include: [getPath('./extension/bmap/**')] + include: [getPathBasedOnECharts('./extension/bmap/**')] } }; }; @@ -105,7 +135,7 @@ exports.createDataTool = function (min) { let postfix = min ? '.min' : ''; return { plugins: getPlugins(min), - input: getPath(`./extension/dataTool/index.js`), + input: getPathBasedOnECharts(`./extension/dataTool/index.js`), legacy: true, // Support IE8- external: ['echarts'], output: { @@ -117,10 +147,10 @@ exports.createDataTool = function (min) { // For UMD `global.echarts` echarts: 'echarts' }, - file: getPath(`dist/extension/dataTool${postfix}.js`) + file: getPathBasedOnECharts(`dist/extension/dataTool${postfix}.js`) }, watch: { - include: [getPath('./extension/dataTool/**')] + include: [getPathBasedOnECharts('./extension/dataTool/**')] } }; }; -- GitLab