-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.ts
203 lines (177 loc) · 4.55 KB
/
webpack.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @module
* Helper functions for modifying webpack config.
*/
import {
Configuration,
Plugin,
RuleSetCondition,
RuleSetRule,
RuleSetUse,
RuleSetUseItem,
} from "webpack";
// https://webpack.js.org/configuration/module/#condition
function testRuleCondition(
condition: RuleSetCondition,
filename: string
): boolean {
if (typeof condition === "function") {
return condition(filename);
}
if (typeof condition === "string") {
return condition.indexOf(filename) === 0;
}
if (condition instanceof RegExp) {
return condition.test(filename);
}
if (condition instanceof Array) {
return condition.some((child) => testRuleCondition(child, filename));
}
// Object-style condition is not supported.
return false;
}
/**
* Returns whether the rule will be applied to a file with given filename.
* @param rule rule to test
* @param filename filename or filepath to test with
* @returns whether the rule is active for the file
*/
export function isRuleAppliedTo(rule: RuleSetRule, filename: string): boolean {
if (!rule.test) {
return false;
}
return testRuleCondition(rule.test, filename);
}
/**
* Replace minimizer library.
* @param config webpack config
* @param minimizer new minimizer
* @returns modified webpack config
*/
export function replaceMinimizer(
config: Configuration,
minimizer: Plugin
): Configuration {
return {
...config,
optimization: {
// Use provided optimization configuration other than minimizer
...(config.optimization ?? {
// In case where config.optimization is empty (probably never happen)
minimize: true,
}),
minimizer: [minimizer],
},
};
}
type AnyConstructor = new (...args: any) => any;
/**
* Remove a plugin from a plugin list in a given webpack config.
* @param config webpack config
* @param ctor constructor of the plugin to remove
* @returns modified webpack config
*/
export function removePlugin(
config: Configuration,
ctor: AnyConstructor
): Configuration {
if (!config.plugins || !config.plugins.length) {
return config;
}
return {
...config,
plugins: config.plugins.filter((plugin) => !(plugin instanceof ctor)),
};
}
type LoaderTester = (loader: RuleSetUseItem, rule: RuleSetRule) => boolean;
export type LoaderReplacer = (
loader: RuleSetUseItem,
rule: RuleSetRule
) => RuleSetUseItem | null;
/**
* Replace or Remove loader matching to `test` with a value returned from `replaceWith`.
* @param config webpack config
* @param test decides whether the loader is replaced (removed). think as 1st parameter of Array.prototype.filter
* @param replaceWith a factory function returns a rule which takes place in. return null to remove the loader
* @returns modified webpack config
*/
export function replaceLoader(
config: Configuration,
test: LoaderTester,
replaceWith: LoaderReplacer
): Configuration {
if (!config.module?.rules) {
return config;
}
return {
...config,
module: {
...config.module,
rules: replaceLoaderInternal(config.module.rules, test, replaceWith),
},
};
}
function replaceLoaderInternal(
rules: readonly RuleSetRule[],
test: LoaderTester,
replaceWith: LoaderReplacer
): RuleSetRule[] {
if (!rules) {
return rules;
}
const replaceRuleSetUse = (
use: RuleSetUse,
rule: RuleSetRule
): RuleSetUse | undefined => {
// NOTE: Function Use needs compiler information to call.
if (typeof use === "function") {
return use;
}
if (use instanceof Array) {
const transformed = use
.map((item) => {
if (test(item, rule)) {
return replaceWith(item, rule);
}
return item;
})
.filter((item): item is NonNullable<typeof item> => !!item);
switch (transformed.length) {
case 0:
return undefined;
case 1:
return transformed[0];
default:
return transformed;
}
}
return test(use, rule) ? replaceWith(use, rule) ?? undefined : use;
};
return rules.map((rule) => {
if (rule.oneOf) {
return {
...rule,
oneOf: replaceLoaderInternal(rule.oneOf, test, replaceWith),
};
}
if (rule.loader) {
return {
...rule,
loader: replaceRuleSetUse(rule.loader, rule),
};
}
if (rule.loaders) {
return {
...rule,
loaders: replaceRuleSetUse(rule.loaders, rule),
};
}
if (rule.use) {
return {
...rule,
use: replaceRuleSetUse(rule.use, rule),
};
}
return rule;
});
}