Skip to content

Commit 9dd7f94

Browse files
feat: add output.analyzableChunkImport option (#22056)
Allow opting out of statically analyzable literal dynamic import() specifiers in ESM output via output.analyzableChunkImport: false. When disabled, chunk loading falls back to runtime resolution with chunk IDs, preventing chunk content hashes from being baked into importing modules and preserving long-term caching. Fixes #22056
1 parent 3a69d29 commit 9dd7f94

13 files changed

Lines changed: 164 additions & 60 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `output.analyzableChunkImport` to opt out of baking chunk filenames into importing modules in ESM output.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,10 @@ export interface Output {
29252925
* Add a container for define/require functions in the AMD module.
29262926
*/
29272927
amdContainer?: AmdContainer;
2928+
/**
2929+
* Bake statically analyzable literal import() specifiers for chunk loading in ESM output. When false, chunk loading uses runtime resolution with chunk IDs, preventing chunk content hashes from being baked into importing modules.
2930+
*/
2931+
analyzableChunkImport?: boolean;
29282932
/**
29292933
* The filename of asset modules as relative path inside the 'output.path' directory.
29302934
*/
@@ -5099,6 +5103,10 @@ export interface OptimizationNormalized {
50995103
* Normalized options affecting the output of the compilation. `output` options tell webpack how to write the compiled files to disk.
51005104
*/
51015105
export interface OutputNormalized {
5106+
/**
5107+
* Bake statically analyzable literal import() specifiers for chunk loading in ESM output. When false, chunk loading uses runtime resolution with chunk IDs, preventing chunk content hashes from being baked into importing modules.
5108+
*/
5109+
analyzableChunkImport?: boolean;
51025110
/**
51035111
* The filename of asset modules as relative path inside the 'output.path' directory.
51045112
*/

‎lib/RuntimeTemplate.js‎

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const rootedParts = (parts) =>
171171
/** @type {string} */ (parts[0][1]).slice(2)
172172
]),
173173
...parts.slice(1)
174-
]
174+
]
175175
: parts;
176176

177177
// Types that ride the chunk itself or render their own asset, so no `.f` handler
@@ -360,7 +360,7 @@ const createHashProbeChunk = (chunk, hash, present) => {
360360
writable: true,
361361
enumerable: true,
362362
configurable: true
363-
}
363+
}
364364
: undefined
365365
}
366366
);
@@ -725,18 +725,29 @@ class RuntimeTemplate {
725725
}
726726

