Skip to content

Commit

Permalink
Merge pull request webpack#12891 from webpack/bugfix/non-js-entry
Browse files Browse the repository at this point in the history
fix problem with startup of non-js initial chunks
  • Loading branch information
sokra committed Mar 14, 2021
2 parents e643b85 + 69218d4 commit 3f2a269
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 52 deletions.
6 changes: 5 additions & 1 deletion lib/Chunk.js
Expand Up @@ -592,7 +592,11 @@ class Chunk {
* @returns {Set<Chunk>} a set of all the initial chunks (including itself)
*/
getAllInitialChunks() {
return intersect(Array.from(this.groupsIterable, g => new Set(g.chunks)));
const chunks = new Set();
for (const group of this.groupsIterable) {
for (const c of group.chunks) chunks.add(c);
}
return chunks;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions lib/ChunkGraph.js
Expand Up @@ -683,11 +683,8 @@ class ChunkGraph {
*/
getChunkConditionMap(chunk, filterFn) {
const map = Object.create(null);
for (const asyncChunk of chunk.getAllAsyncChunks()) {
map[asyncChunk.id] = filterFn(asyncChunk, this);
}
for (const depChunk of this.getChunkEntryDependentChunksIterable(chunk)) {
map[depChunk.id] = filterFn(depChunk, this);
for (const c of chunk.getAllReferencedChunks()) {
map[c.id] = filterFn(c, this);
}
return map;
}
Expand Down
15 changes: 15 additions & 0 deletions lib/javascript/StartupHelpers.js
Expand Up @@ -9,6 +9,7 @@ const Entrypoint = require("../Entrypoint");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const { isSubset } = require("../util/SetHelpers");
const { chunkHasJs } = require("./JavascriptModulesPlugin");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compilation")} Compilation */
Expand Down Expand Up @@ -117,3 +118,17 @@ exports.generateEntryStartup = (
runtime.push("");
return Template.asString(runtime);
};

/**
* @param {Chunk} chunk the chunk
* @param {ChunkGraph} chunkGraph the chunk graph
* @returns {Set<number | string>} initially fulfilled chunk ids
*/
exports.getInitialChunkIds = (chunk, chunkGraph) => {
const initialChunkIds = new Set(chunk.ids);
for (const c of chunk.getAllInitialChunks()) {
if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
for (const id of c.ids) initialChunkIds.add(id);
}
return initialChunkIds;
};
6 changes: 5 additions & 1 deletion lib/node/ReadFileChunkLoadingRuntimeModule.js
Expand Up @@ -11,6 +11,7 @@ const {
chunkHasJs,
getChunkFilenameTemplate
} = require("../javascript/JavascriptModulesPlugin");
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
const { getUndoPath } = require("../util/identifier");

Expand Down Expand Up @@ -45,6 +46,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
);
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
const hasJsMatcher = compileBooleanMatcher(conditionMap);
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);

const outputName = this.compilation.getPath(
getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
Expand All @@ -70,7 +72,9 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
'// "0" means "already loaded", Promise means loading',
"var installedChunks = {",
Template.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 0`).join(
",\n"
)
),
"};",
"",
Expand Down
6 changes: 5 additions & 1 deletion lib/node/RequireChunkLoadingRuntimeModule.js
Expand Up @@ -11,6 +11,7 @@ const {
chunkHasJs,
getChunkFilenameTemplate
} = require("../javascript/JavascriptModulesPlugin");
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
const { getUndoPath } = require("../util/identifier");

Expand Down Expand Up @@ -45,6 +46,7 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
);
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
const hasJsMatcher = compileBooleanMatcher(conditionMap);
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);

const outputName = this.compilation.getPath(
getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
Expand All @@ -70,7 +72,9 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
'// "1" means "loaded", otherwise not loaded yet',
"var installedChunks = {",
Template.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 1`).join(",\n")
Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
",\n"
)
),
"};",
"",
Expand Down
25 changes: 6 additions & 19 deletions lib/web/JsonpChunkLoadingRuntimeModule.js
Expand Up @@ -10,6 +10,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");
const chunkHasJs = require("../javascript/JavascriptModulesPlugin").chunkHasJs;
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
const compileBooleanMatcher = require("../util/compileBooleanMatcher");

