2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
8 const { parseOptions } = require("../container/options");
9 const ConsumeSharedPlugin = require("./ConsumeSharedPlugin");
10 const ProvideSharedPlugin = require("./ProvideSharedPlugin");
11 const { isRequiredVersion } = require("./utils");
13 /** @typedef {import("../../declarations/plugins/sharing/ConsumeSharedPlugin").ConsumeSharedPluginOptions} ConsumeSharedPluginOptions */
14 /** @typedef {import("../../declarations/plugins/sharing/ConsumeSharedPlugin").ConsumesConfig} ConsumesConfig */
15 /** @typedef {import("../../declarations/plugins/sharing/ProvideSharedPlugin").ProvideSharedPluginOptions} ProvideSharedPluginOptions */
16 /** @typedef {import("../../declarations/plugins/sharing/ProvideSharedPlugin").ProvidesConfig} ProvidesConfig */
17 /** @typedef {import("../../declarations/plugins/sharing/SharePlugin").SharePluginOptions} SharePluginOptions */
18 /** @typedef {import("../../declarations/plugins/sharing/SharePlugin").SharedConfig} SharedConfig */
19 /** @typedef {import("../Compiler")} Compiler */
23 * @param {SharePluginOptions} options options
25 constructor(options) {
26 /** @type {[string, SharedConfig][]} */
27 const sharedOptions = parseOptions(
30 if (typeof item !== "string")
31 throw new Error("Unexpected array in shared");
32 /** @type {SharedConfig} */
34 item === key || !isRequiredVersion(item)
46 /** @type {Record<string, ConsumesConfig>[]} */
47 const consumes = sharedOptions.map(([key, options]) => ({
49 import: options.import,
50 shareKey: options.shareKey || key,
51 shareScope: options.shareScope,
52 requiredVersion: options.requiredVersion,
53 strictVersion: options.strictVersion,
54 singleton: options.singleton,
55 packageName: options.packageName,
59 /** @type {Record<string, ProvidesConfig>[]} */
60 const provides = sharedOptions
61 .filter(([, options]) => options.import !== false)
62 .map(([key, options]) => ({
63 [options.import || key]: {
64 shareKey: options.shareKey || key,
65 shareScope: options.shareScope,
66 version: options.version,
70 this._shareScope = options.shareScope;
71 this._consumes = consumes;
72 this._provides = provides;
77 * @param {Compiler} compiler the compiler instance
81 new ConsumeSharedPlugin({
82 shareScope: this._shareScope,
83 consumes: this._consumes
85 new ProvideSharedPlugin({
86 shareScope: this._shareScope,
87 provides: this._provides
92 module.exports = SharePlugin;