Skip to content

Commit 9e5b364

Browse files
fix(html): refuse a page name another page or entry already holds
A second page asking for a taken name was appended to the entry holding it, so both were emitted as one file and the build died on an asset conflict. Claim each page name by request instead: a link to the page that already holds it shares that page, anything else is reported.
1 parent c73fa22 commit 9e5b364

17 files changed

Lines changed: 137 additions & 8 deletions

File tree

‎lib/html/HtmlModulesPlugin.js‎

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const getCssModulesPlugin = memoize(() => require("../css/CssModulesPlugin"));
113113
* @property {boolean=} remove set true to delete the whole element
114114
*/
115115
/** @typedef {{ outputName: string, html: string }} HtmlTransformTagsContext */
116-
/** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined }} HtmlEntrySpec */
116+
/** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined, page: boolean }} HtmlEntrySpec */
117117

118118
const createCompilationHooks = () => ({
119119
/**
@@ -686,7 +686,8 @@ class HtmlModulesPlugin {
686686
request: entry.request,
687687
name: entry.entryName,
688688
index: entry.index,
689-
dependOn
689+
dependOn,
690+
page: groupKind === "html"
690691
});
691692
}
692693
}
@@ -770,8 +771,8 @@ class HtmlModulesPlugin {
770771
holder = compilation.moduleGraph.getIssuer(holder);
771772
}
772773
for (const entry of ordered) {
773-
// A name the page or `output.html.chunkName` asked for is what the
774-
// file is called too; otherwise the tag's url lends its basename.
774+
// A name the page or `module.parser.html.chunkName` asked for is what
775+
// the file is called too; otherwise the tag's url lends its basename.
775776
const base =
776777
wrapsEntry && namedEntry
777778
? namedEntry.name
@@ -789,7 +790,9 @@ class HtmlModulesPlugin {
789790
for (const link of links) {
790791
// A linked page is named after its own path; a name the page asked
791792
// for replaces that, and the link to it is rewritten either way.
792-
if (link.chunkName) pageNames.set(link.entryName, link.chunkName);
793+
if (link.chunkName && !contestedPageNames.has(link.entryName)) {
794+
pageNames.set(link.entryName, link.chunkName);
795+
}
793796
const page = linkedPages.get(link.entryName);
794797
if (page) recordNames(page, seen);
795798
}
@@ -815,6 +818,28 @@ class HtmlModulesPlugin {
815818
);
816819
});
817820

821+
// What each page name was claimed for, so a second page asking for a
822+
// taken one is refused rather than appended to the entry holding it.
823+
/** @type {Map<string, string>} */
824+
const pageRequests = new Map();
825+
// Names a second page asked for and did not get, so it keeps the one its
826+
// own path lends and emits beside the page holding the name.
827+
/** @type {Set<string>} */
828+
const contestedPageNames = new Set();
829+
/**
830+
* @param {string} name the name two things ask for
831+
* @param {string} taken what holds it
832+
* @param {string} request what asked for it
833+
*/
834+
const reportNameTaken = (name, taken, request) => {
835+
contestedPageNames.add(name);
836+
compilation.errors.push(
837+
new WebpackError(
838+
`The name "${name}" is asked for by ${request} and is already taken by ${taken}. A page is emitted as its name, so give one of them another.`
839+
)
840+
);
841+
};
842+
818843
// Create one HTML module's entries. A `type: "html"` link is itself an
819844
// HTML entry, so after it builds we recurse into it — the same handling
820845
// every HTML entry already gets. `processed` guards diamonds and cycles.
@@ -826,7 +851,32 @@ class HtmlModulesPlugin {
826851
processed.add(module);
827852
await Promise.all(
828853
collectEntrySpecs(module).map(async (spec) => {
854+
if (spec.page) {
855+
const claimed = pageRequests.get(spec.name);
856+
// The page holding the name is already an entry of its own:
857+
// a second link to it adds nothing, a second page cannot have it.
858+
if (claimed !== undefined) {
859+
if (claimed !== spec.request) {
860+
reportNameTaken(spec.name, claimed, spec.request);
861+
}
862+
return;
863+
}
864+
pageRequests.set(spec.name, spec.request);
865+
}
829866
const entryModule = await addEntry(spec);
867+
// A page entry webpack made for us holds that page alone; anything
868+
// else in it named the same and would be emitted over.
869+
if (spec.page) {
870+
const entry = compilation.entries.get(spec.name);
871+
if (entry && entry.dependencies.length !== 1) {
872+
reportNameTaken(
873+
spec.name,
874+
`entry "${spec.name}"`,
875+
spec.request
876+
);
877+
return;
878+
}
879+
}
830880
if (entryModule && entryModule.type === HTML_MODULE_TYPE) {
831881
linkedPages.set(spec.name, entryModule);
832882
await processEntry(entryModule);
@@ -1262,9 +1312,8 @@ class HtmlModulesPlugin {
12621312
resolvedSentinelHashCache.set(placeholderSource, cached);
12631313
}
12641314
const resource = module.getResource() || module.resource;
1265-
// A page is named after its own path, unless it asked for a name;
1266-
// a synthetic `output.html` entry is a `data:` module with no path,
1267-
// so it falls back to the entry that named it.
1315+
// A page is named after its own path, unless it asked for a name; a
1316+
// synthetic `output.html` entry has none, so its entry names it.
12681317
const requestedPageName = chunk.name
12691318
? getState(compilation).pageNames.get(chunk.name)
12701319
: undefined;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<title>Links to the same page</title>
3+
<body>
4+
<!-- webpackChunkName: "docs" -->
5+
<a href="./other.html">other</a>

‎test/configCases/html/chunk-name-comment/test.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ it("should name a linked page's own file too", () => {
6161
expect(read("docs.html")).toContain("<title>Linked page</title>");
6262
});
6363

64+
it("should emit one page when two of them link it under one name", () => {
65+
// The second link claims a name the same page already holds, so it shares
66+
// that page rather than asking for a second copy of it.
67+
expect(read("linked2.html")).toContain('href="docs.html"');
68+
expect(read("docs.html")).toContain("<title>Linked page</title>");
69+
});
70+
6471
it("should keep names apart when the page asks for one twice", () => {
6572
const urls = scriptUrls(read("collide.html"));
6673
// The third tag asks for a taken name, and the position it falls back to

‎test/configCases/html/chunk-name-comment/webpack.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
page: "./page.html",
3434
bad: "./bad.html",
3535
linked: "./linked.html",
36+
linked2: "./linked2.html",
3637
collide: "./collide.html"
3738
},
3839
output: { module: true, htmlFilename: "[name].html" },
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<title>Links to x</title>
3+
<body>
4+
<!-- webpackChunkName: "docs" -->
5+
<a href="./x.html">x</a>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<title>Links to y</title>
3+
<body>
4+
<!-- webpackChunkName: "docs" -->
5+
<a href="./y.html">y</a>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
/The name "docs" is asked for by \.\/y\.html and is already taken by \.\/x\.html\./
6+
]
7+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("page name collision");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: ["web", "es2022"],
6+
entry: { main: "./src/main.js", a: "./a.html", b: "./b.html" },
7+
output: { htmlFilename: "[name].html" },
8+
module: {
9+
parser: {
10+
html: { sources: ["...", { tag: "a", attribute: "href", type: "html" }] }
11+
}
12+
},
13+
experiments: { html: true }
14+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<title>Page x</title>

0 commit comments

Comments
 (0)