@@ -18,7 +18,7 @@ class WebpackFactory extends Factory {
18
18
super ( ) ;
19
19
this . __webpackConfig = config || { } ;
20
20
this . __plugins = plugins || { } ;
21
- this . __rules = rules || { } ;
21
+ this . __rules = rules || [ ] ;
22
22
}
23
23
24
24
/**
@@ -50,7 +50,7 @@ class WebpackFactory extends Factory {
50
50
* generate final plugins config for webpack config
51
51
* @return {Array }
52
52
*/
53
- _getFinalPlugins ( ) {
53
+ getPluginConfig ( ) {
54
54
const plugins = [ ] ;
55
55
Object . values ( this . __plugins ) . forEach ( ( v ) => {
56
56
plugins . push ( v . init ( ) ) ;
@@ -62,9 +62,9 @@ class WebpackFactory extends Factory {
62
62
* generate final rules config for webpack config
63
63
* @return {Array }
64
64
*/
65
- _getFinalRules ( ) {
65
+ getRuleConfig ( ) {
66
66
const rules = [ ] ;
67
- Object . values ( this . __rules ) . forEach ( ( v ) => {
67
+ this . __rules . forEach ( ( v ) => {
68
68
rules . push ( v . options ) ;
69
69
} ) ;
70
70
return rules ;
@@ -75,8 +75,8 @@ class WebpackFactory extends Factory {
75
75
* @return {Object }
76
76
*/
77
77
getConfig ( ) {
78
- const plugins = this . _getFinalPlugins ( ) ;
79
- const rules = this . _getFinalRules ( ) ;
78
+ const plugins = this . getPluginConfig ( ) ;
79
+ const rules = this . getRuleConfig ( ) ;
80
80
81
81
this . __webpackConfig . plugins = plugins ;
82
82
const { module : mod } = this . __webpackConfig ;
@@ -127,14 +127,22 @@ class WebpackFactory extends Factory {
127
127
addPlugin ( ...args ) {
128
128
if ( args . length === 1 && is . string ( args [ 0 ] ) ) {
129
129
if ( this . usePlugin ( args [ 0 ] ) ) {
130
- this . __plugins [ args [ 0 ] ] = this . usePlugin ( args [ 0 ] ) ;
130
+ const plugin = this . usePlugin ( args [ 0 ] ) ;
131
+ this . __plugins [ plugin . alias ] = plugin ;
131
132
return this ;
132
133
} else {
133
134
throw new Error (
134
135
`${ args [ 0 ] } the plugin alias not exsit! `
135
136
) ;
136
137
}
137
138
}
139
+ if ( args . length === 1 && args [ 0 ] . constructor === Plugin ) {
140
+ const plugin = args [ 0 ] ;
141
+ this . __plugins [ plugin . alias ] = plugin ;
142
+ return this ;
143
+ }
144
+
145
+
138
146
const pluginObj = new Plugin ( ...args ) ;
139
147
if ( this . __plugins [ pluginObj . alias ] ) {
140
148
throw new Error (
@@ -149,7 +157,7 @@ class WebpackFactory extends Factory {
149
157
if ( is . string ( params ) ) {
150
158
return this . __plugins [ params ] ;
151
159
} else if ( is . function ( params ) ) {
152
- return params ( this . __plugins ) ;
160
+ return params ( Object . values ( this . __plugins ) ) ;
153
161
} else {
154
162
throw new Error (
155
163
'get plugin param type exception!'
@@ -158,7 +166,12 @@ class WebpackFactory extends Factory {
158
166
}
159
167
160
168
setPlugin ( ...args ) {
161
- const pluginObj = new Plugin ( ...args ) ;
169
+ let pluginObj = { } ;
170
+ if ( args . length === 1 && args [ 0 ] . constructor === Plugin ) {
171
+ pluginObj = args [ 0 ] ;
172
+ } else {
173
+ pluginObj = new Plugin ( ...args ) ;
174
+ }
162
175
this . __plugins [ pluginObj . alias ] = pluginObj ;
163
176
return this ;
164
177
}
@@ -169,8 +182,21 @@ class WebpackFactory extends Factory {
169
182
return this ;
170
183
}
171
184
172
- usePlugin ( alias ) {
173
- return Object . getPrototypeOf ( this ) . __definePlugins [ alias ] ;
185
+ usePlugin ( filter ) {
186
+ const definePlugins = Object . getPrototypeOf ( this ) . __definePlugins ;
187
+ if ( is . string ( filter ) ) {
188
+ return definePlugins [ filter ] ;
189
+ } else if ( is . function ( filter ) ) {
190
+ return filter ( Object . values ( definePlugins ) ) ;
191
+ } else {
192
+ throw new Error (
193
+ 'use plugin param type exception!'
194
+ ) ;
195
+ }
196
+ }
197
+
198
+ getDefinePlugins ( ) {
199
+ return Object . getPrototypeOf ( this ) . __definePlugins ;
174
200
}
175
201
176
202
clearPlugin ( ) {
@@ -182,7 +208,7 @@ class WebpackFactory extends Factory {
182
208
if ( args . length === 1 && ! is . object ( args [ 0 ] ) ) {
183
209
const alias = args [ 0 ] ;
184
210
if ( this . useRule ( alias ) ) {
185
- this . __rules [ alias ] = this . useRule ( alias ) ;
211
+ this . __rules . push ( this . useRule ( alias ) ) ;
186
212
return this ;
187
213
} else {
188
214
throw new Error (
@@ -191,31 +217,45 @@ class WebpackFactory extends Factory {
191
217
}
192
218
}
193
219
194
- const ruleObj = new Rule ( ...args ) ;
195
- if ( this . __rules [ ruleObj . alias ] ) {
196
- throw new Error (
197
- `${ ruleObj . alias } the rules alias exsit! please change alias.`
198
- ) ;
220
+ if ( args . length === 1 && args [ 0 ] . constructor === Rule ) {
221
+ this . __rules . push ( args [ 0 ] ) ;
222
+ return this ;
199
223
}
200
- this . __rules [ ruleObj . alias ] = ruleObj ;
224
+
225
+ const ruleObj = new Rule ( ...args ) ;
226
+ this . __rules . push ( ruleObj ) ;
201
227
return this ;
202
228
}
203
229
204
230
getRule ( params ) {
205
231
if ( is . string ( params ) ) {
206
- return this . __rules [ params ] ;
232
+ return this . __rules . find ( v => v . options . test . test ( params ) === true ) ;
207
233
} else if ( is . function ( params ) ) {
208
234
return params ( this . __rules ) ;
235
+ } else if ( is . regexp ( params ) ) {
236
+ return this . __rules . find ( v => v . options . test . toString ( ) === params . toString ( ) ) ;
209
237
} else {
210
238
throw new Error (
211
239
'get rule param type exception!'
212
240
) ;
213
241
}
214
242
}
215
243
244
+
216
245
setRule ( ...args ) {
217
- const ruleObj = new Rule ( ...args ) ;
218
- this . __rules [ ruleObj . alias ] = ruleObj ;
246
+ let ruleObj = { } ;
247
+ if ( args . length === 1 && args [ 0 ] . constructor === Rule ) {
248
+ ruleObj = args [ 0 ] ;
249
+ } else {
250
+ ruleObj = new Rule ( ...args ) ;
251
+ }
252
+
253
+ let exsitRule = this . __rules . find ( v => v . alias . toString ( ) === ruleObj . alias . toString ( ) ) ;
254
+ if ( exsitRule ) {
255
+ exsitRule = ruleObj ;
256
+ } else {
257
+ this . __rules . push ( ruleObj ) ;
258
+ }
219
259
return this ;
220
260
}
221
261
@@ -225,12 +265,27 @@ class WebpackFactory extends Factory {
225
265
return this ;
226
266
}
227
267
228
- useRule ( alias ) {
229
- return Object . getPrototypeOf ( this ) . __defineRules [ alias ] ;
268
+ useRule ( filter ) {
269
+ const defineRules = Object . getPrototypeOf ( this ) . __defineRules ;
270
+ if ( is . function ( filter ) ) {
271
+ return filter ( Object . values ( defineRules ) ) ;
272
+ }
273
+ if ( is . regexp ( filter ) ) {
274
+ return defineRules [ filter . toString ( ) ] ;
275
+ }
276
+ if ( is . string ( filter ) ) {
277
+ return Object . values ( defineRules ) . find ( v => v . options . test . test ( filter ) ) ;
278
+ }
279
+
280
+ return null ;
281
+ }
282
+
283
+ getDefineRules ( ) {
284
+ return Object . getPrototypeOf ( this ) . __defineRules ;
230
285
}
231
286
232
287
clearRule ( ) {
233
- this . __rules = { } ;
288
+ this . __rules = [ ] ;
234
289
}
235
290
236
291
@@ -243,4 +298,5 @@ class WebpackFactory extends Factory {
243
298
return Object . getPrototypeOf ( this ) . __defineloaders [ name ] ;
244
299
}
245
300
}
301
+
246
302
module . exports = WebpackFactory ;
0 commit comments