Skip to content

Commit 9bb5651

Browse files
committed
add asyncChunks: boolean option to disable creation of async chunks
1 parent 970b368 commit 9bb5651

File tree

13 files changed

+125
-29
lines changed

13 files changed

+125
-29
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,10 @@ export interface EntryObject {
10191019
* An object with entry point description.
10201020
*/
10211021
export interface EntryDescription {
1022+
/**
1023+
* Enable/disable creating async chunks that are loaded on demand.
1024+
*/
1025+
asyncChunks?: boolean;
10221026
/**
10231027
* The method of loading chunks (methods included by default are 'jsonp' (web), 'import' (ESM), 'importScripts' (WebWorker), 'require' (sync node.js), 'async-node' (async node.js), but others might be added by plugins).
10241028
*/
@@ -1928,6 +1932,10 @@ export interface Output {
19281932
* The filename of asset modules as relative path inside the 'output.path' directory.
19291933
*/
19301934
assetModuleFilename?: AssetModuleFilename;
1935+
/**
1936+
* Enable/disable creating async chunks that are loaded on demand.
1937+
*/
1938+
asyncChunks?: boolean;
19311939
/**
19321940
* Add a comment in the UMD wrapper.
19331941
*/
@@ -2688,6 +2696,10 @@ export interface EmptyParserOptions {}
26882696
* An object with entry point description.
26892697
*/
26902698
export interface EntryDescriptionNormalized {
2699+
/**
2700+
* Enable/disable creating async chunks that are loaded on demand.
2701+
*/
2702+
asyncChunks?: boolean;
26912703
/**
26922704
* The method of loading chunks (methods included by default are 'jsonp' (web), 'import' (ESM), 'importScripts' (WebWorker), 'require' (sync node.js), 'async-node' (async node.js), but others might be added by plugins).
26932705
*/
@@ -3052,6 +3064,10 @@ export interface OutputNormalized {
30523064
* The filename of asset modules as relative path inside the 'output.path' directory.
30533065
*/
30543066
assetModuleFilename?: AssetModuleFilename;
3067+
/**
3068+
* Enable/disable creating async chunks that are loaded on demand.
3069+
*/
3070+
asyncChunks?: boolean;
30553071
/**
30563072
* Add charset attribute for script tag.
30573073
*/

lib/EntryOptionPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class EntryOptionPlugin {
6464
dependOn: desc.dependOn,
6565
publicPath: desc.publicPath,
6666
chunkLoading: desc.chunkLoading,
67+
asyncChunks: desc.asyncChunks,
6768
wasmLoading: desc.wasmLoading,
6869
library: desc.library
6970
};

lib/buildChunkGraph.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
5050
* @property {Set<ChunkGroupInfo>} availableChildren set of chunk groups which depend on the this chunk group as availableSource
5151
* @property {number} preOrderIndex next pre order index
5252
* @property {number} postOrderIndex next post order index
53-
* @property {boolean} asyncChunkLoading create async chunks
53+
* @property {boolean} chunkLoading has a chunk loading mechanism
54+
* @property {boolean} asyncChunks create async chunks
5455
*/
5556

5657
/**
@@ -306,10 +307,14 @@ const visitModules = (
306307
availableChildren: undefined,
307308
preOrderIndex: 0,
308309
postOrderIndex: 0,
309-
asyncChunkLoading:
310-
chunkGroup.options.chunkLoading === false
311-
? false
312-
: compilation.outputOptions.chunkLoading !== false
310+
chunkLoading:
311+
chunkGroup.options.chunkLoading !== undefined
312+
? chunkGroup.options.chunkLoading !== false
313+
: compilation.outputOptions.chunkLoading !== false,
314+
asyncChunks:
315+
chunkGroup.options.asyncChunks !== undefined
316+
? chunkGroup.options.asyncChunks
317+
: compilation.outputOptions.asyncChunks !== false
313318
};
314319
chunkGroup.index = nextChunkGroupIndex++;
315320
if (chunkGroup.getNumberOfParents() > 0) {
@@ -424,10 +429,14 @@ const visitModules = (
424429
availableChildren: undefined,
425430
preOrderIndex: 0,
426431
postOrderIndex: 0,
427-
asyncChunkLoading:
432+
chunkLoading:
428433
entryOptions.chunkLoading !== undefined
429434
? entryOptions.chunkLoading !== false
430-
: chunkGroupInfo.asyncChunkLoading
435+
: chunkGroupInfo.chunkLoading,
436+
asyncChunks:
437+
entryOptions.asyncChunks !== undefined
438+
? entryOptions.asyncChunks
439+
: chunkGroupInfo.asyncChunks
431440
};
432441
chunkGroupInfoMap.set(entrypoint, cgi);
433442

@@ -451,7 +460,7 @@ const visitModules = (
451460
chunkGroup: entrypoint,
452461
chunkGroupInfo: cgi
453462
});
454-
} else if (!chunkGroupInfo.asyncChunkLoading) {
463+
} else if (!chunkGroupInfo.asyncChunks || !chunkGroupInfo.chunkLoading) {
455464
// Just queue the block into the current chunk group
456465
queue.push({
457466
action: PROCESS_BLOCK,
@@ -484,7 +493,8 @@ const visitModules = (
484493
availableChildren: undefined,
485494
preOrderIndex: 0,
486495
postOrderIndex: 0,
487-
asyncChunkLoading: chunkGroupInfo.asyncChunkLoading
496+
chunkLoading: chunkGroupInfo.chunkLoading,
497+
asyncChunks: chunkGroupInfo.asyncChunks
488498
};
489499
allCreatedChunkGroups.add(c);
490500
chunkGroupInfoMap.set(c, cgi);

lib/config/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ const applyOutputDefaults = (
744744
"Chunk format can't be selected by default when no target is specified"
745745
);
746746
});
747+
D(output, "asyncChunks", true);
747748
F(output, "chunkLoading", () => {
748749
if (tp) {
749750
switch (output.chunkFormat) {

lib/config/normalization.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ const getNormalizedWebpackOptions = config => {
290290
/** @type {OutputNormalized} */
291291
const result = {
292292
assetModuleFilename: output.assetModuleFilename,
293+
asyncChunks: output.asyncChunks,
293294
charset: output.charset,
294295
chunkFilename: output.chunkFilename,
295296
chunkFormat: output.chunkFormat,
@@ -484,6 +485,7 @@ const getNormalizedEntryStatic = entry => {
484485
runtime: value.runtime,
485486
publicPath: value.publicPath,
486487
chunkLoading: value.chunkLoading,
488+
asyncChunks: value.asyncChunks,
487489
wasmLoading: value.wasmLoading,
488490
dependOn:
489491
value.dependOn &&

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@
434434
"type": "object",
435435
"additionalProperties": false,
436436
"properties": {
437+
"asyncChunks": {
438+
"description": "Enable/disable creating async chunks that are loaded on demand.",
439+
"type": "boolean"
440+
},
437441
"chunkLoading": {
438442
"$ref": "#/definitions/ChunkLoading"
439443
},
@@ -487,6 +491,10 @@
487491
"type": "object",
488492
"additionalProperties": false,
489493
"properties": {
494+
"asyncChunks": {
495+
"description": "Enable/disable creating async chunks that are loaded on demand.",
496+
"type": "boolean"
497+
},
490498
"chunkLoading": {
491499
"$ref": "#/definitions/ChunkLoading"
492500
},
@@ -2891,6 +2899,10 @@
28912899
"assetModuleFilename": {
28922900
"$ref": "#/definitions/AssetModuleFilename"
28932901
},
2902+
"asyncChunks": {
2903+
"description": "Enable/disable creating async chunks that are loaded on demand.",
2904+
"type": "boolean"
2905+
},
28942906
"auxiliaryComment": {
28952907
"cli": {
28962908
"exclude": true
@@ -3090,6 +3102,10 @@
30903102
"assetModuleFilename": {
30913103
"$ref": "#/definitions/AssetModuleFilename"
30923104
},
3105+
"asyncChunks": {
3106+
"description": "Enable/disable creating async chunks that are loaded on demand.",
3107+
"type": "boolean"
3108+
},
30933109
"charset": {
30943110
"$ref": "#/definitions/Charset"
30953111
},

test/Defaults.unittest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ describe("Defaults", () => {
296296
},
297297
"output": Object {
298298
"assetModuleFilename": "[hash][ext][query]",
299+
"asyncChunks": true,
299300
"charset": true,
300301
"chunkFilename": "[name].js",
301302
"chunkFormat": "array-push",

test/Validation.test.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,18 @@ describe("Validation", () => {
453453
},
454454
msg =>
455455
expect(msg).toMatchInlineSnapshot(`
456-
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
457-
- configuration.optimization.splitChunks.cacheGroups should not be object { test, … }.
458-
-> Using the cacheGroup shorthand syntax with a cache group named 'test' is a potential config error
459-
Did you intent to define a cache group with a test instead?
460-
cacheGroups: {
461-
<name>: {
462-
test: ...
463-
}
464-
}.
465-
object { <key>: false | RegExp | string | function | object { automaticNameDelimiter?, chunks?, enforce?, enforceSizeThreshold?, filename?, idHint?, layer?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, priority?, reuseExistingChunk?, test?, type?, usedExports? } }
466-
-> Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks, default categories: 'default', 'defaultVendors')."
467-
`)
456+
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
457+
- configuration.optimization.splitChunks.cacheGroups should not be object { test, … }.
458+
-> Using the cacheGroup shorthand syntax with a cache group named 'test' is a potential config error
459+
Did you intent to define a cache group with a test instead?
460+
cacheGroups: {
461+
<name>: {
462+
test: ...
463+
}
464+
}.
465+
object { <key>: false | RegExp | string | function | object { automaticNameDelimiter?, chunks?, enforce?, enforceSizeThreshold?, filename?, idHint?, layer?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, priority?, reuseExistingChunk?, test?, type?, usedExports? } }
466+
-> Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks, default categories: 'default', 'defaultVendors')."
467+
`)
468468
);
469469

470470
createTestCase(
@@ -497,7 +497,7 @@ describe("Validation", () => {
497497
expect(msg).toMatchInlineSnapshot(`
498498
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
499499
- configuration.output has an unknown property 'ecmaVersion'. These properties are valid:
500-
object { assetModuleFilename?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerWasmLoading? }
500+
object { assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerWasmLoading? }
501501
-> Options affecting the output of the compilation. \`output\` options tell webpack how to write the compiled files to disk.
502502
Did you mean output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)?"
503503
`)
@@ -661,11 +661,11 @@ describe("Validation", () => {
661661
},
662662
msg =>
663663
expect(msg).toMatchInlineSnapshot(`
664-
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
665-
- configuration.optimization.splitChunks has an unknown property 'automaticNamePrefix'. These properties are valid:
666-
object { automaticNameDelimiter?, cacheGroups?, chunks?, defaultSizeTypes?, enforceSizeThreshold?, fallbackCacheGroup?, filename?, hidePathInfo?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, usedExports? }
667-
-> Options object for splitting chunks into smaller chunks."
668-
`)
664+
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
665+
- configuration.optimization.splitChunks has an unknown property 'automaticNamePrefix'. These properties are valid:
666+
object { automaticNameDelimiter?, cacheGroups?, chunks?, defaultSizeTypes?, enforceSizeThreshold?, fallbackCacheGroup?, filename?, hidePathInfo?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, usedExports? }
667+
-> Options object for splitting chunks into smaller chunks."
668+
`)
669669
);
670670
});
671671
});

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,6 +4974,19 @@ Object {
49744974
"multiple": false,
49754975
"simpleType": "string",
49764976
},
4977+
"output-async-chunks": Object {
4978+
"configs": Array [
4979+
Object {
4980+
"description": "Enable/disable creating async chunks that are loaded on demand.",
4981+
"multiple": false,
4982+
"path": "output.asyncChunks",
4983+
"type": "boolean",
4984+
},
4985+
],
4986+
"description": "Enable/disable creating async chunks that are loaded on demand.",
4987+
"multiple": false,
4988+
"simpleType": "boolean",
4989+
},
49774990
"output-charset": Object {
49784991
"configs": Array [
49794992
Object {

0 commit comments

Comments
 (0)