-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
SharedManager.ts
155 lines (142 loc) · 4.19 KB
/
SharedManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// @ts-ignore this pkg miss types
import findPkg from 'find-pkg';
import path from 'path';
import fs from 'fs-extra';
import { sharing } from 'webpack';
import {
moduleFederationPlugin,
sharePlugin,
isRequiredVersion,
} from '@module-federation/sdk';
import { NormalizedSharedOptions } from './types';
import { BasicPluginOptionsManager } from './BasicPluginOptionsManager';
import { parseOptions } from './utils';
type SharePluginOptions = ConstructorParameters<typeof sharing.SharePlugin>[0];
class SharedManager extends BasicPluginOptionsManager<moduleFederationPlugin.ModuleFederationPluginOptions> {
normalizedOptions: NormalizedSharedOptions = {};
override get enable(): boolean {
return Boolean(Object.keys(this.sharedPluginOptions.shared).length);
}
get sharedPluginOptions(): SharePluginOptions {
const normalizedShared = this.normalizedOptions;
const shared = Object.keys(normalizedShared).reduce((sum, cur) => {
const {
singleton,
requiredVersion,
version,
eager,
shareScope,
import: sharedImport,
} = normalizedShared[cur];
sum[cur] = {
singleton,
requiredVersion,
version,
eager,
shareScope,
import: sharedImport,
};
return sum;
}, {} as moduleFederationPlugin.SharedObject);
return {
shared,
shareScope: this.options.shareScope || 'default',
};
}
findPkg(
name: string,
shareConfig: sharePlugin.SharedConfig,
): {
pkg: Record<string, any>;
path: string;
pkgPath: string;
} {
try {
let pkgPath: string = '';
let depName = name;
if (shareConfig.import) {
if (path.isAbsolute(shareConfig.import)) {
pkgPath = shareConfig.import;
} else if (shareConfig.import.startsWith('.')) {
pkgPath = path.resolve(process.cwd(), shareConfig.import);
}
} else {
if (shareConfig.packageName) {
depName = shareConfig.packageName;
}
}
pkgPath = pkgPath || require.resolve(depName, { paths: [process.cwd()] });
const pkgJsonPath = findPkg.sync(pkgPath);
return {
pkg: JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')),
path: '',
pkgPath: '',
};
} catch (err) {
return {
pkg: {},
path: '',
pkgPath: '',
};
}
}
transformSharedConfig(
sharedConfig: sharePlugin.SharedConfig,
): sharePlugin.SharedConfig {
const defaultSharedConfig: sharePlugin.SharedConfig = {
singleton: true,
requiredVersion: undefined,
shareScope: 'default',
};
return {
...defaultSharedConfig,
...sharedConfig,
};
}
normalizeOptions(
options: moduleFederationPlugin.ModuleFederationPluginOptions['shared'],
): void {
const normalizedShared: NormalizedSharedOptions = {};
const sharedOptions: [string, sharePlugin.SharedConfig][] = parseOptions(
options!,
(item, key) => {
if (typeof item !== 'string')
throw new Error('Unexpected array in shared');
const config: sharePlugin.SharedConfig =
item === key || !isRequiredVersion(item)
? {
import: item,
}
: {
import: key,
requiredVersion: item,
};
return config;
},
(item) => item,
);
sharedOptions.forEach((item) => {
const [sharedName, sharedOptions] = item;
const pkgInfo = this.findPkg(sharedName, sharedOptions);
const sharedConfig = this.transformSharedConfig(sharedOptions);
normalizedShared[sharedName] = {
...sharedConfig,
requiredVersion:
typeof sharedConfig.requiredVersion !== 'undefined'
? sharedConfig.requiredVersion
: `^${pkgInfo.pkg['version']}`,
name: sharedName,
version: pkgInfo.pkg['version'],
eager: Boolean(sharedConfig.eager),
};
});
this.normalizedOptions = normalizedShared;
}
override init(
options: moduleFederationPlugin.ModuleFederationPluginOptions,
): void {
this.setOptions(options);
this.normalizeOptions(options.shared);
}
}
export { SharedManager };