Skip to content

Commit 551384a

Browse files
authored
Merge pull request webpack#7493 from webpack/bugfix/issue-7492
fixes webpack#7492
2 parents b424645 + ba2f247 commit 551384a

File tree

8 files changed

+102
-6
lines changed

8 files changed

+102
-6
lines changed

lib/CaseSensitiveModulesWarning.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const WebpackError = require("./WebpackError");
1414
*/
1515
const sortModules = modules => {
1616
return modules.slice().sort((a, b) => {
17-
a = a.identifier();
18-
b = b.identifier();
17+
const aIdent = a.identifier();
18+
const bIdent = b.identifier();
1919
/* istanbul ignore next */
20-
if (a < b) return -1;
20+
if (aIdent < bIdent) return -1;
2121
/* istanbul ignore next */
22-
if (a > b) return 1;
22+
if (aIdent > bIdent) return 1;
2323
/* istanbul ignore next */
2424
return 0;
2525
});

lib/Module.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,12 @@ Object.defineProperty(Module.prototype, "meta", {
370370
}, "Module.meta was renamed to Module.buildMeta")
371371
});
372372

373+
/** @type {function(): string} */
373374
Module.prototype.identifier = null;
375+
376+
/** @type {function(RequestShortener): string} */
374377
Module.prototype.readableIdentifier = null;
378+
375379
Module.prototype.build = null;
376380
Module.prototype.source = null;
377381
Module.prototype.size = null;

lib/RecordIdsPlugin.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,53 @@
66

77
const identifierUtils = require("./util/identifier");
88

9+
/** @typedef {import("./Compiler")} Compiler */
10+
/** @typedef {import("./Chunk")} Chunk */
11+
/** @typedef {import("./Module")} Module */
12+
13+
/**
14+
* @typedef {Object} RecordsChunks
15+
* @property {Record<string, number>=} byName
16+
* @property {Record<string, number>=} bySource
17+
* @property {number[]=} usedIds
18+
*/
19+
20+
/**
21+
* @typedef {Object} RecordsModules
22+
* @property {Record<string, number>=} byIdentifier
23+
* @property {Record<string, number>=} bySource
24+
* @property {Record<number, number>=} usedIds
25+
*/
26+
27+
/**
28+
* @typedef {Object} Records
29+
* @property {RecordsChunks=} chunks
30+
* @property {RecordsModules=} modules
31+
*/
32+
933
class RecordIdsPlugin {
34+
/**
35+
* @param {Object} options Options object
36+
* @param {boolean=} options.portableIds true, when ids need to be portable
37+
*/
1038
constructor(options) {
1139
this.options = options || {};
1240
}
1341

42+
/**
43+
* @param {Compiler} compiler the Compiler
44+
* @returns {void}
45+
*/
1446
apply(compiler) {
1547
const portableIds = this.options.portableIds;
1648
compiler.hooks.compilation.tap("RecordIdsPlugin", compilation => {
1749
compilation.hooks.recordModules.tap(
1850
"RecordIdsPlugin",
51+
/**
52+
* @param {Module[]} modules the modules array
53+
* @param {Records} records the records object
54+
* @returns {void}
55+
*/
1956
(modules, records) => {
2057
if (!records.modules) records.modules = {};
2158
if (!records.modules.byIdentifier) records.modules.byIdentifier = {};
@@ -36,9 +73,15 @@ class RecordIdsPlugin {
3673
);
3774
compilation.hooks.reviveModules.tap(
3875
"RecordIdsPlugin",
76+
/**
77+
* @param {Module[]} modules the modules array
78+
* @param {Records} records the records object
79+
* @returns {void}
80+
*/
3981
(modules, records) => {
4082
if (!records.modules) return;
4183
if (records.modules.byIdentifier) {
84+
/** @type {Set<number>} */
4285
const usedIds = new Set();
4386
for (const module of modules) {
4487
if (module.id !== null) continue;
@@ -62,6 +105,10 @@ class RecordIdsPlugin {
62105
}
63106
);
64107

108+
/**
109+
* @param {Module} module the module
110+
* @returns {string} the (portable) identifier
111+
*/
65112
const getModuleIdentifier = module => {
66113
if (portableIds) {
67114
return identifierUtils.makePathsRelative(
@@ -73,7 +120,12 @@ class RecordIdsPlugin {
73120
return module.identifier();
74121
};
75122

123+
/**
124+
* @param {Chunk} chunk the chunk
125+
* @returns {string[]} sources of the chunk
126+
*/
76127
const getChunkSources = chunk => {
128+
/** @type {string[]} */
77129
const sources = [];
78130
for (const chunkGroup of chunk.groupsIterable) {
79131
const index = chunkGroup.chunks.indexOf(chunk);
@@ -108,10 +160,16 @@ class RecordIdsPlugin {
108160

109161
compilation.hooks.recordChunks.tap(
110162
"RecordIdsPlugin",
163+
/**
164+
* @param {Chunk[]} chunks the chunks array
165+
* @param {Records} records the records object
166+
* @returns {void}
167+
*/
111168
(chunks, records) => {
112169
if (!records.chunks) records.chunks = {};
113170
if (!records.chunks.byName) records.chunks.byName = {};
114171
if (!records.chunks.bySource) records.chunks.bySource = {};
172+
/** @type {Set<number>} */
115173
const usedIds = new Set();
116174
for (const chunk of chunks) {
117175
if (typeof chunk.id !== "number") continue;
@@ -128,8 +186,14 @@ class RecordIdsPlugin {
128186
);
129187
compilation.hooks.reviveChunks.tap(
130188
"RecordIdsPlugin",
189+
/**
190+
* @param {Chunk[]} chunks the chunks array
191+
* @param {Records} records the records object
192+
* @returns {void}
193+
*/
131194
(chunks, records) => {
132195
if (!records.chunks) return;
196+
/** @type {Set<number>} */
133197
const usedIds = new Set();
134198
if (records.chunks.byName) {
135199
for (const chunk of chunks) {
@@ -148,8 +212,8 @@ class RecordIdsPlugin {
148212
for (const source of sources) {
149213
const id = records.chunks.bySource[source];
150214
if (id === undefined) continue;
151-
if (usedIds[id]) continue;
152-
usedIds[id] = true;
215+
if (usedIds.has(id)) continue;
216+
usedIds.add(id);
153217
chunk.id = id;
154218
break;
155219
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "vendor";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should load fine", () => {
2+
return import(/* webpackChunkName: "async" */"./async");
3+
});

test/configCases/records/issue-7492/node_modules/vendor.js

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"chunks": {
3+
"byName": {
4+
"vendors~async": 123
5+
},
6+
"bySource": {
7+
"1 index.js ./async": 123
8+
}
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var path = require("path");
2+
3+
module.exports = {
4+
entry: "./index",
5+
recordsInputPath: path.resolve(__dirname, "records.json"),
6+
output: {
7+
chunkFilename: "[name]-[chunkhash].js"
8+
},
9+
optimization: {
10+
splitChunks: {
11+
minSize: 0
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)