| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { SyncHook } = require("tapable");
|
|---|
| 9 | const isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check");
|
|---|
| 10 | const Compilation = require("../Compilation");
|
|---|
| 11 | const SharePlugin = require("../sharing/SharePlugin");
|
|---|
| 12 | const ContainerPlugin = require("./ContainerPlugin");
|
|---|
| 13 | const ContainerReferencePlugin = require("./ContainerReferencePlugin");
|
|---|
| 14 | const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
|
|---|
| 17 | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
|
|---|
| 18 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 19 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Defines the compilation hooks type used by this module.
|
|---|
| 23 | * @typedef {object} CompilationHooks
|
|---|
| 24 | * @property {SyncHook<Dependency>} addContainerEntryDependency
|
|---|
| 25 | * @property {SyncHook<Dependency>} addFederationRuntimeDependency
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | /** @type {WeakMap<Compilation, CompilationHooks>} */
|
|---|
| 29 | const compilationHooksMap = new WeakMap();
|
|---|
| 30 | const PLUGIN_NAME = "ModuleFederationPlugin";
|
|---|
| 31 |
|
|---|
| 32 | class ModuleFederationPlugin {
|
|---|
| 33 | /**
|
|---|
| 34 | * Creates an instance of ModuleFederationPlugin.
|
|---|
| 35 | * @param {ModuleFederationPluginOptions} options options
|
|---|
| 36 | */
|
|---|
| 37 | constructor(options) {
|
|---|
| 38 | /** @type {ModuleFederationPluginOptions} */
|
|---|
| 39 | this.options = options;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Get the compilation hooks associated with this plugin.
|
|---|
| 44 | * @param {Compilation} compilation The compilation instance.
|
|---|
| 45 | * @returns {CompilationHooks} The hooks for the compilation.
|
|---|
| 46 | */
|
|---|
| 47 | static getCompilationHooks(compilation) {
|
|---|
| 48 | if (!(compilation instanceof Compilation)) {
|
|---|
| 49 | throw new TypeError(
|
|---|
| 50 | "The 'compilation' argument must be an instance of Compilation"
|
|---|
| 51 | );
|
|---|
| 52 | }
|
|---|
| 53 | let hooks = compilationHooksMap.get(compilation);
|
|---|
| 54 | if (!hooks) {
|
|---|
| 55 | hooks = {
|
|---|
| 56 | addContainerEntryDependency: new SyncHook(["dependency"]),
|
|---|
| 57 | addFederationRuntimeDependency: new SyncHook(["dependency"])
|
|---|
| 58 | };
|
|---|
| 59 | compilationHooksMap.set(compilation, hooks);
|
|---|
| 60 | }
|
|---|
| 61 | return hooks;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 66 | * @param {Compiler} compiler the compiler instance
|
|---|
| 67 | * @returns {void}
|
|---|
| 68 | */
|
|---|
| 69 | apply(compiler) {
|
|---|
| 70 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 71 | compiler.validate(
|
|---|
| 72 | () =>
|
|---|
| 73 | require("../../schemas/plugins/container/ModuleFederationPlugin.json"),
|
|---|
| 74 | this.options,
|
|---|
| 75 | {
|
|---|
| 76 | name: "Module Federation Plugin",
|
|---|
| 77 | baseDataPath: "options"
|
|---|
| 78 | },
|
|---|
| 79 | (options) =>
|
|---|
| 80 | require("../../schemas/plugins/container/ModuleFederationPlugin.check")(
|
|---|
| 81 | options
|
|---|
| 82 | )
|
|---|
| 83 | );
|
|---|
| 84 | });
|
|---|
| 85 | const { options } = this;
|
|---|
| 86 | const library = options.library || { type: "var", name: options.name };
|
|---|
| 87 | const remoteType =
|
|---|
| 88 | options.remoteType ||
|
|---|
| 89 | (options.library && isValidExternalsType(options.library.type)
|
|---|
| 90 | ? /** @type {ExternalsType} */ (options.library.type)
|
|---|
| 91 | : "script");
|
|---|
| 92 | if (
|
|---|
| 93 | library &&
|
|---|
| 94 | !compiler.options.output.enabledLibraryTypes.includes(library.type)
|
|---|
| 95 | ) {
|
|---|
| 96 | compiler.options.output.enabledLibraryTypes.push(library.type);
|
|---|
| 97 | }
|
|---|
| 98 | compiler.hooks.afterPlugins.tap(PLUGIN_NAME, () => {
|
|---|
| 99 | if (
|
|---|
| 100 | options.exposes &&
|
|---|
| 101 | (Array.isArray(options.exposes)
|
|---|
| 102 | ? options.exposes.length > 0
|
|---|
| 103 | : Object.keys(options.exposes).length > 0)
|
|---|
| 104 | ) {
|
|---|
| 105 | new ContainerPlugin({
|
|---|
| 106 | name: /** @type {string} */ (options.name),
|
|---|
| 107 | library,
|
|---|
| 108 | filename: options.filename,
|
|---|
| 109 | runtime: options.runtime,
|
|---|
| 110 | shareScope: options.shareScope,
|
|---|
| 111 | exposes: options.exposes
|
|---|
| 112 | }).apply(compiler);
|
|---|
| 113 | }
|
|---|
| 114 | if (
|
|---|
| 115 | options.remotes &&
|
|---|
| 116 | (Array.isArray(options.remotes)
|
|---|
| 117 | ? options.remotes.length > 0
|
|---|
| 118 | : Object.keys(options.remotes).length > 0)
|
|---|
| 119 | ) {
|
|---|
| 120 | new ContainerReferencePlugin({
|
|---|
| 121 | remoteType,
|
|---|
| 122 | shareScope: options.shareScope,
|
|---|
| 123 | remotes: options.remotes
|
|---|
| 124 | }).apply(compiler);
|
|---|
| 125 | }
|
|---|
| 126 | if (options.shared) {
|
|---|
| 127 | new SharePlugin({
|
|---|
| 128 | shared: options.shared,
|
|---|
| 129 | shareScope: options.shareScope
|
|---|
| 130 | }).apply(compiler);
|
|---|
| 131 | }
|
|---|
| 132 | new HoistContainerReferences().apply(compiler);
|
|---|
| 133 | });
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | module.exports = ModuleFederationPlugin;
|
|---|