[79a0317] | 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 createSchemaValidation = require("../util/create-schema-validation");
|
---|
| 9 | const memoize = require("../util/memoize");
|
---|
| 10 | const ContainerEntryDependency = require("./ContainerEntryDependency");
|
---|
| 11 | const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory");
|
---|
| 12 | const ContainerExposedDependency = require("./ContainerExposedDependency");
|
---|
| 13 | const { parseOptions } = require("./options");
|
---|
| 14 |
|
---|
| 15 | /** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */
|
---|
| 16 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 17 | /** @typedef {import("./ContainerEntryModule").ExposeOptions} ExposeOptions */
|
---|
| 18 | /** @typedef {import("./ContainerEntryModule").ExposesList} ExposesList */
|
---|
| 19 |
|
---|
| 20 | const getModuleFederationPlugin = memoize(() =>
|
---|
| 21 | require("./ModuleFederationPlugin")
|
---|
| 22 | );
|
---|
| 23 |
|
---|
| 24 | const validate = createSchemaValidation(
|
---|
| 25 | require("../../schemas/plugins/container/ContainerPlugin.check.js"),
|
---|
| 26 | () => require("../../schemas/plugins/container/ContainerPlugin.json"),
|
---|
| 27 | {
|
---|
| 28 | name: "Container Plugin",
|
---|
| 29 | baseDataPath: "options"
|
---|
| 30 | }
|
---|
| 31 | );
|
---|
| 32 |
|
---|
| 33 | const PLUGIN_NAME = "ContainerPlugin";
|
---|
| 34 |
|
---|
| 35 | class ContainerPlugin {
|
---|
| 36 | /**
|
---|
| 37 | * @param {ContainerPluginOptions} options options
|
---|
| 38 | */
|
---|
| 39 | constructor(options) {
|
---|
| 40 | validate(options);
|
---|
| 41 |
|
---|
| 42 | this._options = {
|
---|
| 43 | name: options.name,
|
---|
| 44 | shareScope: options.shareScope || "default",
|
---|
| 45 | library: options.library || {
|
---|
| 46 | type: "var",
|
---|
| 47 | name: options.name
|
---|
| 48 | },
|
---|
| 49 | runtime: options.runtime,
|
---|
| 50 | filename: options.filename || undefined,
|
---|
| 51 | exposes: /** @type {ExposesList} */ (
|
---|
| 52 | parseOptions(
|
---|
| 53 | options.exposes,
|
---|
| 54 | item => ({
|
---|
| 55 | import: Array.isArray(item) ? item : [item],
|
---|
| 56 | name: undefined
|
---|
| 57 | }),
|
---|
| 58 | item => ({
|
---|
| 59 | import: Array.isArray(item.import) ? item.import : [item.import],
|
---|
| 60 | name: item.name || undefined
|
---|
| 61 | })
|
---|
| 62 | )
|
---|
| 63 | )
|
---|
| 64 | };
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Apply the plugin
|
---|
| 69 | * @param {Compiler} compiler the compiler instance
|
---|
| 70 | * @returns {void}
|
---|
| 71 | */
|
---|
| 72 | apply(compiler) {
|
---|
| 73 | const { name, exposes, shareScope, filename, library, runtime } =
|
---|
| 74 | this._options;
|
---|
| 75 |
|
---|
| 76 | if (!compiler.options.output.enabledLibraryTypes.includes(library.type)) {
|
---|
| 77 | compiler.options.output.enabledLibraryTypes.push(library.type);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
|
---|
| 81 | const hooks =
|
---|
| 82 | getModuleFederationPlugin().getCompilationHooks(compilation);
|
---|
| 83 | const dep = new ContainerEntryDependency(name, exposes, shareScope);
|
---|
| 84 | dep.loc = { name };
|
---|
| 85 | compilation.addEntry(
|
---|
| 86 | /** @type {string} */ (compilation.options.context),
|
---|
| 87 | dep,
|
---|
| 88 | {
|
---|
| 89 | name,
|
---|
| 90 | filename,
|
---|
| 91 | runtime,
|
---|
| 92 | library
|
---|
| 93 | },
|
---|
| 94 | error => {
|
---|
| 95 | if (error) return callback(error);
|
---|
| 96 | hooks.addContainerEntryDependency.call(dep);
|
---|
| 97 | callback();
|
---|
| 98 | }
|
---|
| 99 | );
|
---|
| 100 | });
|
---|
| 101 |
|
---|
| 102 | compiler.hooks.thisCompilation.tap(
|
---|
| 103 | PLUGIN_NAME,
|
---|
| 104 | (compilation, { normalModuleFactory }) => {
|
---|
| 105 | compilation.dependencyFactories.set(
|
---|
| 106 | ContainerEntryDependency,
|
---|
| 107 | new ContainerEntryModuleFactory()
|
---|
| 108 | );
|
---|
| 109 |
|
---|
| 110 | compilation.dependencyFactories.set(
|
---|
| 111 | ContainerExposedDependency,
|
---|
| 112 | normalModuleFactory
|
---|
| 113 | );
|
---|
| 114 | }
|
---|
| 115 | );
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | module.exports = ContainerPlugin;
|
---|