提交 ec03c9d6 编写于 作者: S sushuang

Enhance build tool.

上级 a5469419
......@@ -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 <format>',
'The format of output bundle. Can be "umd", "amd", "iife", "cjs", "es".'
)
.option(
'-i, --input <input file path>',
'If input file path is specified, output file path must be specified too.'
)
.option(
'-o, --output <output file path>',
'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);
......
/* 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/**')]
}
};
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册