| [9af201e] | 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").ConsumesConfig} ConsumesConfig */
|
|---|
| 14 | /** @typedef {import("../../declarations/plugins/sharing/ProvideSharedPlugin").ProvidesConfig} ProvidesConfig */
|
|---|
| 15 | /** @typedef {import("../../declarations/plugins/sharing/SharePlugin").SharePluginOptions} SharePluginOptions */
|
|---|
| 16 | /** @typedef {import("../../declarations/plugins/sharing/SharePlugin").SharedConfig} SharedConfig */
|
|---|
| 17 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 18 |
|
|---|
| 19 | class SharePlugin {
|
|---|
| 20 | /**
|
|---|
| 21 | * Creates an instance of SharePlugin.
|
|---|
| 22 | * @param {SharePluginOptions} options options
|
|---|
| 23 | */
|
|---|
| 24 | constructor(options) {
|
|---|
| 25 | /** @type {[string, SharedConfig][]} */
|
|---|
| 26 | const sharedOptions = parseOptions(
|
|---|
| 27 | options.shared,
|
|---|
| 28 | (item, key) => {
|
|---|
| 29 | if (typeof item !== "string") {
|
|---|
| 30 | throw new Error("Unexpected array in shared");
|
|---|
| 31 | }
|
|---|
| 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 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 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;
|
|---|