ModuleFederationPlugin.js 2.8 KB

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