[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 9 | const Template = require("../Template");
|
---|
| 10 | const { isSubset } = require("../util/SetHelpers");
|
---|
| 11 | const { getAllChunks } = require("./ChunkHelpers");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 14 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 15 | /** @typedef {import("../Chunk").ChunkId} ChunkId */
|
---|
| 16 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 17 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 18 | /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
|
---|
| 19 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
---|
| 20 | /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
|
---|
| 21 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
| 22 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
| 23 | /** @typedef {(string|number)[]} EntryItem */
|
---|
| 24 |
|
---|
| 25 | const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `;
|
---|
| 26 |
|
---|
| 27 | /** @typedef {Set<Chunk>} Chunks */
|
---|
| 28 | /** @typedef {ModuleId[]} ModuleIds */
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * @param {ChunkGraph} chunkGraph chunkGraph
|
---|
| 32 | * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
|
---|
| 33 | * @param {EntryModuleWithChunkGroup[]} entries entries
|
---|
| 34 | * @param {Chunk} chunk chunk
|
---|
| 35 | * @param {boolean} passive true: passive startup with on chunks loaded
|
---|
| 36 | * @returns {string} runtime code
|
---|
| 37 | */
|
---|
| 38 | module.exports.generateEntryStartup = (
|
---|
| 39 | chunkGraph,
|
---|
| 40 | runtimeTemplate,
|
---|
| 41 | entries,
|
---|
| 42 | chunk,
|
---|
| 43 | passive
|
---|
| 44 | ) => {
|
---|
| 45 | /** @type {string[]} */
|
---|
| 46 | const runtime = [
|
---|
| 47 | `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
|
---|
| 48 | `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`,
|
---|
| 49 | "moduleId"
|
---|
| 50 | )}`
|
---|
| 51 | ];
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * @param {ModuleId} id id
|
---|
| 55 | * @returns {string} fn to execute
|
---|
| 56 | */
|
---|
| 57 | const runModule = id => `__webpack_exec__(${JSON.stringify(id)})`;
|
---|
| 58 | /**
|
---|
| 59 | * @param {Chunks} chunks chunks
|
---|
| 60 | * @param {ModuleIds} moduleIds module ids
|
---|
| 61 | * @param {boolean=} final true when final, otherwise false
|
---|
| 62 | */
|
---|
| 63 | const outputCombination = (chunks, moduleIds, final) => {
|
---|
| 64 | if (chunks.size === 0) {
|
---|
| 65 | runtime.push(
|
---|
| 66 | `${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});`
|
---|
| 67 | );
|
---|
| 68 | } else {
|
---|
| 69 | const fn = runtimeTemplate.returningFunction(
|
---|
| 70 | moduleIds.map(runModule).join(", ")
|
---|
| 71 | );
|
---|
| 72 | runtime.push(
|
---|
| 73 | `${final && !passive ? EXPORT_PREFIX : ""}${
|
---|
| 74 | passive
|
---|
| 75 | ? RuntimeGlobals.onChunksLoaded
|
---|
| 76 | : RuntimeGlobals.startupEntrypoint
|
---|
| 77 | }(0, ${JSON.stringify(Array.from(chunks, c => c.id))}, ${fn});`
|
---|
| 78 | );
|
---|
| 79 | if (final && passive) {
|
---|
| 80 | runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | /** @type {Chunks | undefined} */
|
---|
| 86 | let currentChunks;
|
---|
| 87 | /** @type {ModuleIds | undefined} */
|
---|
| 88 | let currentModuleIds;
|
---|
| 89 |
|
---|
| 90 | for (const [module, entrypoint] of entries) {
|
---|
| 91 | const runtimeChunk =
|
---|
| 92 | /** @type {Entrypoint} */
|
---|
| 93 | (entrypoint).getRuntimeChunk();
|
---|
| 94 | const moduleId = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
|
---|
| 95 | const chunks = getAllChunks(
|
---|
| 96 | /** @type {Entrypoint} */
|
---|
| 97 | (entrypoint),
|
---|
| 98 | chunk,
|
---|
| 99 | runtimeChunk
|
---|
| 100 | );
|
---|
| 101 | if (
|
---|
| 102 | currentChunks &&
|
---|
| 103 | currentChunks.size === chunks.size &&
|
---|
| 104 | isSubset(currentChunks, chunks)
|
---|
| 105 | ) {
|
---|
| 106 | /** @type {ModuleIds} */
|
---|
| 107 | (currentModuleIds).push(moduleId);
|
---|
| 108 | } else {
|
---|
| 109 | if (currentChunks) {
|
---|
| 110 | outputCombination(
|
---|
| 111 | currentChunks,
|
---|
| 112 | /** @type {ModuleIds} */ (currentModuleIds)
|
---|
| 113 | );
|
---|
| 114 | }
|
---|
| 115 | currentChunks = chunks;
|
---|
| 116 | currentModuleIds = [moduleId];
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | // output current modules with export prefix
|
---|
| 121 | if (currentChunks) {
|
---|
| 122 | outputCombination(
|
---|
| 123 | currentChunks,
|
---|
| 124 | /** @type {ModuleIds} */
|
---|
| 125 | (currentModuleIds),
|
---|
| 126 | true
|
---|
| 127 | );
|
---|
| 128 | }
|
---|
| 129 | runtime.push("");
|
---|
| 130 | return Template.asString(runtime);
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @param {Hash} hash the hash to update
|
---|
| 135 | * @param {ChunkGraph} chunkGraph chunkGraph
|
---|
| 136 | * @param {EntryModuleWithChunkGroup[]} entries entries
|
---|
| 137 | * @param {Chunk} chunk chunk
|
---|
| 138 | * @returns {void}
|
---|
| 139 | */
|
---|
| 140 | module.exports.updateHashForEntryStartup = (
|
---|
| 141 | hash,
|
---|
| 142 | chunkGraph,
|
---|
| 143 | entries,
|
---|
| 144 | chunk
|
---|
| 145 | ) => {
|
---|
| 146 | for (const [module, entrypoint] of entries) {
|
---|
| 147 | const runtimeChunk =
|
---|
| 148 | /** @type {Entrypoint} */
|
---|
| 149 | (entrypoint).getRuntimeChunk();
|
---|
| 150 | const moduleId = chunkGraph.getModuleId(module);
|
---|
| 151 | hash.update(`${moduleId}`);
|
---|
| 152 | for (const c of getAllChunks(
|
---|
| 153 | /** @type {Entrypoint} */ (entrypoint),
|
---|
| 154 | chunk,
|
---|
| 155 | /** @type {Chunk} */ (runtimeChunk)
|
---|
| 156 | )) {
|
---|
| 157 | hash.update(`${c.id}`);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | };
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * @param {Chunk} chunk the chunk
|
---|
| 164 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 165 | * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function
|
---|
| 166 | * @returns {Set<number | string>} initially fulfilled chunk ids
|
---|
| 167 | */
|
---|
| 168 | module.exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
|
---|
| 169 | const initialChunkIds = new Set(chunk.ids);
|
---|
| 170 | for (const c of chunk.getAllInitialChunks()) {
|
---|
| 171 | if (c === chunk || filterFn(c, chunkGraph)) continue;
|
---|
| 172 | for (const id of /** @type {ChunkId[]} */ (c.ids)) {
|
---|
| 173 | initialChunkIds.add(id);
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | return initialChunkIds;
|
---|
| 177 | };
|
---|