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");
|
2018-11-20 17:20:20 +08:00
|
|
|
const Template = require("./Template");
|
2018-11-20 17:33:02 +08:00
|
|
|
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
|
2018-11-18 19:59:33 +08:00
|
|
|
const DefinePropertyGetterRuntimeModule = require("./runtime/DefinePropertyGetterRuntimeModule");
|
2018-11-20 17:22:10 +08:00
|
|
|
const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
|
2018-11-20 17:20:20 +08:00
|
|
|
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 NEED_REQUIRE = [
|
|
|
|
RuntimeGlobals.chunkName,
|
|
|
|
RuntimeGlobals.compatGetDefaultExport,
|
|
|
|
RuntimeGlobals.createFakeNamespaceObject,
|
|
|
|
RuntimeGlobals.definePropertyGetter,
|
|
|
|
RuntimeGlobals.ensureChunk,
|
|
|
|
RuntimeGlobals.entryModuleId,
|
|
|
|
RuntimeGlobals.getFullHash,
|
|
|
|
RuntimeGlobals.hasOwnProperty,
|
|
|
|
RuntimeGlobals.makeNamespaceObject,
|
|
|
|
RuntimeGlobals.moduleCache,
|
|
|
|
RuntimeGlobals.moduleFactories,
|
|
|
|
RuntimeGlobals.publicPath,
|
|
|
|
RuntimeGlobals.scriptNonce,
|
|
|
|
RuntimeGlobals.uncaughtErrorHandler,
|
|
|
|
RuntimeGlobals.wasmInstances
|
|
|
|
];
|
|
|
|
|
|
|
|
class RuntimePlugin {
|
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the Compiler
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
|
|
|
|
for (const req of NEED_REQUIRE) {
|
|
|
|
compilation.hooks.runtimeRequirementInModule
|
|
|
|
.for(req)
|
|
|
|
.tap("RuntimePlugin", (module, set) => {
|
|
|
|
set.add(RuntimeGlobals.require);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
compilation.hooks.runtimeRequirementInModule
|
2018-11-18 19:59:33 +08:00
|
|
|
.for(RuntimeGlobals.createFakeNamespaceObject)
|
2018-11-17 01:18:44 +08:00
|
|
|
.tap("RuntimePlugin", (module, set) => {
|
|
|
|
set.add(RuntimeGlobals.definePropertyGetter);
|
2018-11-20 17:22:10 +08:00
|
|
|
set.add(RuntimeGlobals.makeNamespaceObject);
|
2018-11-17 01:18:44 +08:00
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInModule
|
|
|
|
.for(RuntimeGlobals.compatGetDefaultExport)
|
|
|
|
.tap("RuntimePlugin", (module, set) => {
|
|
|
|
set.add(RuntimeGlobals.definePropertyGetter);
|
|
|
|
});
|
2018-11-18 19:59:33 +08:00
|
|
|
compilation.hooks.runtimeRequirementInModule
|
|
|
|
.for(RuntimeGlobals.definePropertyGetter)
|
|
|
|
.tap("RuntimePlugin", (module, set) => {
|
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
});
|
2018-11-20 17:33:02 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.createFakeNamespaceObject)
|
|
|
|
.tap("RuntimePlugin", chunk => {
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
|
|
|
new CreateFakeNamespaceObjectRuntimeModule()
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
});
|
2018-11-20 17:20:20 +08:00
|
|
|
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;
|