727727
if (form === "import") {
728+
if (outputOptions.analyzableChunkImport === false) {
729+
return this._analyzableBailout(
730+
module,
731+
"output.analyzableChunkImport is disabled",
732+
false
733+
);
734+
}
728735
// Read through a native `import()`, or it is not this feature at all.
729736
if (outputOptions.chunkFormat !== "module") {
730737
return this._analyzableBailout(
731738
module,
732-
`output.chunkFormat is ${JSON.stringify(outputOptions.chunkFormat)}, so chunks are not read through a native import()`,
739+
`output.chunkFormat is ${JSON.stringify(
740+
outputOptions.chunkFormat
741+
)}, so chunks are not read through a native import()`,
733742
false
734743
);
735744
}
736745
if (outputOptions.importFunctionName !== "import") {
737746
return this._analyzableBailout(
738747
module,
739-
`output.importFunctionName is ${JSON.stringify(outputOptions.importFunctionName)}, so the call site is not a native import()`,
748+
`output.importFunctionName is ${JSON.stringify(
749+
outputOptions.importFunctionName
750+
)}, so the call site is not a native import()`,
740751
false
741752
);
742753
}
@@ -751,7 +762,9 @@ class RuntimeTemplate {
751762
if (entryOptions.chunkLoading !== "import") {
752763
return this._analyzableBailout(
753764
module,
754-
`this worker loads its chunks with ${JSON.stringify(entryOptions.chunkLoading)}, not "import"`,
765+
`this worker loads its chunks with ${JSON.stringify(
766+
entryOptions.chunkLoading
767+
)}, not "import"`,
755768
false
756769
);
757770
}
@@ -776,7 +789,9 @@ class RuntimeTemplate {
776789
) {
777790
return this._analyzableBailout(
778791
module,
779-
`devtool ${JSON.stringify(devtool)} wraps the module in eval(), where import.meta does not parse`,
792+
`devtool ${JSON.stringify(
793+
devtool
794+
)} wraps the module in eval(), where import.meta does not parse`,
780795
false
781796
);
782797
}
@@ -1125,7 +1140,9 @@ class RuntimeTemplate {
11251140
if (!isAbsoluteBaseUri(base)) {
11261141
return this._analyzableBailout(
11271142
module,
1128-
`an entry sets a baseUri of ${JSON.stringify(base)}, whose scheme names no base anything can be resolved against`,
1143+
`an entry sets a baseUri of ${JSON.stringify(
1144+
base
1145+
)}, whose scheme names no base anything can be resolved against`,
11291146
null
11301147
);
11311148
}
@@ -1146,7 +1163,9 @@ class RuntimeTemplate {
11461163
} catch (_error) {
11471164
return this._analyzableBailout(
11481165
module,
1149-
`an entry sets a baseUri of ${JSON.stringify(base)}, which is not absolute, so no url can be resolved against it here`,
1166+
`an entry sets a baseUri of ${JSON.stringify(
1167+
base
1168+
)}, which is not absolute, so no url can be resolved against it here`,
11501169
null
11511170
);
11521171
}
@@ -1643,7 +1662,7 @@ class RuntimeTemplate {
16431662
? `${decl} [${items.join(", ")}] = ${value};`
16441663
: Template.asString(
16451664
items.map((item, i) => `${decl} ${item} = ${value}[${i}];`)
1646-
);
1665+
);
16471666
}
16481667

16491668
/**
@@ -1660,7 +1679,7 @@ class RuntimeTemplate {
16601679
items.map(
16611680
(item) => `${decl} ${item} = ${value}${propertyAccess([item])};`
16621681
)
1663-
);
1682+
);
16641683
}
16651684

16661685
/**
@@ -1685,7 +1704,7 @@ class RuntimeTemplate {
16851704
? `for(const ${variable} of ${array}) {\n${Template.indent(body)}\n}`
16861705
: `${array}.forEach(function(${variable}) {\n${Template.indent(
16871706
body
1688-
)}\n});`;
1707+
)}\n});`;
16891708
}
16901709

16911710
/**
@@ -1792,10 +1811,10 @@ class RuntimeTemplate {
17921811
moduleId === null
17931812
? JSON.stringify("Module is not available (weak dependency)")
17941813
: idExpr
1795-
? `"Module '" + ${idExpr} + "' is not available (weak dependency)"`
1796-
: JSON.stringify(
1797-
`Module '${moduleId}' is not available (weak dependency)`
1798-
);
1814+
? `"Module '" + ${idExpr} + "' is not available (weak dependency)"`
1815+
: JSON.stringify(
1816+
`Module '${moduleId}' is not available (weak dependency)`
1817+
);
17991818
const comment = request ? `${Template.toNormalComment(request)} ` : "";
18001819
const errorStatements = `${this.renderConst()} e = new Error(${errorMessage}); ${comment}e.code = 'MODULE_NOT_FOUND'; throw e;`;
18011820
switch (type) {
@@ -2465,8 +2484,8 @@ class RuntimeTemplate {
24652484
return asiSafe
24662485
? `(${importVar}_default()${propertyAccess(exportName, 1)})`
24672486
: asiSafe === false
2468-
? `;(${importVar}_default()${propertyAccess(exportName, 1)})`
2469-
: `${importVar}_default.a${propertyAccess(exportName, 1)}`;
2487+
? `;(${importVar}_default()${propertyAccess(exportName, 1)})`
2488+
: `${importVar}_default.a${propertyAccess(exportName, 1)}`;
24702489

24712490
case "default-only":
24722491
case "default-with-named":
@@ -2545,8 +2564,8 @@ class RuntimeTemplate {
25452564
return asiSafe
25462565
? `(0,${access})`
25472566
: asiSafe === false
2548-
? `;(0,${access})`
2549-
: `/*#__PURE__*/Object(${access})`;
2567+
? `;(0,${access})`
2568+
: `/*#__PURE__*/Object(${access})`;
25502569
}
25512570
return access;
25522571
}
@@ -2573,9 +2592,11 @@ class RuntimeTemplate {
25732592
? RuntimeGlobals.hasSealedDeferredNamespace
25742593
: RuntimeGlobals.hasSynthesizedDeferredNamespace
25752594
);
2576-
const init = `${RuntimeGlobals.makeDeferredNamespaceObject}(${JSON.stringify(
2577-
id
2578-
)}, ${type}${names ? `, ${JSON.stringify(names)}` : ""})`;
2595+
const init = `${
2596+
RuntimeGlobals.makeDeferredNamespaceObject
2597+
}(${JSON.stringify(id)}, ${type}${
2598+
names ? `, ${JSON.stringify(names)}` : ""
2599+
})`;
25792600

25802601
return `/*#__PURE__*/ ${
25812602
asiSafe ? "" : asiSafe === false ? ";" : "Object"
@@ -3258,7 +3279,7 @@ class RuntimeTemplate {
32583279
`${shape}x`,
32593280
/** @type {string} */ (this.outputOptions.path),
32603281
true
3261-
);
3282+
);
32623283
this._publicPathClimbText = undo === "./" ? "" : undo;
32633284
}
32643285
return this._publicPathClimbText;
@@ -3286,7 +3307,7 @@ class RuntimeTemplate {
32863307
chunk,
32873308
runtime: chunk.runtime,
32883309
contentHashType: JAVASCRIPT_TYPE
3289-
});
3310+
});
32903311
const placement = {
32913312
// A name whose shape is unknown has an unknown depth, and the deferred pass
32923313
// reads the real one off the asset it lands in.
@@ -3297,7 +3318,7 @@ class RuntimeTemplate {
32973318
chunkName,
32983319
/** @type {string} */ (outputOptions.path),
32993320
true
3300-
),
3321+
),
33013322
// A chunk in an initial group and an async one both is served at two urls,
33023323
// one public path apart, so no one literal in it is right for both.
33033324
loaded: chunk.isOnlyInitial() ? false : chunk.canBeInitial() ? null : true
@@ -3354,7 +3375,7 @@ class RuntimeTemplate {
33543375
? JavascriptModulesPlugin.getChunkFilenameTemplate(
33553376
chunk,
33563377
this.outputOptions
3357-
)
3378+
)
33583379
: shape,
33593380
{ chunk, contentHashType: JAVASCRIPT_TYPE }
33603381
);
@@ -3522,7 +3543,7 @@ class RuntimeTemplate {
35223543
// not be the one on disk.
35233544
runtime: chunk.runtime,
35243545
contentHashType: sourceType
3525-
});
3546+
});
35263547
/**
35273548
* @param {SpecifierPart[]} prefix what goes in front of the chunk's filename
35283549
* @returns {string} the specifier already quoted

‎lib/config/defaults.js‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
331331
target === false
332332
? /** @type {false} */ (false)
333333
: typeof target === "string"
334-
? getTargetProperties(target, /** @type {Context} */ (options.context))
335-
: getTargetsProperties(
336-
/** @type {string[]} */ (target),
337-
/** @type {Context} */ (options.context)
338-
);
334+
? getTargetProperties(target, /** @type {Context} */ (options.context))
335+
: getTargetsProperties(
336+
/** @type {string[]} */ (target),
337+
/** @type {Context} */ (options.context)
338+
);
339339

