SharePlugin.js 2.9 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 { 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. /** @type {SharedConfig} */
  29. const config =
  30. item === key || !isRequiredVersion(item)
  31. ? {
  32. import: item
  33. }
  34. : {
  35. import: key,
  36. requiredVersion: item
  37. };
  38. return config;
  39. },
  40. item => item
  41. );
  42. /** @type {Record<string, ConsumesConfig>[]} */
  43. const consumes = sharedOptions.map(([key, options]) => ({
  44. [key]: {
  45. import: options.import,
  46. shareKey: options.shareKey || key,
  47. shareScope: options.shareScope,
  48. requiredVersion: options.requiredVersion,
  49. strictVersion: options.strictVersion,
  50. singleton: options.singleton,
  51. packageName: options.packageName,
  52. eager: options.eager
  53. }
  54. }));
  55. /** @type {Record<string, ProvidesConfig>[]} */
  56. const provides = sharedOptions
  57. .filter(([, options]) => options.import !== false)
  58. .map(([key, options]) => ({
  59. [options.import || key]: {
  60. shareKey: options.shareKey || key,
  61. shareScope: options.shareScope,
  62. version: options.version,
  63. eager: options.eager
  64. }
  65. }));
  66. this._shareScope = options.shareScope;
  67. this._consumes = consumes;
  68. this._provides = provides;
  69. }
  70. /**
  71. * Apply the plugin
  72. * @param {Compiler} compiler the compiler instance
  73. * @returns {void}
  74. */
  75. apply(compiler) {
  76. new ConsumeSharedPlugin({
  77. shareScope: this._shareScope,
  78. consumes: this._consumes
  79. }).apply(compiler);
  80. new ProvideSharedPlugin({
  81. shareScope: this._shareScope,
  82. provides: this._provides
  83. }).apply(compiler);
  84. }
  85. }
  86. module.exports = SharePlugin;