webpack/lib/RuntimePlugin.js

303 lines
9.8 KiB
JavaScript
Raw Normal View History

2018-11-17 01:18:44 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const RuntimeGlobals = require("./RuntimeGlobals");
const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
2019-11-15 00:28:43 +08:00
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
const CompatRuntimePlugin = require("./runtime/CompatRuntimePlugin");
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
2018-11-21 18:32:10 +08:00
const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
2018-12-06 19:11:01 +08:00
const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
2019-12-03 21:27:39 +08:00
const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
2018-11-20 17:22:10 +08:00
const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
const SystemContextRuntimeModule = require("./runtime/SystemContextRuntimeModule");
2020-05-23 22:08:51 +08:00
const ShareRuntimeModule = require("./sharing/ShareRuntimeModule");
2018-11-17 01:18:44 +08:00
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Module")} Module */
const GLOBALS_ON_REQUIRE = [
RuntimeGlobals.chunkName,
RuntimeGlobals.compatGetDefaultExport,
RuntimeGlobals.createFakeNamespaceObject,
RuntimeGlobals.definePropertyGetters,
RuntimeGlobals.ensureChunk,
RuntimeGlobals.entryModuleId,
RuntimeGlobals.getFullHash,
RuntimeGlobals.global,
RuntimeGlobals.makeNamespaceObject,
RuntimeGlobals.moduleCache,
RuntimeGlobals.moduleFactories,
RuntimeGlobals.moduleFactoriesAddOnly,
RuntimeGlobals.interceptModuleExecution,
RuntimeGlobals.publicPath,
RuntimeGlobals.scriptNonce,
RuntimeGlobals.uncaughtErrorHandler,
RuntimeGlobals.wasmInstances,
2020-05-23 22:08:51 +08:00
RuntimeGlobals.instantiateWasm,
RuntimeGlobals.shareScopeMap,
RuntimeGlobals.initializeSharing,
RuntimeGlobals.loadScript
];
const MODULE_DEPENDENCIES = {
[RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module],
[RuntimeGlobals.moduleId]: [RuntimeGlobals.module]
};
const TREE_DEPENDENCIES = {
2019-12-03 21:27:39 +08:00
[RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty],
[RuntimeGlobals.compatGetDefaultExport]: [
RuntimeGlobals.definePropertyGetters
],
[RuntimeGlobals.createFakeNamespaceObject]: [
RuntimeGlobals.definePropertyGetters,
RuntimeGlobals.makeNamespaceObject,
RuntimeGlobals.require
2020-05-23 22:08:51 +08:00
],
[RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
[RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
};
2018-11-17 01:18:44 +08:00
class RuntimePlugin {
/**
* @param {Compiler} compiler the Compiler
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
compilation.dependencyTemplates.set(
RuntimeRequirementsDependency,
new RuntimeRequirementsDependency.Template()
);
for (const req of GLOBALS_ON_REQUIRE) {
2018-11-17 01:18:44 +08:00
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[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[req];
compilation.hooks.runtimeRequirementInModule
.for(req)
.tap("RuntimePlugin", (chunk, set) => {
for (const dep of deps) set.add(dep);
2018-11-17 01:18:44 +08:00
});
}
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.definePropertyGetters)
.tap("RuntimePlugin", chunk => {
compilation.addRuntimeModule(
chunk,
new DefinePropertyGettersRuntimeModule()
);
return true;
});
2018-11-20 17:22:10 +08:00
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;
});
2019-12-03 21:27:39 +08:00
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.publicPath)
.tap("RuntimePlugin", chunk => {
compilation.addRuntimeModule(chunk, new PublicPathRuntimeModule());
return true;
});
2018-12-06 19:11:01 +08:00
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.global)
.tap("RuntimePlugin", chunk => {
compilation.addRuntimeModule(chunk, new GlobalRuntimeModule());
return true;
});
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.systemContext)
.tap("RuntimePlugin", chunk => {
if (compilation.outputOptions.library.type === "system") {
compilation.addRuntimeModule(
chunk,
new SystemContextRuntimeModule()
);
}
return true;
});
2018-11-21 18:32:10 +08:00
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.getChunkScriptFilename)
.tap("RuntimePlugin", (chunk, set) => {
2018-12-26 03:14:58 +08:00
if (
/\[(full)?hash(:\d+)?\]/.test(
compilation.outputOptions.chunkFilename
)
)
2018-11-21 18:32:10 +08:00
set.add(RuntimeGlobals.getFullHash);
compilation.addRuntimeModule(
chunk,
new GetChunkFilenameRuntimeModule(
"javascript",
"javascript",
2018-11-21 18:32:10 +08:00
RuntimeGlobals.getChunkScriptFilename,
chunk =>
chunk.filenameTemplate ||
(chunk.isOnlyInitial()
? compilation.outputOptions.filename
: compilation.outputOptions.chunkFilename),
false
2018-11-21 18:32:10 +08:00
)
);
return true;
});
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.getChunkUpdateScriptFilename)
.tap("RuntimePlugin", (chunk, set) => {
if (
2018-12-26 03:14:58 +08:00
/\[(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 (
2018-12-26 03:14:58 +08:00
/\[(full)?hash(:\d+)?\]/.test(
compilation.outputOptions.hotUpdateMainFilename
)
2018-12-18 05:05:08 +08:00
) {
set.add(RuntimeGlobals.getFullHash);
2018-12-18 05:05:08 +08:00
}
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);
});
2020-05-23 22:08:51 +08:00
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) => {
compilation.addRuntimeModule(chunk, new LoadScriptRuntimeModule());
return true;
});
// TODO webpack 6: remove CompatRuntimePlugin
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 CompatRuntimePlugin());
}
}
);
2019-11-15 00:28:43 +08:00
JavascriptModulesPlugin.getCompilationHooks(compilation).chunkHash.tap(
"RuntimePlugin",
(chunk, hash, { chunkGraph }) => {
for (const m of chunkGraph.getChunkRuntimeModulesInOrder(chunk)) {
hash.update(chunkGraph.getModuleHash(m));
}
}
);
2018-11-17 01:18:44 +08:00
});
}
}
module.exports = RuntimePlugin;