[6a3a178] | 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("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} */
|
---|
| 29 |
|
---|
| 30 | const getJavascriptModulesPlugin = memoize(() =>
|
---|
| 31 | require("./javascript/JavascriptModulesPlugin")
|
---|
| 32 | );
|
---|
| 33 | const getJsonpTemplatePlugin = memoize(() =>
|
---|
| 34 | require("./web/JsonpTemplatePlugin")
|
---|
| 35 | );
|
---|
| 36 | const getLoadScriptRuntimeModule = memoize(() =>
|
---|
| 37 | require("./runtime/LoadScriptRuntimeModule")
|
---|
| 38 | );
|
---|
| 39 |
|
---|
| 40 | // TODO webpack 6 remove this class
|
---|
| 41 | class 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 =
|
---|
| 243 | getLoadScriptRuntimeModule().getCompilationHooks(compilation);
|
---|
| 244 | return hooks.createScript;
|
---|
| 245 | },
|
---|
| 246 | get linkPrefetch() {
|
---|
| 247 | const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
|
---|
| 248 | return hooks.linkPrefetch;
|
---|
| 249 | },
|
---|
| 250 | get linkPreload() {
|
---|
| 251 | const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
|
---|
| 252 | return hooks.linkPreload;
|
---|
| 253 | }
|
---|
| 254 | });
|
---|
| 255 |
|
---|
| 256 | this.renderCurrentHashCode = util.deprecate(
|
---|
| 257 | /**
|
---|
| 258 | * @deprecated
|
---|
| 259 | * @param {string} hash the hash
|
---|
| 260 | * @param {number=} length length of the hash
|
---|
| 261 | * @returns {string} generated code
|
---|
| 262 | */ (hash, length) => {
|
---|
| 263 | if (length) {
|
---|
| 264 | return `${RuntimeGlobals.getFullHash} ? ${
|
---|
| 265 | RuntimeGlobals.getFullHash
|
---|
| 266 | }().slice(0, ${length}) : ${hash.slice(0, length)}`;
|
---|
| 267 | }
|
---|
| 268 | return `${RuntimeGlobals.getFullHash} ? ${RuntimeGlobals.getFullHash}() : ${hash}`;
|
---|
| 269 | },
|
---|
| 270 | "MainTemplate.renderCurrentHashCode is deprecated (use RuntimeGlobals.getFullHash runtime function instead)",
|
---|
| 271 | "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_CURRENT_HASH_CODE"
|
---|
| 272 | );
|
---|
| 273 |
|
---|
| 274 | this.getPublicPath = util.deprecate(
|
---|
| 275 | /**
|
---|
| 276 | *
|
---|
| 277 | * @param {object} options get public path options
|
---|
| 278 | * @returns {string} hook call
|
---|
| 279 | */ options => {
|
---|
| 280 | return compilation.getAssetPath(
|
---|
| 281 | compilation.outputOptions.publicPath,
|
---|
| 282 | options
|
---|
| 283 | );
|
---|
| 284 | },
|
---|
| 285 | "MainTemplate.getPublicPath is deprecated (use Compilation.getAssetPath(compilation.outputOptions.publicPath, options) instead)",
|
---|
| 286 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_PUBLIC_PATH"
|
---|
| 287 | );
|
---|
| 288 |
|
---|
| 289 | this.getAssetPath = util.deprecate(
|
---|
| 290 | (path, options) => {
|
---|
| 291 | return compilation.getAssetPath(path, options);
|
---|
| 292 | },
|
---|
| 293 | "MainTemplate.getAssetPath is deprecated (use Compilation.getAssetPath instead)",
|
---|
| 294 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH"
|
---|
| 295 | );
|
---|
| 296 |
|
---|
| 297 | this.getAssetPathWithInfo = util.deprecate(
|
---|
| 298 | (path, options) => {
|
---|
| 299 | return compilation.getAssetPathWithInfo(path, options);
|
---|
| 300 | },
|
---|
| 301 | "MainTemplate.getAssetPathWithInfo is deprecated (use Compilation.getAssetPath instead)",
|
---|
| 302 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH_WITH_INFO"
|
---|
| 303 | );
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | Object.defineProperty(MainTemplate.prototype, "requireFn", {
|
---|
| 308 | get: util.deprecate(
|
---|
| 309 | () => "__webpack_require__",
|
---|
| 310 | 'MainTemplate.requireFn is deprecated (use "__webpack_require__")',
|
---|
| 311 | "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE_FN"
|
---|
| 312 | )
|
---|
| 313 | });
|
---|
| 314 |
|
---|
| 315 | Object.defineProperty(MainTemplate.prototype, "outputOptions", {
|
---|
| 316 | get: util.deprecate(
|
---|
| 317 | /**
|
---|
| 318 | * @this {MainTemplate}
|
---|
| 319 | * @returns {OutputOptions} output options
|
---|
| 320 | */
|
---|
| 321 | function () {
|
---|
| 322 | return this._outputOptions;
|
---|
| 323 | },
|
---|
| 324 | "MainTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
|
---|
| 325 | "DEP_WEBPACK_MAIN_TEMPLATE_OUTPUT_OPTIONS"
|
---|
| 326 | )
|
---|
| 327 | });
|
---|
| 328 |
|
---|
| 329 | module.exports = MainTemplate;
|
---|