@@ -28,159 +28,162 @@ interface IBundleOpts {
28
28
configProvider : BundleConfigProvider ;
29
29
buildDependencies ?: string [ ] ;
30
30
watch ?: boolean ;
31
+ watchOnly ?: boolean ;
31
32
}
32
33
33
- function bundle ( opts : Omit < IBundleOpts , 'watch' > ) : Promise < void > ;
34
+ function bundle ( opts : Omit < IBundleOpts , 'watch' | 'watchOnly' > ) : Promise < void > ;
34
35
function bundle ( opts : IBundleOpts ) : Promise < IBundleWatcher > ;
35
36
async function bundle ( opts : IBundleOpts ) : Promise < void | IBundleWatcher > {
36
37
const enableCache = process . env . FATHER_CACHE !== 'none' ;
37
38
const closeHandlers : webpack . Watching [ 'close' ] [ ] = [ ] ;
38
39
39
- for ( let i = 0 ; i < opts . configProvider . configs . length ; i += 1 ) {
40
- const config = opts . configProvider . configs [ i ] ;
41
- const { plugins : extraPostCSSPlugins , ...postcssLoader } =
42
- config . postcssOptions || { } ;
43
- const babelSCOpts = getBabelStyledComponentsOpts ( opts . configProvider . pkg ) ;
44
- // workaround for combine continuous onBuildComplete log in watch mode
45
- const logStatus = lodash . debounce (
46
- ( ) =>
47
- logger . info (
48
- `Bundle from ${ chalk . yellow ( config . entry ) } to ${ chalk . yellow (
49
- path . join ( config . output . path , config . output . filename ) ,
50
- ) } `,
51
- ) ,
52
- 100 ,
53
- { leading : true , trailing : false } ,
54
- ) ;
55
-
56
- // log for normal build
57
- ! opts . watch && logStatus ( ) ;
58
- await bundler . build ( {
59
- cwd : opts . cwd ,
60
- watch : opts . watch ,
61
- config : {
62
- alias : config . alias ,
63
- autoprefixer : config . autoprefixer ,
64
- chainWebpack : config . chainWebpack ,
65
- define : config . define ,
66
- devtool : config . sourcemap && 'source-map' ,
67
- externals : config . externals ,
68
- outputPath : config . output . path ,
69
-
70
- // postcss config
71
- extraPostCSSPlugins,
72
- postcssLoader,
73
-
74
- ...( config . extractCSS !== false ? { } : { styleLoader : { } } ) ,
75
-
76
- // less config
77
- theme : config . theme ,
78
-
79
- // compatible with IE11 by default
80
- targets : getBundleTargets ( config ) ,
81
- jsMinifier : JSMinifier . terser ,
82
- cssMinifier : CSSMinifier . cssnano ,
83
- extraBabelIncludes : [ / n o d e _ m o d u l e s / ] ,
84
-
85
- // set cache parent directory, will join it with `bundler-webpack`
86
- // ref: https://github.com/umijs/umi/blob/8dad8c5af0197cd62db11f4b4c85d6bc1db57db1/packages/bundler-webpack/src/build.ts#L32
87
- cacheDirectoryPath : getCachePath ( ) ,
88
- } ,
89
- entry : {
90
- [ path . parse ( config . output . filename ) . name ] : path . join (
91
- opts . cwd ,
92
- config . entry ,
93
- ) ,
94
- } ,
95
- babelPreset : [
96
- require . resolve ( '@umijs/babel-preset-umi' ) ,
97
- {
98
- presetEnv : {
99
- targets : getBundleTargets ( config ) ,
100
- } ,
101
- presetReact : getBabelPresetReactOpts (
102
- opts . configProvider . pkg ,
40
+ if ( ! opts . watchOnly ) {
41
+ for ( let i = 0 ; i < opts . configProvider . configs . length ; i += 1 ) {
42
+ const config = opts . configProvider . configs [ i ] ;
43
+ const { plugins : extraPostCSSPlugins , ...postcssLoader } =
44
+ config . postcssOptions || { } ;
45
+ const babelSCOpts = getBabelStyledComponentsOpts ( opts . configProvider . pkg ) ;
46
+ // workaround for combine continuous onBuildComplete log in watch mode
47
+ const logStatus = lodash . debounce (
48
+ ( ) =>
49
+ logger . info (
50
+ `Bundle from ${ chalk . yellow ( config . entry ) } to ${ chalk . yellow (
51
+ path . join ( config . output . path , config . output . filename ) ,
52
+ ) } `,
53
+ ) ,
54
+ 100 ,
55
+ { leading : true , trailing : false } ,
56
+ ) ;
57
+
58
+ // log for normal build
59
+ ! opts . watch && logStatus ( ) ;
60
+ await bundler . build ( {
61
+ cwd : opts . cwd ,
62
+ watch : opts . watch ,
63
+ config : {
64
+ alias : config . alias ,
65
+ autoprefixer : config . autoprefixer ,
66
+ chainWebpack : config . chainWebpack ,
67
+ define : config . define ,
68
+ devtool : config . sourcemap && 'source-map' ,
69
+ externals : config . externals ,
70
+ outputPath : config . output . path ,
71
+
72
+ // postcss config
73
+ extraPostCSSPlugins,
74
+ postcssLoader,
75
+
76
+ ...( config . extractCSS !== false ? { } : { styleLoader : { } } ) ,
77
+
78
+ // less config
79
+ theme : config . theme ,
80
+
81
+ // compatible with IE11 by default
82
+ targets : getBundleTargets ( config ) ,
83
+ jsMinifier : JSMinifier . terser ,
84
+ cssMinifier : CSSMinifier . cssnano ,
85
+ extraBabelIncludes : [ / n o d e _ m o d u l e s / ] ,
86
+
87
+ // set cache parent directory, will join it with `bundler-webpack`
88
+ // ref: https://github.com/umijs/umi/blob/8dad8c5af0197cd62db11f4b4c85d6bc1db57db1/packages/bundler-webpack/src/build.ts#L32
89
+ cacheDirectoryPath : getCachePath ( ) ,
90
+ } ,
91
+ entry : {
92
+ [ path . parse ( config . output . filename ) . name ] : path . join (
103
93
opts . cwd ,
94
+ config . entry ,
104
95
) ,
105
- presetTypeScript : { } ,
106
- pluginTransformRuntime : { } ,
107
- pluginLockCoreJS : { } ,
108
- pluginDynamicImportNode : false ,
109
96
} ,
110
- ] ,
111
- beforeBabelPlugins : [
112
- require . resolve ( 'babel-plugin-dynamic-import-node' ) ,
113
- ...( babelSCOpts
114
- ? [ [ require . resolve ( 'babel-plugin-styled-components' ) , babelSCOpts ] ]
115
- : [ ] ) ,
116
- ] ,
117
- extraBabelPresets : config . extraBabelPresets ,
118
- extraBabelPlugins : config . extraBabelPlugins ,
119
-
120
- // configure library related options
121
- chainWebpack ( memo : any ) {
122
- memo . output . libraryTarget ( 'umd' ) ;
123
-
124
- if ( config ?. name ) {
125
- memo . output . library ( config . name ) ;
126
- }
127
-
128
- // modify webpack target
129
- if ( config . platform === 'node' ) {
130
- memo . target ( 'node' ) ;
131
- }
132
-
133
- if ( enableCache ) {
134
- // use father version as cache version
135
- memo . merge ( {
136
- cache : { version : require ( '../../../package.json' ) . version } ,
137
- } ) ;
138
- }
139
-
140
- // also bundle svg as asset, because father force disable svgr
141
- const imgRule = memo . module . rule ( 'asset' ) . oneOf ( 'image' ) ;
142
-
143
- imgRule . test (
144
- new RegExp ( imgRule . get ( 'test' ) . source . replace ( / ( \| p n g ) / , '$1|svg' ) ) ,
145
- ) ;
97
+ babelPreset : [
98
+ require . resolve ( '@umijs/babel-preset-umi' ) ,
99
+ {
100
+ presetEnv : {
101
+ targets : getBundleTargets ( config ) ,
102
+ } ,
103
+ presetReact : getBabelPresetReactOpts (
104
+ opts . configProvider . pkg ,
105
+ opts . cwd ,
106
+ ) ,
107
+ presetTypeScript : { } ,
108
+ pluginTransformRuntime : { } ,
109
+ pluginLockCoreJS : { } ,
110
+ pluginDynamicImportNode : false ,
111
+ } ,
112
+ ] ,
113
+ beforeBabelPlugins : [
114
+ require . resolve ( 'babel-plugin-dynamic-import-node' ) ,
115
+ ...( babelSCOpts
116
+ ? [ [ require . resolve ( 'babel-plugin-styled-components' ) , babelSCOpts ] ]
117
+ : [ ] ) ,
118
+ ] ,
119
+ extraBabelPresets : config . extraBabelPresets ,
120
+ extraBabelPlugins : config . extraBabelPlugins ,
121
+
122
+ // configure library related options
123
+ chainWebpack ( memo : any ) {
124
+ memo . output . libraryTarget ( 'umd' ) ;
125
+
126
+ if ( config ?. name ) {
127
+ memo . output . library ( config . name ) ;
128
+ }
146
129
147
- // disable progress bar
148
- memo . plugins . delete ( 'progress-plugin' ) ;
130
+ // modify webpack target
131
+ if ( config . platform === 'node' ) {
132
+ memo . target ( 'node' ) ;
133
+ }
149
134
150
- // auto bump analyze port
151
- /* istanbul ignore if -- @preserve */
152
- if ( process . env . ANALYZE ) {
153
- memo . plugin ( 'webpack-bundle-analyzer' ) . tap ( ( args : any ) => {
154
- args [ 0 ] . analyzerPort += i ;
135
+ if ( enableCache ) {
136
+ // use father version as cache version
137
+ memo . merge ( {
138
+ cache : { version : require ( '../../../package.json' ) . version } ,
139
+ } ) ;
140
+ }
155
141
156
- return args ;
157
- } ) ;
158
- }
142
+ // also bundle svg as asset, because father force disable svgr
143
+ const imgRule = memo . module . rule ( 'asset' ) . oneOf ( 'image' ) ;
159
144
160
- return memo ;
161
- } ,
145
+ imgRule . test (
146
+ new RegExp ( imgRule . get ( 'test' ) . source . replace ( / ( \| p n g ) / , '$1|svg' ) ) ,
147
+ ) ;
148
+
149
+ // disable progress bar
150
+ memo . plugins . delete ( 'progress-plugin' ) ;
151
+
152
+ // auto bump analyze port
153
+ /* istanbul ignore if -- @preserve */
154
+ if ( process . env . ANALYZE ) {
155
+ memo . plugin ( 'webpack-bundle-analyzer' ) . tap ( ( args : any ) => {
156
+ args [ 0 ] . analyzerPort += i ;
157
+
158
+ return args ;
159
+ } ) ;
160
+ }
161
+
162
+ return memo ;
163
+ } ,
162
164
163
- // enable webpack persistent cache
164
- ...( enableCache
165
- ? {
165
+ // enable webpack persistent cache
166
+ ...( enableCache
167
+ ? {
166
168
cache : {
167
169
buildDependencies : opts . buildDependencies ,
168
170
} ,
169
171
}
170
- : { } ) ,
172
+ : { } ) ,
171
173
172
- // collect close handlers for watch mode
173
- ...( opts . watch
174
- ? {
174
+ // collect close handlers for watch mode
175
+ ...( opts . watch
176
+ ? {
175
177
onBuildComplete ( { isFirstCompile, close } : any ) {
176
178
if ( isFirstCompile ) closeHandlers . push ( close ) ;
177
179
// log for watch mode
178
180
else logStatus ( ) ;
179
181
} ,
180
182
}
181
- : { } ) ,
182
- disableCopy : true ,
183
- } ) ;
183
+ : { } ) ,
184
+ disableCopy : true ,
185
+ } ) ;
186
+ }
184
187
}
185
188
186
189
// return watching closer for watch mode
0 commit comments