2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const { SyncWaterfallHook } = require("tapable");
9 const util = require("util");
10 const RuntimeGlobals = require("./RuntimeGlobals");
11 const memoize = require("./util/memoize");
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("./javascript/JavascriptModulesPlugin").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} */
30 const getJavascriptModulesPlugin = memoize(() =>
31 require("./javascript/JavascriptModulesPlugin")
33 const getJsonpTemplatePlugin = memoize(() =>
34 require("./web/JsonpTemplatePlugin")
36 const getLoadScriptRuntimeModule = memoize(() =>
37 require("./runtime/LoadScriptRuntimeModule")
40 // TODO webpack 6 remove this class
44 * @param {OutputOptions} outputOptions output options for the MainTemplate
45 * @param {Compilation} compilation the compilation
47 constructor(outputOptions, compilation) {
48 /** @type {OutputOptions} */
49 this._outputOptions = outputOptions || {};
50 this.hooks = Object.freeze({
54 compilation.hooks.renderManifest.tap(
56 (entries, options) => {
57 if (!options.chunk.hasRuntime()) return entries;
58 return fn(entries, options);
62 "MainTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
63 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_MANIFEST"
69 "MainTemplate.hooks.modules has been removed (there is no replacement, please create an issue to request that)"
76 "MainTemplate.hooks.moduleObj has been removed (there is no replacement, please create an issue to request that)"
83 getJavascriptModulesPlugin()
84 .getCompilationHooks(compilation)
85 .renderRequire.tap(options, fn);
87 "MainTemplate.hooks.require is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderRequire instead)",
88 "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE"
94 "MainTemplate.hooks.beforeStartup has been removed (use RuntimeGlobals.startupOnlyBefore instead)"
101 "MainTemplate.hooks.startup has been removed (use RuntimeGlobals.startup instead)"
108 "MainTemplate.hooks.afterStartup has been removed (use RuntimeGlobals.startupOnlyAfter instead)"
115 getJavascriptModulesPlugin()
116 .getCompilationHooks(compilation)
117 .render.tap(options, (source, renderContext) => {
119 renderContext.chunkGraph.getNumberOfEntryModules(
122 !renderContext.chunk.hasRuntime()
130 compilation.moduleTemplates.javascript,
131 compilation.dependencyTemplates
135 "MainTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
136 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER"
142 getJavascriptModulesPlugin()
143 .getCompilationHooks(compilation)
144 .render.tap(options, (source, renderContext) => {
146 renderContext.chunkGraph.getNumberOfEntryModules(
149 !renderContext.chunk.hasRuntime()
153 return fn(source, renderContext.chunk, compilation.hash);
156 "MainTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
157 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_WITH_ENTRY"
163 compilation.hooks.assetPath.tap(options, fn);
165 "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
166 "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
168 call: util.deprecate(
169 (filename, options) => {
170 return compilation.getAssetPath(filename, options);
172 "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
173 "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
179 compilation.hooks.fullHash.tap(options, fn);
181 "MainTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
182 "DEP_WEBPACK_MAIN_TEMPLATE_HASH"
188 getJavascriptModulesPlugin()
189 .getCompilationHooks(compilation)
190 .chunkHash.tap(options, (chunk, hash) => {
191 if (!chunk.hasRuntime()) return;
192 return fn(hash, chunk);
195 "MainTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
196 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
202 "MainTemplate.hooks.globalHashPaths has been removed (it's no longer needed)",
203 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
209 "MainTemplate.hooks.globalHash has been removed (it's no longer needed)",
210 "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
216 "MainTemplate.hooks.hotBootstrap has been removed (use your own RuntimeModule instead)"
221 // for compatibility:
222 /** @type {SyncWaterfallHook<[string, Chunk, string, ModuleTemplate, DependencyTemplates]>} */
223 bootstrap: new SyncWaterfallHook([
228 "dependencyTemplates"
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([
243 getLoadScriptRuntimeModule().getCompilationHooks(compilation);
244 return hooks.createScript;
247 const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
248 return hooks.linkPrefetch;
251 const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
252 return hooks.linkPreload;
256 this.renderCurrentHashCode = util.deprecate(
259 * @param {string} hash the hash
260 * @param {number=} length length of the hash
261 * @returns {string} generated code
262 */ (hash, length) => {
264 return `${RuntimeGlobals.getFullHash} ? ${
265 RuntimeGlobals.getFullHash
266 }().slice(0, ${length}) : ${hash.slice(0, length)}`;
268 return `${RuntimeGlobals.getFullHash} ? ${RuntimeGlobals.getFullHash}() : ${hash}`;
270 "MainTemplate.renderCurrentHashCode is deprecated (use RuntimeGlobals.getFullHash runtime function instead)",
271 "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_CURRENT_HASH_CODE"
274 this.getPublicPath = util.deprecate(
277 * @param {object} options get public path options
278 * @returns {string} hook call
280 return compilation.getAssetPath(
281 compilation.outputOptions.publicPath,
285 "MainTemplate.getPublicPath is deprecated (use Compilation.getAssetPath(compilation.outputOptions.publicPath, options) instead)",
286 "DEP_WEBPACK_MAIN_TEMPLATE_GET_PUBLIC_PATH"
289 this.getAssetPath = util.deprecate(
291 return compilation.getAssetPath(path, options);
293 "MainTemplate.getAssetPath is deprecated (use Compilation.getAssetPath instead)",
294 "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH"
297 this.getAssetPathWithInfo = util.deprecate(
299 return compilation.getAssetPathWithInfo(path, options);
301 "MainTemplate.getAssetPathWithInfo is deprecated (use Compilation.getAssetPath instead)",
302 "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH_WITH_INFO"
307 Object.defineProperty(MainTemplate.prototype, "requireFn", {
309 () => "__webpack_require__",
310 'MainTemplate.requireFn is deprecated (use "__webpack_require__")',
311 "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE_FN"
315 Object.defineProperty(MainTemplate.prototype, "outputOptions", {
318 * @this {MainTemplate}
319 * @returns {OutputOptions} output options
322 return this._outputOptions;
324 "MainTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
325 "DEP_WEBPACK_MAIN_TEMPLATE_OUTPUT_OPTIONS"
329 module.exports = MainTemplate;