source: frontend/node_modules/webpack/lib/MainTemplate.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 12.9 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 util = require("util");
9const { SyncWaterfallHook } = require("tapable");
10const RuntimeGlobals = require("./RuntimeGlobals");
11const memoize = require("./util/memoize");
12
13/** @typedef {import("tapable").Tap} Tap */
14/** @typedef {import("webpack-sources").Source} Source */
15/** @typedef {import("../declarations/WebpackOptions").Output} OutputOptions */
16/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
17/** @typedef {import("./Chunk")} Chunk */
18/** @typedef {import("./Compilation")} Compilation */
19/** @typedef {import("./Compilation").AssetInfo} AssetInfo */
20/** @typedef {import("./Compilation").InterpolatedPathAndAssetInfo} InterpolatedPathAndAssetInfo */
21/** @typedef {import("./util/Hash")} Hash */
22/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
23/** @typedef {import("./javascript/JavascriptModulesPlugin").RenderBootstrapContext} RenderBootstrapContext */
24/** @typedef {import("./Template").RenderManifestOptions} RenderManifestOptions */
25/** @typedef {import("./Template").RenderManifestEntry} RenderManifestEntry */
26/** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
27/** @typedef {import("./TemplatedPathPlugin").PathData} PathData */
28/**
29 * Defines the if set type used by this module.
30 * @template T
31 * @typedef {import("tapable").IfSet<T>} IfSet
32 */
33
34const getJavascriptModulesPlugin = memoize(() =>
35 require("./javascript/JavascriptModulesPlugin")
36);
37const getJsonpTemplatePlugin = memoize(() =>
38 require("./web/JsonpTemplatePlugin")
39);
40const getLoadScriptRuntimeModule = memoize(() =>
41 require("./runtime/LoadScriptRuntimeModule")
42);
43
44// TODO webpack 6 remove this class
45class MainTemplate {
46 /**
47 * Creates an instance of MainTemplate.
48 * @param {OutputOptions} outputOptions output options for the MainTemplate
49 * @param {Compilation} compilation the compilation
50 */
51 constructor(outputOptions, compilation) {
52 /** @type {OutputOptions} */
53 this._outputOptions = outputOptions || {};
54 this.hooks = Object.freeze({
55 renderManifest: {
56 tap: util.deprecate(
57 /**
58 * Handles the callback logic for this hook.
59 * @template AdditionalOptions
60 * @param {string | Tap & IfSet<AdditionalOptions>} options options
61 * @param {(renderManifestEntries: RenderManifestEntry[], renderManifestOptions: RenderManifestOptions) => RenderManifestEntry[]} fn fn
62 */
63 (options, fn) => {
64 compilation.hooks.renderManifest.tap(
65 options,
66 (entries, options) => {
67 if (!options.chunk.hasRuntime()) return entries;
68 return fn(entries, options);
69 }
70 );
71 },
72 "MainTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
73 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_MANIFEST"
74 )
75 },
76 modules: {
77 tap: () => {
78 throw new Error(
79 "MainTemplate.hooks.modules has been removed (there is no replacement, please create an issue to request that)"
80 );
81 }
82 },
83 moduleObj: {
84 tap: () => {
85 throw new Error(
86 "MainTemplate.hooks.moduleObj has been removed (there is no replacement, please create an issue to request that)"
87 );
88 }
89 },
90 require: {
91 tap: util.deprecate(
92 /**
93 * Handles the callback logic for this hook.
94 * @template AdditionalOptions
95 * @param {string | Tap & IfSet<AdditionalOptions>} options options
96 * @param {(value: string, renderBootstrapContext: RenderBootstrapContext) => string} fn fn
97 */
98 (options, fn) => {
99 getJavascriptModulesPlugin()
100 .getCompilationHooks(compilation)
101 .renderRequire.tap(options, fn);
102 },
103 "MainTemplate.hooks.require is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderRequire instead)",
104 "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE"
105 )
106 },
107 beforeStartup: {
108 tap: () => {
109 throw new Error(
110 "MainTemplate.hooks.beforeStartup has been removed (use RuntimeGlobals.startupOnlyBefore instead)"
111 );
112 }
113 },
114 startup: {
115 tap: () => {
116 throw new Error(
117 "MainTemplate.hooks.startup has been removed (use RuntimeGlobals.startup instead)"
118 );
119 }
120 },
121 afterStartup: {
122 tap: () => {
123 throw new Error(
124 "MainTemplate.hooks.afterStartup has been removed (use RuntimeGlobals.startupOnlyAfter instead)"
125 );
126 }
127 },
128 render: {
129 tap: util.deprecate(
130 /**
131 * Handles the callback logic for this hook.
132 * @template AdditionalOptions
133 * @param {string | Tap & IfSet<AdditionalOptions>} options options
134 * @param {(source: Source, chunk: Chunk, hash: string | undefined, moduleTemplate: ModuleTemplate, dependencyTemplates: DependencyTemplates) => Source} fn fn
135 */
136 (options, fn) => {
137 getJavascriptModulesPlugin()
138 .getCompilationHooks(compilation)
139 .render.tap(options, (source, renderContext) => {
140 if (
141 renderContext.chunkGraph.getNumberOfEntryModules(
142 renderContext.chunk
143 ) === 0 ||
144 !renderContext.chunk.hasRuntime()
145 ) {
146 return source;
147 }
148 return fn(
149 source,
150 renderContext.chunk,
151 compilation.hash,
152 compilation.moduleTemplates.javascript,
153 compilation.dependencyTemplates
154 );
155 });
156 },
157 "MainTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
158 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER"
159 )
160 },
161 renderWithEntry: {
162 tap: util.deprecate(
163 /**
164 * Handles the callback logic for this hook.
165 * @template AdditionalOptions
166 * @param {string | Tap & IfSet<AdditionalOptions>} options options
167 * @param {(source: Source, chunk: Chunk, hash: string | undefined) => Source} fn fn
168 */
169 (options, fn) => {
170 getJavascriptModulesPlugin()
171 .getCompilationHooks(compilation)
172 .render.tap(options, (source, renderContext) => {
173 if (
174 renderContext.chunkGraph.getNumberOfEntryModules(
175 renderContext.chunk
176 ) === 0 ||
177 !renderContext.chunk.hasRuntime()
178 ) {
179 return source;
180 }
181 return fn(source, renderContext.chunk, compilation.hash);
182 });
183 },
184 "MainTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
185 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_WITH_ENTRY"
186 )
187 },
188 assetPath: {
189 tap: util.deprecate(
190 /**
191 * Handles the callback logic for this hook.
192 * @template AdditionalOptions
193 * @param {string | Tap & IfSet<AdditionalOptions>} options options
194 * @param {(value: string, path: PathData, assetInfo: AssetInfo | undefined) => string} fn fn
195 */
196 (options, fn) => {
197 compilation.hooks.assetPath.tap(options, fn);
198 },
199 "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
200 "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
201 ),
202 call: util.deprecate(
203 /**
204 * Handles the call callback for this hook.
205 * @param {TemplatePath} filename used to get asset path with hash
206 * @param {PathData} options context data
207 * @returns {string} interpolated path
208 */
209 (filename, options) => compilation.getAssetPath(filename, options),
210 "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
211 "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
212 )
213 },
214 hash: {
215 tap: util.deprecate(
216 /**
217 * Handles the callback logic for this hook.
218 * @template AdditionalOptions
219 * @param {string | Tap & IfSet<AdditionalOptions>} options options
220 * @param {(hash: Hash) => void} fn fn
221 */
222 (options, fn) => {
223 compilation.hooks.fullHash.tap(options, fn);
224 },
225 "MainTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
226 "DEP_WEBPACK_MAIN_TEMPLATE_HASH"
227 )
228 },
229 hashForChunk: {
230 tap: util.deprecate(
231 /**
232 * Handles the callback logic for this hook.
233 * @template AdditionalOptions
234 * @param {string | Tap & IfSet<AdditionalOptions>} options options
235 * @param {(hash: Hash, chunk: Chunk) => void} fn fn
236 */
237 (options, fn) => {
238 getJavascriptModulesPlugin()
239 .getCompilationHooks(compilation)
240 .chunkHash.tap(options, (chunk, hash) => {
241 if (!chunk.hasRuntime()) return;
242 return fn(hash, chunk);
243 });
244 },
245 "MainTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
246 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
247 )
248 },
249 globalHashPaths: {
250 tap: util.deprecate(
251 () => {},
252 "MainTemplate.hooks.globalHashPaths has been removed (it's no longer needed)",
253 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
254 )
255 },
256 globalHash: {
257 tap: util.deprecate(
258 () => {},
259 "MainTemplate.hooks.globalHash has been removed (it's no longer needed)",
260 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
261 )
262 },
263 hotBootstrap: {
264 tap: () => {
265 throw new Error(
266 "MainTemplate.hooks.hotBootstrap has been removed (use your own RuntimeModule instead)"
267 );
268 }
269 },
270
271 // for compatibility:
272 /** @type {SyncWaterfallHook<[string, Chunk, string, ModuleTemplate, DependencyTemplates]>} */
273 bootstrap: new SyncWaterfallHook([
274 "source",
275 "chunk",
276 "hash",
277 "moduleTemplate",
278 "dependencyTemplates"
279 ]),
280 /** @type {SyncWaterfallHook<[string, Chunk, string]>} */
281 localVars: new SyncWaterfallHook(["source", "chunk", "hash"]),
282 /** @type {SyncWaterfallHook<[string, Chunk, string]>} */
283 requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
284 /** @type {SyncWaterfallHook<[string, Chunk, string, string]>} */
285 requireEnsure: new SyncWaterfallHook([
286 "source",
287 "chunk",
288 "hash",
289 "chunkIdExpression"
290 ]),
291 get jsonpScript() {
292 const hooks =
293 getLoadScriptRuntimeModule().getCompilationHooks(compilation);
294 return hooks.createScript;
295 },
296 get linkPrefetch() {
297 const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
298 return hooks.linkPrefetch;
299 },
300 get linkPreload() {
301 const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
302 return hooks.linkPreload;
303 }
304 });
305
306 this.renderCurrentHashCode = util.deprecate(
307 /**
308 * Handles the require ensure callback for this hook.
309 * @deprecated
310 * @param {string} hash the hash
311 * @param {number=} length length of the hash
312 * @returns {string} generated code
313 */
314 (hash, length) => {
315 if (length) {
316 return `${RuntimeGlobals.getFullHash} ? ${
317 RuntimeGlobals.getFullHash
318 }().slice(0, ${length}) : ${hash.slice(0, length)}`;
319 }
320 return `${RuntimeGlobals.getFullHash} ? ${RuntimeGlobals.getFullHash}() : ${hash}`;
321 },
322 "MainTemplate.renderCurrentHashCode is deprecated (use RuntimeGlobals.getFullHash runtime function instead)",
323 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_CURRENT_HASH_CODE"
324 );
325
326 this.getPublicPath = util.deprecate(
327 /**
328 * Handles the callback logic for this hook.
329 * @param {PathData} options context data
330 * @returns {string} interpolated path
331 */ (options) =>
332 compilation.getAssetPath(compilation.outputOptions.publicPath, options),
333 "MainTemplate.getPublicPath is deprecated (use Compilation.getAssetPath(compilation.outputOptions.publicPath, options) instead)",
334 "DEP_WEBPACK_MAIN_TEMPLATE_GET_PUBLIC_PATH"
335 );
336
337 this.getAssetPath = util.deprecate(
338 /**
339 * Handles the callback logic for this hook.
340 * @param {TemplatePath} path used to get asset path with hash
341 * @param {PathData} options context data
342 * @returns {string} interpolated path
343 */
344 (path, options) => compilation.getAssetPath(path, options),
345 "MainTemplate.getAssetPath is deprecated (use Compilation.getAssetPath instead)",
346 "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH"
347 );
348
349 this.getAssetPathWithInfo = util.deprecate(
350 /**
351 * Handles the callback logic for this hook.
352 * @param {TemplatePath} path used to get asset path with hash
353 * @param {PathData} options context data
354 * @returns {InterpolatedPathAndAssetInfo} interpolated path and asset info
355 */
356 (path, options) => compilation.getAssetPathWithInfo(path, options),
357 "MainTemplate.getAssetPathWithInfo is deprecated (use Compilation.getAssetPath instead)",
358 "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH_WITH_INFO"
359 );
360 }
361}
362
363Object.defineProperty(MainTemplate.prototype, "requireFn", {
364 get: util.deprecate(
365 () => RuntimeGlobals.require,
366 `MainTemplate.requireFn is deprecated (use "${RuntimeGlobals.require}")`,
367 "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE_FN"
368 )
369});
370
371Object.defineProperty(MainTemplate.prototype, "outputOptions", {
372 get: util.deprecate(
373 /**
374 * Returns output options.
375 * @this {MainTemplate}
376 * @returns {OutputOptions} output options
377 */
378 function outputOptions() {
379 return this._outputOptions;
380 },
381 "MainTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
382 "DEP_WEBPACK_MAIN_TEMPLATE_OUTPUT_OPTIONS"
383 )
384});
385
386module.exports = MainTemplate;
Note: See TracBrowser for help on using the repository browser.