@@ -8,18 +8,63 @@ function hasProcessFlag(flag) {
88 return process . argv . join ( '' ) . indexOf ( flag ) > - 1
99}
1010
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+ */
1154export type WebpackConfigWithMetadata = WebpackConfig & { metadata ?: any }
1255export type EasyWebpackConfig = WebpackConfigWithMetadata | ( ( this : WebpackConfigWithMetadata ) => WebpackConfigWithMetadata )
56+ export const generateConfigOptions = { addDefaultMetadata : true , alwaysAddBaseMetadata : false }
1357
1458export 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 = {
1762 port : process . env . WEBPACK_PORT || 9000 ,
1863 host : process . env . WEBPACK_HOST || 'localhost' ,
1964 ENV : process . env . NODE_ENV || process . env . ENV || 'development' ,
2065 HMR : hasProcessFlag ( 'hot' ) || ! ! process . env . WEBPACK_HMR ,
2166 }
22- } as WebpackConfigWithMetadata
67+ }
2368
2469 for ( let configMethod of configs ) {
2570 if ( typeof configMethod === 'function' ) {
0 commit comments