source: imaps-frontend/node_modules/webpack/lib/esm/ModuleChunkFormatPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource } = require("webpack-sources");
9const { RuntimeGlobals } = require("..");
10const HotUpdateChunk = require("../HotUpdateChunk");
11const Template = require("../Template");
12const { getAllChunks } = require("../javascript/ChunkHelpers");
13const {
14 chunkHasJs,
15 getCompilationHooks,
16 getChunkFilenameTemplate
17} = require("../javascript/JavascriptModulesPlugin");
18const { updateHashForEntryStartup } = require("../javascript/StartupHelpers");
19const { getUndoPath } = require("../util/identifier");
20
21/** @typedef {import("../Chunk")} Chunk */
22/** @typedef {import("../Compiler")} Compiler */
23/** @typedef {import("../Entrypoint")} Entrypoint */
24
25class ModuleChunkFormatPlugin {
26 /**
27 * Apply the plugin
28 * @param {Compiler} compiler the compiler instance
29 * @returns {void}
30 */
31 apply(compiler) {
32 compiler.hooks.thisCompilation.tap(
33 "ModuleChunkFormatPlugin",
34 compilation => {
35 compilation.hooks.additionalChunkRuntimeRequirements.tap(
36 "ModuleChunkFormatPlugin",
37 (chunk, set) => {
38 if (chunk.hasRuntime()) return;
39 if (compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0) {
40 set.add(RuntimeGlobals.require);
41 set.add(RuntimeGlobals.startupEntrypoint);
42 set.add(RuntimeGlobals.externalInstallChunk);
43 }
44 }
45 );
46 const hooks = getCompilationHooks(compilation);
47 hooks.renderChunk.tap(
48 "ModuleChunkFormatPlugin",
49 (modules, renderContext) => {
50 const { chunk, chunkGraph, runtimeTemplate } = renderContext;
51 const hotUpdateChunk =
52 chunk instanceof HotUpdateChunk ? chunk : null;
53 const source = new ConcatSource();
54 if (hotUpdateChunk) {
55 throw new Error(
56 "HMR is not implemented for module chunk format yet"
57 );
58 } else {
59 source.add(
60 `export const __webpack_id__ = ${JSON.stringify(chunk.id)};\n`
61 );
62 source.add(
63 `export const __webpack_ids__ = ${JSON.stringify(chunk.ids)};\n`
64 );
65 source.add("export const __webpack_modules__ = ");
66 source.add(modules);
67 source.add(";\n");
68 const runtimeModules =
69 chunkGraph.getChunkRuntimeModulesInOrder(chunk);
70 if (runtimeModules.length > 0) {
71 source.add("export const __webpack_runtime__ =\n");
72 source.add(
73 Template.renderChunkRuntimeModules(
74 runtimeModules,
75 renderContext
76 )
77 );
78 }
79 const entries = Array.from(
80 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
81 );
82 if (entries.length > 0) {
83 const runtimeChunk =
84 /** @type {Entrypoint[][]} */
85 (entries)[0][1].getRuntimeChunk();
86 const currentOutputName = compilation
87 .getPath(
88 getChunkFilenameTemplate(chunk, compilation.outputOptions),
89 {
90 chunk,
91 contentHashType: "javascript"
92 }
93 )
94 .replace(/^\/+/g, "")
95 .split("/");
96
97 /**
98 * @param {Chunk} chunk the chunk
99 * @returns {string} the relative path
100 */
101 const getRelativePath = chunk => {
102 const baseOutputName = currentOutputName.slice();
103 const chunkOutputName = compilation
104 .getPath(
105 getChunkFilenameTemplate(
106 chunk,
107 compilation.outputOptions
108 ),
109 {
110 chunk,
111 contentHashType: "javascript"
112 }
113 )
114 .replace(/^\/+/g, "")
115 .split("/");
116
117 // remove common parts except filename
118 while (
119 baseOutputName.length > 1 &&
120 chunkOutputName.length > 1 &&
121 baseOutputName[0] === chunkOutputName[0]
122 ) {
123 baseOutputName.shift();
124 chunkOutputName.shift();
125 }
126 const last = chunkOutputName.join("/");
127 // create final path
128 return (
129 getUndoPath(baseOutputName.join("/"), last, true) + last
130 );
131 };
132
133 const entrySource = new ConcatSource();
134 entrySource.add(source);
135 entrySource.add(";\n\n// load runtime\n");
136 entrySource.add(
137 `import ${RuntimeGlobals.require} from ${JSON.stringify(
138 getRelativePath(/** @type {Chunk} */ (runtimeChunk))
139 )};\n`
140 );
141
142 const startupSource = new ConcatSource();
143 startupSource.add(
144 `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
145 `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`,
146 "moduleId"
147 )}\n`
148 );
149
150 const loadedChunks = new Set();
151 let index = 0;
152 for (let i = 0; i < entries.length; i++) {
153 const [module, entrypoint] = entries[i];
154 const final = i + 1 === entries.length;
155 const moduleId = chunkGraph.getModuleId(module);
156 const chunks = getAllChunks(
157 /** @type {Entrypoint} */ (entrypoint),
158 /** @type {Chunk} */ (runtimeChunk),
159 undefined
160 );
161 for (const chunk of chunks) {
162 if (
163 loadedChunks.has(chunk) ||
164 !chunkHasJs(chunk, chunkGraph)
165 )
166 continue;
167 loadedChunks.add(chunk);
168 startupSource.add(
169 `import * as __webpack_chunk_${index}__ from ${JSON.stringify(
170 getRelativePath(chunk)
171 )};\n`
172 );
173 startupSource.add(
174 `${RuntimeGlobals.externalInstallChunk}(__webpack_chunk_${index}__);\n`
175 );
176 index++;
177 }
178 startupSource.add(
179 `${
180 final ? `var ${RuntimeGlobals.exports} = ` : ""
181 }__webpack_exec__(${JSON.stringify(moduleId)});\n`
182 );
183 }
184
185 entrySource.add(
186 hooks.renderStartup.call(
187 startupSource,
188 entries[entries.length - 1][0],
189 {
190 ...renderContext,
191 inlined: false
192 }
193 )
194 );
195 return entrySource;
196 }
197 }
198 return source;
199 }
200 );
201 hooks.chunkHash.tap(
202 "ModuleChunkFormatPlugin",
203 (chunk, hash, { chunkGraph, runtimeTemplate }) => {
204 if (chunk.hasRuntime()) return;
205 hash.update("ModuleChunkFormatPlugin");
206 hash.update("1");
207 const entries = Array.from(
208 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
209 );
210 updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
211 }
212 );
213 }
214 );
215 }
216}
217
218module.exports = ModuleChunkFormatPlugin;
Note: See TracBrowser for help on using the repository browser.