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