/** @typedef {import("../Chunk")} Chunk */
Expand Down Expand Up @@ -98,17 +99,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
)}]`;
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
const hasJsMatcher = compileBooleanMatcher(conditionMap);
const nonJsEntryConditionMap = chunkGraph.getChunkConditionMap(
chunk,
chunk => {
if (chunkHasJs(chunk, chunkGraph)) return false;
for (const chunkGroup of chunk.groupsIterable) {
if (chunk.isInGroup(chunkGroup)) return true;
}
return false;
}
);
const nonJsEntryMatcher = compileBooleanMatcher(nonJsEntryConditionMap);
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);

return Template.asString([
withBaseURI
Expand All @@ -122,7 +113,9 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
"// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded",
"var installedChunks = {",
Template.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 0`).join(
",\n"
)
),
"};",
"",
Expand Down Expand Up @@ -383,13 +376,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
? `${
RuntimeGlobals.onChunksLoaded
}.j = ${runtimeTemplate.returningFunction(
nonJsEntryMatcher === true
? "true"
: `installedChunks[chunkId] === 0${
nonJsEntryMatcher === false
? ""
: ` || (${nonJsEntryMatcher("chunkId")})`
}`,
"installedChunks[chunkId] === 0",
"chunkId"
)};`
: "// no on chunks loaded",
Expand Down
6 changes: 5 additions & 1 deletion lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js
Expand Up @@ -11,6 +11,7 @@ const {
getChunkFilenameTemplate,
chunkHasJs
} = require("../javascript/JavascriptModulesPlugin");
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
const { getUndoPath } = require("../util/identifier");

Expand Down Expand Up @@ -49,6 +50,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
const hasJsMatcher = compileBooleanMatcher(
chunkGraph.getChunkConditionMap(chunk, chunkHasJs)
);
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);

