ModuleFederationPlugin.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. exposes: options.exposes
  63. }).apply(compiler);
  64. }
  65. if (
  66. options.remotes &&
  67. (Array.isArray(options.remotes)
  68. ? options.remotes.length > 0
  69. : Object.keys(options.remotes).length > 0)
  70. ) {
  71. new ContainerReferencePlugin({
  72. remoteType,
  73. remotes: options.remotes
  74. }).apply(compiler);
  75. }
  76. if (options.shared) {
  77. new SharePlugin({
  78. shared: options.shared,
  79. shareScope: options.shareScope
  80. }).apply(compiler);
  81. }
  82. });
  83. }
  84. }
  85. module.exports = ModuleFederationPlugin;