@umijs/plugins 4.0.7 → 4.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/locale.js CHANGED
@@ -1,199 +1,221 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.packageNormalize = void 0;
4
- const fs_1 = require("fs");
5
- const path_1 = require("path");
6
- const plugin_utils_1 = require("umi/plugin-utils");
7
- const localeUtils_1 = require("./utils/localeUtils");
8
- const withTmpPath_1 = require("./utils/withTmpPath");
9
- const packageNormalize = (packageName) => packageName.replace(/[@\/\-.]/g, '_');
10
- exports.packageNormalize = packageNormalize;
11
- // TODO: runtime plugin
12
- exports.default = (api) => {
13
- // TODO: antd 的校验考虑 antd 插件
14
- let hasAntd = false;
15
- try {
16
- hasAntd = !!require.resolve('antd');
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
17
20
  }
18
- catch (e) {
19
- api.logger.warn('antd is not installed. <SelecLang /> unavailable');
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __export = (target, all) => {
25
+ for (var name in all)
26
+ __defProp(target, name, { get: all[name], enumerable: true });
27
+ };
28
+ var __copyProps = (to, from, except, desc) => {
29
+ if (from && typeof from === "object" || typeof from === "function") {
30
+ for (let key of __getOwnPropNames(from))
31
+ if (!__hasOwnProp.call(to, key) && key !== except)
32
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
33
+ }
34
+ return to;
35
+ };
36
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+
39
+ // src/locale.ts
40
+ var locale_exports = {};
41
+ __export(locale_exports, {
42
+ default: () => locale_default,
43
+ packageNormalize: () => packageNormalize
44
+ });
45
+ module.exports = __toCommonJS(locale_exports);
46
+ var import_fs = require("fs");
47
+ var import_path = require("path");
48
+ var import_plugin_utils = require("umi/plugin-utils");
49
+ var import_localeUtils = require("./utils/localeUtils");
50
+ var import_withTmpPath = require("./utils/withTmpPath");
51
+ var packageNormalize = (packageName) => packageName.replace(/[@\/\-.]/g, "_");
52
+ var locale_default = (api) => {
53
+ let hasAntd = false;
54
+ try {
55
+ hasAntd = !!require.resolve("antd");
56
+ } catch (e) {
57
+ api.logger.warn("antd is not installed. <SelecLang /> unavailable");
58
+ }
59
+ const defaultConfig = {
60
+ baseNavigator: true,
61
+ useLocalStorage: true,
62
+ baseSeparator: "-",
63
+ antd: hasAntd
64
+ };
65
+ api.describe({
66
+ key: "locale",
67
+ config: {
68
+ schema(Joi) {
69
+ return Joi.alternatives().try(Joi.object({
70
+ default: Joi.string(),
71
+ useLocalStorage: Joi.boolean(),
72
+ baseNavigator: Joi.boolean(),
73
+ title: Joi.boolean(),
74
+ antd: Joi.boolean(),
75
+ baseSeparator: Joi.string()
76
+ }), Joi.boolean().invalid(true));
77
+ }
78
+ },
79
+ enableBy: api.EnableBy.config
80
+ });
81
+ const reactIntlPkgPath = (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve("react-intl/package")));
82
+ api.addEntryImportsAhead(() => (0, import_localeUtils.isNeedPolyfill)(api.config.targets || {}) ? [
83
+ {
84
+ source: require.resolve("intl")
20
85
  }
21
- const defaultConfig = {
22
- baseNavigator: true,
23
- useLocalStorage: true,
24
- baseSeparator: '-',
25
- antd: hasAntd,
26
- };
27
- api.describe({
28
- key: 'locale',
29
- config: {
30
- schema(joi) {
31
- return joi.object({
32
- default: joi.string(),
33
- useLocalStorage: joi.boolean(),
34
- baseNavigator: joi.boolean(),
35
- title: joi.boolean(),
36
- antd: joi.boolean(),
37
- baseSeparator: joi.string(),
38
- });
39
- },
40
- },
41
- enableBy: api.EnableBy.config,
86
+ ] : []);
87
+ const addAntdLocales = async (args) => {
88
+ var _a;
89
+ return await api.applyPlugins({
90
+ key: "addAntdLocales",
91
+ type: api.ApplyPluginsType.add,
92
+ initialValue: [
93
+ `antd/${((_a = api.config) == null ? void 0 : _a.ssr) ? "lib" : "es"}/locale/${(0, import_localeUtils.getAntdLocale)(args.lang, args.country)}`
94
+ ],
95
+ args
42
96
  });
43
- const reactIntlPkgPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('react-intl/package')));
44
- // polyfill
45
- api.addEntryImportsAhead(() => (0, localeUtils_1.isNeedPolyfill)(api.config.targets || {})
46
- ? [
47
- {
48
- source: require.resolve('intl'),
49
- },
50
- ]
51
- : []);
52
- const addAntdLocales = async (args) => {
53
- var _a;
54
- return await api.applyPlugins({
55
- key: 'addAntdLocales',
56
- type: api.ApplyPluginsType.add,
57
- initialValue: [
58
- `antd/${((_a = api.config) === null || _a === void 0 ? void 0 : _a.ssr) ? 'lib' : 'es'}/locale/${(0, localeUtils_1.getAntdLocale)(args.lang, args.country)}`,
59
- ],
60
- args,
61
- });
62
- };
63
- const getList = async (resolveKey) => {
64
- var _a;
65
- const { paths } = api;
66
- return (0, localeUtils_1.getLocaleList)({
67
- localeFolder: 'locales',
68
- separator: (_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.baseSeparator,
69
- absSrcPath: paths.absSrcPath,
70
- absPagesPath: paths.absPagesPath,
71
- addAntdLocales,
72
- resolveKey,
73
- });
97
+ };
98
+ const getList = async (resolveKey) => {
99
+ var _a;
100
+ const { paths } = api;
101
+ return (0, import_localeUtils.getLocaleList)({
102
+ localeFolder: "locales",
103
+ separator: (_a = api.config.locale) == null ? void 0 : _a.baseSeparator,
104
+ absSrcPath: paths.absSrcPath,
105
+ absPagesPath: paths.absPagesPath,
106
+ addAntdLocales,
107
+ resolveKey
108
+ });
109
+ };
110
+ api.onGenerateFiles(async () => {
111
+ var _a, _b, _c, _d;
112
+ const localeTpl = (0, import_fs.readFileSync)((0, import_path.join)(__dirname, "../libs/locale/locale.tpl"), "utf-8");
113
+ const resolveKey = api.config.moment2dayjs ? "dayjs" : "moment";
114
+ const momentPkgPath = (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve(`${resolveKey}/package.json`)));
115
+ const EventEmitterPkg = (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve("event-emitter/package")));
116
+ const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = __spreadValues(__spreadValues({}, defaultConfig), api.config.locale);
117
+ const defaultLocale = ((_a = api.config.locale) == null ? void 0 : _a.default) || `zh${baseSeparator}CN`;
118
+ const localeList = await getList(resolveKey);
119
+ const momentLocales = localeList.map(({ momentLocale }) => momentLocale).filter((locale) => locale);
120
+ const antdLocales = localeList.map(({ antdLocale }) => antdLocale).filter((locale) => locale);
121
+ let MomentLocales = momentLocales;
122
+ let DefaultMomentLocale = "";
123
+ if (!MomentLocales.length && ((_b = api.config.locale) == null ? void 0 : _b.default)) {
124
+ const [lang, country = ""] = defaultLocale.split(baseSeparator);
125
+ const { momentLocale } = (0, import_localeUtils.getMomentLocale)(lang, country, resolveKey);
126
+ if (momentLocale) {
127
+ MomentLocales = [momentLocale];
128
+ DefaultMomentLocale = momentLocale;
129
+ }
130
+ }
131
+ let DefaultAntdLocales = [];
132
+ if (!antdLocales.length && ((_c = api.config.locale) == null ? void 0 : _c.antd)) {
133
+ const [lang, country = ""] = defaultLocale.split(baseSeparator);
134
+ DefaultAntdLocales = import_plugin_utils.lodash.uniq(await addAntdLocales({
135
+ lang,
136
+ country
137
+ }));
138
+ }
139
+ const NormalizeAntdLocalesName = function() {
140
+ return packageNormalize(this);
74
141
  };
75
- api.onGenerateFiles(async () => {
76
- var _a, _b, _c, _d;
77
- const localeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/locale.tpl'), 'utf-8');
78
- // moment2dayjs
79
- const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
80
- const momentPkgPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve(`${resolveKey}/package.json`)));
81
- const EventEmitterPkg = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('event-emitter/package')));
82
- const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = {
83
- ...defaultConfig,
84
- ...api.config.locale,
85
- };
86
- const defaultLocale = ((_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.default) || `zh${baseSeparator}CN`;
87
- const localeList = await getList(resolveKey);
88
- const momentLocales = localeList
89
- .map(({ momentLocale }) => momentLocale)
90
- .filter((locale) => locale);
91
- const antdLocales = localeList
92
- .map(({ antdLocale }) => antdLocale)
93
- .filter((locale) => locale);
94
- let MomentLocales = momentLocales;
95
- let DefaultMomentLocale = '';
96
- // set moment default accounding to locale.default
97
- if (!MomentLocales.length && ((_b = api.config.locale) === null || _b === void 0 ? void 0 : _b.default)) {
98
- const [lang, country = ''] = defaultLocale.split(baseSeparator);
99
- const { momentLocale } = (0, localeUtils_1.getMomentLocale)(lang, country, resolveKey);
100
- if (momentLocale) {
101
- MomentLocales = [momentLocale];
102
- DefaultMomentLocale = momentLocale;
103
- }
104
- }
105
- let DefaultAntdLocales = [];
106
- // set antd default locale
107
- if (!antdLocales.length && ((_c = api.config.locale) === null || _c === void 0 ? void 0 : _c.antd)) {
108
- const [lang, country = ''] = defaultLocale.split(baseSeparator);
109
- DefaultAntdLocales = plugin_utils_1.lodash.uniq(await addAntdLocales({
110
- lang,
111
- country,
112
- }));
113
- }
114
- const NormalizeAntdLocalesName = function () {
115
- // @ts-ignore
116
- return (0, exports.packageNormalize)(this);
117
- };
118
- api.writeTmpFile({
119
- content: plugin_utils_1.Mustache.render(localeTpl, {
120
- MomentLocales,
121
- DefaultMomentLocale,
122
- NormalizeAntdLocalesName,
123
- DefaultAntdLocales,
124
- Antd: !!antd,
125
- Title: title && api.config.title,
126
- BaseSeparator: baseSeparator,
127
- DefaultLocale: defaultLocale,
128
- DefaultLang: defaultLocale,
129
- momentPkgPath,
130
- }),
131
- path: 'locale.tsx',
132
- });
133
- const localeExportsTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/localeExports.tpl'), 'utf-8');
134
- const localeDirName = 'locales';
135
- const localeDirPath = (0, path_1.join)(api.paths.absSrcPath, localeDirName);
136
- api.writeTmpFile({
137
- path: 'localeExports.ts',
138
- content: plugin_utils_1.Mustache.render(localeExportsTpl, {
139
- EventEmitterPkg,
140
- BaseSeparator: baseSeparator,
141
- BaseNavigator: baseNavigator,
142
- UseLocalStorage: !!useLocalStorage,
143
- LocaleDir: localeDirName,
144
- ExistLocaleDir: (0, fs_1.existsSync)(localeDirPath),
145
- LocaleList: localeList.map((locale) => ({
146
- ...locale,
147
- antdLocale: locale.antdLocale.map((antdLocale, index) => ({
148
- locale: antdLocale,
149
- index: index,
150
- })),
151
- paths: locale.paths.map((path, index) => ({
152
- path,
153
- index,
154
- })),
155
- })),
156
- Antd: !!antd,
157
- DefaultLocale: JSON.stringify(defaultLocale),
158
- warningPkgPath: (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('warning/package'))),
159
- reactIntlPkgPath,
160
- }),
161
- });
162
- // runtime.tsx
163
- const runtimeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/runtime.tpl'), 'utf-8');
164
- api.writeTmpFile({
165
- path: 'runtime.tsx',
166
- content: plugin_utils_1.Mustache.render(runtimeTpl, {
167
- Title: !!title,
168
- }),
169
- });
170
- // SelectLang.tsx
171
- const selectLang = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/SelectLang.tpl'), 'utf-8');
172
- api.writeTmpFile({
173
- path: 'SelectLang.tsx',
174
- content: plugin_utils_1.Mustache.render(selectLang, {
175
- Antd: !!antd,
176
- LocaleList: localeList,
177
- ShowSelectLang: localeList.length > 1 && !!antd,
178
- antdFiles: ((_d = api.config) === null || _d === void 0 ? void 0 : _d.ssr) ? 'lib' : 'es',
179
- }),
180
- });
181
- // index.ts
182
- api.writeTmpFile({
183
- path: 'index.ts',
184
- content: `
185
- export { addLocale, setLocale, getLocale, getIntl, useIntl, injectIntl, formatMessage, FormattedMessage, getAllLocales } from './localeExports.ts';
186
- export { SelectLang } from './SelectLang.tsx';
187
- `,
188
- });
142
+ api.writeTmpFile({
143
+ content: import_plugin_utils.Mustache.render(localeTpl, {
144
+ MomentLocales,
145
+ DefaultMomentLocale,
146
+ NormalizeAntdLocalesName,
147
+ DefaultAntdLocales,
148
+ Antd: !!antd,
149
+ Title: title && api.config.title,
150
+ BaseSeparator: baseSeparator,
151
+ DefaultLocale: defaultLocale,
152
+ DefaultLang: defaultLocale,
153
+ momentPkgPath
154
+ }),
155
+ path: "locale.tsx"
156
+ });
157
+ const localeExportsTpl = (0, import_fs.readFileSync)((0, import_path.join)(__dirname, "../libs/locale/localeExports.tpl"), "utf-8");
158
+ const localeDirName = "locales";
159
+ const localeDirPath = (0, import_path.join)(api.paths.absSrcPath, localeDirName);
160
+ api.writeTmpFile({
161
+ path: "localeExports.ts",
162
+ content: import_plugin_utils.Mustache.render(localeExportsTpl, {
163
+ EventEmitterPkg,
164
+ BaseSeparator: baseSeparator,
165
+ BaseNavigator: baseNavigator,
166
+ UseLocalStorage: !!useLocalStorage,
167
+ LocaleDir: localeDirName,
168
+ ExistLocaleDir: (0, import_fs.existsSync)(localeDirPath),
169
+ LocaleList: localeList.map((locale) => __spreadProps(__spreadValues({}, locale), {
170
+ antdLocale: locale.antdLocale.map((antdLocale, index) => ({
171
+ locale: antdLocale,
172
+ index
173
+ })),
174
+ paths: locale.paths.map((path, index) => ({
175
+ path,
176
+ index
177
+ }))
178
+ })),
179
+ Antd: !!antd,
180
+ DefaultLocale: JSON.stringify(defaultLocale),
181
+ warningPkgPath: (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve("warning/package"))),
182
+ reactIntlPkgPath
183
+ })
184
+ });
185
+ const runtimeTpl = (0, import_fs.readFileSync)((0, import_path.join)(__dirname, "../libs/locale/runtime.tpl"), "utf-8");
186
+ api.writeTmpFile({
187
+ path: "runtime.tsx",
188
+ content: import_plugin_utils.Mustache.render(runtimeTpl, {
189
+ Title: !!title
190
+ })
191
+ });
192
+ const selectLang = (0, import_fs.readFileSync)((0, import_path.join)(__dirname, "../libs/locale/SelectLang.tpl"), "utf-8");
193
+ api.writeTmpFile({
194
+ path: "SelectLang.tsx",
195
+ content: import_plugin_utils.Mustache.render(selectLang, {
196
+ Antd: !!antd,
197
+ LocaleList: localeList,
198
+ ShowSelectLang: localeList.length > 1 && !!antd,
199
+ antdFiles: ((_d = api.config) == null ? void 0 : _d.ssr) ? "lib" : "es"
200
+ })
189
201
  });
190
- // Runtime Plugin
191
- api.addRuntimePlugin(() => [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })]);
192
- api.addRuntimePluginKey(() => ['locale']);
193
- // watch locale files
194
- api.addTmpGenerateWatcherPaths(async () => {
195
- const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
196
- const localeList = await getList(resolveKey);
197
- return (0, localeUtils_1.exactLocalePaths)(localeList);
202
+ api.writeTmpFile({
203
+ path: "index.ts",
204
+ content: `
205
+ export { addLocale, setLocale, getLocale, getIntl, useIntl, injectIntl, formatMessage, FormattedMessage, getAllLocales, FormattedDate, FormattedDateParts, FormattedDisplayName, FormattedHTMLMessage, FormattedList, FormattedNumber, FormattedNumberParts, FormattedPlural, FormattedRelativeTime, FormattedTime, FormattedTimeParts, IntlProvider, RawIntlProvider } from './localeExports';
206
+ export { SelectLang } from './SelectLang';
207
+ `
198
208
  });
209
+ });
210
+ api.addRuntimePlugin(() => [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })]);
211
+ api.addRuntimePluginKey(() => ["locale"]);
212
+ api.addTmpGenerateWatcherPaths(async () => {
213
+ const resolveKey = api.config.moment2dayjs ? "dayjs" : "moment";
214
+ const localeList = await getList(resolveKey);
215
+ return (0, import_localeUtils.exactLocalePaths)(localeList);
216
+ });
199
217
  };
