[79a0317] | 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 RuntimeGlobals = require("./RuntimeGlobals");
|
---|
| 9 | const { getChunkFilenameTemplate } = require("./css/CssModulesPlugin");
|
---|
| 10 | const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
|
---|
| 11 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
---|
| 12 | const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
|
---|
| 13 | const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
|
---|
| 14 | const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
|
---|
| 15 | const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
|
---|
| 16 | const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
|
---|
| 17 | const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
|
---|
| 18 | const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
|
---|
| 19 | const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
|
---|
| 20 | const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
|
---|
| 21 | const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
|
---|
| 22 | const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
|
---|
| 23 | const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
|
---|
| 24 | const GetTrustedTypesPolicyRuntimeModule = require("./runtime/GetTrustedTypesPolicyRuntimeModule");
|
---|
| 25 | const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
|
---|
| 26 | const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
|
---|
| 27 | const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
|
---|
| 28 | const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
|
---|
| 29 | const NonceRuntimeModule = require("./runtime/NonceRuntimeModule");
|
---|
| 30 | const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule");
|
---|
| 31 | const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
|
---|
| 32 | const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule");
|
---|
| 33 | const RuntimeIdRuntimeModule = require("./runtime/RuntimeIdRuntimeModule");
|
---|
| 34 | const SystemContextRuntimeModule = require("./runtime/SystemContextRuntimeModule");
|
---|
| 35 | const ShareRuntimeModule = require("./sharing/ShareRuntimeModule");
|
---|
| 36 | const StringXor = require("./util/StringXor");
|
---|
| 37 | const memoize = require("./util/memoize");
|
---|
| 38 |
|
---|
| 39 | /** @typedef {import("../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
| 40 | /** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputNormalized */
|
---|
| 41 | /** @typedef {import("./Chunk")} Chunk */
|
---|
| 42 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 43 | /** @typedef {import("./Module")} Module */
|
---|
| 44 | /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
|
---|
| 45 |
|
---|
| 46 | const getJavascriptModulesPlugin = memoize(() =>
|
---|
| 47 | require("./javascript/JavascriptModulesPlugin")
|
---|
| 48 | );
|
---|
| 49 | const getCssModulesPlugin = memoize(() => require("./css/CssModulesPlugin"));
|
---|
| 50 |
|
---|
| 51 | const GLOBALS_ON_REQUIRE = [
|
---|
| 52 | RuntimeGlobals.chunkName,
|
---|
| 53 | RuntimeGlobals.runtimeId,
|
---|
| 54 | RuntimeGlobals.compatGetDefaultExport,
|
---|
| 55 | RuntimeGlobals.createFakeNamespaceObject,
|
---|
| 56 | RuntimeGlobals.createScript,
|
---|
| 57 | RuntimeGlobals.createScriptUrl,
|
---|
| 58 | RuntimeGlobals.getTrustedTypesPolicy,
|
---|
| 59 | RuntimeGlobals.definePropertyGetters,
|
---|
| 60 | RuntimeGlobals.ensureChunk,
|
---|
| 61 | RuntimeGlobals.entryModuleId,
|
---|
| 62 | RuntimeGlobals.getFullHash,
|
---|
| 63 | RuntimeGlobals.global,
|
---|
| 64 | RuntimeGlobals.makeNamespaceObject,
|
---|
| 65 | RuntimeGlobals.moduleCache,
|
---|
| 66 | RuntimeGlobals.moduleFactories,
|
---|
| 67 | RuntimeGlobals.moduleFactoriesAddOnly,
|
---|
| 68 | RuntimeGlobals.interceptModuleExecution,
|
---|
| 69 | RuntimeGlobals.publicPath,
|
---|
| 70 | RuntimeGlobals.baseURI,
|
---|
| 71 | RuntimeGlobals.relativeUrl,
|
---|
| 72 | // TODO webpack 6 - rename to nonce, because we use it for CSS too
|
---|
| 73 | RuntimeGlobals.scriptNonce,
|
---|
| 74 | RuntimeGlobals.uncaughtErrorHandler,
|
---|
| 75 | RuntimeGlobals.asyncModule,
|
---|
| 76 | RuntimeGlobals.wasmInstances,
|
---|
| 77 | RuntimeGlobals.instantiateWasm,
|
---|
| 78 | RuntimeGlobals.shareScopeMap,
|
---|
| 79 | RuntimeGlobals.initializeSharing,
|
---|
| 80 | RuntimeGlobals.loadScript,
|
---|
| 81 | RuntimeGlobals.systemContext,
|
---|
| 82 | RuntimeGlobals.onChunksLoaded
|
---|
| 83 | ];
|
---|
| 84 |
|
---|
| 85 | const MODULE_DEPENDENCIES = {
|
---|
| 86 | [RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module],
|
---|
| 87 | [RuntimeGlobals.moduleId]: [RuntimeGlobals.module]
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | const TREE_DEPENDENCIES = {
|
---|
| 91 | [RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty],
|
---|
| 92 | [RuntimeGlobals.compatGetDefaultExport]: [
|
---|
| 93 | RuntimeGlobals.definePropertyGetters
|
---|
| 94 | ],
|
---|
| 95 | [RuntimeGlobals.createFakeNamespaceObject]: [
|
---|
| 96 | RuntimeGlobals.definePropertyGetters,
|
---|
| 97 | RuntimeGlobals.makeNamespaceObject,
|
---|
| 98 | RuntimeGlobals.require
|
---|
| 99 | ],
|
---|
| 100 | [RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
|
---|
| 101 | [RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | class RuntimePlugin {
|
---|
| 105 | /**
|
---|
| 106 | * @param {Compiler} compiler the Compiler
|
---|
| 107 | * @returns {void}
|
---|
| 108 | */
|
---|
| 109 | apply(compiler) {
|
---|
| 110 | compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
|
---|
| 111 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
| 112 | /**
|
---|
| 113 | * @param {Chunk} chunk chunk
|
---|
| 114 | * @returns {boolean} true, when chunk loading is disabled for the chunk
|
---|
| 115 | */
|
---|
| 116 | const isChunkLoadingDisabledForChunk = chunk => {
|
---|
| 117 | const options = chunk.getEntryOptions();
|
---|
| 118 | const chunkLoading =
|
---|
| 119 | options && options.chunkLoading !== undefined
|
---|
| 120 | ? options.chunkLoading
|
---|
| 121 | : globalChunkLoading;
|
---|
| 122 | return chunkLoading === false;
|
---|
| 123 | };
|
---|
| 124 | compilation.dependencyTemplates.set(
|
---|
| 125 | RuntimeRequirementsDependency,
|
---|
| 126 | new RuntimeRequirementsDependency.Template()
|
---|
| 127 | );
|
---|
| 128 | for (const req of GLOBALS_ON_REQUIRE) {
|
---|
| 129 | compilation.hooks.runtimeRequirementInModule
|
---|
| 130 | .for(req)
|
---|
| 131 | .tap("RuntimePlugin", (module, set) => {
|
---|
| 132 | set.add(RuntimeGlobals.requireScope);
|
---|
| 133 | });
|
---|
| 134 | compilation.hooks.runtimeRequirementInTree
|
---|
| 135 | .for(req)
|
---|
| 136 | .tap("RuntimePlugin", (module, set) => {
|
---|
| 137 | set.add(RuntimeGlobals.requireScope);
|
---|
| 138 | });
|
---|
| 139 | }
|
---|
| 140 | for (const req of Object.keys(TREE_DEPENDENCIES)) {
|
---|
| 141 | const deps =
|
---|
| 142 | TREE_DEPENDENCIES[/** @type {keyof TREE_DEPENDENCIES} */ (req)];
|
---|
| 143 | compilation.hooks.runtimeRequirementInTree
|
---|
| 144 | .for(req)
|
---|
| 145 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 146 | for (const dep of deps) set.add(dep);
|
---|
| 147 | });
|
---|
| 148 | }
|
---|
| 149 | for (const req of Object.keys(MODULE_DEPENDENCIES)) {
|
---|
| 150 | const deps =
|
---|
| 151 | MODULE_DEPENDENCIES[/** @type {keyof MODULE_DEPENDENCIES} */ (req)];
|
---|
| 152 | compilation.hooks.runtimeRequirementInModule
|
---|
| 153 | .for(req)
|
---|
| 154 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 155 | for (const dep of deps) set.add(dep);
|
---|
| 156 | });
|
---|
| 157 | }
|
---|
| 158 | compilation.hooks.runtimeRequirementInTree
|
---|
| 159 | .for(RuntimeGlobals.definePropertyGetters)
|
---|
| 160 | .tap("RuntimePlugin", chunk => {
|
---|
| 161 | compilation.addRuntimeModule(
|
---|
| 162 | chunk,
|
---|
| 163 | new DefinePropertyGettersRuntimeModule()
|
---|
| 164 | );
|
---|
| 165 | return true;
|
---|
| 166 | });
|
---|
| 167 | compilation.hooks.runtimeRequirementInTree
|
---|
| 168 | .for(RuntimeGlobals.makeNamespaceObject)
|
---|
| 169 | .tap("RuntimePlugin", chunk => {
|
---|
| 170 | compilation.addRuntimeModule(
|
---|
| 171 | chunk,
|
---|
| 172 | new MakeNamespaceObjectRuntimeModule()
|
---|
| 173 | );
|
---|
| 174 | return true;
|
---|
| 175 | });
|
---|
| 176 | compilation.hooks.runtimeRequirementInTree
|
---|
| 177 | .for(RuntimeGlobals.createFakeNamespaceObject)
|
---|
| 178 | .tap("RuntimePlugin", chunk => {
|
---|
| 179 | compilation.addRuntimeModule(
|
---|
| 180 | chunk,
|
---|
| 181 | new CreateFakeNamespaceObjectRuntimeModule()
|
---|
| 182 | );
|
---|
| 183 | return true;
|
---|
| 184 | });
|
---|
| 185 | compilation.hooks.runtimeRequirementInTree
|
---|
| 186 | .for(RuntimeGlobals.hasOwnProperty)
|
---|
| 187 | .tap("RuntimePlugin", chunk => {
|
---|
| 188 | compilation.addRuntimeModule(
|
---|
| 189 | chunk,
|
---|
| 190 | new HasOwnPropertyRuntimeModule()
|
---|
| 191 | );
|
---|
| 192 | return true;
|
---|
| 193 | });
|
---|
| 194 | compilation.hooks.runtimeRequirementInTree
|
---|
| 195 | .for(RuntimeGlobals.compatGetDefaultExport)
|
---|
| 196 | .tap("RuntimePlugin", chunk => {
|
---|
| 197 | compilation.addRuntimeModule(
|
---|
| 198 | chunk,
|
---|
| 199 | new CompatGetDefaultExportRuntimeModule()
|
---|
| 200 | );
|
---|
| 201 | return true;
|
---|
| 202 | });
|
---|
| 203 | compilation.hooks.runtimeRequirementInTree
|
---|
| 204 | .for(RuntimeGlobals.runtimeId)
|
---|
| 205 | .tap("RuntimePlugin", chunk => {
|
---|
| 206 | compilation.addRuntimeModule(chunk, new RuntimeIdRuntimeModule());
|
---|
| 207 | return true;
|
---|
| 208 | });
|
---|
| 209 | compilation.hooks.runtimeRequirementInTree
|
---|
| 210 | .for(RuntimeGlobals.publicPath)
|
---|
| 211 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 212 | const { outputOptions } = compilation;
|
---|
| 213 | const { publicPath: globalPublicPath, scriptType } = outputOptions;
|
---|
| 214 | const entryOptions = chunk.getEntryOptions();
|
---|
| 215 | const publicPath =
|
---|
| 216 | entryOptions && entryOptions.publicPath !== undefined
|
---|
| 217 | ? entryOptions.publicPath
|
---|
| 218 | : globalPublicPath;
|
---|
| 219 |
|
---|
| 220 | if (publicPath === "auto") {
|
---|
| 221 | const module = new AutoPublicPathRuntimeModule();
|
---|
| 222 | if (scriptType !== "module") set.add(RuntimeGlobals.global);
|
---|
| 223 | compilation.addRuntimeModule(chunk, module);
|
---|
| 224 | } else {
|
---|
| 225 | const module = new PublicPathRuntimeModule(publicPath);
|
---|
| 226 |
|
---|
| 227 | if (
|
---|
| 228 | typeof publicPath !== "string" ||
|
---|
| 229 | /\[(full)?hash\]/.test(publicPath)
|
---|
| 230 | ) {
|
---|
| 231 | module.fullHash = true;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | compilation.addRuntimeModule(chunk, module);
|
---|
| 235 | }
|
---|
| 236 | return true;
|
---|
| 237 | });
|
---|
| 238 | compilation.hooks.runtimeRequirementInTree
|
---|
| 239 | .for(RuntimeGlobals.global)
|
---|
| 240 | .tap("RuntimePlugin", chunk => {
|
---|
| 241 | compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
|
---|
| 242 | return true;
|
---|
| 243 | });
|
---|
| 244 | compilation.hooks.runtimeRequirementInTree
|
---|
| 245 | .for(RuntimeGlobals.asyncModule)
|
---|
| 246 | .tap("RuntimePlugin", chunk => {
|
---|
| 247 | compilation.addRuntimeModule(chunk, new AsyncModuleRuntimeModule());
|
---|
| 248 | return true;
|
---|
| 249 | });
|
---|
| 250 | compilation.hooks.runtimeRequirementInTree
|
---|
| 251 | .for(RuntimeGlobals.systemContext)
|
---|
| 252 | .tap("RuntimePlugin", chunk => {
|
---|
| 253 | const entryOptions = chunk.getEntryOptions();
|
---|
| 254 | const libraryType =
|
---|
| 255 | entryOptions && entryOptions.library !== undefined
|
---|
| 256 | ? entryOptions.library.type
|
---|
| 257 | : /** @type {LibraryOptions} */
|
---|
| 258 | (compilation.outputOptions.library).type;
|
---|
| 259 |
|
---|
| 260 | if (libraryType === "system") {
|
---|
| 261 | compilation.addRuntimeModule(
|
---|
| 262 | chunk,
|
---|
| 263 | new SystemContextRuntimeModule()
|
---|
| 264 | );
|
---|
| 265 | }
|
---|
| 266 | return true;
|
---|
| 267 | });
|
---|
| 268 | compilation.hooks.runtimeRequirementInTree
|
---|
| 269 | .for(RuntimeGlobals.getChunkScriptFilename)
|
---|
| 270 | .tap("RuntimePlugin", (chunk, set, { chunkGraph }) => {
|
---|
| 271 | if (
|
---|
| 272 | typeof compilation.outputOptions.chunkFilename === "string" &&
|
---|
| 273 | /\[(full)?hash(:\d+)?\]/.test(
|
---|
| 274 | compilation.outputOptions.chunkFilename
|
---|
| 275 | )
|
---|
| 276 | ) {
|
---|
| 277 | set.add(RuntimeGlobals.getFullHash);
|
---|
| 278 | }
|
---|
| 279 | compilation.addRuntimeModule(
|
---|
| 280 | chunk,
|
---|
| 281 | new GetChunkFilenameRuntimeModule(
|
---|
| 282 | "javascript",
|
---|
| 283 | "javascript",
|
---|
| 284 | RuntimeGlobals.getChunkScriptFilename,
|
---|
| 285 | chunk =>
|
---|
| 286 | getJavascriptModulesPlugin().chunkHasJs(chunk, chunkGraph) &&
|
---|
| 287 | /** @type {TemplatePath} */ (
|
---|
| 288 | chunk.filenameTemplate ||
|
---|
| 289 | (chunk.canBeInitial()
|
---|
| 290 | ? compilation.outputOptions.filename
|
---|
| 291 | : compilation.outputOptions.chunkFilename)
|
---|
| 292 | ),
|
---|
| 293 | false
|
---|
| 294 | )
|
---|
| 295 | );
|
---|
| 296 | return true;
|
---|
| 297 | });
|
---|
| 298 | compilation.hooks.runtimeRequirementInTree
|
---|
| 299 | .for(RuntimeGlobals.getChunkCssFilename)
|
---|
| 300 | .tap("RuntimePlugin", (chunk, set, { chunkGraph }) => {
|
---|
| 301 | if (
|
---|
| 302 | typeof compilation.outputOptions.cssChunkFilename === "string" &&
|
---|
| 303 | /\[(full)?hash(:\d+)?\]/.test(
|
---|
| 304 | compilation.outputOptions.cssChunkFilename
|
---|
| 305 | )
|
---|
| 306 | ) {
|
---|
| 307 | set.add(RuntimeGlobals.getFullHash);
|
---|
| 308 | }
|
---|
| 309 | compilation.addRuntimeModule(
|
---|
| 310 | chunk,
|
---|
| 311 | new GetChunkFilenameRuntimeModule(
|
---|
| 312 | "css",
|
---|
| 313 | "css",
|
---|
| 314 | RuntimeGlobals.getChunkCssFilename,
|
---|
| 315 | chunk =>
|
---|
| 316 | getCssModulesPlugin().chunkHasCss(chunk, chunkGraph) &&
|
---|
| 317 | getChunkFilenameTemplate(chunk, compilation.outputOptions),
|
---|
| 318 | set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
| 319 | )
|
---|
| 320 | );
|
---|
| 321 | return true;
|
---|
| 322 | });
|
---|
| 323 | compilation.hooks.runtimeRequirementInTree
|
---|
| 324 | .for(RuntimeGlobals.getChunkUpdateScriptFilename)
|
---|
| 325 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 326 | if (
|
---|
| 327 | /\[(full)?hash(:\d+)?\]/.test(
|
---|
| 328 | /** @type {NonNullable<OutputNormalized["hotUpdateChunkFilename"]>} */
|
---|
| 329 | (compilation.outputOptions.hotUpdateChunkFilename)
|
---|
| 330 | )
|
---|
| 331 | )
|
---|
| 332 | set.add(RuntimeGlobals.getFullHash);
|
---|
| 333 | compilation.addRuntimeModule(
|
---|
| 334 | chunk,
|
---|
| 335 | new GetChunkFilenameRuntimeModule(
|
---|
| 336 | "javascript",
|
---|
| 337 | "javascript update",
|
---|
| 338 | RuntimeGlobals.getChunkUpdateScriptFilename,
|
---|
| 339 | c =>
|
---|
| 340 | /** @type {NonNullable<OutputNormalized["hotUpdateChunkFilename"]>} */
|
---|
| 341 | (compilation.outputOptions.hotUpdateChunkFilename),
|
---|
| 342 | true
|
---|
| 343 | )
|
---|
| 344 | );
|
---|
| 345 | return true;
|
---|
| 346 | });
|
---|
| 347 | compilation.hooks.runtimeRequirementInTree
|
---|
| 348 | .for(RuntimeGlobals.getUpdateManifestFilename)
|
---|
| 349 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 350 | if (
|
---|
| 351 | /\[(full)?hash(:\d+)?\]/.test(
|
---|
| 352 | /** @type {NonNullable<OutputNormalized["hotUpdateMainFilename"]>} */
|
---|
| 353 | (compilation.outputOptions.hotUpdateMainFilename)
|
---|
| 354 | )
|
---|
| 355 | ) {
|
---|
| 356 | set.add(RuntimeGlobals.getFullHash);
|
---|
| 357 | }
|
---|
| 358 | compilation.addRuntimeModule(
|
---|
| 359 | chunk,
|
---|
| 360 | new GetMainFilenameRuntimeModule(
|
---|
| 361 | "update manifest",
|
---|
| 362 | RuntimeGlobals.getUpdateManifestFilename,
|
---|
| 363 | /** @type {NonNullable<OutputNormalized["hotUpdateMainFilename"]>} */
|
---|
| 364 | (compilation.outputOptions.hotUpdateMainFilename)
|
---|
| 365 | )
|
---|
| 366 | );
|
---|
| 367 | return true;
|
---|
| 368 | });
|
---|
| 369 | compilation.hooks.runtimeRequirementInTree
|
---|
| 370 | .for(RuntimeGlobals.ensureChunk)
|
---|
| 371 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 372 | const hasAsyncChunks = chunk.hasAsyncChunks();
|
---|
| 373 | if (hasAsyncChunks) {
|
---|
| 374 | set.add(RuntimeGlobals.ensureChunkHandlers);
|
---|
| 375 | }
|
---|
| 376 | compilation.addRuntimeModule(
|
---|
| 377 | chunk,
|
---|
| 378 | new EnsureChunkRuntimeModule(set)
|
---|
| 379 | );
|
---|
| 380 | return true;
|
---|
| 381 | });
|
---|
| 382 | compilation.hooks.runtimeRequirementInTree
|
---|
| 383 | .for(RuntimeGlobals.ensureChunkIncludeEntries)
|
---|
| 384 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 385 | set.add(RuntimeGlobals.ensureChunkHandlers);
|
---|
| 386 | });
|
---|
| 387 | compilation.hooks.runtimeRequirementInTree
|
---|
| 388 | .for(RuntimeGlobals.shareScopeMap)
|
---|
| 389 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 390 | compilation.addRuntimeModule(chunk, new ShareRuntimeModule());
|
---|
| 391 | return true;
|
---|
| 392 | });
|
---|
| 393 | compilation.hooks.runtimeRequirementInTree
|
---|
| 394 | .for(RuntimeGlobals.loadScript)
|
---|
| 395 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 396 | const withCreateScriptUrl = Boolean(
|
---|
| 397 | compilation.outputOptions.trustedTypes
|
---|
| 398 | );
|
---|
| 399 | if (withCreateScriptUrl) {
|
---|
| 400 | set.add(RuntimeGlobals.createScriptUrl);
|
---|
| 401 | }
|
---|
| 402 | const withFetchPriority = set.has(RuntimeGlobals.hasFetchPriority);
|
---|
| 403 | compilation.addRuntimeModule(
|
---|
| 404 | chunk,
|
---|
| 405 | new LoadScriptRuntimeModule(withCreateScriptUrl, withFetchPriority)
|
---|
| 406 | );
|
---|
| 407 | return true;
|
---|
| 408 | });
|
---|
| 409 | compilation.hooks.runtimeRequirementInTree
|
---|
| 410 | .for(RuntimeGlobals.createScript)
|
---|
| 411 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 412 | if (compilation.outputOptions.trustedTypes) {
|
---|
| 413 | set.add(RuntimeGlobals.getTrustedTypesPolicy);
|
---|
| 414 | }
|
---|
| 415 | compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
|
---|
| 416 | return true;
|
---|
| 417 | });
|
---|
| 418 | compilation.hooks.runtimeRequirementInTree
|
---|
| 419 | .for(RuntimeGlobals.createScriptUrl)
|
---|
| 420 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 421 | if (compilation.outputOptions.trustedTypes) {
|
---|
| 422 | set.add(RuntimeGlobals.getTrustedTypesPolicy);
|
---|
| 423 | }
|
---|
| 424 | compilation.addRuntimeModule(
|
---|
| 425 | chunk,
|
---|
| 426 | new CreateScriptUrlRuntimeModule()
|
---|
| 427 | );
|
---|
| 428 | return true;
|
---|
| 429 | });
|
---|
| 430 | compilation.hooks.runtimeRequirementInTree
|
---|
| 431 | .for(RuntimeGlobals.getTrustedTypesPolicy)
|
---|
| 432 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 433 | compilation.addRuntimeModule(
|
---|
| 434 | chunk,
|
---|
| 435 | new GetTrustedTypesPolicyRuntimeModule(set)
|
---|
| 436 | );
|
---|
| 437 | return true;
|
---|
| 438 | });
|
---|
| 439 | compilation.hooks.runtimeRequirementInTree
|
---|
| 440 | .for(RuntimeGlobals.relativeUrl)
|
---|
| 441 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 442 | compilation.addRuntimeModule(chunk, new RelativeUrlRuntimeModule());
|
---|
| 443 | return true;
|
---|
| 444 | });
|
---|
| 445 | compilation.hooks.runtimeRequirementInTree
|
---|
| 446 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
| 447 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 448 | compilation.addRuntimeModule(
|
---|
| 449 | chunk,
|
---|
| 450 | new OnChunksLoadedRuntimeModule()
|
---|
| 451 | );
|
---|
| 452 | return true;
|
---|
| 453 | });
|
---|
| 454 | compilation.hooks.runtimeRequirementInTree
|
---|
| 455 | .for(RuntimeGlobals.baseURI)
|
---|
| 456 | .tap("RuntimePlugin", chunk => {
|
---|
| 457 | if (isChunkLoadingDisabledForChunk(chunk)) {
|
---|
| 458 | compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
|
---|
| 459 | return true;
|
---|
| 460 | }
|
---|
| 461 | });
|
---|
| 462 | compilation.hooks.runtimeRequirementInTree
|
---|
| 463 | .for(RuntimeGlobals.scriptNonce)
|
---|
| 464 | .tap("RuntimePlugin", chunk => {
|
---|
| 465 | compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
|
---|
| 466 | return true;
|
---|
| 467 | });
|
---|
| 468 | // TODO webpack 6: remove CompatRuntimeModule
|
---|
| 469 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
---|
| 470 | "RuntimePlugin",
|
---|
| 471 | (chunk, set) => {
|
---|
| 472 | const { mainTemplate } = compilation;
|
---|
| 473 | if (
|
---|
| 474 | mainTemplate.hooks.bootstrap.isUsed() ||
|
---|
| 475 | mainTemplate.hooks.localVars.isUsed() ||
|
---|
| 476 | mainTemplate.hooks.requireEnsure.isUsed() ||
|
---|
| 477 | mainTemplate.hooks.requireExtensions.isUsed()
|
---|
| 478 | ) {
|
---|
| 479 | compilation.addRuntimeModule(chunk, new CompatRuntimeModule());
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | );
|
---|
| 483 | JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
|
---|
| 484 | "RuntimePlugin",
|
---|
| 485 | (chunk, hash, { chunkGraph }) => {
|
---|
| 486 | const xor = new StringXor();
|
---|
| 487 | for (const m of chunkGraph.getChunkRuntimeModulesIterable(chunk)) {
|
---|
| 488 | xor.add(chunkGraph.getModuleHash(m, chunk.runtime));
|
---|
| 489 | }
|
---|
| 490 | xor.updateHash(hash);
|
---|
| 491 | }
|
---|
| 492 | );
|
---|
| 493 | });
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 | module.exports = RuntimePlugin;
|
---|