340340
const development = mode === "development";
341341
const production = mode === "production" || !mode;
@@ -434,13 +434,13 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
434434
? {
435435
type: "css",
436436
use: "source-map"
437-
}
437+
}
438438
: undefined,
439439
{
440440
type: "javascript",
441441
use: "eval"
442442
}
443-
].filter(Boolean)
443+
].filter(Boolean)
444444
: false
445445
)
446446
);
@@ -537,8 +537,8 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
537537
validExternalTypes.includes(options.output.library.type)
538538
? /** @type {ExternalsType} */ (options.output.library.type)
539539
: options.output.module
540-
? "module-import"
541-
: "var";
540+
? "module-import"
541+
: "var";
542542
});
543543

544544
applyNodeDefaults(options.node, {
@@ -616,7 +616,7 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
616616
// spans both web and node (target "universal" or ["web", "node"])
617617
universal:
618618
targetProperties.node === null && targetProperties.web === null
619-
}
619+
}
620620
};
621621
};
622622

@@ -796,7 +796,7 @@ const applySnapshotDefaults = (snapshot, { production, futureDefaults }) => {
796796
process.versions.pnp === "3"
797797
? [
798798
/^(.+?(?:[\\/]\.yarn[\\/]unplugged[\\/][^\\/]+)?[\\/]node_modules[\\/])/
799-
]
799+
]
800800
: [/^(.+?[\\/]node_modules[\\/])/]
801801
);
802802
F(snapshot, "immutablePaths", () =>
@@ -1632,8 +1632,9 @@ const applyOutputDefaults = (
16321632
} catch (err) {
16331633
if (/** @type {Error & { code: string }} */ (err).code !== "ENOENT") {
16341634
/** @type {Error & { code: string }} */
1635-
(err).message +=
1636-
`\nwhile determining default 'output.uniqueName' from 'name' in ${pkgPath}`;
1635+
(
1636+
err
1637+
).message += `\nwhile determining default 'output.uniqueName' from 'name' in ${pkgPath}`;
16371638
throw err;
16381639
}
16391640
return "";
@@ -1645,9 +1646,9 @@ const applyOutputDefaults = (
16451646
F(output, "module", () =>
16461647
Boolean(
16471648
tp &&
1648-
((tp.node === null && tp.web === null && tp.module !== false) ||
1649-
tp.deno === true ||
1650-
tp.bun === true)
1649+
((tp.node === null && tp.web === null && tp.module !== false) ||
1650+
tp.deno === true ||
1651+
tp.bun === true)
16511652
)
16521653
);
16531654

@@ -1987,6 +1988,7 @@ const applyOutputDefaults = (
19871988
"Chunk format can't be selected by default when no target is specified"
19881989
);
19891990
});
1991+
D(output, "analyzableChunkImport", true);
19901992
D(output, "asyncChunks", true);
19911993
F(output, "chunkLoading", () => {
19921994
if (tp) {
@@ -2546,7 +2548,7 @@ const applyOptimizationDefaults = (
25462548
? getTargetsBrowsers(
25472549
Array.isArray(target) ? target : [target],
25482550
/** @type {Context} */ (compiler.options.context)
2549-
)
2551+
)
25502552
: undefined;
25512553
const cssEnvironment = {
25522554
browsers,

0 commit comments

Comments
 (0)