webpack/lib/container/ContainerReferencePlugin.js

140 lines
4.0 KiB
JavaScript
Raw Normal View History

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";
const ExternalsPlugin = require("../ExternalsPlugin");
const RuntimeGlobals = require("../RuntimeGlobals");
const createSchemaValidation = require("../util/create-schema-validation");
const FallbackDependency = require("./FallbackDependency");
const FallbackItemDependency = require("./FallbackItemDependency");
const FallbackModuleFactory = require("./FallbackModuleFactory");
2020-02-25 04:18:14 +08:00
const RemoteModule = require("./RemoteModule");
const RemoteRuntimeModule = require("./RemoteRuntimeModule");
const RemoteToExternalDependency = require("./RemoteToExternalDependency");
const { parseOptions } = require("./options");
2020-02-25 04:18:14 +08:00
/** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").ContainerReferencePluginOptions} ContainerReferencePluginOptions */
2020-02-25 04:18:14 +08:00
/** @typedef {import("../Compiler")} Compiler */
const validate = createSchemaValidation(
require("../../schemas/plugins/container/ContainerReferencePlugin.check"),
() =>
require("../../schemas/plugins/container/ContainerReferencePlugin.json"),
{
name: "Container Reference Plugin",
baseDataPath: "options"
}
);
const slashCode = "/".charCodeAt(0);
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "ContainerReferencePlugin";
class ContainerReferencePlugin {
/**
* @param {ContainerReferencePluginOptions} options options
*/
2020-02-25 04:18:14 +08:00
constructor(options) {
validate(options);
2020-02-25 04:18:14 +08:00
this._remoteType = options.remoteType;
this._remotes = parseOptions(
options.remotes,
(item) => ({
external: Array.isArray(item) ? item : [item],
shareScope: options.shareScope || "default"
}),
(item) => ({
external: Array.isArray(item.external)
? item.external
: [item.external],
shareScope: item.shareScope || options.shareScope || "default"
})
);
2020-02-25 04:18:14 +08:00
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
2020-02-25 04:18:14 +08:00
* @returns {void}
*/
apply(compiler) {
const { _remotes: remotes, _remoteType: remoteType } = this;
2020-02-25 04:18:14 +08:00
/** @type {Record<string, string>} */
2020-02-25 04:18:14 +08:00
const remoteExternals = {};
for (const [key, config] of remotes) {
let i = 0;
for (const external of config.external) {
if (external.startsWith("internal ")) continue;
remoteExternals[
`webpack/container/reference/${key}${i ? `/fallback-${i}` : ""}`
] = external;
i++;
}
2020-02-25 04:18:14 +08:00
}
new ExternalsPlugin(remoteType, remoteExternals).apply(compiler);
2020-02-25 04:18:14 +08:00
compiler.hooks.compilation.tap(
2025-04-23 20:03:37 +08:00
PLUGIN_NAME,
2020-02-25 04:18:14 +08:00
(compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(
RemoteToExternalDependency,
normalModuleFactory
);
2020-02-27 06:27:56 +08:00
compilation.dependencyFactories.set(
FallbackItemDependency,
2020-02-27 06:27:56 +08:00
normalModuleFactory
);
compilation.dependencyFactories.set(
FallbackDependency,
new FallbackModuleFactory()
);
normalModuleFactory.hooks.factorize.tap(PLUGIN_NAME, (data) => {
2025-04-23 20:03:37 +08:00
if (!data.request.includes("!")) {
for (const [key, config] of remotes) {
if (
data.request.startsWith(`${key}`) &&
(data.request.length === key.length ||
data.request.charCodeAt(key.length) === slashCode)
) {
return new RemoteModule(
data.request,
config.external.map((external, i) =>
external.startsWith("internal ")
? external.slice(9)
: `webpack/container/reference/${key}${
i ? `/fallback-${i}` : ""
}`
),
`.${data.request.slice(key.length)}`,
config.shareScope
);
2020-02-25 04:18:14 +08:00
}
}
}
2025-04-23 20:03:37 +08:00
});
2020-02-25 04:18:14 +08:00
2020-02-27 05:32:48 +08:00
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.ensureChunkHandlers)
2025-04-23 20:03:37 +08:00
.tap(PLUGIN_NAME, (chunk, set) => {
2020-02-25 04:18:14 +08:00
set.add(RuntimeGlobals.module);
2020-05-27 18:02:43 +08:00
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
2020-02-25 04:18:14 +08:00
set.add(RuntimeGlobals.hasOwnProperty);
set.add(RuntimeGlobals.initializeSharing);
set.add(RuntimeGlobals.shareScopeMap);
2020-02-25 04:18:14 +08:00
compilation.addRuntimeModule(chunk, new RemoteRuntimeModule());
2020-02-27 05:32:48 +08:00
});
2020-02-25 04:18:14 +08:00
}
);
}
}
module.exports = ContainerReferencePlugin;