Skip to content

Commit 399b5f4

Browse files
committed
cr fixes
1 parent 0e3795b commit 399b5f4

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/NamedModulesPlugin.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
*/
55
"use strict";
66

7+
const createHash = require("./util/createHash");
8+
const RequestShortener = require("./RequestShortener");
9+
10+
const getHash = str => {
11+
const hash = createHash("md4");
12+
hash.update(str);
13+
return hash.digest("hex").substr(0, 4);
14+
};
15+
716
class NamedModulesPlugin {
817
constructor(options) {
918
this.options = options || {};
@@ -12,7 +21,7 @@ class NamedModulesPlugin {
1221
apply(compiler) {
1322
compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => {
1423
compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => {
15-
let namedModules = {};
24+
const namedModules = new Map();
1625

1726
for (const module of modules) {
1827
if (module.id === null && module.libIdent) {
@@ -21,18 +30,23 @@ class NamedModulesPlugin {
2130
});
2231
}
2332

24-
if (module.id) {
25-
(namedModules[module.id] && namedModules[module.id].push(module)) ||
26-
(namedModules[module.id] = Array.of(module));
33+
if (module.id !== null) {
34+
if (namedModules.has(module.id)) {
35+
namedModules.get(module.id).push(module);
36+
} else {
37+
namedModules.set(module.id, [module]);
38+
}
2739
}
2840
}
2941

30-
Object.keys(namedModules).forEach(key => {
31-
const namedModule = namedModules[key];
42+
namedModules.forEach(namedModule => {
3243
if (namedModule.length > 1) {
3344
namedModule.forEach(module => {
3445
if (module.issuer && module.issuer.id) {
35-
module.id = `${module.issuer.id}~${module.id}`;
46+
const requestShortener = new RequestShortener(module.context);
47+
module.id = `${module.id}?${getHash(
48+
requestShortener.shorten(module.identifier())
49+
)}`;
3650
}
3751
});
3852
}

test/configCases/module-name/different-issuers-for-same-module/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ it("should assign different names to the same module with different issuers ", f
22
var fs = require("fs");
33
var path = require("path");
44
var bundle = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
5-
bundle.should.containEql("./a.js~./c.js");
6-
bundle.should.containEql("./a.js~./c.js");
5+
bundle.should.match(/\.\/c\.js\?\w{4}/g);
6+
require("./a").should.be.equal("loader-a");
7+
require("./b").should.be.equal("loader-b");
78
});

0 commit comments

Comments
 (0)