1 |
|
2 |
|
3 |
|
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 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class SharePlugin {
|
22 | |
23 |
|
24 |
|
25 | constructor(options) {
|
26 |
|
27 | const sharedOptions = parseOptions(
|
28 | options.shared,
|
29 | (item, key) => {
|
30 | if (typeof item !== "string")
|
31 | throw new Error("Unexpected array in shared");
|
32 |
|
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 |
|
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 |
|
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 |
|
77 |
|
78 |
|
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;
|