diff --git a/lib/web/JsonpMainTemplatePlugin.js b/lib/web/JsonpMainTemplatePlugin.js index 78e53b50744..412e4d68ef5 100644 --- a/lib/web/JsonpMainTemplatePlugin.js +++ b/lib/web/JsonpMainTemplatePlugin.js @@ -108,9 +108,9 @@ class JsonpMainTemplatePlugin { mainTemplate.hooks.localVars.tap( "JsonpMainTemplatePlugin", (source, chunk, hash) => { + const extraCode = []; if (needChunkLoadingCode(chunk)) { - return Template.asString([ - source, + extraCode.push( "", "// object to store loaded and loading chunks", "// undefined = chunk not loaded, null = chunk preloaded/prefetched", @@ -120,6 +120,12 @@ class JsonpMainTemplatePlugin { chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n") ), "};", + "", + needEntryDeferringCode(chunk) ? "var deferredModules = [];" : "" + ); + } + if (needChunkOnDemandLoadingCode(chunk)) { + extraCode.push( "", "// script path function", "function jsonpScriptSrc(chunkId) {", @@ -130,12 +136,11 @@ class JsonpMainTemplatePlugin { "chunkId" )}` ]), - "}", - "", - needEntryDeferringCode(chunk) ? "var deferredModules = [];" : "" - ]); + "}" + ); } - return source; + if (extraCode.length === 0) return source; + return Template.asString([source, ...extraCode]); } ); diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index 531e24da9df..fd608c497c1 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -615,7 +615,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-0 Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - entry-1.js 5.79 KiB 0 [emitted] entry-1 + entry-1.js 5.62 KiB 0 [emitted] entry-1 vendor-1~entry-1.js 314 bytes 1 [emitted] vendor-1~entry-1 Entrypoint entry-1 = vendor-1~entry-1.js entry-1.js [0] ./entry-1.js 145 bytes {0} [built] @@ -632,7 +632,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-In Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - entry-1.js 5.79 KiB 0 [emitted] entry-1 + entry-1.js 5.62 KiB 0 [emitted] entry-1 vendor-1.js 314 bytes 1 [emitted] vendor-1 Entrypoint entry-1 = vendor-1.js entry-1.js [0] ./entry-1.js 145 bytes {0} [built] @@ -651,7 +651,7 @@ Child Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - app.js 5.9 KiB 0 [emitted] app + app.js 5.71 KiB 0 [emitted] app vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js [./constants.js] 87 bytes {1} [built] @@ -664,7 +664,7 @@ Child Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - app.js 5.92 KiB 0 [emitted] app + app.js 5.72 KiB 0 [emitted] app vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js [./constants.js] 87 bytes {1} [built] @@ -1321,7 +1321,7 @@ exports[`StatsTestCases should print correct stats for named-chunks-plugin 1`] = Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - entry.js 5.64 KiB entry [emitted] entry + entry.js 5.47 KiB entry [emitted] entry vendor.js 269 bytes vendor [emitted] vendor Entrypoint entry = vendor.js entry.js [./entry.js] 72 bytes {entry} [built] diff --git a/test/configCases/split-chunks/runtime-chunk-no-async/index.js b/test/configCases/split-chunks/runtime-chunk-no-async/index.js new file mode 100644 index 00000000000..6068c292057 --- /dev/null +++ b/test/configCases/split-chunks/runtime-chunk-no-async/index.js @@ -0,0 +1 @@ +it("should compile and evaluate fine", () => {}); diff --git a/test/configCases/split-chunks/runtime-chunk-no-async/test.config.js b/test/configCases/split-chunks/runtime-chunk-no-async/test.config.js new file mode 100644 index 00000000000..7eafe4ed79c --- /dev/null +++ b/test/configCases/split-chunks/runtime-chunk-no-async/test.config.js @@ -0,0 +1,7 @@ +const fs = require("fs"); +module.exports = { + findBundle: function(i, options) { + var files = fs.readdirSync(options.output.path); + return ["runtime.js", files.filter(f => /^main/.test(f))[0]]; + } +}; diff --git a/test/configCases/split-chunks/runtime-chunk-no-async/webpack.config.js b/test/configCases/split-chunks/runtime-chunk-no-async/webpack.config.js new file mode 100644 index 00000000000..5f4da7943f0 --- /dev/null +++ b/test/configCases/split-chunks/runtime-chunk-no-async/webpack.config.js @@ -0,0 +1,13 @@ +module.exports = { + entry: { + main: "./index" + }, + target: "web", + output: { + filename: "[name].js", + chunkFilename: "main.[contenthash:8].js" + }, + optimization: { + runtimeChunk: "single" + } +};