@@ -6,6 +6,7 @@ const {resolve} = require('path');
66const config = require ( './config.js' ) ;
77const commander = require ( 'commander' ) ;
88const { build, watch, color} = require ( './helper' ) ;
9+ const ecLangPlugin = require ( './rollup-plugin-ec-lang' ) ;
910
1011function run ( ) {
1112
@@ -19,31 +20,40 @@ function run() {
1920 * (2) `node ./build/build.js --help` will print helper info and exit.
2021 */
2122
23+ let descIndent = ' ' ;
24+ let egIndent = ' ' ;
25+
2226 commander
2327 . usage ( '[options]' )
2428 . description ( [
2529 'Build echarts and generate result files in directory `echarts/dist`.' ,
2630 '' ,
2731 ' For example:' ,
2832 '' ,
29- ' node build/build.js --release # Build all to `dist` folder.' ,
30- ' node build/build.js --type "" # Only generate `dist/echarts.js`.' ,
31- ' node build/build.js --type common --min # Only generate `dist/echarts.common.min.js`.' ,
32- ' node build/build.js --type simple --min --lang en # Only generate `dist/echarts-en.simple.min.js`.' ,
33- ' node build/build.js --min --lang en -i "/my/index.js" -o "/my/ec.js" '
34- + '# Take `/my/index.js` as input and generate a bundle `/my/ec.js`, '
35- + 'which is in EN language and has been minified.' ,
33+ egIndent + 'node build/build.js --release'
34+ + '\n' + descIndent + '# Build all to `dist` folder.' ,
35+ egIndent + 'node build/build.js --type ""'
36+ + '\n' + descIndent + '# Only generate `dist/echarts.js`.' ,
37+ egIndent + 'node build/build.js --type common --min'
38+ + '\n' + descIndent + '# Only generate `dist/echarts.common.min.js`.' ,
39+ egIndent + 'node build/build.js --type simple --min --lang en'
40+ + '\n' + descIndent + '# Only generate `dist/echarts-en.simple.min.js`.' ,
41+ egIndent + 'node build/build.js --lang "my/lang.js" -i "my/index.js" -o "my/bundle.js"'
42+ + '\n' + descIndent + '# Take `<cwd>/my/index.js` as input and generate `<cwd>/my/bundle.js`,'
43+ + '\n' + descIndent + 'where `<cwd>/my/lang.js` is used as language file.' ,
3644 ] . join ( '\n' ) )
3745 . option (
38- '-w, --watch' ,
39- 'Watch modifications of files and auto-compile to dist file (e.g., `echarts/dist/echarts.js`).'
40- )
46+ '-w, --watch' , [
47+ 'Watch modifications of files and auto-compile to dist file. For example,' ,
48+ descIndent + '`echarts/dist/echarts.js`.'
49+ ] . join ( '\n' ) )
4150 . option (
42- '--lang <language shortcut>' ,
43- 'Only generate a dist file with specified language in directory `echarts/dist`. '
44- + 'A langXX.js file is required in directory `echarts`. '
45- + 'e.g., `--lang en`, where a file `langEN.js` is required.'
46- )
51+ '--lang <language file path or shortcut>' , [
52+ 'Use the specified file instead of `echarts/src/lang.js`. For example:' ,
53+ descIndent + '`--lang en` will use `echarts/src/langEN.js`.' ,
54+ descIndent + '`--lang my/langDE.js` will use `<cwd>/my/langDE.js`. -o must be specified in this case.' ,
55+ descIndent + '`--lang /my/indexSW.js` will use `/my/indexSW.js`. -o must be specified in this case.'
56+ ] . join ( '\n' ) )
4757 . option (
4858 '--release' ,
4959 'Build all for release'
@@ -53,10 +63,10 @@ function run() {
5363 'Whether to compress the output file, and remove error-log-print code.'
5464 )
5565 . option (
56- '--type <type name>' ,
57- 'Can be "simple" or "common" or "" (default). '
58- + 'e.g., `--type ""` or `--type "common"`.'
59- )
66+ '--type <type name>' , [
67+ 'Can be "simple" or "common" or "" (default). For example,' ,
68+ descIndent + '`--type ""` or `--type "common"`.'
69+ ] . join ( '\n' ) )
6070 . option (
6171 '--sourcemap' ,
6272 'Whether output sourcemap.'
@@ -88,11 +98,8 @@ function run() {
8898 format : commander . format || 'umd'
8999 } ;
90100
91- if ( ( opt . input != null && opt . output == null )
92- || ( opt . input == null && opt . output != null )
93- ) {
94- throw new Error ( '`input` and `output` must be both set.' ) ;
95- }
101+ validateIO ( opt . input , opt . output ) ;
102+ validateLang ( opt . lang , opt . output ) ;
96103
97104 // Clear `echarts/dist`
98105 if ( isRelease ) {
@@ -148,14 +155,37 @@ function checkCode(singleConfig) {
148155 // Make sure __DEV__ is eliminated.
149156 let code = fs . readFileSync ( singleConfig . output . file , { encoding : 'utf-8' } ) ;
150157 if ( ! code ) {
151- throw new Error ( singleConfig . output . file + ' is empty' ) ;
158+ throw new Error ( ` ${ singleConfig . output . file } is empty` ) ;
152159 }
153160 if ( code . indexOf ( '__DEV__' ) >= 0 ) {
154161 throw new Error ( '__DEV__ is not removed.' ) ;
155162 }
156163 console . log ( color ( 'fgGreen' , 'dim' ) ( 'Check code: correct.' ) ) ;
157164}
158165
166+ function validateIO ( input , output ) {
167+ if ( ( input != null && output == null )
168+ || ( input == null && output != null )
169+ ) {
170+ throw new Error ( '`input` and `output` must be both set.' ) ;
171+ }
172+ }
173+
174+ function validateLang ( lang , output ) {
175+ if ( ! lang ) {
176+ return ;
177+ }
178+
179+ let langInfo = ecLangPlugin . getLangFileInfo ( lang ) ;
180+
181+ if ( langInfo . isOuter && ! output ) {
182+ throw new Error ( '`-o` or `--output` must be specified if using a file path in `--lang`.' ) ;
183+ }
184+ if ( ! langInfo . absolutePath || ! fs . statSync ( langInfo . absolutePath ) . isFile ( ) ) {
185+ throw new Error ( `File ${ langInfo . absolutePath } does not exist yet. Contribution is welcome!` ) ;
186+ }
187+ }
188+
159189/**
160190 * @param {string } relativePath Based on echarts directory.
161191 * @return {string } Absolute path.
0 commit comments