Skip to content

Commit 6818f63

Browse files
fix(html): keep a library page's own chunk in a resource hint
A hint naming a page expanded to the entries extracted from it and dropped the page's chunk, so an `output.library` page lost its own asset from the hint. `chunkHasJs` cannot decide this — it holds for any runtime chunk, including a page that emits no JavaScript.
1 parent ecf9010 commit 6818f63

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎lib/dependencies/HtmlEntryDependency.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,19 @@ const buildLinkTag = (rel, attrs) =>
596596
* @returns {Chunk[]} the chunks to hint at
597597
*/
598598
const resolvePageChunk = (compilation, chunk) => {
599-
const modules = compilation.chunkGraph.getChunkModulesIterableBySourceType(
599+
const { chunkGraph } = compilation;
600+
const modules = chunkGraph.getChunkModulesIterableBySourceType(
600601
chunk,
601602
HTML_TYPE
602603
);
604+
// A page with a JavaScript side of its own (an `output.library` entry) is
605+
// hinted too — `chunkHasJs` can't say so, it holds for any runtime chunk.
606+
const javascript = chunkGraph.getChunkModulesIterableBySourceType(
607+
chunk,
608+
JAVASCRIPT_TYPE
609+
);
603610
/** @type {Chunk[]} */
604-
const chunks = [];
611+
const chunks = javascript === undefined ? [] : [chunk];
605612
for (const module of modules || []) {
606613
const { htmlEntries } =
607614
/** @type {import("../html/HtmlModule").HtmlModuleBuildInfo} */ (

‎test/configCases/html/html-entry-library-js-asset/test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ it("should keep the JS asset of an HTML entry exposed as a library", () => {
1010
const html = fs.readFileSync(path.resolve(__dirname, "page.html"), "utf-8");
1111
expect(html).toMatch(/<script src="__html_[a-f0-9]+_0\.chunk\.js">/);
1212

13+
// The hint keeps the page's own library asset and adds the extracted one.
14+
expect(html).toMatch(/<link rel="preload" as="script" href="page\.js">/);
15+
expect(html).toMatch(
16+
/<link rel="preload" as="script" href="__html_[a-f0-9]+_0\.chunk\.js">/
17+
);
18+
1319
// The library hands back the page markup.
1420
expect(require("./page.js")).toContain("<title>HTML entry exposed as a library</title>");
1521
});

‎test/configCases/html/html-entry-library-js-asset/webpack.config.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = {
1717
chunkFilename: "[name].chunk.js",
1818
library: {
1919
type: "commonjs2"
20-
}
20+
},
21+
// A hint naming this entry covers both halves of the page: its own
22+
// library JavaScript and the script the parser extracted from it.
23+
resourceHints: [{ rel: "preload", as: "script", entry: "page" }]
2124
},
2225
optimization: {
2326
chunkIds: "named"

0 commit comments

Comments
 (0)