| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const memoize = require("../util/memoize");
|
|---|
| 9 | const ContainerEntryDependency = require("./ContainerEntryDependency");
|
|---|
| 10 | const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory");
|
|---|
| 11 | const ContainerExposedDependency = require("./ContainerExposedDependency");
|
|---|
| 12 | const { parseOptions } = require("./options");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */
|
|---|
| 15 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 16 | /** @typedef {import("./ContainerEntryModule").ExposesList} ExposesList */
|
|---|
| 17 |
|
|---|
| 18 | const getModuleFederationPlugin = memoize(() =>
|
|---|
| 19 | require("./ModuleFederationPlugin")
|
|---|
| 20 | );
|
|---|
| 21 |
|
|---|
| 22 | const PLUGIN_NAME = "ContainerPlugin";
|
|---|
| 23 |
|
|---|
| 24 | class ContainerPlugin {
|
|---|
| 25 | /**
|
|---|
| 26 | * Creates an instance of ContainerPlugin.
|
|---|
| 27 | * @param {ContainerPluginOptions} options options
|
|---|
| 28 | */
|
|---|
| 29 | constructor(options) {
|
|---|
| 30 | /** @type {ContainerPluginOptions} */
|
|---|
| 31 | this.options = options;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 36 | * @param {Compiler} compiler the compiler instance
|
|---|
| 37 | * @returns {void}
|
|---|
| 38 | */
|
|---|
| 39 | apply(compiler) {
|
|---|
| 40 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 41 | compiler.validate(
|
|---|
| 42 | () => require("../../schemas/plugins/container/ContainerPlugin.json"),
|
|---|
| 43 | this.options,
|
|---|
| 44 | {
|
|---|
| 45 | name: "Container Plugin",
|
|---|
| 46 | baseDataPath: "options"
|
|---|
| 47 | },
|
|---|
| 48 | (options) =>
|
|---|
| 49 | require("../../schemas/plugins/container/ContainerPlugin.check")(
|
|---|
| 50 | options
|
|---|
| 51 | )
|
|---|
| 52 | );
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | const library = this.options.library || {
|
|---|
| 56 | type: "var",
|
|---|
| 57 | name: this.options.name
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | if (!compiler.options.output.enabledLibraryTypes.includes(library.type)) {
|
|---|
| 61 | compiler.options.output.enabledLibraryTypes.push(library.type);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | const exposes = /** @type {ExposesList} */ (
|
|---|
| 65 | parseOptions(
|
|---|
| 66 | this.options.exposes,
|
|---|
| 67 | (item) => ({
|
|---|
| 68 | import: Array.isArray(item) ? item : [item],
|
|---|
| 69 | name: undefined
|
|---|
| 70 | }),
|
|---|
| 71 | (item) => ({
|
|---|
| 72 | import: Array.isArray(item.import) ? item.import : [item.import],
|
|---|
| 73 | name: item.name || undefined
|
|---|
| 74 | })
|
|---|
| 75 | )
|
|---|
| 76 | );
|
|---|
| 77 |
|
|---|
| 78 | const shareScope = this.options.shareScope || "default";
|
|---|
| 79 |
|
|---|
| 80 | compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
|
|---|
| 81 | const hooks =
|
|---|
| 82 | getModuleFederationPlugin().getCompilationHooks(compilation);
|
|---|
| 83 | const dep = new ContainerEntryDependency(
|
|---|
| 84 | this.options.name,
|
|---|
| 85 | exposes,
|
|---|
| 86 | shareScope
|
|---|
| 87 | );
|
|---|
| 88 | dep.loc = { name: this.options.name };
|
|---|
| 89 | compilation.addEntry(
|
|---|
| 90 | compilation.options.context,
|
|---|
| 91 | dep,
|
|---|
| 92 | {
|
|---|
| 93 | name: this.options.name,
|
|---|
| 94 | filename: this.options.filename,
|
|---|
| 95 | runtime: this.options.runtime,
|
|---|
| 96 | library
|
|---|
| 97 | },
|
|---|
| 98 | (error) => {
|
|---|
| 99 | if (error) return callback(error);
|
|---|
| 100 | hooks.addContainerEntryDependency.call(dep);
|
|---|
| 101 | callback();
|
|---|
| 102 | }
|
|---|
| 103 | );
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | compiler.hooks.thisCompilation.tap(
|
|---|
| 107 | PLUGIN_NAME,
|
|---|
| 108 | (compilation, { normalModuleFactory }) => {
|
|---|
| 109 | compilation.dependencyFactories.set(
|
|---|
| 110 | ContainerEntryDependency,
|
|---|
| 111 | new ContainerEntryModuleFactory()
|
|---|
| 112 | );
|
|---|
| 113 |
|
|---|
| 114 | compilation.dependencyFactories.set(
|
|---|
| 115 | ContainerExposedDependency,
|
|---|
| 116 | normalModuleFactory
|
|---|
| 117 | );
|
|---|
| 118 | }
|
|---|
| 119 | );
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | module.exports = ContainerPlugin;
|
|---|