build.js
8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env node
/*
* 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.
*/
const fsExtra = require('fs-extra');
const fs = require('fs');
const {resolve} = require('path');
const config = require('./config.js');
const commander = require('commander');
const {build, watch, color} = require('zrender/build/helper');
const ecLangPlugin = require('./rollup-plugin-ec-lang');
const prePublish = require('./pre-publish');
const recheckDEV = require('zrender/build/babel-plugin-transform-remove-dev').recheckDEV;
function run() {
/**
* Tips for `commander`:
* (1) If arg xxx not specified, `commander.xxx` is undefined.
* Otherwise:
* If '-x, --xxx', `commander.xxx` can only be true/false, even if '--xxx yyy' input.
* If '-x, --xxx <some>', the 'some' string is required, or otherwise error will be thrown.
* If '-x, --xxx [some]', the 'some' string is optional, that is, `commander.xxx` can be boolean or string.
* (2) `node ./build/build.js --help` will print helper info and exit.
*/
let descIndent = ' ';
let egIndent = ' ';
commander
.usage('[options]')
.description([
'Build echarts and generate result files in directory `echarts/dist`.',
'',
' For example:',
'',
egIndent + 'node build/build.js --release'
+ '\n' + descIndent + '# Build all to `dist` folder.',
egIndent + 'node build/build.js --prepublish'
+ '\n' + descIndent + '# Only prepublish.',
egIndent + 'node build/build.js --removedev'
+ '\n' + descIndent + '# Remove __DEV__ code. If --min, __DEV__ always be removed.',
egIndent + 'node build/build.js --type ""'
+ '\n' + descIndent + '# Only generate `dist/echarts.js`.',
egIndent + 'node build/build.js --type common --min'
+ '\n' + descIndent + '# Only generate `dist/echarts.common.min.js`.',
egIndent + 'node build/build.js --type simple --min --lang en'
+ '\n' + descIndent + '# Only generate `dist/echarts-en.simple.min.js`.',
egIndent + 'node build/build.js --lang "my/lang.js" -i "my/index.js" -o "my/bundle.js"'
+ '\n' + descIndent + '# Take `<cwd>/my/index.js` as input and generate `<cwd>/my/bundle.js`,'
+ '\n' + descIndent + 'where `<cwd>/my/lang.js` is used as language file.',
].join('\n'))
.option(
'-w, --watch', [
'Watch modifications of files and auto-compile to dist file. For example,',
descIndent + '`echarts/dist/echarts.js`.'
].join('\n'))
.option(
'--lang <language file path or shortcut>', [
'Use the specified file instead of `echarts/src/lang.js`. For example:',
descIndent + '`--lang en` will use `echarts/src/langEN.js`.',
descIndent + '`--lang my/langDE.js` will use `<cwd>/my/langDE.js`. -o must be specified in this case.',
descIndent + '`--lang /my/indexSW.js` will use `/my/indexSW.js`. -o must be specified in this case.'
].join('\n'))
.option(
'--release',
'Build all for release'
)
.option(
'--prepublish',
'Build all for release'
)
.option(
'--removedev',
'Remove __DEV__ code. If --min, __DEV__ always be removed.'
)
.option(
'--min',
'Whether to compress the output file, and remove error-log-print code.'
)
.option(
'--type <type name>', [
'Can be "simple" or "common" or "" (default). For example,',
descIndent + '`--type ""` or `--type "common"`.'
].join('\n'))
.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 isWatch = !!commander.watch;
let isRelease = !!commander.release;
let isPrePublish = !!commander.prepublish;
let opt = {
lang: commander.lang,
min: commander.min,
type: commander.type || '',
input: commander.input,
output: commander.output,
format: commander.format,
sourcemap: commander.sourcemap,
removeDev: commander.removedev,
addBundleVersion: isWatch
};
validateIO(opt.input, opt.output);
validateLang(opt.lang, opt.output);
normalizeParams(opt);
// Clear `echarts/dist`
if (isRelease) {
fsExtra.removeSync(getPath('./dist'));
}
if (isWatch) {
watch(config.createECharts(opt));
}
else if (isPrePublish) {
prePublish();
}
else if (isRelease) {
let configs = [];
let configForCheck;
[
{min: false},
{min: true},
{min: false, lang: 'en'},
{min: true, lang: 'en'}
].forEach(function (opt) {
['', 'simple', 'common'].forEach(function (type) {
let singleOpt = Object.assign({type}, opt);
normalizeParams(singleOpt);
let singleConfig = config.createECharts(singleOpt);
configs.push(singleConfig);
if (singleOpt.min && singleOpt.type === '') {
configForCheck = singleConfig;
}
});
});
configs.push(
config.createBMap(false),
config.createBMap(true),
config.createDataTool(false),
config.createDataTool(true)
);
build(configs)
.then(function () {
checkCode(configForCheck);
prePublish();
}).catch(handleBuildError);
}
else {
let cfg = config.createECharts(opt);
build([cfg])
.then(function () {
if (opt.removeDev) {
checkCode(cfg);
}
})
.catch(handleBuildError);
}
}
function normalizeParams(opt) {
if (opt.sourcemap == null) {
opt.sourcemap = !(opt.min || opt.type);
}
if (opt.removeDev == null) {
opt.removeDev = !!opt.min;
}
}
function handleBuildError(err) {
console.log(err);
}
function checkCode(singleConfig) {
// Make sure __DEV__ is eliminated.
let code = fs.readFileSync(singleConfig.output.file, {encoding: 'utf-8'});
if (!code) {
throw new Error(`${singleConfig.output.file} is empty`);
}
recheckDEV(code);
console.log(color('fgGreen', 'dim')('Check code: correct.'));
}
function validateIO(input, output) {
if ((input != null && output == null)
|| (input == null && output != null)
) {
throw new Error('`input` and `output` must be both set.');
}
}
function validateLang(lang, output) {
if (!lang) {
return;
}
let langInfo = ecLangPlugin.getLangFileInfo(lang);
if (langInfo.isOuter && !output) {
throw new Error('`-o` or `--output` must be specified if using a file path in `--lang`.');
}
if (!langInfo.absolutePath || !fs.statSync(langInfo.absolutePath).isFile()) {
throw new Error(`File ${langInfo.absolutePath} does not exist yet. Contribution is welcome!`);
}
}
/**
* @param {string} relativePath Based on echarts directory.
* @return {string} Absolute path.
*/
function getPath(relativePath) {
return resolve(__dirname, '../', relativePath);
}
run();