Skip to content

Commit f60c8d8

Browse files
committed
bugfixes and optimizations
optimize name map for name equals id bugfixes in named ids assigning assign name before automatic ids
1 parent ca46218 commit f60c8d8

File tree

7 files changed

+91
-74
lines changed

7 files changed

+91
-74
lines changed

lib/ids/IdHelpers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ exports.getLongModuleName = getLongModuleName;
9393
* @returns {string} short chunk name
9494
*/
9595
const getShortChunkName = (chunk, chunkGraph, context, delimiter) => {
96-
if (chunk.name) return chunk.name;
9796
const modules = chunkGraph.getChunkRootModules(chunk);
9897
const shortModuleNames = modules.map(m =>
9998
requestToId(getShortModuleName(m, context))
@@ -121,7 +120,6 @@ const getLongChunkName = (
121120
requestShortener,
122121
delimiter
123122
) => {
124-
if (chunk.name) return chunk.name;
125123
const modules = chunkGraph.getChunkRootModules(chunk);
126124
const shortModuleNames = modules.map(m =>
127125
requestToId(getShortModuleName(m, context))
@@ -270,12 +268,15 @@ const assignNames = (
270268
}
271269
} else if (items.length === 1 && !usedIds.has(name)) {
272270
assignName(items[0], name);
271+
usedIds.add(name);
273272
} else {
274273
items.sort(comparator);
275274
let i = 0;
276275
for (const item of items) {
277276
while (nameToItems2.has(name + i) && usedIds.has(name + i)) i++;
278277
assignName(item, name + i);
278+
usedIds.add(name + i);
279+
i++;
279280
}
280281
}
281282
}

lib/ids/NamedChunkIdsPlugin.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class NamedChunkIdsPlugin {
4141

4242
const unnamedChunks = assignNames(
4343
Array.from(chunks).filter(chunk => {
44+
if (chunk.name) {
45+
chunk.id = chunk.name;
46+
chunk.ids = [chunk.name];
47+
}
4448
return chunk.id === null;
4549
}),
4650
chunk => getShortChunkName(chunk, chunkGraph, context, delimiter),
@@ -62,20 +66,6 @@ class NamedChunkIdsPlugin {
6266
if (unnamedChunks.length > 0) {
6367
assignAscendingChunkIds(unnamedChunks, compilation);
6468
}
65-
66-
const usedIds = new Set();
67-
if (compilation.usedChunkIds) {
68-
for (const id of compilation.usedChunkIds) {
69-
usedIds.add(id);
70-
}
71-
}
72-
73-
for (const chunk of chunks) {
74-
const chunkId = chunk.id;
75-
if (chunkId !== null) {
76-
usedIds.add(chunkId);
77-
}
78-
}
7969
});
8070
});
8171
}

lib/runtime/GetChunkFilenameRuntimeModule.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ const RuntimeGlobals = require("../RuntimeGlobals");
88
const RuntimeModule = require("../RuntimeModule");
99
const Template = require("../Template");
1010

11+
const createChunkNameExpr = obj => {
12+
const optimizedObj = {};
13+
let hasEntries = false;
14+
for (const key of Object.keys(obj)) {
15+
const value = obj[key];
16+
if (key !== value) {
17+
optimizedObj[key] = value;
18+
hasEntries = true;
19+
}
20+
}
21+
return hasEntries
22+
? `(${JSON.stringify(optimizedObj)}[chunkId]||chunkId)`
23+
: "chunkId";
24+
};
25+
1126
class GetChunkFilenameRuntimeModule extends RuntimeModule {
1227
constructor(compilation, chunk, contentType, name, global, filename) {
1328
super(`get ${name} chunk filename`);
@@ -44,7 +59,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
4459
}
4560
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
4661
},
47-
name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`,
62+
name: `" + ${createChunkNameExpr(chunkMaps.name)} + "`,
4863
contentHash: {
4964
[contentType]: `" + ${JSON.stringify(
5065
chunkMaps.contentHash[contentType]

0 commit comments

Comments
 (0)