Skip to content

Commit a83e0da

Browse files
committed
replace RequestShortener with contextify in id generation
1 parent a21bc44 commit a83e0da

8 files changed

+63
-76
lines changed

lib/ids/DeterministicChunkIdsPlugin.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
"use strict";
77

8-
const RequestShortener = require("../RequestShortener");
98
const { compareChunksNatural } = require("../util/comparators");
109
const {
1110
getFullChunkName,
@@ -33,17 +32,17 @@ class DeterministicChunkIdsPlugin {
3332
"DeterministicChunkIdsPlugin",
3433
chunks => {
3534
const chunkGraph = compilation.chunkGraph;
36-
const requestShortener = this.options.context
37-
? new RequestShortener(this.options.context)
38-
: compilation.requestShortener;
35+
const context = this.options.context
36+
? this.options.context
37+
: compiler.context;
3938

4039
const compareNatural = compareChunksNatural(chunkGraph);
4140

4241
assignDeterministicIds(
4342
Array.from(chunks).filter(chunk => {
4443
return chunk.id === null;
4544
}),
46-
chunk => getFullChunkName(chunk, chunkGraph, requestShortener),
45+
chunk => getFullChunkName(chunk, chunkGraph, context),
4746
compareNatural,
4847
this.options.maxLength || 3,
4948
getUsedChunkIds(compilation),

lib/ids/DeterministicModuleIdsPlugin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
"use strict";
77

8-
const RequestShortener = require("../RequestShortener");
98
const {
109
compareModulesByPreOrderIndexOrIdentifier
1110
} = require("../util/comparators");
12-
const { getUsedModuleIds, assignDeterministicIds } = require("./IdHelpers");
11+
const {
12+
getUsedModuleIds,
13+
getFullModuleName,
14+
assignDeterministicIds
15+
} = require("./IdHelpers");
1316

1417
/** @typedef {import("../Compiler")} Compiler */
1518
/** @typedef {import("../Module")} Module */
@@ -31,17 +34,17 @@ class DeterministicModuleIdsPlugin {
3134
"DeterministicModuleIdsPlugin",
3235
modules => {
3336
const chunkGraph = compilation.chunkGraph;
34-
const requestShortener = this.options.context
35-
? new RequestShortener(this.options.context)
36-
: compilation.requestShortener;
37+
const context = this.options.context
38+
? this.options.context
39+
: compiler.context;
3740

3841
assignDeterministicIds(
3942
Array.from(modules).filter(module => {
4043
if (chunkGraph.getNumberOfModuleChunks(module) === 0)
4144
return false;
4245
return chunkGraph.getModuleId(module) === null;
4346
}),
44-
module => requestShortener.shorten(module.identifier()),
47+
module => getFullModuleName(module, context),
4548
compareModulesByPreOrderIndexOrIdentifier(
4649
compilation.moduleGraph
4750
),

lib/ids/HashedModuleIdsPlugin.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
const validateOptions = require("schema-utils");
99
const schema = require("../../schemas/plugins/HashedModuleIdsPlugin.json");
10-
const RequestShortener = require("../RequestShortener");
1110
const {
1211
compareModulesByPreOrderIndexOrIdentifier
1312
} = require("../util/comparators");
1413
const createHash = require("../util/createHash");
15-
const { getUsedModuleIds } = require("./IdHelpers");
14+
const { getUsedModuleIds, getFullModuleName } = require("./IdHelpers");
1615

1716
/** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
1817

@@ -40,9 +39,9 @@ class HashedModuleIdsPlugin {
4039
compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => {
4140
compilation.hooks.moduleIds.tap("HashedModuleIdsPlugin", modules => {
4241
const chunkGraph = compilation.chunkGraph;
43-
const requestShortener = this.options.context
44-
? new RequestShortener(this.options.context)
45-
: compilation.requestShortener;
42+
const context = this.options.context
43+
? this.options.context
44+
: compiler.context;
4645

4746
const usedIds = getUsedModuleIds(compilation);
4847
const modulesInNaturalOrder = Array.from(modules)
@@ -54,7 +53,7 @@ class HashedModuleIdsPlugin {
5453
compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
5554
);
5655
for (const module of modulesInNaturalOrder) {
57-
const ident = requestShortener.shorten(module.identifier());
56+
const ident = getFullModuleName(module, context);
5857
const hash = createHash(options.hashFunction);
5958
hash.update(ident || "");
6059
const hashId = hash.digest(options.hashDigest);

lib/ids/IdHelpers.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"use strict";
77

88
const createHash = require("../util/createHash");
9+
const { contextify } = require("../util/identifier");
910

1011
/** @typedef {import("../Chunk")} Chunk */
1112
/** @typedef {import("../ChunkGraph")} ChunkGraph */
1213
/** @typedef {import("../Compilation")} Compilation */
1314
/** @typedef {import("../Module")} Module */
14-
/** @typedef {import("../RequestShortener")} RequestShortener */
1515

1616
const getHash = (str, len) => {
1717
const hash = createHash("md4");
@@ -76,15 +76,25 @@ exports.getShortModuleName = getShortModuleName;
7676
/**
7777
* @param {string} shortName the short name
7878
* @param {Module} module the module
79-
* @param {RequestShortener} requestShortener a request shortener
79+
* @param {string} context context directory
8080
* @returns {string} long module name
8181
*/
82-
const getLongModuleName = (shortName, module, requestShortener) => {
83-
const localIdentifier = requestShortener.shorten(module.identifier());
82+
const getLongModuleName = (shortName, module, context) => {
83+
const localIdentifier = contextify(context, module.identifier());
8484
return `${shortName}?${getHash(localIdentifier, 4)}`;
8585
};
8686
exports.getLongModuleName = getLongModuleName;
8787

88+
/**
89+
* @param {Module} module the module
90+
* @param {string} context context directory
91+
* @returns {string} full module name
92+
*/
93+
const getFullModuleName = (module, context) => {
94+
return contextify(context, module.identifier());
95+
};
96+
exports.getFullModuleName = getFullModuleName;
97+
8898
/**
8999
* @param {Chunk} chunk the chunk
90100
* @param {ChunkGraph} chunkGraph the chunk grph
@@ -109,23 +119,16 @@ exports.getShortChunkName = getShortChunkName;
109119
* @param {Chunk} chunk the chunk
110120
* @param {ChunkGraph} chunkGraph the chunk grph
111121
* @param {string} context context directory
112-
* @param {RequestShortener} requestShortener a request shortener
113122
* @param {string} delimiter delimiter for names
114123
* @returns {string} short chunk name
115124
*/
116-
const getLongChunkName = (
117-
chunk,
118-
chunkGraph,
119-
context,
120-
requestShortener,
121-
delimiter
122-
) => {
125+
const getLongChunkName = (chunk, chunkGraph, context, delimiter) => {
123126
const modules = chunkGraph.getChunkRootModules(chunk);
124127
const shortModuleNames = modules.map(m =>
125128
requestToId(getShortModuleName(m, context))
126129
);
127130
const longModuleNames = modules.map(m =>
128-
requestToId(getLongModuleName("", m, requestShortener))
131+
requestToId(getLongModuleName("", m, context))
129132
);
130133
chunk.idNameHints.sort();
131134
const chunkName = Array.from(chunk.idNameHints)
@@ -138,15 +141,13 @@ exports.getLongChunkName = getLongChunkName;
138141
/**
139142
* @param {Chunk} chunk the chunk
140143
* @param {ChunkGraph} chunkGraph the chunk grph
141-
* @param {RequestShortener} requestShortener a request shortener
142-
* @returns {string} short chunk name
144+
* @param {string} context context directory
145+
* @returns {string} full chunk name
143146
*/
144-
const getFullChunkName = (chunk, chunkGraph, requestShortener) => {
147+
const getFullChunkName = (chunk, chunkGraph, context) => {
145148
if (chunk.name) return chunk.name;
146149
const modules = chunkGraph.getChunkRootModules(chunk);
147-
const fullModuleNames = modules.map(m =>
148-
requestShortener.shorten(m.identifier())
149-
);
150+
const fullModuleNames = modules.map(m => contextify(context, m.identifier()));
150151
return fullModuleNames.join();
151152
};
152153
exports.getFullChunkName = getFullChunkName;

lib/ids/NamedChunkIdsPlugin.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
"use strict";
77

8-
const RequestShortener = require("../RequestShortener");
98
const { compareChunksNatural } = require("../util/comparators");
109
const {
1110
getShortChunkName,
@@ -34,9 +33,6 @@ class NamedChunkIdsPlugin {
3433
compilation.hooks.chunkIds.tap("NamedChunkIdsPlugin", chunks => {
3534
const chunkGraph = compilation.chunkGraph;
3635
const context = this.context ? this.context : compiler.context;
37-
const requestShortener = this.context
38-
? new RequestShortener(this.context)
39-
: compilation.requestShortener;
4036
const delimiter = this.delimiter;
4137

4238
const unnamedChunks = assignNames(
@@ -48,14 +44,7 @@ class NamedChunkIdsPlugin {
4844
return chunk.id === null;
4945
}),
5046
chunk => getShortChunkName(chunk, chunkGraph, context, delimiter),
51-
chunk =>
52-
getLongChunkName(
53-
chunk,
54-
chunkGraph,
55-
context,
56-
requestShortener,
57-
delimiter
58-
),
47+
chunk => getLongChunkName(chunk, chunkGraph, context, delimiter),
5948
compareChunksNatural(chunkGraph),
6049
getUsedChunkIds(compilation),
6150
(chunk, name) => {

lib/ids/NamedModuleIdsPlugin.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
"use strict";
77

8-
const RequestShortener = require("../RequestShortener");
98
const { compareModulesByIdentifier } = require("../util/comparators");
109
const {
1110
getShortModuleName,
@@ -34,17 +33,14 @@ class NamedModuleIdsPlugin {
3433
const context = this.options.context
3534
? this.options.context
3635
: compiler.context;
37-
const requestShortener = this.options.context
38-
? new RequestShortener(this.options.context)
39-
: compilation.requestShortener;
4036

4137
const unnamedModules = assignNames(
4238
Array.from(modules).filter(module => {
4339
if (chunkGraph.getNumberOfModuleChunks(module) === 0) return false;
4440
return chunkGraph.getModuleId(module) === null;
4541
}),
4642
m => getShortModuleName(m, context),
47-
(m, shortName) => getLongModuleName(shortName, m, requestShortener),
43+
(m, shortName) => getLongModuleName(shortName, m, context),
4844
compareModulesByIdentifier,
4945
getUsedModuleIds(compilation),
5046
(m, name) => chunkGraph.setModuleId(m, name)

test/__snapshots__/ConfigTestCases.test.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ exports[`ConfigTestCases records issue-2991 exported tests should write relative
1414
},
1515
\\"modules\\": {
1616
\\"byIdentifier\\": {
17-
\\"external \\\\\\"fs\\\\\\"\\": 66,
18-
\\"external \\\\\\"path\\\\\\"\\": 589,
19-
\\"ignored pkgs/somepackage/foo\\": 583,
17+
\\"external \\\\\\"fs\\\\\\"\\": 582,
18+
\\"external \\\\\\"path\\\\\\"\\": 476,
19+
\\"ignored pkgs/somepackage/foo\\": 940,
2020
\\"test.js\\": 316
2121
},
2222
\\"usedIds\\": [
23-
66,
2423
316,
25-
583,
26-
589
24+
476,
25+
582,
26+
940
2727
]
2828
}
2929
}"
@@ -43,19 +43,19 @@ exports[`ConfigTestCases records issue-7339 exported tests should write relative
4343
},
4444
\\"modules\\": {
4545
\\"byIdentifier\\": {
46-
\\"dependencies sync /^\\\\\\\\.\\\\\\\\/.*$/\\": 154,
46+
\\"dependencies sync /^\\\\\\\\.\\\\\\\\/.*$/\\": 750,
4747
\\"dependencies/bar.js\\": 853,
4848
\\"dependencies/foo.js\\": 494,
49-
\\"external \\\\\\"fs\\\\\\"\\": 66,
50-
\\"external \\\\\\"path\\\\\\"\\": 589,
49+
\\"external \\\\\\"fs\\\\\\"\\": 582,
50+
\\"external \\\\\\"path\\\\\\"\\": 476,
5151
\\"test.js\\": 316
5252
},
5353
\\"usedIds\\": [
54-
66,
55-
154,
5654
316,
55+
476,
5756
494,
58-
589,
57+
582,
58+
750,
5959
853
6060
]
6161
}

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,26 +707,26 @@ Unexpected end of JSON input while parsing near ''"
707707
`;
708708

709709
exports[`StatsTestCases should print correct stats for exclude-with-loader 1`] = `
710-
"Hash: b19f92f53cde62bd84fa
710+
"Hash: 5b058b2e733480315c39
711711
Time: Xms
712712
Built at: Thu Jan 01 1970 00:00:00 GMT
713713
Asset Size Chunks Chunk Names
714714
bundle.js 1.87 KiB {404} [emitted] main
715715
+ 1 hidden asset
716716
Entrypoint main = bundle.js
717-
[279] ./a.txt 43 bytes {404} [built]
717+
[237] ./a.txt 43 bytes {404} [built]
718718
[967] ./index.js 77 bytes {404} [built]
719719
+ 3 hidden modules"
720720
`;
721721

722722
exports[`StatsTestCases should print correct stats for external 1`] = `
723-
"Hash: f82f876aa71fe8ac2251
723+
"Hash: b6fc48424bf235879198
724724
Time: Xms
725725
Built at: Thu Jan 01 1970 00:00:00 GMT
726726
Asset Size Chunks Chunk Names
727727
main.js 1.35 KiB {404} [emitted] main
728728
Entrypoint main = main.js
729-
[397] external \\"test\\" 42 bytes {404} [built]
729+
[317] external \\"test\\" 42 bytes {404} [built]
730730
[967] ./index.js 17 bytes {404} [built]"
731731
`;
732732

@@ -1073,7 +1073,7 @@ chunk {trees} trees.js (trees) 70 bytes <{main}> [rendered]
10731073
`;
10741074

10751075
exports[`StatsTestCases should print correct stats for import-context-filter 1`] = `
1076-
"Hash: 9c6ab35bb82b94f495fa
1076+
"Hash: 84a276e97488bda9f7f0
10771077
Time: Xms
10781078
Built at: Thu Jan 01 1970 00:00:00 GMT
10791079
Asset Size Chunks Chunk Names
@@ -1082,7 +1082,7 @@ Built at: Thu Jan 01 1970 00:00:00 GMT
10821082
920.js 320 bytes {920} [emitted]
10831083
entry.js 7.03 KiB {707} [emitted] entry
10841084
Entrypoint entry = entry.js
1085-
[169] ./templates lazy ^\\\\.\\\\/.*$ include: \\\\.js$ exclude: \\\\.noimport\\\\.js$ namespace object 160 bytes {707} [optional] [built]
1085+
[281] ./templates lazy ^\\\\.\\\\/.*$ include: \\\\.js$ exclude: \\\\.noimport\\\\.js$ namespace object 160 bytes {707} [optional] [built]
10861086
[374] ./templates/baz.js 38 bytes {374} [optional] [built]
10871087
[457] ./templates/foo.js 38 bytes {457} [optional] [built]
10881088
[865] ./entry.js 450 bytes {707} [built]
@@ -1301,18 +1301,18 @@ Entrypoint main = main.js
13011301
`;
13021302

13031303
exports[`StatsTestCases should print correct stats for module-assets 1`] = `
1304-
"Hash: 2c99756b09cb439e3746
1304+
"Hash: b43089f554599541b1d1
13051305
Time: Xms
13061306
Built at: Thu Jan 01 1970 00:00:00 GMT
13071307
Entrypoint main = main.js
13081308
chunk {404} main.js (main) 12 bytes (javascript) 4.45 KiB (runtime) >{687}< [entry] [rendered]
13091309
[967] ./index.js 12 bytes {404} [built]
13101310
+ 7 hidden chunk modules
13111311
chunk {687} 687.js 68 bytes <{404}> [rendered]
1312-
[400] ./node_modules/a/1.png 51 bytes {687} [built] [1 asset]
13131312
[687] ./node_modules/a/index.js 17 bytes {687} [built]
1314-
[400] ./node_modules/a/1.png 51 bytes {687} [built] [1 asset]
1313+
[777] ./node_modules/a/1.png 51 bytes {687} [built] [1 asset]
13151314
[687] ./node_modules/a/index.js 17 bytes {687} [built]
1315+
[777] ./node_modules/a/1.png 51 bytes {687} [built] [1 asset]
13161316
[967] ./index.js 12 bytes {404} [built]
13171317
+ 7 hidden modules"
13181318
`;
@@ -2210,7 +2210,7 @@ Entrypoint e2 = runtime.js e2.js"
22102210
`;
22112211

22122212
exports[`StatsTestCases should print correct stats for scope-hoisting-bailouts 1`] = `
2213-
"Hash: e4f7044e92f3bd41cef0
2213+
"Hash: cfed930245338a5f209d
22142214
Time: Xms
22152215
Built at: Thu Jan 01 1970 00:00:00 GMT
22162216
Entrypoint index = index.js
@@ -2226,7 +2226,7 @@ Entrypoint entry = entry.js
22262226
ModuleConcatenation bailout: Module is not an ECMAScript module
22272227
[628] ./eval.js 35 bytes {800} [built]
22282228
ModuleConcatenation bailout: Module uses eval()
2229-
[673] external \\"external\\" 42 bytes {800} [built]
2229+
[860] external \\"external\\" 42 bytes {800} [built]
22302230
ModuleConcatenation bailout: Module is not an ECMAScript module
22312231
[865] ./entry.js 32 bytes {707} {800} [built]
22322232
[967] ./index.js 150 bytes {800} [built]

0 commit comments

Comments
 (0)