UNPKG

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