2020-02-25 04:18:14 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-02-27 04:14:49 +08:00
|
|
|
const validateOptions = require("schema-utils");
|
2020-04-30 19:39:58 +08:00
|
|
|
const schema = require("../../schemas/plugins/container/ContainerReferencePlugin.json");
|
2020-02-25 04:18:14 +08:00
|
|
|
const ExternalsPlugin = require("../ExternalsPlugin");
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RemoteModule = require("./RemoteModule");
|
2020-02-27 06:27:56 +08:00
|
|
|
const RemoteOverrideDependency = require("./RemoteOverrideDependency");
|
2020-05-04 21:00:23 +08:00
|
|
|
const RemoteOverridesDependency = require("./RemoteOverridesDependency");
|
|
|
|
const RemoteOverridesModuleFactory = require("./RemoteOverridesModuleFactory");
|
2020-02-25 04:18:14 +08:00
|
|
|
const RemoteRuntimeModule = require("./RemoteRuntimeModule");
|
|
|
|
const RemoteToExternalDependency = require("./RemoteToExternalDependency");
|
|
|
|
const parseOptions = require("./parseOptions");
|
|
|
|
|
2020-04-30 19:39:58 +08:00
|
|
|
/** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").ContainerReferencePluginOptions} ContainerReferencePluginOptions */
|
2020-02-25 04:18:14 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
|
|
|
module.exports = class ContainerReferencePlugin {
|
2020-04-30 19:39:58 +08:00
|
|
|
/**
|
|
|
|
* @param {ContainerReferencePluginOptions} options options
|
|
|
|
*/
|
2020-02-25 04:18:14 +08:00
|
|
|
constructor(options) {
|
2020-04-30 19:39:58 +08:00
|
|
|
validateOptions(schema, options, { name: "Container Reference Plugin" });
|
2020-02-25 04:18:14 +08:00
|
|
|
|
2020-04-30 19:39:58 +08:00
|
|
|
this._remoteType = options.remoteType;
|
|
|
|
this._remotes = parseOptions(options.remotes || []);
|
2020-05-04 21:00:23 +08:00
|
|
|
this._overrides = parseOptions(options.overrides || {}).sort(([a], [b]) => {
|
|
|
|
if (a < b) return -1;
|
|
|
|
if (b < a) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2020-02-25 04:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-30 19:39:58 +08:00
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
2020-02-25 04:18:14 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
2020-04-30 19:39:58 +08:00
|
|
|
const { _remotes: remotes, _remoteType: remoteType } = this;
|
2020-02-25 04:18:14 +08:00
|
|
|
|
|
|
|
const remoteExternals = {};
|
|
|
|
for (const [key, value] of remotes) {
|
2020-05-05 16:43:18 +08:00
|
|
|
remoteExternals[`webpack/container/reference/${key}`] = value;
|
2020-02-25 04:18:14 +08:00
|
|
|
}
|
|
|
|
|
2020-04-30 19:39:58 +08:00
|
|
|
new ExternalsPlugin(remoteType, remoteExternals).apply(compiler);
|
2020-02-25 04:18:14 +08:00
|
|
|
|
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"ContainerReferencePlugin",
|
|
|
|
(compilation, { normalModuleFactory }) => {
|
|
|
|
compilation.dependencyFactories.set(
|
|
|
|
RemoteToExternalDependency,
|
|
|
|
normalModuleFactory
|
|
|
|
);
|
|
|
|
|
2020-02-27 06:27:56 +08:00
|
|
|
compilation.dependencyFactories.set(
|
|
|
|
RemoteOverrideDependency,
|
|
|
|
normalModuleFactory
|
|
|
|
);
|
|
|
|
|
2020-02-27 00:16:01 +08:00
|
|
|
compilation.dependencyFactories.set(
|
2020-05-04 21:00:23 +08:00
|
|
|
RemoteOverridesDependency,
|
|
|
|
new RemoteOverridesModuleFactory()
|
2020-02-27 00:16:01 +08:00
|
|
|
);
|
|
|
|
|
2020-02-25 04:18:14 +08:00
|
|
|
normalModuleFactory.hooks.factorize.tap(
|
|
|
|
"ContainerReferencePlugin",
|
|
|
|
data => {
|
|
|
|
if (!data.request.includes("!")) {
|
|
|
|
for (const [key] of remotes) {
|
|
|
|
if (data.request.startsWith(`${key}/`)) {
|
|
|
|
return new RemoteModule(
|
|
|
|
data.request,
|
2020-04-30 19:39:58 +08:00
|
|
|
this._overrides,
|
2020-05-05 16:43:18 +08:00
|
|
|
`webpack/container/reference/${key}`,
|
2020-02-25 04:18:14 +08:00
|
|
|
data.request.slice(key.length + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-27 05:32:48 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
|
|
|
.tap("OverridablesPlugin", (chunk, set) => {
|
2020-02-25 04:18:14 +08:00
|
|
|
set.add(RuntimeGlobals.module);
|
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
|
|
|
compilation.addRuntimeModule(chunk, new RemoteRuntimeModule());
|
2020-02-27 05:32:48 +08:00
|
|
|
});
|
2020-02-25 04:18:14 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|