@@ -8,18 +8,63 @@ function hasProcessFlag(flag) {
8
8
return process . argv . join ( '' ) . indexOf ( flag ) > - 1
9
9
}
10
10
11
+ export const mergeSummary = { dependencies : [ ] , merged : [ ] , skipped : [ ] }
12
+ export interface ConfigDescription {
13
+ name ?: string
14
+ dependencies ?: Array < string >
15
+ description ?: string
16
+ enabled ?: undefined | boolean | ( ( config ?: WebpackConfig ) => boolean )
17
+ action ?: 'append' | 'prepend' | 'replace' | ( ( previousConfig : WebpackConfig , thisConfig ?: WebpackConfig , name ?: string ) => WebpackConfig )
18
+ }
19
+
20
+ export const description = '📄'
21
+ export type WebpackConfigWithDescription = WebpackConfig & { '📄' ?: ConfigDescription }
22
+
23
+ export function merge ( config : WebpackConfig , ...configs : Array < WebpackConfigWithDescription > ) {
24
+ mergeSummary . dependencies = [ ]
25
+ mergeSummary . merged = [ ]
26
+ mergeSummary . skipped = [ ]
27
+ let i = 0
28
+ for ( let overlayConfig of configs ) {
29
+ let configDescription = overlayConfig [ description ] || { }
30
+ let name = configDescription . name || `unnamed config ${ String ( i ++ ) } `
31
+ let enabled = configDescription . enabled
32
+ let action = configDescription . action || 'append'
33
+ if ( configDescription . dependencies && configDescription . dependencies . length ) {
34
+ mergeSummary . dependencies . push ( ...overlayConfig [ description ] . dependencies )
35
+ }
36
+ delete overlayConfig [ description ]
37
+ if ( enabled === undefined || enabled === true || ( typeof enabled === 'function' && enabled ( config ) ) ) {
38
+ config = typeof action === 'function' ? action ( config , overlayConfig , name ) : assign ( config , overlayConfig , name , action )
39
+ mergeSummary . merged . push ( name )
40
+ } else {
41
+ mergeSummary . skipped . push ( name )
42
+ }
43
+ }
44
+ return config
45
+ }
46
+
47
+ /**
48
+ * Below are backwards compatibile easy-webpack configs:
49
+ */
50
+
51
+ /**
52
+ * A webpack config object with optional 'metadata'
53
+ */
11
54
export type WebpackConfigWithMetadata = WebpackConfig & { metadata ?: any }
12
55
export type EasyWebpackConfig = WebpackConfigWithMetadata | ( ( this : WebpackConfigWithMetadata ) => WebpackConfigWithMetadata )
56
+ export const generateConfigOptions = { addDefaultMetadata : true , alwaysAddBaseMetadata : false }
13
57
14
58
export function generateConfig ( ...configs : Array < EasyWebpackConfig > ) {
15
- let config = {
16
- metadata : {
59
+ let config = { } as WebpackConfigWithMetadata
60
+ if ( generateConfigOptions . alwaysAddBaseMetadata || ( ! config . metadata && generateConfigOptions . addDefaultMetadata ) ) {
61
+ config . metadata = {
17
62
port : process . env . WEBPACK_PORT || 9000 ,
18
63
host : process . env . WEBPACK_HOST || 'localhost' ,
19
64
ENV : process . env . NODE_ENV || process . env . ENV || 'development' ,
20
65
HMR : hasProcessFlag ( 'hot' ) || ! ! process . env . WEBPACK_HMR ,
21
66
}
22
- } as WebpackConfigWithMetadata
67
+ }
23
68
24
69
for ( let configMethod of configs ) {
25
70
if ( typeof configMethod === 'function' ) {
0 commit comments