Skip to content

Commit 50c420f

Browse files
fix(html): name a page's extracted chunks after its entry
An HTML page emits no JavaScript of its own, so the page's chunk no longer occupies the entry's filename and the scripts and stylesheets extracted from it take it instead: an `output.html` entry emits `main.js` again, not `__html_<hash>_0.chunk.js`.
1 parent 948c004 commit 50c420f

31 files changed

Lines changed: 305 additions & 106 deletions

File tree

‎.changeset/181-html-entry-js-asset.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Drop the JS asset an extracted HTML entry leaves behind, which no page loads.
5+
Emit no JavaScript copy of an HTML entry's page, and name its chunks after it.

‎lib/dependencies/HtmlEntryDependency.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const {
88
CSS_IMPORT_TYPE,
99
CSS_TYPE,
10+
HTML_TYPE,
1011
JAVASCRIPT_TYPE
1112
} = require("../ModuleSourceTypeConstants");
1213
const ResourceHintPlugin = require("../prefetch/ResourceHintPlugin");
@@ -587,6 +588,37 @@ const buildLinkTag = (rel, attrs) =>
587588
: ""
588589
}>`;
589590

591+
/**
592+
* The chunks a hint means when it names a page: a page loads no JavaScript of
593+
* its own, so the hint follows the entries the parser extracted from it.
594+
* @param {import("../Compilation")} compilation compilation
595+
* @param {Chunk} chunk chunk a hint resolved to
596+
* @returns {Chunk[]} the chunks to hint at
597+
*/
598+
const resolvePageChunk = (compilation, chunk) => {
599+
const modules = compilation.chunkGraph.getChunkModulesIterableBySourceType(
600+
chunk,
601+
HTML_TYPE
602+
);
603+
/** @type {Chunk[]} */
604+
const chunks = [];
605+
for (const module of modules || []) {
606+
const { htmlEntries } =
607+
/** @type {import("../html/HtmlModule").HtmlModuleBuildInfo} */ (
608+
module.buildInfo
609+
) || {};
610+
if (!htmlEntries) continue;
611+
for (const group of Object.values(htmlEntries)) {
612+
for (const { entryName } of group) {
613+
const entrypoint = compilation.entrypoints.get(entryName);
614+
const entryChunk = entrypoint && entrypoint.getEntrypointChunk();
615+
if (entryChunk) chunks.push(entryChunk);
616+
}
617+
}
618+
}
619+
return chunks.length === 0 ? [chunk] : chunks;
620+
};
621+
590622
/**
591623
* Resolve one `output.html.resourceHints` descriptor to zero or more `<link>`
592624
* tags. A `chunk`/`entry` reference becomes the emitted chunk URL (a sentinel
@@ -633,6 +665,14 @@ const resolveResourceHint = (desc, ctx) => {
633665
} else {
634666
return [];
635667
}
668+
/** @type {Chunk[]} */
669+
const hinted = [];
670+
for (const chunk of chunks) {
671+
for (const resolved of resolvePageChunk(ctx.compilation, chunk)) {
672+
hinted.push(resolved);
673+
}
674+
}
675+
chunks = hinted;
636676
const kind = desc.as === "style" ? "css" : "javascript";
637677
const as = desc.as || (rel === "modulepreload" ? undefined : "script");
638678
// A hint targeting another chunk must match how that chunk is fetched, so

‎lib/html/HtmlGenerator.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ConcatenationScope = require("../ConcatenationScope");
99
const Generator = require("../Generator");
1010
const {
1111
HTML_TYPE,
12+
HTML_TYPES,
1213
JAVASCRIPT_TYPE,
1314
JAVASCRIPT_TYPES
1415
} = require("../ModuleSourceTypeConstants");
@@ -1047,13 +1048,16 @@ class HtmlGenerator extends Generator {
10471048
* Creates an instance of HtmlGenerator.
10481049
* @param {HtmlGeneratorOptions=} options generator options
10491050
* @param {ModuleGraph=} moduleGraph the module graph; used to detect when an HTML module is reached as a compilation entry so `extract` can default to `true` for it
1051+
* @param {((module: NormalModule) => boolean)=} isPageOnly tells whether the module's only output is its emitted page, so it exposes no JavaScript
10501052
*/
1051-
constructor(options, moduleGraph) {
1053+
constructor(options, moduleGraph, isPageOnly) {
10521054
super();
10531055
/** @type {HtmlGeneratorOptions} */
10541056
this.options = options || {};
10551057
/** @type {ModuleGraph | undefined} */
10561058
this._moduleGraph = moduleGraph;
1059+
/** @type {((module: NormalModule) => boolean) | undefined} */
1060+
this._isPageOnly = isPageOnly;
10571061
// Raw dependency-template render (before `[webpack/auto]` resolution),
10581062
// shared between the JS and HTML type passes of a single
10591063
// `codeGeneration()` call. Keyed by that call's `runtimeRequirements`
@@ -1134,6 +1138,9 @@ class HtmlGenerator extends Generator {
11341138
*/
11351139
getTypes(module) {
11361140
if (this._shouldExposeHtmlType(module)) {
1141+
// A page nothing else reads has no JavaScript side: that chunk would
1142+
// only re-export the markup already emitted as the `.html` asset.
1143+
if (this._isPageOnly && this._isPageOnly(module)) return HTML_TYPES;
11371144
return JAVASCRIPT_AND_HTML_TYPES;
11381145
}
11391146
return JAVASCRIPT_TYPES;
@@ -1143,10 +1150,9 @@ class HtmlGenerator extends Generator {
11431150
* @returns {boolean} whether getTypes() depends on the module's incoming connections
11441151
*/
11451152
getTypesDependOnIncomingConnections() {
1146-
// A fixed extract (`true` / `false` / `"inline"`) gives a stable type set;
1147-
// only the unset default reads `_isEntryModule` (incoming connections).
1148-
const { extract } = this.options;
1149-
return extract !== true && extract !== false && extract !== "inline";
1153+
// Every page that can expose the html type reads incoming connections —
1154+
// for the `extract` default, and for the page-only JavaScript drop.
1155+
return this.options.extract !== false;
11501156
}
11511157

11521158
/**

0 commit comments

Comments
 (0)