Skip to content

Commit 18d8438

Browse files
committed
feat: return new object for webpack config and upgrade Readme.md
1 parent d0e8ff0 commit 18d8438

File tree

5 files changed

+150
-21
lines changed

5 files changed

+150
-21
lines changed

packages/beidou-webpack/README-ZH.md

+67-6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ exports.webpack = {
7979

8080
**custom.configPath**: 先定义 webpack 配置文件路径
8181

82+
#### 配置文件内容示例:
83+
8284
```js
8385
// webpack.config.js
8486
// 配置方案1:
@@ -131,10 +133,8 @@ module.exports = {
131133
hot: true,
132134
},
133135
};
134-
```
135136

136-
高级自定义配置:
137-
```js
137+
// 自定义配置3 :
138138
// 更多配置方法,可参看单测用例
139139
module.exports = (app, defaultConfig, dev, target) => {
140140
const factory = app.webpackFactory;
@@ -187,10 +187,14 @@ module.exports = (app, defaultConfig, dev, target) => {
187187
return factory.getConfig(); // 返回配置
188188

189189
};
190+
190191
```
191192

193+
#### 数据结构:
194+
192195
> Class Plugin Struct
193-
```
196+
197+
```js
194198
class Plugin {
195199
object , // instance object for webpack plugin
196200
class, // class for webpack plugin
@@ -201,15 +205,72 @@ class Plugin {
201205
```
202206

203207
> Class Rule Struct
204-
```
208+
209+
```js
205210
class Rule {
206211
opitons, // initialize config for rule
207212
alias
208213
}
214+
```
215+
216+
## 配置扩展
217+
218+
##### 自定义 Loader:
219+
```js
220+
app.webpackFactory.defineLoader({
221+
'babel-loader',
222+
require.resolve('babel-loader')
223+
})
224+
```
225+
226+
##### 自定义 Rule:
227+
```js
228+
app.webpackFactory.defineRule({
229+
test: /\.tsx?$/,
230+
exclude: /node_modules/,
231+
use: {
232+
loader: app.webpackFactory.useLoader('babel-loader'), //使用自定义loader
233+
options: {
234+
babelrc: false,
235+
presets: ['preset-typescript'],
236+
},
237+
},
238+
},'tsx')
239+
// 使用自定义Rule增加到webpack项中
240+
app.webpackFactory.addRule(
241+
app.webpackFactory.useRule('tsx')
242+
)
243+
// 获取已配置的Rule
244+
app.webpackFactory.getRule('tsx'); // return Rule Object
245+
```
246+
247+
##### 自定义 Plugin:
248+
```js
249+
app.webpackFactory.definePlugin(webpack.NoEmitOnErrorsPlugin,{},'NoEmitOnErrorsPlugin')
250+
251+
// 使用自定义Plugin增加到webpack项中
252+
app.webpackFactory.addPlugin(
253+
app.webpackFactory.usePlugin('NoEmitOnErrorsPlugin')
254+
)
255+
// 直接增加Plugin到webpack中
256+
//方式1:
257+
app.webpackFactory.addPlugin(webpack.optimize.UglifyJsPlugin, {
258+
compress: {
259+
warnings: false,
260+
},
261+
}, 'UglifyJsPlugin')
262+
// 方式2:
263+
app.webpackFactory.addPlugin(
264+
new webpack.optimize.UglifyJsPlugin({
265+
compress: {
266+
warnings: false,
267+
},
268+
})
269+
)
209270

210271
```
211272

212-
> app.webpackFactory 提供的函数如下:
273+
## webpackFactory常用函数说明:
213274

214275
### 重置配置项: reset(value)
215276
#### Parameters

packages/beidou-webpack/README.md

+69-8
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ module.exports = {
134134
},
135135
};
136136

137-
```
138-
139-
> Advanced custom configuration :
140-
```js
137+
// Advanced custom configuration :
141138
// find more example in unittest, please
142139
module.exports = (app, defaultConfig, dev, target) => {
143140
const factory = app.webpackFactory;
@@ -192,8 +189,12 @@ module.exports = (app, defaultConfig, dev, target) => {
192189
};
193190

194191
```
195-
> Class Plugin Struct
196-
```
192+
193+
#### Class Struct
194+
195+
> Class Struct for Plugin
196+
197+
```js
197198
class Plugin {
198199
object , // instance object for webpack plugin
199200
class, // class for webpack plugin
@@ -203,14 +204,74 @@ class Plugin {
203204

204205
```
205206

206-
> Class Rule Struct
207-
```
207+
> Class Struct for Rule
208+
209+
```js
208210
class Rule {
209211
opitons, // initialize config for rule
210212
alias
211213
}
212214

213215
```
216+
217+
## Webpackfactory
218+
219+
##### Define Custom Loader:
220+
```js
221+
app.webpackFactory.defineLoader({
222+
'babel-loader',
223+
require.resolve('babel-loader')
224+
})
225+
```
226+
227+
##### Define Custom Rule:
228+
```js
229+
app.webpackFactory.defineRule({
230+
test: /\.tsx?$/,
231+
exclude: /node_modules/,
232+
use: {
233+
loader: app.webpackFactory.useLoader('babel-loader'), //used the defined loader.
234+
options: {
235+
babelrc: false,
236+
presets: ['preset-typescript'],
237+
},
238+
},
239+
},'tsx')
240+
//add the rule config to webpack factory
241+
app.webpackFactory.addRule(
242+
app.webpackFactory.useRule('tsx')
243+
)
244+
//add the rule config to webpack factory
245+
app.webpackFactory.getRule('tsx'); // return Rule Object
246+
```
247+
248+
##### Define Custom Plugin:
249+
```js
250+
app.webpackFactory.definePlugin(webpack.NoEmitOnErrorsPlugin,{},'NoEmitOnErrorsPlugin')
251+
252+
// add defined plugin to webpack factory
253+
app.webpackFactory.addPlugin(
254+
app.webpackFactory.usePlugin('NoEmitOnErrorsPlugin')
255+
)
256+
257+
//method 1:
258+
app.webpackFactory.addPlugin(webpack.optimize.UglifyJsPlugin, {
259+
compress: {
260+
warnings: false,
261+
},
262+
}, 'UglifyJsPlugin')
263+
//method 2:
264+
app.webpackFactory.addPlugin(
265+
new webpack.optimize.UglifyJsPlugin({
266+
compress: {
267+
warnings: false,
268+
},
269+
})
270+
)
271+
272+
```
273+
274+
214275
> app.webpackFactory methods list:
215276
216277
### reset(value)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = (app, entry, dev) => {
6767
app.webpackFactory.addPlugin('UglifyJsPlugin');
6868
app.webpackFactory.addPlugin('DefinePlugin');
6969
} else {
70-
app.webpackFactory.getConfig().devServer.hot = true;
70+
app.webpackFactory.get('devServer').hot = true;
7171
app.webpackFactory.setPlugin(
7272
webpack.DefinePlugin, {
7373
'process.env.NODE_ENV': JSON.stringify('development'),

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const {
1414

1515
module.exports = (app, entry, dev) => {
1616
common(app, entry, dev);
17-
app.webpackFactory.getConfig().output.libraryTarget = 'commonjs';
18-
app.webpackFactory.getConfig().target = 'node';
19-
app.webpackFactory.getConfig().externals = /^react(-dom)?$/;
20-
app.webpackFactory.getConfig().node = {
17+
app.webpackFactory.get('output').libraryTarget = 'commonjs';
18+
app.webpackFactory.set('target', 'node');
19+
app.webpackFactory.set('externals', /^react(-dom)?$/);
20+
app.webpackFactory.set('node', {
2121
__filename: true,
2222
__dirname: true,
23-
};
23+
});
2424

2525
[
2626
{

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class WebpackFactory extends Factory {
8787
} else {
8888
this.__webpackConfig.module.rules = rules;
8989
}
90-
return this.__webpackConfig;
90+
return Object.assign({}, this.__webpackConfig);
9191
}
9292

9393

@@ -268,6 +268,13 @@ class WebpackFactory extends Factory {
268268
if (is.regexp(filter)) {
269269
return defineRules[filter.toString()];
270270
}
271+
272+
if (is.string(filter)) {
273+
const rule = Object.values(defineRules).find(v => v.alias === filter);
274+
if (rule) {
275+
return rule;
276+
}
277+
}
271278
if (is.string(filter)) {
272279
return Object.values(defineRules).find(v => v.options.test.test(filter));
273280
}

0 commit comments

Comments
 (0)