SharePlugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { parseOptions } = require("../container/options");
  7. const ConsumeSharedPlugin = require("./ConsumeSharedPlugin");
  8. const ProvideSharedPlugin = require("./ProvideSharedPlugin");
  9. const { isRequiredVersion } = require("./utils");
  10. /**
  11. * @import {
  12. * ConsumesConfig
  13. * } from "../../declarations/plugins/sharing/ConsumeSharedPlugin"
  14. */
  15. /**
  16. * @import {
  17. * ProvidesConfig
  18. * } from "../../declarations/plugins/sharing/ProvideSharedPlugin"
  19. */
  20. /**
  21. * @import {
  22. * SharePluginOptions,
  23. * SharedConfig
  24. * } from "../../declarations/plugins/sharing/SharePlugin"
  25. */
  26. /** @import Compiler from "../Compiler" */
  27. class SharePlugin {
  28. /**
  29. * Creates an instance of SharePlugin.
  30. * @param {SharePluginOptions} options options
  31. */
  32. constructor(options) {
  33. /** @type {[string, SharedConfig][]} */
  34. const sharedOptions = parseOptions(
  35. options.shared,
  36. (item, key) => {
  37. if (typeof item !== "string") {
  38. throw new Error("Unexpected array in shared");
  39. }
  40. /** @type {SharedConfig} */
  41. const config =
  42. item === key || !isRequiredVersion(item)
  43. ? {
  44. import: item
  45. }
  46. : {
  47. import: key,
  48. requiredVersion: item
  49. };
  50. return config;
  51. },
  52. (item) => item
  53. );
  54. /** @type {Record<string, ConsumesConfig>[]} */
  55. const consumes = sharedOptions.map(([key, options]) => ({
  56. [key]: {
  57. import: options.import,
  58. shareKey: options.shareKey || key,
  59. shareScope: options.shareScope,
  60. requiredVersion: options.requiredVersion,
  61. strictVersion: options.strictVersion,
  62. singleton: options.singleton,
  63. packageName: options.packageName,
  64. eager: options.eager
  65. }
  66. }));
  67. /** @type {Record<string, ProvidesConfig>[]} */
  68. const provides = sharedOptions
  69. .filter(([, options]) => options.import !== false)
  70. .map(([key, options]) => ({
  71. [options.import || key]: {
  72. shareKey: options.shareKey || key,
  73. shareScope: options.shareScope,
  74. version: options.version,
  75. eager: options.eager
  76. }
  77. }));
  78. /** @type {string | undefined} */
  79. this._shareScope = options.shareScope;
  80. /** @type {Record<string, ConsumesConfig>[]} */
  81. this._consumes = consumes;
  82. /** @type {Record<string, ProvidesConfig>[]} */
  83. this._provides = provides;
  84. }
  85. /**
  86. * Applies the plugin by registering its hooks on the compiler.
  87. * @param {Compiler} compiler the compiler instance
  88. * @returns {void}
  89. */
  90. apply(compiler) {
  91. new ConsumeSharedPlugin({
  92. shareScope: this._shareScope,
  93. consumes: this._consumes
  94. }).apply(compiler);
  95. new ProvideSharedPlugin({
  96. shareScope: this._shareScope,
  97. provides: this._provides
  98. }).apply(compiler);
  99. }
  100. }
  101. module.exports = SharePlugin;