@@ -28,15 +28,18 @@ class WebpackFactory extends Factory {
28
28
*/
29
29
env ( env ) {
30
30
if ( ! env ) {
31
- throw new Error (
32
- 'Env param is required!'
33
- ) ;
31
+ throw new Error ( 'Env param is required!' ) ;
34
32
}
35
33
const factories = Object . getPrototypeOf ( this ) . __envFactories ;
36
34
if ( factories [ env ] ) {
37
35
return factories [ env ] ;
38
36
} else {
39
- const factory = new WebpackFactory ( this . __webpackConfig , this . __plugins , this . __rules ) ;
37
+ const factory = new WebpackFactory (
38
+ Object . assign ( { } , this . __webpackConfig ) ,
39
+ Object . assign ( { } , this . __plugins ) ,
40
+ [ ] . concat ( this . __rules )
41
+ ) ;
42
+ factory . env = env ;
40
43
Object . getPrototypeOf ( this ) . __envFactories [ env ] = factory ;
41
44
return factory ;
42
45
}
@@ -90,7 +93,6 @@ class WebpackFactory extends Factory {
90
93
return Object . assign ( { } , this . __webpackConfig ) ;
91
94
}
92
95
93
-
94
96
set ( key , config ) {
95
97
this . __webpackConfig [ key ] = config ;
96
98
return this ;
@@ -131,9 +133,7 @@ class WebpackFactory extends Factory {
131
133
this . __plugins [ plugin . alias ] = plugin ;
132
134
return this ;
133
135
} else {
134
- throw new Error (
135
- `${ args [ 0 ] } the plugin alias not exsit! `
136
- ) ;
136
+ throw new Error ( `${ args [ 0 ] } the plugin alias not exsit! ` ) ;
137
137
}
138
138
}
139
139
if ( args . length === 1 && args [ 0 ] . constructor === Plugin ) {
@@ -151,18 +151,21 @@ class WebpackFactory extends Factory {
151
151
if ( is . string ( params ) ) {
152
152
return this . __plugins [ params ] ;
153
153
} else if ( is . function ( params ) ) {
154
- return params ( Object . values ( this . __plugins ) ) ;
154
+ for ( const p of Object . values ( this . __plugins ) ) {
155
+ if ( params ( p ) ) {
156
+ return p ;
157
+ }
158
+ }
159
+ return null ;
155
160
} else {
156
- throw new Error (
157
- 'get plugin param type exception!'
158
- ) ;
161
+ throw new Error ( 'get plugin param type exception!' ) ;
159
162
}
160
163
}
161
164
162
165
setPlugin ( ...args ) {
163
166
let pluginObj = { } ;
164
167
if ( args . length === 1 && args [ 0 ] . constructor === Plugin ) {
165
- pluginObj = args [ 0 ] ;
168
+ [ pluginObj ] = args ;
166
169
} else {
167
170
pluginObj = new Plugin ( ...args ) ;
168
171
}
@@ -183,9 +186,7 @@ class WebpackFactory extends Factory {
183
186
} else if ( is . function ( filter ) ) {
184
187
return filter ( Object . values ( definePlugins ) ) ;
185
188
} else {
186
- throw new Error (
187
- 'use plugin param type exception!'
188
- ) ;
189
+ throw new Error ( 'use plugin param type exception!' ) ;
189
190
}
190
191
}
191
192
@@ -197,17 +198,14 @@ class WebpackFactory extends Factory {
197
198
this . __plugins = { } ;
198
199
}
199
200
200
-
201
201
addRule ( ...args ) {
202
202
if ( args . length === 1 && ! is . object ( args [ 0 ] ) ) {
203
203
const alias = args [ 0 ] ;
204
204
if ( this . useRule ( alias ) ) {
205
205
this . __rules . push ( this . useRule ( alias ) ) ;
206
206
return this ;
207
207
} else {
208
- throw new Error (
209
- `${ args [ 0 ] } the rule alias not exsit! `
210
- ) ;
208
+ throw new Error ( `${ args [ 0 ] } the rule alias not exsit! ` ) ;
211
209
}
212
210
}
213
211
@@ -223,28 +221,36 @@ class WebpackFactory extends Factory {
223
221
224
222
getRule ( params ) {
225
223
if ( is . string ( params ) ) {
226
- return this . __rules . find ( v => v . options . test . test ( params ) === true ) ;
224
+ return this . __rules . find (
225
+ v => v . alias === params || v . options . test . test ( params ) === true
226
+ ) ;
227
227
} else if ( is . function ( params ) ) {
228
- return params ( this . __rules ) ;
228
+ for ( const rule of this . __rules ) {
229
+ if ( params ( rule ) ) {
230
+ return rule ;
231
+ }
232
+ }
233
+ return null ;
229
234
} else if ( is . regexp ( params ) ) {
230
- return this . __rules . find ( v => v . options . test . toString ( ) === params . toString ( ) ) ;
231
- } else {
232
- throw new Error (
233
- 'get rule param type exception!'
235
+ return this . __rules . find (
236
+ v => v . options . test . toString ( ) === params . toString ( )
234
237
) ;
238
+ } else {
239
+ throw new Error ( 'get rule param type exception!' ) ;
235
240
}
236
241
}
237
242
238
-
239
243
setRule ( ...args ) {
240
244
let ruleObj = { } ;
241
245
if ( args . length === 1 && args [ 0 ] . constructor === Rule ) {
242
- ruleObj = args [ 0 ] ;
246
+ [ ruleObj ] = args ;
243
247
} else {
244
248
ruleObj = new Rule ( ...args ) ;
245
249
}
246
250
247
- let exsitRule = this . __rules . find ( v => v . alias . toString ( ) === ruleObj . alias . toString ( ) ) ;
251
+ let exsitRule = this . __rules . find (
252
+ v => v . alias . toString ( ) === ruleObj . alias . toString ( )
253
+ ) ;
248
254
if ( exsitRule ) {
249
255
exsitRule = ruleObj ;
250
256
} else {
@@ -290,9 +296,9 @@ class WebpackFactory extends Factory {
290
296
this . __rules = [ ] ;
291
297
}
292
298
293
-
294
299
defineLoader ( name , resolve ) {
295
- Object . getPrototypeOf ( this ) . __defineloaders [ name ] = resolve || require . resolve ( name ) ;
300
+ Object . getPrototypeOf ( this ) . __defineloaders [ name ] =
301
+ resolve || require . resolve ( name ) ;
296
302
return this ;
297
303
}
298
304
0 commit comments