Skip to content

Commit 06c0bfe

Browse files
committed
fix: Merge webpack config when build component
1 parent 7ed5477 commit 06c0bfe

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { merge } from 'webpack-merge'
2+
import Chain from 'webpack-chain'
3+
import isObject from 'lodash/isObject'
4+
import isFunction from 'lodash/isFunction'
5+
import getBabelConfig from '@ols-scripts/config/babel/getBabelConfig'
6+
import processNodeEnv from '@ols-scripts/util/processNodeEnv'
7+
import getDefaultWebpackConfig from './webpack/config'
8+
9+
class Config {
10+
[x: string]: any
11+
12+
getBabelConfig = getBabelConfig
13+
14+
constructor(ctx) {
15+
this.ctx = ctx
16+
// 初始化node env
17+
processNodeEnv(ctx.commandApi.command)
18+
}
19+
20+
getUserConfig() {
21+
let userConfig
22+
try {
23+
userConfig = require(this.ctx.userConfigDir)
24+
} catch (error) {
25+
userConfig = {}
26+
}
27+
28+
return userConfig
29+
}
30+
31+
resolveChainWebpackConfig(webpackConfig) {
32+
const webpackChain = new Chain()
33+
34+
this.ctx.webpackChainFns.forEach((fn) => {
35+
isFunction(fn) && fn(webpackChain)
36+
})
37+
38+
return merge(webpackConfig, webpackChain.toConfig())
39+
}
40+
41+
resolveConfigureWebpackConfig(config) {
42+
this.ctx.webpackConfigFns.forEach((fn) => {
43+
if (isFunction(fn)) {
44+
const res = fn(config, merge)
45+
if (res) config = res
46+
} else if (isObject(fn)) {
47+
config = merge(config, fn)
48+
}
49+
})
50+
51+
return config
52+
}
53+
54+
async getWebpackConfig() {
55+
// 默认config
56+
let webpackConfig = getDefaultWebpackConfig(this.ctx)
57+
58+
// 处理chainWebpack
59+
webpackConfig = this.resolveChainWebpackConfig(webpackConfig)
60+
61+
// 处理configureWebpack
62+
webpackConfig = this.resolveConfigureWebpackConfig(webpackConfig)
63+
64+
return webpackConfig
65+
}
66+
}
67+
68+
export default Config

packages/registry-component/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logger from '@ols-scripts/util/logger'
2-
import webpackConfig from './webpack/config'
2+
import ConfigAPI from './Config'
33

44
import dev from './commands/dev'
55
import build from './commands/build'
@@ -13,7 +13,7 @@ module.exports = {
1313
ctx.eventHooks = {}
1414
ctx.webpackChainFns = []
1515
ctx.webpackConfigFns = []
16-
ctx.webpackConfig = webpackConfig(ctx)
16+
ctx.config = new ConfigAPI(ctx)
1717

1818
if (ctx.userConfig.chainWebpack) {
1919
ctx.webpackChainFns.push(ctx.userConfig.chainWebpack)
@@ -22,6 +22,8 @@ module.exports = {
2222
ctx.webpackConfigFns.push(ctx.userConfig.configureWebpack)
2323
}
2424

25+
ctx.webpackConfig = await ctx.config.getWebpackConfig()
26+
2527
ctx.applyHook = async function applyHook(key, opts = {}) {
2628
const hooks = ctx.eventHooks[key] || []
2729
const results = []

0 commit comments

Comments
 (0)