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