webpack/lib/RuntimePlugin.js

132 lines
4.2 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 Template = require("./Template");
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
const DefinePropertyGetterRuntimeModule = require("./runtime/DefinePropertyGetterRuntimeModule");
2018-11-20 17:22:10 +08:00
const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
const { compareIds } = require("./util/comparators");
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.definePropertyGetter
],
[RuntimeGlobals.createFakeNamespaceObject]: [
RuntimeGlobals.require,
RuntimeGlobals.definePropertyGetter,
RuntimeGlobals.makeNamespaceObject
],
[RuntimeGlobals.definePropertyGetter]: [RuntimeGlobals.require],
[RuntimeGlobals.ensureChunk]: [
RuntimeGlobals.require,
RuntimeGlobals.publicPath
],
[RuntimeGlobals.entryModuleId]: [RuntimeGlobals.require],
[RuntimeGlobals.getFullHash]: [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]
};
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.definePropertyGetter)
.tap("RuntimePlugin", chunk => {
compilation.addRuntimeModule(
chunk,
new DefinePropertyGetterRuntimeModule()
);
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;
});
compilation.mainTemplate.hooks.runtime.tap(
"RuntimePlugin",
(source, { chunk, chunkGraph }) => {
const buf = [];
if (chunkGraph.getNumberOfRuntimeModules(chunk) > 0) {
buf.push("// Bootstrap all runtime modules");
const runtimeModules = Array.from(
chunkGraph.getChunkRuntimeModulesIterable(chunk),
m => chunkGraph.getModuleId(m)
);
runtimeModules.sort(compareIds);
for (const moduleId of runtimeModules) {
buf.push(
`modules[${JSON.stringify(moduleId)}](0,0,__webpack_require__);`
);
}
}
return Template.asString(buf);
}
);
2018-11-17 01:18:44 +08:00
});
}
}
module.exports = RuntimePlugin;