123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const RuntimeGlobals = require("./RuntimeGlobals");
- const { getChunkFilenameTemplate } = require("./css/CssModulesPlugin");
- const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
- const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
- const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
- const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
- const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
- const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
- const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
- const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
- const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
- const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
- const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
- const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
- const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
- const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
- const GetTrustedTypesPolicyRuntimeModule = require("./runtime/GetTrustedTypesPolicyRuntimeModule");
- const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
- const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
- const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
- const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
- const NonceRuntimeModule = require("./runtime/NonceRuntimeModule");
- const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule");
- const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
- const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule");
- const RuntimeIdRuntimeModule = require("./runtime/RuntimeIdRuntimeModule");
- const SystemContextRuntimeModule = require("./runtime/SystemContextRuntimeModule");
- const ShareRuntimeModule = require("./sharing/ShareRuntimeModule");
- const StringXor = require("./util/StringXor");
- /** @typedef {import("./Chunk")} Chunk */
- /** @typedef {import("./Compiler")} Compiler */
- /** @typedef {import("./Module")} Module */
- const GLOBALS_ON_REQUIRE = [
- RuntimeGlobals.chunkName,
- RuntimeGlobals.runtimeId,
- RuntimeGlobals.compatGetDefaultExport,
- RuntimeGlobals.createFakeNamespaceObject,
- RuntimeGlobals.createScript,
- RuntimeGlobals.createScriptUrl,
- RuntimeGlobals.getTrustedTypesPolicy,
- RuntimeGlobals.definePropertyGetters,
- RuntimeGlobals.ensureChunk,
- RuntimeGlobals.entryModuleId,
- RuntimeGlobals.getFullHash,
- RuntimeGlobals.global,
- RuntimeGlobals.makeNamespaceObject,
- RuntimeGlobals.moduleCache,
- RuntimeGlobals.moduleFactories,
- RuntimeGlobals.moduleFactoriesAddOnly,
- RuntimeGlobals.interceptModuleExecution,
- RuntimeGlobals.publicPath,
- RuntimeGlobals.baseURI,
- RuntimeGlobals.relativeUrl,
- // TODO webpack 6 - rename to nonce, because we use it for CSS too
- RuntimeGlobals.scriptNonce,
- RuntimeGlobals.uncaughtErrorHandler,
- RuntimeGlobals.asyncModule,
- RuntimeGlobals.wasmInstances,
- RuntimeGlobals.instantiateWasm,
- RuntimeGlobals.shareScopeMap,
- RuntimeGlobals.initializeSharing,
- RuntimeGlobals.loadScript,
- RuntimeGlobals.systemContext,
- RuntimeGlobals.onChunksLoaded
- ];
- const MODULE_DEPENDENCIES = {
- [RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module],
- [RuntimeGlobals.moduleId]: [RuntimeGlobals.module]
- };
- const TREE_DEPENDENCIES = {
- [RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty],
- [RuntimeGlobals.compatGetDefaultExport]: [
- RuntimeGlobals.definePropertyGetters
- ],
- [RuntimeGlobals.createFakeNamespaceObject]: [
- RuntimeGlobals.definePropertyGetters,
- RuntimeGlobals.makeNamespaceObject,
- RuntimeGlobals.require
- ],
- [RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
- [RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
- };
- class RuntimePlugin {
- /**
- * @param {Compiler} compiler the Compiler
- * @returns {void}
- */
- apply(compiler) {
- compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
- const globalChunkLoading = compilation.outputOptions.chunkLoading;
- /**
- * @param {Chunk} chunk chunk
- * @returns {boolean} true, when chunk loading is disabled for the chunk
- */
- const isChunkLoadingDisabledForChunk = chunk => {
- const options = chunk.getEntryOptions();
- const chunkLoading =
- options && options.chunkLoading !== undefined
- ? options.chunkLoading
- : globalChunkLoading;
- return chunkLoading === false;
- };
- compilation.dependencyTemplates.set(
- RuntimeRequirementsDependency,
- new RuntimeRequirementsDependency.Template()
- );
- for (const req of GLOBALS_ON_REQUIRE) {
- compilation.hooks.runtimeRequirementInModule
- .for(req)
- .tap("RuntimePlugin", (module, set) => {
- set.add(RuntimeGlobals.requireScope);
- });
- compilation.hooks.runtimeRequirementInTree
- .for(req)
- .tap("RuntimePlugin", (module, set) => {
- set.add(RuntimeGlobals.requireScope);
- });
- }
- for (const req of Object.keys(TREE_DEPENDENCIES)) {
- const deps =
- TREE_DEPENDENCIES[/** @type {keyof TREE_DEPENDENCIES} */ (req)];
- compilation.hooks.runtimeRequirementInTree
- .for(req)
- .tap("RuntimePlugin", (chunk, set) => {
- for (const dep of deps) set.add(dep);
- });
- }
- for (const req of Object.keys(MODULE_DEPENDENCIES)) {
- const deps =
- MODULE_DEPENDENCIES[/** @type {keyof MODULE_DEPENDENCIES} */ (req)];
- compilation.hooks.runtimeRequirementInModule
- .for(req)
- .tap("RuntimePlugin", (chunk, set) => {
- for (const dep of deps) set.add(dep);
- });
- }
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.definePropertyGetters)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(
- chunk,
- new DefinePropertyGettersRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.makeNamespaceObject)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(
- chunk,
- new MakeNamespaceObjectRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.createFakeNamespaceObject)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(
- chunk,
- new CreateFakeNamespaceObjectRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.hasOwnProperty)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(
- chunk,
- new HasOwnPropertyRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.compatGetDefaultExport)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(
- chunk,
- new CompatGetDefaultExportRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.runtimeId)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(chunk, new RuntimeIdRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.publicPath)
- .tap("RuntimePlugin", (chunk, set) => {
- const { outputOptions } = compilation;
- const { publicPath: globalPublicPath, scriptType } = outputOptions;
- const entryOptions = chunk.getEntryOptions();
- const publicPath =
- entryOptions && entryOptions.publicPath !== undefined
- ? entryOptions.publicPath
- : globalPublicPath;
- if (publicPath === "auto") {
- const module = new AutoPublicPathRuntimeModule();
- if (scriptType !== "module") set.add(RuntimeGlobals.global);
- compilation.addRuntimeModule(chunk, module);
- } else {
- const module = new PublicPathRuntimeModule(publicPath);
- if (
- typeof publicPath !== "string" ||
- /\[(full)?hash\]/.test(publicPath)
- ) {
- module.fullHash = true;
- }
- compilation.addRuntimeModule(chunk, module);
- }
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.global)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.asyncModule)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(chunk, new AsyncModuleRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.systemContext)
- .tap("RuntimePlugin", chunk => {
- const { outputOptions } = compilation;
- const { library: globalLibrary } = outputOptions;
- const entryOptions = chunk.getEntryOptions();
- const libraryType =
- entryOptions && entryOptions.library !== undefined
- ? entryOptions.library.type
- : globalLibrary.type;
- if (libraryType === "system") {
- compilation.addRuntimeModule(
- chunk,
- new SystemContextRuntimeModule()
- );
- }
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.getChunkScriptFilename)
- .tap("RuntimePlugin", (chunk, set) => {
- if (
- typeof compilation.outputOptions.chunkFilename === "string" &&
- /\[(full)?hash(:\d+)?\]/.test(
- compilation.outputOptions.chunkFilename
- )
- ) {
- set.add(RuntimeGlobals.getFullHash);
- }
- compilation.addRuntimeModule(
- chunk,
- new GetChunkFilenameRuntimeModule(
- "javascript",
- "javascript",
- RuntimeGlobals.getChunkScriptFilename,
- chunk =>
- chunk.filenameTemplate ||
- (chunk.canBeInitial()
- ? compilation.outputOptions.filename
- : compilation.outputOptions.chunkFilename),
- false
- )
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.getChunkCssFilename)
- .tap("RuntimePlugin", (chunk, set) => {
- if (
- typeof compilation.outputOptions.cssChunkFilename === "string" &&
- /\[(full)?hash(:\d+)?\]/.test(
- compilation.outputOptions.cssChunkFilename
- )
- ) {
- set.add(RuntimeGlobals.getFullHash);
- }
- compilation.addRuntimeModule(
- chunk,
- new GetChunkFilenameRuntimeModule(
- "css",
- "css",
- RuntimeGlobals.getChunkCssFilename,
- chunk =>
- getChunkFilenameTemplate(chunk, compilation.outputOptions),
- set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
- )
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.getChunkUpdateScriptFilename)
- .tap("RuntimePlugin", (chunk, set) => {
- if (
- /\[(full)?hash(:\d+)?\]/.test(
- compilation.outputOptions.hotUpdateChunkFilename
- )
- )
- set.add(RuntimeGlobals.getFullHash);
- compilation.addRuntimeModule(
- chunk,
- new GetChunkFilenameRuntimeModule(
- "javascript",
- "javascript update",
- RuntimeGlobals.getChunkUpdateScriptFilename,
- c => compilation.outputOptions.hotUpdateChunkFilename,
- true
- )
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.getUpdateManifestFilename)
- .tap("RuntimePlugin", (chunk, set) => {
- if (
- /\[(full)?hash(:\d+)?\]/.test(
- compilation.outputOptions.hotUpdateMainFilename
- )
- ) {
- set.add(RuntimeGlobals.getFullHash);
- }
- compilation.addRuntimeModule(
- chunk,
- new GetMainFilenameRuntimeModule(
- "update manifest",
- RuntimeGlobals.getUpdateManifestFilename,
- compilation.outputOptions.hotUpdateMainFilename
- )
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.ensureChunk)
- .tap("RuntimePlugin", (chunk, set) => {
- const hasAsyncChunks = chunk.hasAsyncChunks();
- if (hasAsyncChunks) {
- set.add(RuntimeGlobals.ensureChunkHandlers);
- }
- compilation.addRuntimeModule(
- chunk,
- new EnsureChunkRuntimeModule(set)
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.ensureChunkIncludeEntries)
- .tap("RuntimePlugin", (chunk, set) => {
- set.add(RuntimeGlobals.ensureChunkHandlers);
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.shareScopeMap)
- .tap("RuntimePlugin", (chunk, set) => {
- compilation.addRuntimeModule(chunk, new ShareRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.loadScript)
- .tap("RuntimePlugin", (chunk, set) => {
- const withCreateScriptUrl = !!compilation.outputOptions.trustedTypes;
- if (withCreateScriptUrl) {
- set.add(RuntimeGlobals.createScriptUrl);
- }
- const withFetchPriority = set.has(RuntimeGlobals.hasFetchPriority);
- compilation.addRuntimeModule(
- chunk,
- new LoadScriptRuntimeModule(withCreateScriptUrl, withFetchPriority)
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.createScript)
- .tap("RuntimePlugin", (chunk, set) => {
- if (compilation.outputOptions.trustedTypes) {
- set.add(RuntimeGlobals.getTrustedTypesPolicy);
- }
- compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.createScriptUrl)
- .tap("RuntimePlugin", (chunk, set) => {
- if (compilation.outputOptions.trustedTypes) {
- set.add(RuntimeGlobals.getTrustedTypesPolicy);
- }
- compilation.addRuntimeModule(
- chunk,
- new CreateScriptUrlRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.getTrustedTypesPolicy)
- .tap("RuntimePlugin", (chunk, set) => {
- compilation.addRuntimeModule(
- chunk,
- new GetTrustedTypesPolicyRuntimeModule(set)
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.relativeUrl)
- .tap("RuntimePlugin", (chunk, set) => {
- compilation.addRuntimeModule(chunk, new RelativeUrlRuntimeModule());
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.onChunksLoaded)
- .tap("RuntimePlugin", (chunk, set) => {
- compilation.addRuntimeModule(
- chunk,
- new OnChunksLoadedRuntimeModule()
- );
- return true;
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.baseURI)
- .tap("RuntimePlugin", chunk => {
- if (isChunkLoadingDisabledForChunk(chunk)) {
- compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
- return true;
- }
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.scriptNonce)
- .tap("RuntimePlugin", chunk => {
- compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
- return true;
- });
- // TODO webpack 6: remove CompatRuntimeModule
- compilation.hooks.additionalTreeRuntimeRequirements.tap(
- "RuntimePlugin",
- (chunk, set) => {
- const { mainTemplate } = compilation;
- if (
- mainTemplate.hooks.bootstrap.isUsed() ||
- mainTemplate.hooks.localVars.isUsed() ||
- mainTemplate.hooks.requireEnsure.isUsed() ||
- mainTemplate.hooks.requireExtensions.isUsed()
- ) {
- compilation.addRuntimeModule(chunk, new CompatRuntimeModule());
- }
- }
- );
- JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
- "RuntimePlugin",
- (chunk, hash, { chunkGraph }) => {
- const xor = new StringXor();
- for (const m of chunkGraph.getChunkRuntimeModulesIterable(chunk)) {
- xor.add(chunkGraph.getModuleHash(m, chunk.runtime));
- }
- xor.updateHash(hash);
- }
- );
- });
- }
- }
- module.exports = RuntimePlugin;
|