Skip to content

Commit 14cee2e

Browse files
authored
Merge pull request webpack#7152 from MhMadHamster/handle-module-name-conflicts
fix module name conflicts
2 parents 677eaba + 87dd7a2 commit 14cee2e

File tree

8 files changed

+66
-3
lines changed

8 files changed

+66
-3
lines changed

lib/NamedModulesPlugin.js

Lines changed: 33 additions & 3 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,11 +21,32 @@ class NamedModulesPlugin {
1221
apply(compiler) {
1322
compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => {
1423
compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => {
24+
const namedModules = new Map();
25+
const context = this.options.context || compiler.options.context;
26+
1527
for (const module of modules) {
1628
if (module.id === null && module.libIdent) {
17-
module.id = module.libIdent({
18-
context: this.options.context || compiler.options.context
19-
});
29+
module.id = module.libIdent({ context });
30+
}
31+
32+
if (module.id !== null) {
33+
const namedModule = namedModules.get(module.id);
34+
if (namedModule !== undefined) {
35+
namedModule.push(module);
36+
} else {
37+
namedModules.set(module.id, [module]);
38+
}
39+
}
40+
}
41+
42+
for (const namedModule of namedModules.values()) {
43+
if (namedModule.length > 1) {
44+
for (const module of namedModule) {
45+
const requestShortener = new RequestShortener(context);
46+
module.id = `${module.id}?${getHash(
47+
requestShortener.shorten(module.identifier())
48+
)}`;
49+
}
2050
}
2151
}
2252
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./c");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./c");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(src) {
2+
return `module.exports = "loader-a" + module.id`;
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(src) {
2+
return `module.exports = "loader-b" + module.id`;
3+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
it("should assign different names to the same module with different issuers ", function() {
2+
var regex = "\\./c\\.js\\?\\w{4}";
3+
expect(require("./c")).toMatch(new RegExp(regex));
4+
expect(require("./a")).toMatch(new RegExp("loader-a" + regex));
5+
expect(require("./b")).toMatch(new RegExp("loader-b" + regex));
6+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: ["./a", "./b", "./test"],
4+
module: {
5+
rules: [
6+
{
7+
test: /c\.js/,
8+
issuer: /a\.js/,
9+
loader: "./loader-a"
10+
},
11+
{
12+
test: /c\.js/,
13+
issuer: /b\.js/,
14+
loader: "./loader-b"
15+
}
16+
]
17+
}
18+
};

0 commit comments

Comments
 (0)