@@ -6,22 +6,64 @@ const buildVue = require('./build-vue');
6
6
const buildSvelte = require ( './build-svelte' ) ;
7
7
const buildStyles = require ( './build-styles' ) ;
8
8
const buildAngular = require ( './build-angular' ) ;
9
+ const outputCheckSize = require ( './check-size' ) ;
10
+
11
+ const env = process . env . NODE_ENV || 'development' ;
12
+ const outputDir = env === 'development' ? 'build' : 'package' ;
13
+ class Build {
14
+ constructor ( ) {
15
+ this . argv = process . argv . slice ( 2 ) . map ( ( v ) => v . toLowerCase ( ) ) ;
16
+ this . size = this . argv . includes ( '--size' ) ;
17
+ this . tasks = [ ] ;
18
+ return this ;
19
+ }
20
+
21
+ add ( flag , buildFn ) {
22
+ if ( ! this . argv . includes ( '--only' ) || this . argv . includes ( flag ) ) {
23
+ this . tasks . push ( buildFn ) ;
24
+ }
25
+ return this ;
26
+ }
27
+
28
+ addMultipleFormats ( flag , buildFn ) {
29
+ return this . add ( flag , async ( ) =>
30
+ Promise . all ( [ 'esm' , 'cjs' ] . map ( ( format ) => buildFn ( format , outputDir ) ) ) ,
31
+ ) ;
32
+ }
33
+
34
+ async run ( ) {
35
+ let start ;
36
+ let end ;
37
+ if ( this . size ) {
38
+ start = outputCheckSize ( ) ;
39
+ }
40
+
41
+ const res = await Promise . all ( this . tasks . map ( ( v ) => v ( ) ) ) . catch ( ( err ) => {
42
+ console . error ( err ) ;
43
+ process . exit ( 1 ) ;
44
+ } ) ;
45
+ if ( this . size ) {
46
+ const sizeMessage = ( value , label = '' ) =>
47
+ `difference ${ label } : ${ value > 0 ? `+${ value } ` : value } bytes` ;
48
+
49
+ end = outputCheckSize ( ) ;
50
+
51
+ console . log ( sizeMessage ( start . size - start . size ) ) ;
52
+ console . log ( sizeMessage ( end . gzippedSize - end . gzippedSize , 'gzipped' ) ) ;
53
+ }
54
+ return res ;
55
+ }
56
+ }
9
57
10
- const formats = [ 'esm' , 'cjs' ] ;
11
58
( async ( ) => {
12
- const env = process . env . NODE_ENV || 'development' ;
13
- const outputDir = env === 'development' ? 'build' : 'package' ;
14
- return Promise . all ( [
15
- buildJsBundle ( ) ,
16
- buildJsCore ( ) ,
17
- buildTypes ( ) ,
18
- Promise . all ( formats . map ( ( format ) => buildReact ( format , outputDir ) ) ) ,
19
- Promise . all ( formats . map ( ( format ) => buildVue ( format , outputDir ) ) ) ,
20
- Promise . all ( formats . map ( ( format ) => buildSvelte ( format , outputDir ) ) ) ,
21
- buildStyles ( outputDir ) ,
22
- buildAngular ( ) ,
23
- ] ) . catch ( ( err ) => {
24
- console . error ( err ) ;
25
- process . exit ( 1 ) ;
26
- } ) ;
59
+ await new Build ( )
60
+ . add ( 'core' , buildJsCore )
61
+ . add ( 'bundle' , buildJsBundle )
62
+ . add ( 'types' , buildTypes )
63
+ . addMultipleFormats ( 'react' , buildReact )
64
+ . addMultipleFormats ( 'vue' , buildVue )
65
+ . addMultipleFormats ( 'svelte' , buildSvelte )
66
+ . add ( 'angular' , buildAngular )
67
+ . add ( 'styles' , ( ) => buildStyles ( outputDir ) )
68
+ . run ( ) ;
27
69
} ) ( ) ;
0 commit comments