SharePlugin.js 2.9 KB

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