Skip to content
This repository was archived by the owner on May 29, 2022. It is now read-only.

Commit 5998808

Browse files
committed
feat(index): implement a merge function with simpler syntax
BREAKING CHANGE: generateConfig only adds metadata when it does not already exist! Set generateConfigOptions.alwaysAddBaseMetadata = true to go back to previous behavior. You can now also turn off generateConfig merging metadata altogether.
1 parent 0afe8e7 commit 5998808

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/index.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
1154
export type WebpackConfigWithMetadata = WebpackConfig & { metadata?: any }
1255
export type EasyWebpackConfig = WebpackConfigWithMetadata | ((this: WebpackConfigWithMetadata) => WebpackConfigWithMetadata)
56+
export const generateConfigOptions = { addDefaultMetadata: true, alwaysAddBaseMetadata: false }
1357

1458
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 = {
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

Comments
 (0)