Skip to content

Commit ecd7b18

Browse files
committed
feat: 配置工厂函数修整
1 parent f91d6f0 commit ecd7b18

File tree

8 files changed

+108
-38
lines changed

8 files changed

+108
-38
lines changed

packages/beidou-webpack/README-ZH.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,28 @@ class Rule {
284284

285285
### 获取定义的插件: usePlugin(alias)
286286
#### Parameters
287-
* alias {String} 插件别名
287+
* alias {String|Fucntion|Regexp} 插件别名|自定义函数|正则
288288
#### return
289289
* {Plugin}
290290

291291

292-
### 设置配置规则: addRule(options,alias)
292+
### 增加配置规则: addRule(options,alias)
293293
#### Parameters
294-
* options {Object} 配置项
294+
* options {Object|Rule} 配置项|Rule实例
295+
* [alias] {String} 别名
296+
#### return
297+
* this
298+
299+
### 设置配置规则: setRule(options,alias)
300+
#### Parameters
301+
* options {Object|Rule} 配置项|Rule实例
295302
* [alias] {String} 别名
296303
#### return
297304
* this
298305

299306
### 获取配置规则: getRule(filter)
300307
#### Parameters
301-
* filter {String|Function} 配置项|自定义函数
308+
* filter {String|Function|Regexp} 配置项|自定义函数|正则
302309
#### return
303310
* {Rule}
304311

@@ -311,7 +318,7 @@ class Rule {
311318

312319
### 获取定义的配置规则: useRule(alias)
313320
#### Parameters
314-
* alias {String} 别名
321+
* alias {String|Function|Regexp} 别名|自定义函数|正则
315322

316323
#### return
317324
* {Rule}

packages/beidou-webpack/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,16 @@ class Rule {
290290
* {Plugin}
291291

292292

293-
### addRule(options,alias)
293+
### addRule(options,alias)
294294
#### Parameters
295-
* options {Object}
295+
* options {Object|Rule}
296+
* [alias] {String}
297+
#### return
298+
* this
299+
300+
### setRule(options,alias)
301+
#### Parameters
302+
* options {Object|Rule}
296303
* [alias] {String}
297304
#### return
298305
* this

packages/beidou-webpack/config/webpack/webpack.browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = (app, entry, dev) => {
3737
imageLoaderConfig,
3838
fileLoaderConfig,
3939
].forEach((v) => {
40-
app.webpackFactory.defineRule(v).addRule(v.test);
40+
app.webpackFactory.defineRule(v).addRule(v);
4141
});
4242

4343
app.webpackFactory

packages/beidou-webpack/config/webpack/webpack.common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (app, entry, dev) => {
2323
app.webpackFactory.definePlugin(app.IsomorphicPlugin);
2424
const { universal } = app.config.isomorphic;
2525
if (universal) {
26-
app.webpackFactory.setPlugin(app.IsomorphicPlugin, universal);
26+
app.webpackFactory.addPlugin(app.IsomorphicPlugin, universal);
2727
}
2828

2929
let finalConfig = {};

packages/beidou-webpack/config/webpack/webpack.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = (app, entry, dev) => {
4343
imageLoaderConfig,
4444
fileLoaderConfig,
4545
].forEach((v) => {
46-
app.webpackFactory.defineRule(v).addRule(v.test);
46+
app.webpackFactory.defineRule(v).addRule(v);
4747
});
4848

4949
app.webpackFactory

packages/beidou-webpack/lib/factory/rule.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ class Rule {
1010
this.options = options;
1111
this.alias = alias || options.test;
1212
}
13+
14+
init() {
15+
return this.options;
16+
}
1317
}
1418
module.exports = Rule;

packages/beidou-webpack/lib/factory/webpack.js

+80-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WebpackFactory extends Factory {
1818
super();
1919
this.__webpackConfig = config || {};
2020
this.__plugins = plugins || {};
21-
this.__rules = rules || {};
21+
this.__rules = rules || [];
2222
}
2323

2424
/**
@@ -50,7 +50,7 @@ class WebpackFactory extends Factory {
5050
* generate final plugins config for webpack config
5151
* @return {Array}
5252
*/
53-
_getFinalPlugins() {
53+
getPluginConfig() {
5454
const plugins = [];
5555
Object.values(this.__plugins).forEach((v) => {
5656
plugins.push(v.init());
@@ -62,9 +62,9 @@ class WebpackFactory extends Factory {
6262
* generate final rules config for webpack config
6363
* @return {Array}
6464
*/
65-
_getFinalRules() {
65+
getRuleConfig() {
6666
const rules = [];
67-
Object.values(this.__rules).forEach((v) => {
67+
this.__rules.forEach((v) => {
6868
rules.push(v.options);
6969
});
7070
return rules;
@@ -75,8 +75,8 @@ class WebpackFactory extends Factory {
7575
* @return {Object}
7676
*/
7777
getConfig() {
78-
const plugins = this._getFinalPlugins();
79-
const rules = this._getFinalRules();
78+
const plugins = this.getPluginConfig();
79+
const rules = this.getRuleConfig();
8080

8181
this.__webpackConfig.plugins = plugins;
8282
const { module: mod } = this.__webpackConfig;
@@ -127,14 +127,22 @@ class WebpackFactory extends Factory {
127127
addPlugin(...args) {
128128
if (args.length === 1 && is.string(args[0])) {
129129
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;
131132
return this;
132133
} else {
133134
throw new Error(
134135
`${args[0]} the plugin alias not exsit! `
135136
);
136137
}
137138
}
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+
138146
const pluginObj = new Plugin(...args);
139147
if (this.__plugins[pluginObj.alias]) {
140148
throw new Error(
@@ -149,7 +157,7 @@ class WebpackFactory extends Factory {
149157
if (is.string(params)) {
150158
return this.__plugins[params];
151159
} else if (is.function(params)) {
152-
return params(this.__plugins);
160+
return params(Object.values(this.__plugins));
153161
} else {
154162
throw new Error(
155163
'get plugin param type exception!'
@@ -158,7 +166,12 @@ class WebpackFactory extends Factory {
158166
}
159167

160168
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+
}
162175
this.__plugins[pluginObj.alias] = pluginObj;
163176
return this;
164177
}
@@ -169,8 +182,21 @@ class WebpackFactory extends Factory {
169182
return this;
170183
}
171184

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;
174200
}
175201

176202
clearPlugin() {
@@ -182,7 +208,7 @@ class WebpackFactory extends Factory {
182208
if (args.length === 1 && !is.object(args[0])) {
183209
const alias = args[0];
184210
if (this.useRule(alias)) {
185-
this.__rules[alias] = this.useRule(alias);
211+
this.__rules.push(this.useRule(alias));
186212
return this;
187213
} else {
188214
throw new Error(
@@ -191,31 +217,45 @@ class WebpackFactory extends Factory {
191217
}
192218
}
193219

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;
199223
}
200-
this.__rules[ruleObj.alias] = ruleObj;
224+
225+
const ruleObj = new Rule(...args);
226+
this.__rules.push(ruleObj);
201227
return this;
202228
}
203229

204230
getRule(params) {
205231
if (is.string(params)) {
206-
return this.__rules[params];
232+
return this.__rules.find(v => v.options.test.test(params) === true);
207233
} else if (is.function(params)) {
208234
return params(this.__rules);
235+
} else if (is.regexp(params)) {
236+
return this.__rules.find(v => v.options.test.toString() === params.toString());
209237
} else {
210238
throw new Error(
211239
'get rule param type exception!'
212240
);
213241
}
214242
}
215243

244+
216245
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+
}
219259
return this;
220260
}
221261

@@ -225,12 +265,27 @@ class WebpackFactory extends Factory {
225265
return this;
226266
}
227267

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;
230285
}
231286

232287
clearRule() {
233-
this.__rules = {};
288+
this.__rules = [];
234289
}
235290

236291

@@ -243,4 +298,5 @@ class WebpackFactory extends Factory {
243298
return Object.getPrototypeOf(this).__defineloaders[name];
244299
}
245300
}
301+
246302
module.exports = WebpackFactory;

packages/beidou-webpack/test/fixtures/custom-webpack-function/webpack/custom.webpack.config.js

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ module.exports = (app, defaultConfig, entry, isDev) => {
2424
},
2525
module: {
2626
"strictExportPresence": true,
27-
rules: [{
28-
test: /\.jsx?$/,
29-
use: 'babel-loader',
30-
}, ],
3127
},
3228
resolve: {
3329
extensions: ['.json', '.js', '.jsx'],

0 commit comments

Comments
 (0)