| | |
| | | |
| | | "use strict"; |
| | | |
| | | const ExternalModule = require("../ExternalModule"); |
| | | 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"); |
| | |
| | | /** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").ContainerReferencePluginOptions} ContainerReferencePluginOptions */ |
| | | /** @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); |
| | | const PLUGIN_NAME = "ContainerReferencePlugin"; |
| | | |
| | | class ContainerReferencePlugin { |
| | | /** |
| | | * Creates an instance of ContainerReferencePlugin. |
| | | * @param {ContainerReferencePluginOptions} options options |
| | | */ |
| | | constructor(options) { |
| | | validate(options); |
| | | /** @typedef {ContainerReferencePluginOptions} */ |
| | | this.options = options; |
| | | } |
| | | |
| | | this._remoteType = options.remoteType; |
| | | this._remotes = parseOptions( |
| | | options.remotes, |
| | | /** |
| | | * Applies the plugin by registering its hooks on the compiler. |
| | | * @param {Compiler} compiler the compiler instance |
| | | * @returns {void} |
| | | */ |
| | | apply(compiler) { |
| | | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| | | compiler.validate( |
| | | () => |
| | | require("../../schemas/plugins/container/ContainerReferencePlugin.json"), |
| | | this.options, |
| | | { |
| | | name: "Container Reference Plugin", |
| | | baseDataPath: "options" |
| | | }, |
| | | (options) => |
| | | require("../../schemas/plugins/container/ContainerReferencePlugin.check")( |
| | | options |
| | | ) |
| | | ); |
| | | }); |
| | | |
| | | const { remoteType } = this.options; |
| | | const remotes = parseOptions( |
| | | this.options.remotes, |
| | | (item) => ({ |
| | | external: Array.isArray(item) ? item : [item], |
| | | shareScope: options.shareScope || "default" |
| | | shareScope: this.options.shareScope || "default" |
| | | }), |
| | | (item) => ({ |
| | | external: Array.isArray(item.external) |
| | | ? item.external |
| | | : [item.external], |
| | | shareScope: item.shareScope || options.shareScope || "default" |
| | | shareScope: item.shareScope || this.options.shareScope || "default" |
| | | }) |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Apply the plugin |
| | | * @param {Compiler} compiler the compiler instance |
| | | * @returns {void} |
| | | */ |
| | | apply(compiler) { |
| | | const { _remotes: remotes, _remoteType: remoteType } = this; |
| | | |
| | | /** @type {Record<string, string>} */ |
| | | const remoteExternals = {}; |
| | |
| | | set.add(RuntimeGlobals.shareScopeMap); |
| | | compilation.addRuntimeModule(chunk, new RemoteRuntimeModule()); |
| | | }); |
| | | |
| | | const { chunkCondition } = |
| | | ExternalModule.getCompilationHooks(compilation); |
| | | |
| | | // External modules issued by remote modules should be placed in entry chunks |
| | | // to ensure they are loaded and initialize first |
| | | chunkCondition.tap( |
| | | PLUGIN_NAME, |
| | | (chunk, compilation) => |
| | | compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0 |
| | | ); |
| | | } |
| | | ); |
| | | } |