webpack/lib/RuntimePlugin.js

208 lines
6.6 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 CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
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");
2018-11-20 17:22:10 +08:00
const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
2018-11-17 01:18:44 +08:00
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Module")} Module */
const DEPENDENCIES = {
[RuntimeGlobals.chunkName]: [RuntimeGlobals.require],
[RuntimeGlobals.compatGetDefaultExport]: [
RuntimeGlobals.require,
RuntimeGlobals.definePropertyGetters
],
[RuntimeGlobals.createFakeNamespaceObject]: [
RuntimeGlobals.require,
RuntimeGlobals.definePropertyGetters,
RuntimeGlobals.makeNamespaceObject
],
[RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.require],
[RuntimeGlobals.ensureChunk]: [RuntimeGlobals.require],
[RuntimeGlobals.entryModuleId]: [RuntimeGlobals.require],
[RuntimeGlobals.getFullHash]: [RuntimeGlobals.require],
2018-12-06 19:11:01 +08:00
[RuntimeGlobals.global]: [RuntimeGlobals.require],
[RuntimeGlobals.makeNamespaceObject]: [RuntimeGlobals.require],
[RuntimeGlobals.moduleCache]: [RuntimeGlobals.require],
[RuntimeGlobals.moduleFactories]: [RuntimeGlobals.require],
[RuntimeGlobals.publicPath]: [RuntimeGlobals.require],
[RuntimeGlobals.scriptNonce]: [RuntimeGlobals.require],
[RuntimeGlobals.uncaughtErrorHandler]: [RuntimeGlobals.require],
[RuntimeGlobals.wasmInstances]: [RuntimeGlobals.require],
[RuntimeGlobals.instantiateWasm]: [RuntimeGlobals.require]
};
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 => {
for (const req of Object.keys(DEPENDENCIES)) {
const deps = DEPENDENCIES[req];
2018-11-17 01:18:44 +08:00
compilation.hooks.runtimeRequirementInModule
.for(req)
.tap("RuntimePlugin", (module, 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;
});
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(compilation)
);
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;
});
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(
compilation,
chunk,
"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(
compilation,
chunk,
"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(
compilation,
"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);
});
2018-11-17 01:18:44 +08:00
});
}
}
module.exports = RuntimePlugin;