[6a3a178] | 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 isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check.js");
|
---|
| 9 | const SharePlugin = require("../sharing/SharePlugin");
|
---|
| 10 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
| 11 | const ContainerPlugin = require("./ContainerPlugin");
|
---|
| 12 | const ContainerReferencePlugin = require("./ContainerReferencePlugin");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
|
---|
| 15 | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
|
---|
| 16 | /** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").Shared} Shared */
|
---|
| 17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 18 |
|
---|
| 19 | const validate = createSchemaValidation(
|
---|
| 20 | require("../../schemas/plugins/container/ModuleFederationPlugin.check.js"),
|
---|
| 21 | () => require("../../schemas/plugins/container/ModuleFederationPlugin.json"),
|
---|
| 22 | {
|
---|
| 23 | name: "Module Federation Plugin",
|
---|
| 24 | baseDataPath: "options"
|
---|
| 25 | }
|
---|
| 26 | );
|
---|
| 27 | class ModuleFederationPlugin {
|
---|
| 28 | /**
|
---|
| 29 | * @param {ModuleFederationPluginOptions} options options
|
---|
| 30 | */
|
---|
| 31 | constructor(options) {
|
---|
| 32 | validate(options);
|
---|
| 33 |
|
---|
| 34 | this._options = options;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Apply the plugin
|
---|
| 39 | * @param {Compiler} compiler the compiler instance
|
---|
| 40 | * @returns {void}
|
---|
| 41 | */
|
---|
| 42 | apply(compiler) {
|
---|
| 43 | const { _options: options } = this;
|
---|
| 44 | const library = options.library || { type: "var", name: options.name };
|
---|
| 45 | const remoteType =
|
---|
| 46 | options.remoteType ||
|
---|
| 47 | (options.library && isValidExternalsType(options.library.type)
|
---|
| 48 | ? /** @type {ExternalsType} */ (options.library.type)
|
---|
| 49 | : "script");
|
---|
| 50 | if (
|
---|
| 51 | library &&
|
---|
| 52 | !compiler.options.output.enabledLibraryTypes.includes(library.type)
|
---|
| 53 | ) {
|
---|
| 54 | compiler.options.output.enabledLibraryTypes.push(library.type);
|
---|
| 55 | }
|
---|
| 56 | compiler.hooks.afterPlugins.tap("ModuleFederationPlugin", () => {
|
---|
| 57 | if (
|
---|
| 58 | options.exposes &&
|
---|
| 59 | (Array.isArray(options.exposes)
|
---|
| 60 | ? options.exposes.length > 0
|
---|
| 61 | : Object.keys(options.exposes).length > 0)
|
---|
| 62 | ) {
|
---|
| 63 | new ContainerPlugin({
|
---|
| 64 | name: options.name,
|
---|
| 65 | library,
|
---|
| 66 | filename: options.filename,
|
---|
| 67 | runtime: options.runtime,
|
---|
| 68 | exposes: options.exposes
|
---|
| 69 | }).apply(compiler);
|
---|
| 70 | }
|
---|
| 71 | if (
|
---|
| 72 | options.remotes &&
|
---|
| 73 | (Array.isArray(options.remotes)
|
---|
| 74 | ? options.remotes.length > 0
|
---|
| 75 | : Object.keys(options.remotes).length > 0)
|
---|
| 76 | ) {
|
---|
| 77 | new ContainerReferencePlugin({
|
---|
| 78 | remoteType,
|
---|
| 79 | remotes: options.remotes
|
---|
| 80 | }).apply(compiler);
|
---|
| 81 | }
|
---|
| 82 | if (options.shared) {
|
---|
| 83 | new SharePlugin({
|
---|
| 84 | shared: options.shared,
|
---|
| 85 | shareScope: options.shareScope
|
---|
| 86 | }).apply(compiler);
|
---|
| 87 | }
|
---|
| 88 | });
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | module.exports = ModuleFederationPlugin;
|
---|