-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathcommonsChunkPlugin.ts
255 lines (215 loc) · 7.33 KB
/
commonsChunkPlugin.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import {
addOrUpdateConfigObject,
createIdentifierOrLiteral,
createProperty,
findAndRemovePluginByName,
findPluginsByName,
findRootNodesByName
} from "@webpack-cli/utils/ast-utils";
import { JSCodeshift, Node } from "../types/NodePath";
// merge test entry prop and function expression. case 6[x]
// TODO: set the proper type once moved to @types/jscodeshift
// eslint-disable-next-line
const mergeTestPropArrowFunction = (j, chunkKey, testFunc): any => {
return j.property(
"init",
createIdentifierOrLiteral(j, "test"),
j.arrowFunctionExpression(
[j.identifier("module")],
j.blockStatement([
j.ifStatement(
j.callExpression(
j.memberExpression(
j.callExpression(j.memberExpression(j.identifier("module"), j.identifier("getChunks")), []),
j.identifier("some"),
false
),
[
j.arrowFunctionExpression(
[j.identifier("chunk")],
j.binaryExpression(
"===",
j.memberExpression(j.identifier("chunk"), j.identifier("name")),
j.literal(chunkKey)
)
)
]
),
j.returnStatement(j.literal(true))
),
j.variableDeclaration("const", [j.variableDeclarator(j.identifier("fn"), testFunc)]),
j.returnStatement(j.callExpression(j.identifier("fn"), [j.identifier("module")]))
])
)
);
};
/**
*
* Transform for CommonsChunkPlugin. If found, removes the
* plugin and sets optimizations.namedModules to true
*
* @param {Object} j - jscodeshift top-level import
* @param {Node} ast - jscodeshift ast to transform
* @returns {Node} ast - jscodeshift ast
*/
export default function(j: JSCodeshift, ast: Node): Node {
const splitChunksProps: Node[] = [];
const cacheGroupsProps: Node[] = [];
const optimizationProps: object = {};
let commonCacheGroupsProps: Node[] = [createProperty(j, "chunks", "initial"), createProperty(j, "enforce", true)];
// find old options
const CommonsChunkPlugin: Node = findPluginsByName(j, ast, ["webpack.optimize.CommonsChunkPlugin"]);
if (!CommonsChunkPlugin.size()) {
return ast;
}
// cache group options based on keys
let cacheGroup: object = {};
let cacheGroups: Node[] = [];
// iterate each CommonsChunkPlugin instance
CommonsChunkPlugin.forEach((path: Node): void => {
const CCPProps: Node[] = (path.value as Node).arguments[0].properties;
// reset chunks from old props
cacheGroup = {};
cacheGroups = [];
commonCacheGroupsProps = [createProperty(j, "chunks", "initial"), createProperty(j, "enforce", true)];
let chunkKey: string;
let chunkCount = 0;
// iterate CCP props and map SCP props
CCPProps.forEach((p: Node): void => {
const propKey: string = p.key.name;
switch (propKey) {
case "names":
(p.value as Node).elements.forEach(({ value: chunkValue }): void => {
if (chunkValue === "runtime") {
optimizationProps["runtimeChunk"] = j.objectExpression([
createProperty(j, "name", chunkValue)
]);
} else {
if (!Array.isArray(cacheGroup[chunkValue as string])) {
cacheGroup[chunkValue as string] = [];
}
findRootNodesByName(j, ast, "entry").forEach(({ value }): void => {
const { properties: entries } = (value as Node).value as Node;
chunkCount = entries.length;
entries.forEach(({ key: { name: entryName } }): void => {
if (entryName === chunkValue) {
cacheGroup[chunkValue as string].push(createProperty(j, "test", entryName));
}
});
});
}
});
break;
case "name": {
const nameKey = (p.value as Node).value as string;
if (nameKey === "runtime") {
optimizationProps["runtimeChunk"] = j.objectExpression([createProperty(j, "name", nameKey)]);
} else {
chunkKey = nameKey;
if (!Array.isArray(cacheGroup[nameKey])) {
cacheGroup[nameKey] = [];
}
findRootNodesByName(j, ast, "entry").forEach(({ value }): void => {
const { properties: entries } = (value as Node).value as Node;
chunkCount = entries.length;
entries.forEach(({ key: { name: entryName } }): void => {
if (entryName === nameKey) {
cacheGroup[nameKey].push(createProperty(j, "test", entryName));
}
});
});
}
break;
}
case "filename":
if (chunkKey) {
if (!Array.isArray(cacheGroup[chunkKey])) {
cacheGroup[chunkKey] = [];
}
cacheGroup[chunkKey].push(createProperty(j, propKey, (p.value as Node).value as string));
}
break;
case "async":
if (!Array.isArray(cacheGroup[chunkKey])) {
cacheGroup[chunkKey] = [];
}
cacheGroup[chunkKey].push(createProperty(j, "chunks", "async"));
break;
case "minSize":
if (!Array.isArray(cacheGroup[chunkKey])) {
cacheGroup[chunkKey] = [];
}
cacheGroup[chunkKey].push(
j.property("init", createIdentifierOrLiteral(j, propKey), p.value as Node)
);
break;
case "minChunks": {
const { value: pathValue }: Node = p;
// minChunk is a function
if (
(pathValue as Node).type === "ArrowFunctionExpression" ||
(pathValue as Node).type === "FunctionExpression"
) {
if (!Array.isArray(cacheGroup[chunkKey])) {
cacheGroup[chunkKey] = [];
}
// eslint-disable-next-line
cacheGroup[chunkKey] = cacheGroup[chunkKey].map((prop): string | void =>
prop.key.name === "test" ? mergeTestPropArrowFunction(j, chunkKey, pathValue) : prop
);
}
break;
}
}
});
Object.keys(cacheGroup).forEach((chunkName: string): void => {
let chunkProps: Node[] = [createProperty(j, "name", chunkName)];
const chunkPropsToAdd = cacheGroup[chunkName];
const chunkPropsKeys = chunkPropsToAdd.map((prop): string => prop.key.name);
commonCacheGroupsProps = commonCacheGroupsProps.filter(
(commonProp): boolean => !chunkPropsKeys.includes(commonProp.key.name)
);
chunkProps.push(...commonCacheGroupsProps);
if (chunkCount > 1) {
chunkProps.push(
j.property(
"init",
createIdentifierOrLiteral(j, "minChunks"),
createIdentifierOrLiteral(j, chunkCount)
)
);
}
const chunkPropsContainTest = chunkPropsToAdd.some(
(prop): boolean => prop.key.name === "test" && prop.value.type === "Literal"
);
if (chunkPropsContainTest) {
chunkProps = chunkProps.filter((prop): boolean => prop.key.name !== "minChunks");
}
if (chunkPropsToAdd && Array.isArray(chunkPropsToAdd) && chunkPropsToAdd.length > 0) {
chunkProps.push(...chunkPropsToAdd);
}
cacheGroups.push(
j.property("init", createIdentifierOrLiteral(j, chunkName), j.objectExpression([...chunkProps]))
);
});
if (cacheGroups.length > 0) {
cacheGroupsProps.push(...cacheGroups);
}
});
// Remove old plugin
const root: Node = findAndRemovePluginByName(j, ast, "webpack.optimize.CommonsChunkPlugin");
const rootProps: Node[] = [...splitChunksProps];
if (cacheGroupsProps.length > 0) {
rootProps.push(
j.property("init", createIdentifierOrLiteral(j, "cacheGroups"), j.objectExpression([...cacheGroupsProps]))
);
}
// Add new optimizations splitChunks option
if (root) {
addOrUpdateConfigObject(j, root, "optimizations", "splitChunks", j.objectExpression([...rootProps]));
Object.keys(optimizationProps).forEach((key: string): void => {
addOrUpdateConfigObject(j, root, "optimizations", key, optimizationProps[key]);
});
}
return ast;
}