[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { parseOptions } = require("../container/options");
|
---|
| 9 | const ConsumeSharedPlugin = require("./ConsumeSharedPlugin");
|
---|
| 10 | const ProvideSharedPlugin = require("./ProvideSharedPlugin");
|
---|
| 11 | const { isRequiredVersion } = require("./utils");
|
---|
| 12 |
|
---|
| 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 */
|
---|
| 20 |
|
---|
| 21 | class SharePlugin {
|
---|
| 22 | /**
|
---|
| 23 | * @param {SharePluginOptions} options options
|
---|
| 24 | */
|
---|
| 25 | constructor(options) {
|
---|
| 26 | /** @type {[string, SharedConfig][]} */
|
---|
| 27 | const sharedOptions = parseOptions(
|
---|
| 28 | options.shared,
|
---|
| 29 | (item, key) => {
|
---|
| 30 | if (typeof item !== "string")
|
---|
| 31 | throw new Error("Unexpected array in shared");
|
---|
| 32 | /** @type {SharedConfig} */
|
---|
| 33 | const config =
|
---|
| 34 | item === key || !isRequiredVersion(item)
|
---|
| 35 | ? {
|
---|
| 36 | import: item
|
---|
| 37 | }
|
---|
| 38 | : {
|
---|
| 39 | import: key,
|
---|
| 40 | requiredVersion: item
|
---|
| 41 | };
|
---|
| 42 | return config;
|
---|
| 43 | },
|
---|
| 44 | item => item
|
---|
| 45 | );
|
---|
| 46 | /** @type {Record<string, ConsumesConfig>[]} */
|
---|
| 47 | const consumes = sharedOptions.map(([key, options]) => ({
|
---|
| 48 | [key]: {
|
---|
| 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,
|
---|
| 56 | eager: options.eager
|
---|
| 57 | }
|
---|
| 58 | }));
|
---|
| 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,
|
---|
| 67 | eager: options.eager
|
---|
| 68 | }
|
---|
| 69 | }));
|
---|
| 70 | this._shareScope = options.shareScope;
|
---|
| 71 | this._consumes = consumes;
|
---|
| 72 | this._provides = provides;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Apply the plugin
|
---|
| 77 | * @param {Compiler} compiler the compiler instance
|
---|
| 78 | * @returns {void}
|
---|
| 79 | */
|
---|
| 80 | apply(compiler) {
|
---|
| 81 | new ConsumeSharedPlugin({
|
---|
| 82 | shareScope: this._shareScope,
|
---|
| 83 | consumes: this._consumes
|
---|
| 84 | }).apply(compiler);
|
---|
| 85 | new ProvideSharedPlugin({
|
---|
| 86 | shareScope: this._shareScope,
|
---|
| 87 | provides: this._provides
|
---|
| 88 | }).apply(compiler);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | module.exports = SharePlugin;
|
---|