webpack
Version:
Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.
88 lines (80 loc) • 2.66 kB
JavaScript
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
;
const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");
/** @import Chunk from "../Chunk" */
/** @import ChunkGraph from "../ChunkGraph" */
/** @import Compilation from "../Compilation" */
class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
/**
* @param {boolean} asyncChunkLoading use async chunk loading
*/
constructor(asyncChunkLoading) {
super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER);
/** @type {boolean} */
this.asyncChunkLoading = asyncChunkLoading;
}
/**
* The `[handlerMap, key]` pairs this module installs onto a chunk handler map
* such as `__webpack_require__.f`, or `null` where it cannot name them.
* @returns {[string, string][] | null} installed chunk handlers (do not mutate)
*/
getInstalledChunkHandlers() {
return RuntimeModule.NO_CHUNK_HANDLERS;
}
/**
* Generates runtime code for this runtime module.
* @returns {string | null} runtime code
*/
generate() {
const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
const chunk = /** @type {Chunk} */ (this.chunk);
const chunkIds = [
...chunkGraph.getChunkEntryDependentChunksIterable(chunk)
].map((chunk) => chunk.id);
const compilation = /** @type {Compilation} */ (this.compilation);
const { runtimeTemplate } = compilation;
return Template.asString([
`${runtimeTemplate.renderConst()} next = ${RuntimeGlobals.startup};`,
`${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction(
"",
!this.asyncChunkLoading
? [
...chunkIds.map(
(id) => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`
),
"return next();"
]
: chunkIds.length === 1
? `return ${RuntimeGlobals.ensureChunk}(${JSON.stringify(
chunkIds[0]
)}).then(next);`
: chunkIds.length > 2
? [
// using map is shorter for 3 or more chunks
`return Promise.all(${JSON.stringify(chunkIds)}.map(${
RuntimeGlobals.ensureChunk
}, ${RuntimeGlobals.require})).then(next);`
]
: [
// 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);"
]
)};`
]);
}
}
module.exports = StartupChunkDependenciesRuntimeModule;