File tree 2 files changed +72
-2
lines changed
packages/registry-component/src
2 files changed +72
-2
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
import logger from '@ols-scripts/util/logger'
2
- import webpackConfig from './webpack/config '
2
+ import ConfigAPI from './Config '
3
3
4
4
import dev from './commands/dev'
5
5
import build from './commands/build'
@@ -13,7 +13,7 @@ module.exports = {
13
13
ctx . eventHooks = { }
14
14
ctx . webpackChainFns = [ ]
15
15
ctx . webpackConfigFns = [ ]
16
- ctx . webpackConfig = webpackConfig ( ctx )
16
+ ctx . config = new ConfigAPI ( ctx )
17
17
18
18
if ( ctx . userConfig . chainWebpack ) {
19
19
ctx . webpackChainFns . push ( ctx . userConfig . chainWebpack )
@@ -22,6 +22,8 @@ module.exports = {
22
22
ctx . webpackConfigFns . push ( ctx . userConfig . configureWebpack )
23
23
}
24
24
25
+ ctx . webpackConfig = await ctx . config . getWebpackConfig ( )
26
+
25
27
ctx . applyHook = async function applyHook ( key , opts = { } ) {
26
28
const hooks = ctx . eventHooks [ key ] || [ ]
27
29
const results = [ ]
You can’t perform that action at this time.
0 commit comments