2018-12-29 19:48:59 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RuntimeModule = require("../RuntimeModule");
|
|
|
|
const Template = require("../Template");
|
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
|
|
|
class StartupChunkDependenciesPlugin {
|
2019-05-21 04:53:58 +08:00
|
|
|
constructor(options) {
|
|
|
|
this.asyncChunkLoading =
|
|
|
|
options && typeof options.asyncChunkLoading === "boolean"
|
|
|
|
? options.asyncChunkLoading
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:48:59 +08:00
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
2018-12-29 19:48:59 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.thisCompilation.tap(
|
|
|
|
"StartupChunkDependenciesPlugin",
|
|
|
|
compilation => {
|
|
|
|
compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
|
|
|
"StartupChunkDependenciesPlugin",
|
|
|
|
(chunk, set) => {
|
|
|
|
if (compilation.chunkGraph.hasChunkEntryDependentChunks(chunk)) {
|
|
|
|
set.add(RuntimeGlobals.startup);
|
|
|
|
set.add(RuntimeGlobals.ensureChunk);
|
|
|
|
set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
|
|
|
new StartupChunkDependenciesRuntimeModule(
|
2019-05-21 04:53:58 +08:00
|
|
|
this.asyncChunkLoading
|
2018-12-29 19:48:59 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
|
2019-08-27 02:21:07 +08:00
|
|
|
constructor(asyncChunkLoading) {
|
2018-12-29 19:48:59 +08:00
|
|
|
super("startup chunk dependencies");
|
2019-05-21 04:53:58 +08:00
|
|
|
this.asyncChunkLoading = asyncChunkLoading;
|
2018-12-29 19:48:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
generate() {
|
2019-08-27 02:21:07 +08:00
|
|
|
const { chunk, compilation } = this;
|
|
|
|
const { chunkGraph, runtimeTemplate } = compilation;
|
2018-12-29 19:48:59 +08:00
|
|
|
const chunkIds = Array.from(
|
|
|
|
chunkGraph.getChunkEntryDependentChunksIterable(chunk)
|
|
|
|
).map(chunk => {
|
|
|
|
return chunk.id;
|
|
|
|
});
|
|
|
|
return Template.asString([
|
|
|
|
`var next = ${RuntimeGlobals.startup};`,
|
2019-08-27 02:21:07 +08:00
|
|
|
`${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction(
|
|
|
|
"",
|
2019-05-21 04:53:58 +08:00
|
|
|
!this.asyncChunkLoading
|
|
|
|
? chunkIds
|
|
|
|
.map(
|
|
|
|
id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`
|
|
|
|
)
|
|
|
|
.concat("return next();")
|
|
|
|
: chunkIds.length === 1
|
2018-12-29 19:48:59 +08:00
|
|
|
? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify(
|
|
|
|
chunkIds[0]
|
|
|
|
)}).then(next);`
|
|
|
|
: chunkIds.length > 2
|
2019-05-21 04:53:58 +08:00
|
|
|
? [
|
2019-02-06 22:37:11 +08:00
|
|
|
// using map is shorter for 3 or more chunks
|
|
|
|
`return Promise.all(${JSON.stringify(chunkIds)}.map(${
|
|
|
|
RuntimeGlobals.ensureChunk
|
|
|
|
}, __webpack_require__)).then(next);`
|
2019-05-21 04:53:58 +08:00
|
|
|
]
|
|
|
|
: [
|
2019-02-06 22:37:11 +08:00
|
|
|
// calling ensureChunk directly is shorter for 0 - 2 chunks
|
|
|
|
"return Promise.all([",
|
|
|
|
Template.indent(
|
|
|
|
chunkIds
|
|
|
|
.map(
|
|
|
|
id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})`
|
|
|
|
)
|
|
|
|
.join(",\n")
|
|
|
|
),
|
|
|
|
"]).then(next);"
|
2019-05-21 04:53:58 +08:00
|
|
|
]
|
2019-08-27 02:21:07 +08:00
|
|
|
)};`
|
2018-12-29 19:48:59 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StartupChunkDependenciesPlugin;
|