-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
merge-webpack-config.ts
172 lines (149 loc) · 4.14 KB
/
merge-webpack-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import type {
AddWebpackPlugins,
Configure,
CracoConfig,
WebpackAlias,
WebpackContext,
} from '@craco/types';
import type { Configuration as WebpackConfig } from 'webpack';
import merge from 'webpack-merge';
import { log } from '../../logger';
import { isArray, isFunction } from '../../utils';
import {
addPlugins as addWebpackPlugins,
pluginByName,
removePlugins as removeWebpackPlugins,
} from '../../webpack-plugins';
import { applyWebpackConfigPlugins } from '../plugins';
import { overrideBabel } from './babel';
import { overrideEsLint } from './eslint';
import { overrideStyle } from './style/style';
import { overrideTypeScript } from './typescript';
function addAlias(webpackConfig: WebpackConfig, webpackAlias: WebpackAlias) {
if (webpackConfig.resolve) {
// TODO: ensure is a plain object, if not, log an error.
webpackConfig.resolve.alias = Object.assign(
webpackConfig.resolve.alias || {},
webpackAlias
);
}
log('Added webpack alias.');
}
function addPlugins(
webpackConfig: WebpackConfig,
webpackPlugins: AddWebpackPlugins
) {
if (isArray(webpackPlugins)) {
addWebpackPlugins(webpackConfig, webpackPlugins);
log('Added webpack plugins.');
} else {
throw new Error(
`craco: 'webpack.plugins.add' needs to be a an array of plugins`
);
}
}
function removePluginsFromWebpackConfig(
webpackConfig: WebpackConfig,
remove: string[] | undefined
) {
if (!remove) {
return;
}
if (isArray(remove)) {
for (const pluginName of remove) {
const { hasRemovedAny } = removeWebpackPlugins(
webpackConfig,
pluginByName(pluginName)
);
if (hasRemovedAny) {
log(`Removed webpack plugin ${pluginName}.`);
} else {
log(`Did not remove webpack plugin ${pluginName}.`);
}
}
log('Removed webpack plugins.');
} else {
throw new Error(
`craco: 'webpack.plugins.remove' needs to be a an array of plugin names`
);
}
}
function giveTotalControl(
webpackConfig: WebpackConfig,
configureWebpack: Configure<WebpackConfig, WebpackContext>,
context: WebpackContext
) {
if (isFunction(configureWebpack)) {
webpackConfig = configureWebpack(webpackConfig, context);
if (!webpackConfig) {
throw new Error(
"craco: 'webpack.configure' function didn't returned a webpack config object."
);
}
} else {
// TODO: ensure is otherwise a plain object, if not, log an error.
webpackConfig = merge(webpackConfig, configureWebpack);
}
log("Merged webpack config with 'webpack.configure'.");
return webpackConfig;
}
export function mergeWebpackConfig(
cracoConfig: CracoConfig,
webpackConfig: WebpackConfig,
context: WebpackContext
) {
let resultingWebpackConfig = webpackConfig;
resultingWebpackConfig = overrideBabel(
cracoConfig,
resultingWebpackConfig,
context
);
resultingWebpackConfig = overrideEsLint(
cracoConfig,
resultingWebpackConfig,
context
);
resultingWebpackConfig = overrideStyle(
cracoConfig,
resultingWebpackConfig,
context
);
resultingWebpackConfig = overrideTypeScript(
cracoConfig,
resultingWebpackConfig
);
if (cracoConfig.webpack) {
const { alias, plugins, configure } = cracoConfig.webpack;
if (alias) {
addAlias(resultingWebpackConfig, alias);
}
if (plugins) {
// we still support the old format of plugin: [] where the array is a list of the plugins to add
if (isArray(plugins)) {
addPlugins(resultingWebpackConfig, plugins);
} else {
const { add, remove } = plugins;
if (remove) {
removePluginsFromWebpackConfig(resultingWebpackConfig, remove);
}
// Add after removing to preserve any plugins explicitly added via the Craco config
if (add) {
addPlugins(resultingWebpackConfig, add);
}
}
}
if (configure) {
resultingWebpackConfig = giveTotalControl(
resultingWebpackConfig,
configure,
context
);
}
}
resultingWebpackConfig = applyWebpackConfigPlugins(
cracoConfig,
resultingWebpackConfig,
context
);
return resultingWebpackConfig;
}