const outputName = this.compilation.getPath(
getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
Expand All @@ -72,7 +74,9 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
'// "1" means "already loaded"',
"var installedChunks = {",
Template.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 1`).join(",\n")
Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
",\n"
)
),
"};",
"",
Expand Down
48 changes: 24 additions & 24 deletions test/__snapshots__/StatsTestCases.test.js.snap
Expand Up @@ -1139,11 +1139,11 @@ webpack x.x.x compiled successfully in X ms
assets by chunk 895 bytes (id hint: all)
asset c-all-b_js-fe3b412293f057de38c8.js 502 bytes [emitted] [immutable] (id hint: all)
asset c-all-c_js-172fc4c28be7e411e551.js 393 bytes [emitted] [immutable] (id hint: all)
asset c-runtime~main-61f021fac209297b0356.js 13.4 KiB [emitted] [immutable] (name: runtime~main)
asset c-runtime~main-68e3736bd683bb157d83.js 13.5 KiB [emitted] [immutable] (name: runtime~main)
asset c-main-012ece0a038732fde0a4.js 673 bytes [emitted] [immutable] (name: main)
asset c-vendors-node_modules_vendor_js-499179597d8c965dd5e0.js 185 bytes [emitted] [immutable] (id hint: vendors)
Entrypoint main 14.5 KiB = c-runtime~main-61f021fac209297b0356.js 13.4 KiB c-all-c_js-172fc4c28be7e411e551.js 393 bytes c-main-012ece0a038732fde0a4.js 673 bytes
runtime modules 8.54 KiB 13 modules
Entrypoint main 14.5 KiB = c-runtime~main-68e3736bd683bb157d83.js 13.5 KiB c-all-c_js-172fc4c28be7e411e551.js 393 bytes c-main-012ece0a038732fde0a4.js 673 bytes
runtime modules 8.56 KiB 13 modules
cacheable modules 101 bytes
./c.js 61 bytes [built] [code generated]
./b.js 17 bytes [built] [code generated]
Expand Down Expand Up @@ -2440,19 +2440,19 @@ LOG from webpack.FileSystemInfo
exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `
"a-normal:
assets by path *.js 3.19 KiB
asset e8e56a295a1d995abc13-e8e56a.js 2.67 KiB [emitted] [immutable] [minimized] (name: runtime)
assets by path *.js 3.22 KiB
asset 738219e054ec761016cb-738219.js 2.69 KiB [emitted] [immutable] [minimized] (name: runtime)
asset a639a9edc4557744bf94-a639a9.js 288 bytes [emitted] [immutable] [minimized] (name: lazy)
asset e00b58ce2785691cd374-e00b58.js 225 bytes [emitted] [immutable] [minimized] (name: index)
asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b)
assets by chunk 20.4 KiB (auxiliary name: lazy)
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 2.89 KiB (5.89 KiB) = e8e56a295a1d995abc13-e8e56a.js 2.67 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset
Entrypoint index 2.91 KiB (5.89 KiB) = 738219e054ec761016cb-738219.js 2.69 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset
Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.19 KiB 9 modules
runtime modules 7.18 KiB 9 modules
orphan modules 23 bytes [orphan] 1 module
cacheable modules 514 bytes (javascript) 26.3 KiB (asset)
javascript modules 388 bytes
Expand All @@ -2467,19 +2467,19 @@ exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `
a-normal (webpack x.x.x) compiled successfully in X ms
b-normal:
assets by path *.js 3.19 KiB
asset dcb5f80b97576d2a9db6-dcb5f8.js 2.67 KiB [emitted] [immutable] [minimized] (name: runtime)
assets by path *.js 3.22 KiB
asset 4ee69fdb492561515054-4ee69f.js 2.69 KiB [emitted] [immutable] [minimized] (name: runtime)
asset a639a9edc4557744bf94-a639a9.js 288 bytes [emitted] [immutable] [minimized] (name: lazy)
asset e00b58ce2785691cd374-e00b58.js 225 bytes [emitted] [immutable] [minimized] (name: index)
asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b)
assets by chunk 20.4 KiB (auxiliary name: lazy)
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 2.89 KiB (5.89 KiB) = dcb5f80b97576d2a9db6-dcb5f8.js 2.67 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset
Entrypoint index 2.91 KiB (5.89 KiB) = 4ee69fdb492561515054-4ee69f.js 2.69 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset
Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.19 KiB 9 modules
runtime modules 7.18 KiB 9 modules
orphan modules 19 bytes [orphan] 1 module
cacheable modules 469 bytes (javascript) 26.3 KiB (asset)
javascript modules 343 bytes
Expand All @@ -2494,9 +2494,9 @@ b-normal:
b-normal (webpack x.x.x) compiled successfully in X ms
a-source-map:
assets by path *.js 3.41 KiB
asset 4f97fa2636befa21dcc1-4f97fa.js 2.73 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap 4f97fa2636befa21dcc1-4f97fa.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime)
assets by path *.js 3.43 KiB
asset 117cbb23cc5507b6c50f-117cbb.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap 117cbb23cc5507b6c50f-117cbb.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime)
asset 05c637d15dff2d0dc5fd-05c637.js 344 bytes [emitted] [immutable] [minimized] (name: lazy)
sourceMap 05c637d15dff2d0dc5fd-05c637.js.map 399 bytes [emitted] [dev] (auxiliary name: lazy)
asset c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes [emitted] [immutable] [minimized] (name: index)
Expand All @@ -2507,10 +2507,10 @@ a-source-map:
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 3 KiB (20.4 KiB) = 4f97fa2636befa21dcc1-4f97fa.js 2.73 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets
Entrypoint index 3.02 KiB (20.5 KiB) = 117cbb23cc5507b6c50f-117cbb.js 2.75 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets
Entrypoint a 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
Entrypoint b 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
runtime modules 7.19 KiB 9 modules
runtime modules 7.18 KiB 9 modules
orphan modules 23 bytes [orphan] 1 module
cacheable modules 514 bytes (javascript) 26.3 KiB (asset)
javascript modules 388 bytes
Expand All @@ -2525,9 +2525,9 @@ a-source-map:
a-source-map (webpack x.x.x) compiled successfully in X ms
b-source-map:
assets by path *.js 3.41 KiB
asset 6be55260880baef884c5-6be552.js 2.73 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap 6be55260880baef884c5-6be552.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime)
assets by path *.js 3.43 KiB
asset c50369ca0ab118d40137-c50369.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap c50369ca0ab118d40137-c50369.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime)
asset 05c637d15dff2d0dc5fd-05c637.js 344 bytes [emitted] [immutable] [minimized] (name: lazy)
sourceMap 05c637d15dff2d0dc5fd-05c637.js.map 395 bytes [emitted] [dev] (auxiliary name: lazy)
asset c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes [emitted] [immutable] [minimized] (name: index)
Expand All @@ -2538,10 +2538,10 @@ b-source-map:
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 3 KiB (20.4 KiB) = 6be55260880baef884c5-6be552.js 2.73 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets
Entrypoint index 3.02 KiB (20.4 KiB) = c50369ca0ab118d40137-c50369.js 2.75 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets
Entrypoint a 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
Entrypoint b 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
runtime modules 7.19 KiB 9 modules
runtime modules 7.18 KiB 9 modules
orphan modules 19 bytes [orphan] 1 module
cacheable modules 469 bytes (javascript) 26.3 KiB (asset)
javascript modules 343 bytes
Expand Down Expand Up @@ -2717,7 +2717,7 @@ exports[`StatsTestCases should print correct stats for runtime-chunk-integration
asset without-505.js 1.22 KiB [emitted]
asset without-main1.js 848 bytes [emitted] (name: main1)
Entrypoint main1 12.8 KiB = without-runtime.js 12 KiB without-main1.js 848 bytes
runtime modules 7.42 KiB 10 modules
runtime modules 7.41 KiB 10 modules
cacheable modules 126 bytes
./main1.js 66 bytes [built] [code generated]
./b.js 20 bytes [built] [code generated]
Expand All @@ -2734,7 +2734,7 @@ static custom name:
Entrypoint main1 12.8 KiB = with-manifest.js 12 KiB with-main1.js 848 bytes
Entrypoint main2 12.4 KiB = with-manifest.js 12 KiB with-main2.js 467 bytes
Entrypoint main3 12.4 KiB = with-manifest.js 12 KiB with-main3.js 467 bytes
runtime modules 7.41 KiB 10 modules
runtime modules 7.4 KiB 10 modules
cacheable modules 166 bytes
./main1.js 66 bytes [built] [code generated]
./main2.js 20 bytes [built] [code generated]
Expand All @@ -2754,7 +2754,7 @@ dynamic custom name:
Entrypoint main1 12.8 KiB = func-b.js 12 KiB func-main1.js 848 bytes
Entrypoint main2 12.4 KiB = func-b.js 12 KiB func-main2.js 467 bytes
Entrypoint main3 5.25 KiB = func-a.js 4.79 KiB func-main3.js 467 bytes
runtime modules 9.75 KiB 13 modules
runtime modules 9.73 KiB 13 modules
cacheable modules 166 bytes
./main1.js 66 bytes [built] [code generated]
./main2.js 20 bytes [built] [code generated]
Expand Down
@@ -0,0 +1,6 @@
import p from "package";
import o from "other-package";

it("should run", () => {
console.log.bind(console, p, o);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,9 @@
module.exports = {
findBundle: function (i, options) {
return [
"runtime.js",
"main.js",
"vendors-node_modules_other-package_index_js-node_modules_package_index_js.js"
];
}
};
@@ -0,0 +1,29 @@
const { ProvideSharedPlugin } = require("../../../../").sharing;

/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
filename: "[name].js"
},
target: "web",
optimization: {
chunkIds: "named",
runtimeChunk: "single",
splitChunks: {
chunks: "all",
minSize: 1,
cacheGroups: {
share: {
type: "provide-module",
name: "provide-module",
enforce: true
}
}
}
},
plugins: [
new ProvideSharedPlugin({
provides: ["package"]
})
]
};

0 comments on commit 3f2a269

Please sign in to comment.