gameStatusUpdate/node_modules/webpack/lib/container/ModuleFederationPlugin.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-09-18 08:27:46 -07:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
*/
"use strict";
2024-11-04 11:06:36 -08:00
const { SyncHook } = require("tapable");
2024-09-18 08:27:46 -07:00
const isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check.js");
2024-11-04 11:06:36 -08:00
const Compilation = require("../Compilation");
2024-09-18 08:27:46 -07:00
const SharePlugin = require("../sharing/SharePlugin");
const createSchemaValidation = require("../util/create-schema-validation");
const ContainerPlugin = require("./ContainerPlugin");
const ContainerReferencePlugin = require("./ContainerReferencePlugin");
2024-11-04 11:06:36 -08:00
const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
2024-09-18 08:27:46 -07:00
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").Shared} Shared */
/** @typedef {import("../Compiler")} Compiler */
2024-11-04 11:06:36 -08:00
/** @typedef {import("../Dependency")} Dependency */
/**
* @typedef {object} CompilationHooks
* @property {SyncHook<Dependency>} addContainerEntryDependency
* @property {SyncHook<Dependency>} addFederationRuntimeDependency
*/
2024-09-18 08:27:46 -07:00
const validate = createSchemaValidation(
require("../../schemas/plugins/container/ModuleFederationPlugin.check.js"),
() => require("../../schemas/plugins/container/ModuleFederationPlugin.json"),
{
name: "Module Federation Plugin",
baseDataPath: "options"
}
);
2024-11-04 11:06:36 -08:00
/** @type {WeakMap<Compilation, CompilationHooks>} */
const compilationHooksMap = new WeakMap();
2024-09-18 08:27:46 -07:00
class ModuleFederationPlugin {
/**
* @param {ModuleFederationPluginOptions} options options
*/
constructor(options) {
validate(options);
this._options = options;
}
2024-11-04 11:06:36 -08:00
/**
* Get the compilation hooks associated with this plugin.
* @param {Compilation} compilation The compilation instance.
* @returns {CompilationHooks} The hooks for the compilation.
*/
static getCompilationHooks(compilation) {
if (!(compilation instanceof Compilation)) {
throw new TypeError(
"The 'compilation' argument must be an instance of Compilation"
);
}
let hooks = compilationHooksMap.get(compilation);
if (!hooks) {
hooks = {
addContainerEntryDependency: new SyncHook(["dependency"]),
addFederationRuntimeDependency: new SyncHook(["dependency"])
};
compilationHooksMap.set(compilation, hooks);
}
return hooks;
}
2024-09-18 08:27:46 -07:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const { _options: options } = this;
const library = options.library || { type: "var", name: options.name };
const remoteType =
options.remoteType ||
(options.library && isValidExternalsType(options.library.type)
? /** @type {ExternalsType} */ (options.library.type)
: "script");
if (
library &&
!compiler.options.output.enabledLibraryTypes.includes(library.type)
) {
compiler.options.output.enabledLibraryTypes.push(library.type);
}
compiler.hooks.afterPlugins.tap("ModuleFederationPlugin", () => {
if (
options.exposes &&
(Array.isArray(options.exposes)
? options.exposes.length > 0
: Object.keys(options.exposes).length > 0)
) {
new ContainerPlugin({
2024-11-04 11:06:36 -08:00
name: /** @type {string} */ (options.name),
2024-09-18 08:27:46 -07:00
library,
filename: options.filename,
runtime: options.runtime,
shareScope: options.shareScope,
exposes: options.exposes
}).apply(compiler);
}
if (
options.remotes &&
(Array.isArray(options.remotes)
? options.remotes.length > 0
: Object.keys(options.remotes).length > 0)
) {
new ContainerReferencePlugin({
remoteType,
shareScope: options.shareScope,
remotes: options.remotes
}).apply(compiler);
}
if (options.shared) {
new SharePlugin({
shared: options.shared,
shareScope: options.shareScope
}).apply(compiler);
}
2024-11-04 11:06:36 -08:00
new HoistContainerReferences().apply(compiler);
2024-09-18 08:27:46 -07:00
});
}
}
module.exports = ModuleFederationPlugin;