218
+ // Annotate the CommonJS export names for ESM import in node:
219
+ 0 && (module.exports = {
220
+ packageNormalize
221
+ });
package/dist/mf.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { IApi } from 'umi';
2
+ export default function mf(api: IApi): void;
package/dist/mf.js ADDED
@@ -0,0 +1,197 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/mf.ts
20
+ var mf_exports = {};
21
+ __export(mf_exports, {
22
+ default: () => mf
23
+ });
24
+ module.exports = __toCommonJS(mf_exports);
25
+ var import_fs = require("fs");
26
+ var import_path = require("path");
27
+ var import_plugin_utils = require("umi/plugin-utils");
28
+ var { isEmpty } = import_plugin_utils.lodash;
29
+ var mfSetupPathFileName = "_mf_setup-public-path.js";
30
+ var mfAsyncEntryFileName = "asyncEntry.ts";
31
+ function mf(api) {
32
+ api.describe({
33
+ key: "mf",
34
+ config: {
35
+ schema(Joi) {
36
+ return Joi.object({
37
+ name: Joi.string(),
38
+ remotes: Joi.array().items(Joi.object({
39
+ aliasName: Joi.string(),
40
+ name: Joi.string().required(),
41
+ entry: Joi.string(),
42
+ entries: Joi.object(),
43
+ keyResolver: Joi.string()
44
+ })),
45
+ shared: Joi.object(),
46
+ library: Joi.object()
47
+ });
48
+ }
49
+ },
50
+ enableBy: api.EnableBy.config
51
+ });
52
+ api.modifyWebpackConfig(async (config, { webpack }) => {
53
+ const exposes = await constructExposes();
54
+ const remotes = formatRemotes();
55
+ const shared = getShared();
56
+ if (isEmpty(remotes) && isEmpty(exposes)) {
57
+ api.logger.warn("ModuleFederation exposes and remotes are empty, plugin will not work");
58
+ return config;
59
+ }
60
+ if (!isEmpty(remotes)) {
61
+ if (!api.config.mfsu) {
62
+ changeUmiEntry(config);
63
+ }
64
+ }
65
+ let name = "_";
66
+ if (!isEmpty(exposes)) {
67
+ name = mfName();
68
+ addMFEntry(config, name, (0, import_path.join)(api.paths.absTmpPath, "plugin-mf", mfSetupPathFileName));
69
+ }
70
+ const mfConfig = {
71
+ name,
72
+ remotes,
73
+ filename: "remote.js",
74
+ exposes,
75
+ shared,
76
+ library: api.config.mf.library
77
+ };
78
+ const { ModuleFederationPlugin } = webpack.container;
79
+ config.plugins.push(new ModuleFederationPlugin(mfConfig));
80
+ api.logger.debug(`ModuleFederationPlugin is enabled with config ${JSON.stringify(mfConfig)}`);
81
+ return config;
82
+ });
83
+ api.onGenerateFiles(() => {
84
+ api.writeTmpFile({
85
+ content: `/* infer remote public */;
86
+ __webpack_public_path__ = document.currentScript.src + '/../';`,
87
+ path: mfSetupPathFileName
88
+ });
89
+ if (api.env === "development" && api.config.mfsu) {
90
+ return;
91
+ }
92
+ api.writeTmpFile({
93
+ content: `import('${(0, import_plugin_utils.winPath)((0, import_path.join)(api.paths.absTmpPath, "umi.ts"))}')`,
94
+ path: mfAsyncEntryFileName
95
+ });
96
+ });
97
+ function formatRemotes() {
98
+ const { remotes = [] } = api.config.mf;
99
+ const memo = {};
100
+ remotes.forEach((remote) => {
101
+ const aliasName = remote.aliasName || remote.name;
102
+ const r = formatRemote(remote);
103
+ if (memo[aliasName]) {
104
+ return api.logger.error(`${aliasName} already set as ${memo[aliasName]}, new value ${r} will be ignored`);
105
+ }
106
+ memo[aliasName] = r;
107
+ });
108
+ return memo;
109
+ }
110
+ function formatRemote(remote) {
111
+ if (remote.entry) {
112
+ return `${remote.name}@${remote.entry}`;
113
+ }
114
+ if (remote.entries && remote.keyResolver) {
115
+ const dynamicUrl = `promise new Promise(resolve => {
116
+ const entries = ${JSON.stringify(remote.entries)};
117
+ const key = ${remote.keyResolver};
118
+
119
+ const remoteUrlWithVersion = entries[key];
120
+ const script = document.createElement('script')
121
+ script.src = remoteUrlWithVersion
122
+ script.onload = () => {
123
+ // the injected script has loaded and is available on window
124
+ // we can now resolve this Promise
125
+ const proxy = {
126
+ get: (request) => window.${remote.name}.get(request),
127
+ init: (arg) => {
128
+ try {
129
+ return window.${remote.name}.init(arg)
130
+ } catch(e) {
131
+ console.log('remote container already initialized')
132
+ }
133
+ }
134
+ }
135
+ resolve(proxy)
136
+ }
137
+ // inject this script with the src set to the versioned remoteEntry.js
138
+ document.head.appendChild(script);
139
+ })
140
+ `;
141
+ return dynamicUrl;
142
+ } else {
143
+ api.logger.error("you should provider entry or entries and keyResolver");
144
+ throw Error("Wrong MF#remotes config");
145
+ }
146
+ }
147
+ async function constructExposes() {
148
+ const exposes = {};
149
+ const exposesPath = (0, import_path.join)(api.paths.absSrcPath, "exposes");
150
+ if (!(0, import_fs.existsSync)(exposesPath)) {
151
+ return exposes;
152
+ }
153
+ const dir = (0, import_fs.opendirSync)(exposesPath);
154
+ for await (const dirent of dir) {
155
+ if (dirent.isDirectory()) {
156
+ exposes["./" + dirent.name] = (0, import_plugin_utils.winPath)((0, import_path.join)(api.paths.absSrcPath, "exposes", dirent.name));
157
+ } else {
158
+ api.logger.warn(`${dirent.name} is not a directory, ignore in ModuleFederation expose`);
159
+ }
160
+ }
161
+ return exposes;
162
+ }
163
+ function mfName() {
164
+ const name = api.config.mf.name;
165
+ if (!name) {
166
+ api.logger.warn(`module federation name is not defined , "unNamedMF" will be used`);
167
+ }
168
+ return name || "unNamedMF";
169
+ }
170
+ function getShared() {
171
+ const { shared = {} } = api.config.mf;
172
+ return shared;
173
+ }
174
+ function changeUmiEntry(config) {
175
+ const { entry } = config;
176
+ const asyncEntryPath = (0, import_plugin_utils.winPath)((0, import_path.join)(api.paths.absTmpPath, "plugin-mf", mfAsyncEntryFileName));
177
+ if (entry.umi) {
178
+ if (typeof entry.umi === "string") {
179
+ entry.umi = asyncEntryPath;
180
+ } else if (Array.isArray(entry.umi)) {
181
+ const i = entry.umi.findIndex((f) => f.endsWith("umi.ts"));
182
+ if (i >= 0) {
183
+ entry.umi[i] = asyncEntryPath;
184
+ } else {
185
+ api.logger.info(`umi.ts not found in entry.umi ${JSON.stringify(entry.umi)}`);
186
+ }
187
+ }
188
+ } else {
189
+ api.logger.warn("umi entry not found");
190
+ }
191
+ }
192
+ function addMFEntry(config, mfName2, path) {
193
+ config.entry[mfName2] = path;
194
+ }
195
+ }
196
+ // Annotate the CommonJS export names for ESM import in node:
197
+ 0 && (module.exports = {});