@umijs/plugins 4.0.0-beta.8 → 4.0.0-canary-20240513.3
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/LICENSE +21 -0
- package/README.md +4 -1
- package/dist/access.d.ts +1 -1
- package/dist/access.js +194 -4
- package/dist/{sass.d.ts → analytics.d.ts} +1 -1
- package/dist/analytics.js +118 -0
- package/dist/antd.d.ts +1 -1
- package/dist/antd.js +345 -157
- package/dist/{icons.d.ts → confetti.d.ts} +1 -1
- package/dist/confetti.js +64 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +30 -0
- package/dist/dva.d.ts +4 -1
- package/dist/dva.js +302 -8
- package/dist/initial-state.d.ts +1 -1
- package/dist/initial-state.js +147 -4
- package/dist/layout.d.ts +1 -1
- package/dist/layout.js +754 -4
- package/dist/locale.d.ts +2 -1
- package/dist/locale.js +257 -4
- package/dist/mf.d.ts +2 -0
- package/dist/mf.js +358 -0
- package/dist/model.d.ts +1 -1
- package/dist/model.js +109 -4
- package/dist/moment2dayjs.d.ts +3 -0
- package/dist/moment2dayjs.js +125 -0
- package/dist/qiankun/constants.d.ts +5 -0
- package/dist/qiankun/constants.js +41 -0
- package/dist/qiankun/master.d.ts +6 -0
- package/dist/qiankun/master.js +236 -0
- package/dist/qiankun/slave.d.ts +3 -0
- package/dist/qiankun/slave.js +345 -0
- package/dist/qiankun.d.ts +1 -1
- package/dist/qiankun.js +51 -4
- package/dist/react-query.d.ts +3 -0
- package/dist/react-query.js +193 -0
- package/dist/request.d.ts +1 -1
- package/dist/request.js +384 -4
- package/dist/styled-components.d.ts +3 -0
- package/dist/styled-components.js +150 -0
- package/dist/tailwindcss.d.ts +3 -0
- package/dist/tailwindcss.js +106 -0
- package/dist/unocss.d.ts +3 -0
- package/dist/unocss.js +71 -0
- package/dist/utils/astUtils.d.ts +3 -0
- package/dist/utils/astUtils.js +49 -0
- package/dist/utils/localeUtils.d.ts +33 -0
- package/dist/utils/localeUtils.js +139 -0
- package/dist/utils/mfUtils.d.ts +14 -0
- package/dist/utils/mfUtils.js +56 -0
- package/dist/utils/modelUtils.d.ts +40 -0
- package/dist/utils/modelUtils.js +286 -0
- package/dist/utils/resolveProjectDep.d.ts +5 -0
- package/dist/utils/resolveProjectDep.js +40 -0
- package/dist/utils/withTmpPath.d.ts +6 -0
- package/dist/utils/withTmpPath.js +39 -0
- package/dist/valtio.d.ts +3 -0
- package/dist/valtio.js +81 -0
- package/libs/dva.tsx +45 -0
- package/libs/model.tsx +180 -0
- package/libs/qiankun/master/AntdErrorBoundary.tsx +34 -0
- package/libs/qiankun/master/AntdLoader.tsx +15 -0
- package/libs/qiankun/master/ErrorBoundary.tsx +7 -0
- package/libs/qiankun/master/MicroApp.tsx +327 -0
- package/libs/qiankun/master/MicroAppWithMemoHistory.tsx +43 -0
- package/libs/qiankun/master/common.ts +172 -0
- package/libs/qiankun/master/constants.ts +19 -0
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +45 -0
- package/libs/qiankun/master/masterRuntimePlugin.tsx +161 -0
- package/libs/qiankun/master/routeUtils.ts +26 -0
- package/libs/qiankun/master/types.ts +54 -0
- package/libs/qiankun/slave/MicroAppLink.tsx +122 -0
- package/libs/qiankun/slave/connectMaster.tsx +14 -0
- package/libs/qiankun/slave/constants.ts +5 -0
- package/libs/qiankun/slave/lifecycles.ts +164 -0
- package/libs/qiankun/slave/qiankunModel.ts +18 -0
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +36 -0
- package/package.json +54 -24
- package/templates/antd/runtime.ts.tpl +170 -0
- package/templates/antd/types.d.ts.tpl +17 -0
- package/templates/locale/SelectLang.tpl +502 -0
- package/templates/locale/locale.tpl +82 -0
- package/templates/locale/localeExports.tpl +307 -0
- package/templates/locale/runtime.tpl +34 -0
- package/templates/mf/runtime.ts.tpl +149 -0
- package/dist/access.d.ts.map +0 -1
- package/dist/antd.d.ts.map +0 -1
- package/dist/dva.d.ts.map +0 -1
- package/dist/icons.d.ts.map +0 -1
- package/dist/icons.js +0 -5
- package/dist/initial-state.d.ts.map +0 -1
- package/dist/layout.d.ts.map +0 -1
- package/dist/locale.d.ts.map +0 -1
- package/dist/model.d.ts.map +0 -1
- package/dist/qiankun.d.ts.map +0 -1
- package/dist/request.d.ts.map +0 -1
- package/dist/sass.d.ts.map +0 -1
- package/dist/sass.js +0 -5
package/dist/locale.d.ts
CHANGED
package/dist/locale.js
CHANGED
|
@@ -1,5 +1,258 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
10
|
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/locale.ts
|
|
30
|
+
var locale_exports = {};
|
|
31
|
+
__export(locale_exports, {
|
|
32
|
+
default: () => locale_default,
|
|
33
|
+
packageNormalize: () => packageNormalize
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(locale_exports);
|
|
36
|
+
var import_fs = require("fs");
|
|
37
|
+
var import_path = require("path");
|
|
38
|
+
var import_umi = require("umi");
|
|
39
|
+
var import_plugin_utils = require("umi/plugin-utils");
|
|
40
|
+
var import_constants = require("./constants");
|
|
41
|
+
var import_localeUtils = require("./utils/localeUtils");
|
|
42
|
+
var import_withTmpPath = require("./utils/withTmpPath");
|
|
43
|
+
var LOCALE_TEMPLATES_DIR = (0, import_path.join)(import_constants.TEMPLATES_DIR, "locale");
|
|
44
|
+
var packageNormalize = (packageName) => packageName.replace(/[@\/\-.]/g, "_");
|
|
45
|
+
var locale_default = (api) => {
|
|
46
|
+
let hasAntd = false;
|
|
47
|
+
try {
|
|
48
|
+
hasAntd = !!require.resolve("antd");
|
|
49
|
+
} catch (e) {
|
|
50
|
+
api.logger.warn("antd is not installed. <SelecLang /> unavailable");
|
|
51
|
+
}
|
|
52
|
+
const defaultConfig = {
|
|
53
|
+
baseNavigator: true,
|
|
54
|
+
useLocalStorage: true,
|
|
55
|
+
baseSeparator: "-",
|
|
56
|
+
antd: hasAntd
|
|
57
|
+
};
|
|
58
|
+
api.describe({
|
|
59
|
+
key: "locale",
|
|
60
|
+
config: {
|
|
61
|
+
schema({ zod }) {
|
|
62
|
+
return zod.object({
|
|
63
|
+
default: zod.string(),
|
|
64
|
+
useLocalStorage: zod.boolean(),
|
|
65
|
+
baseNavigator: zod.boolean(),
|
|
66
|
+
title: zod.boolean(),
|
|
67
|
+
antd: zod.boolean(),
|
|
68
|
+
baseSeparator: zod.string()
|
|
69
|
+
}).partial();
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
enableBy: api.EnableBy.config
|
|
73
|
+
});
|
|
74
|
+
const reactIntlPkgPath = (0, import_plugin_utils.winPath)(
|
|
75
|
+
(0, import_path.dirname)(require.resolve("react-intl/package"))
|
|
76
|
+
);
|
|
77
|
+
api.addEntryImportsAhead(
|
|
78
|
+
() => (0, import_localeUtils.isNeedPolyfill)(api.config.targets || {}) ? [
|
|
79
|
+
{
|
|
80
|
+
source: require.resolve("intl")
|
|
81
|
+
}
|
|
82
|
+
] : []
|
|
83
|
+
);
|
|
84
|
+
const addAntdLocales = async (args) => {
|
|
85
|
+
var _a;
|
|
86
|
+
return await api.applyPlugins({
|
|
87
|
+
key: "addAntdLocales",
|
|
88
|
+
type: api.ApplyPluginsType.add,
|
|
89
|
+
initialValue: [
|
|
90
|
+
`antd/${((_a = api.config) == null ? void 0 : _a.ssr) ? "lib" : "es"}/locale/${(0, import_localeUtils.getAntdLocale)(
|
|
91
|
+
args.lang,
|
|
92
|
+
args.country
|
|
93
|
+
)}`
|
|
94
|
+
],
|
|
95
|
+
args
|
|
96
|
+
});
|
|
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;
|
|
112
|
+
const localeTpl = (0, import_fs.readFileSync)(
|
|
113
|
+
(0, import_path.join)(LOCALE_TEMPLATES_DIR, "locale.tpl"),
|
|
114
|
+
"utf-8"
|
|
115
|
+
);
|
|
116
|
+
const resolveKey = api.config.moment2dayjs ? "dayjs" : "moment";
|
|
117
|
+
const momentPkgPath = (0, import_plugin_utils.winPath)(
|
|
118
|
+
(0, import_path.dirname)(require.resolve(`${resolveKey}/package.json`))
|
|
119
|
+
);
|
|
120
|
+
const EventEmitterPkg = (0, import_plugin_utils.winPath)(
|
|
121
|
+
(0, import_path.dirname)(require.resolve("event-emitter/package"))
|
|
122
|
+
);
|
|
123
|
+
const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = {
|
|
124
|
+
...defaultConfig,
|
|
125
|
+
...api.config.locale
|
|
126
|
+
};
|
|
127
|
+
const defaultLocale = ((_a = api.config.locale) == null ? void 0 : _a.default) || `zh${baseSeparator}CN`;
|
|
128
|
+
const localeList = await getList(resolveKey);
|
|
129
|
+
const momentLocales = localeList.map(({ momentLocale }) => momentLocale).filter((locale) => locale);
|
|
130
|
+
const antdLocales = localeList.map(({ antdLocale }) => antdLocale).filter((locale) => locale);
|
|
131
|
+
let MomentLocales = momentLocales;
|
|
132
|
+
let DefaultMomentLocale = "";
|
|
133
|
+
if (!MomentLocales.length && ((_b = api.config.locale) == null ? void 0 : _b.default)) {
|
|
134
|
+
const [lang, country = ""] = defaultLocale.split(baseSeparator);
|
|
135
|
+
const { momentLocale } = (0, import_localeUtils.getMomentLocale)(lang, country, resolveKey);
|
|
136
|
+
if (momentLocale) {
|
|
137
|
+
MomentLocales = [momentLocale];
|
|
138
|
+
DefaultMomentLocale = momentLocale;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let DefaultAntdLocales = [];
|
|
142
|
+
if (!antdLocales.length && antd) {
|
|
143
|
+
const [lang, country = ""] = defaultLocale.split(baseSeparator);
|
|
144
|
+
DefaultAntdLocales = import_plugin_utils.lodash.uniq(
|
|
145
|
+
await addAntdLocales({
|
|
146
|
+
lang,
|
|
147
|
+
country
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const NormalizeAntdLocalesName = function() {
|
|
152
|
+
return packageNormalize(this);
|
|
153
|
+
};
|
|
154
|
+
api.writeTmpFile({
|
|
155
|
+
content: import_plugin_utils.Mustache.render(localeTpl, {
|
|
156
|
+
MomentLocales,
|
|
157
|
+
DefaultMomentLocale,
|
|
158
|
+
NormalizeAntdLocalesName,
|
|
159
|
+
DefaultAntdLocales,
|
|
160
|
+
Antd: !!antd,
|
|
161
|
+
Title: title && api.config.title,
|
|
162
|
+
BaseSeparator: baseSeparator,
|
|
163
|
+
DefaultLocale: defaultLocale,
|
|
164
|
+
DefaultLang: defaultLocale,
|
|
165
|
+
momentPkgPath
|
|
166
|
+
}),
|
|
167
|
+
path: "locale.tsx"
|
|
168
|
+
});
|
|
169
|
+
const localeExportsTpl = (0, import_fs.readFileSync)(
|
|
170
|
+
(0, import_path.join)(LOCALE_TEMPLATES_DIR, "localeExports.tpl"),
|
|
171
|
+
"utf-8"
|
|
172
|
+
);
|
|
173
|
+
const localeDirName = "locales";
|
|
174
|
+
const localeDirPath = (0, import_path.join)(api.paths.absSrcPath, localeDirName);
|
|
175
|
+
api.writeTmpFile({
|
|
176
|
+
path: "localeExports.ts",
|
|
177
|
+
content: import_plugin_utils.Mustache.render(localeExportsTpl, {
|
|
178
|
+
EventEmitterPkg,
|
|
179
|
+
BaseSeparator: baseSeparator,
|
|
180
|
+
BaseNavigator: baseNavigator,
|
|
181
|
+
UseLocalStorage: !!useLocalStorage,
|
|
182
|
+
LocaleDir: localeDirName,
|
|
183
|
+
ExistLocaleDir: (0, import_fs.existsSync)(localeDirPath),
|
|
184
|
+
LocaleList: localeList.map((locale) => ({
|
|
185
|
+
...locale,
|
|
186
|
+
antdLocale: locale.antdLocale.map((antdLocale, index) => ({
|
|
187
|
+
locale: antdLocale,
|
|
188
|
+
index
|
|
189
|
+
})),
|
|
190
|
+
paths: locale.paths.map((path, index) => ({
|
|
191
|
+
path,
|
|
192
|
+
index
|
|
193
|
+
}))
|
|
194
|
+
})),
|
|
195
|
+
Antd: !!antd,
|
|
196
|
+
DefaultLocale: JSON.stringify(defaultLocale),
|
|
197
|
+
warningPkgPath: (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve("warning/package"))),
|
|
198
|
+
reactIntlPkgPath
|
|
199
|
+
})
|
|
200
|
+
});
|
|
201
|
+
const runtimeTpl = (0, import_fs.readFileSync)(
|
|
202
|
+
(0, import_path.join)(LOCALE_TEMPLATES_DIR, "runtime.tpl"),
|
|
203
|
+
"utf-8"
|
|
204
|
+
);
|
|
205
|
+
api.writeTmpFile({
|
|
206
|
+
path: "runtime.tsx",
|
|
207
|
+
content: import_plugin_utils.Mustache.render(runtimeTpl, {
|
|
208
|
+
Title: !!title
|
|
209
|
+
})
|
|
210
|
+
});
|
|
211
|
+
const selectLang = (0, import_fs.readFileSync)(
|
|
212
|
+
(0, import_path.join)(LOCALE_TEMPLATES_DIR, "SelectLang.tpl"),
|
|
213
|
+
"utf-8"
|
|
214
|
+
);
|
|
215
|
+
api.writeTmpFile({
|
|
216
|
+
path: "SelectLang.tsx",
|
|
217
|
+
content: import_plugin_utils.Mustache.render(selectLang, {
|
|
218
|
+
Antd: !!antd,
|
|
219
|
+
LocaleList: localeList,
|
|
220
|
+
ShowSelectLang: localeList.length > 1 && !!antd,
|
|
221
|
+
antdFiles: ((_c = api.config) == null ? void 0 : _c.ssr) ? "lib" : "es"
|
|
222
|
+
})
|
|
223
|
+
});
|
|
224
|
+
api.writeTmpFile({
|
|
225
|
+
path: "index.ts",
|
|
226
|
+
content: `
|
|
227
|
+
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';
|
|
228
|
+
export { SelectLang } from './SelectLang';
|
|
229
|
+
`
|
|
230
|
+
});
|
|
231
|
+
api.writeTmpFile({
|
|
232
|
+
path: import_umi.RUNTIME_TYPE_FILE_NAME,
|
|
233
|
+
content: `
|
|
234
|
+
import {
|
|
235
|
+
IntlCache,
|
|
236
|
+
createIntl,
|
|
237
|
+
} from '${reactIntlPkgPath}';
|
|
238
|
+
type OptionalIntlConfig = Omit<Parameters<typeof createIntl>[0], 'locale' | 'defaultLocale'>;
|
|
239
|
+
export interface IRuntimeConfig {
|
|
240
|
+
locale?: {
|
|
241
|
+
getLocale?: () => string;
|
|
242
|
+
cache?: IntlCache;
|
|
243
|
+
} & OptionalIntlConfig;
|
|
244
|
+
};`
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
api.addRuntimePlugin(() => [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })]);
|
|
248
|
+
api.addRuntimePluginKey(() => ["locale"]);
|
|
249
|
+
api.addTmpGenerateWatcherPaths(async () => {
|
|
250
|
+
const resolveKey = api.config.moment2dayjs ? "dayjs" : "moment";
|
|
251
|
+
const localeList = await getList(resolveKey);
|
|
252
|
+
return (0, import_localeUtils.exactLocalePaths)(localeList);
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
256
|
+
0 && (module.exports = {
|
|
257
|
+
packageNormalize
|
|
258
|
+
});
|
package/dist/mf.d.ts
ADDED
package/dist/mf.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
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_bundler_utils = require("@umijs/bundler-utils");
|
|
26
|
+
var import_fs = require("fs");
|
|
27
|
+
var import_path = require("path");
|
|
28
|
+
var import_plugin_utils = require("umi/plugin-utils");
|
|
29
|
+
var import_constants = require("./constants");
|
|
30
|
+
var import_mfUtils = require("./utils/mfUtils");
|
|
31
|
+
var { isEmpty } = import_plugin_utils.lodash;
|
|
32
|
+
var mfSetupPathFileName = "_mf_setup-public-path.js";
|
|
33
|
+
var mfAsyncEntryFileName = "asyncEntry.ts";
|
|
34
|
+
var MF_TEMPLATES_DIR = (0, import_path.join)(import_constants.TEMPLATES_DIR, "mf");
|
|
35
|
+
function mf(api) {
|
|
36
|
+
api.describe({
|
|
37
|
+
key: "mf",
|
|
38
|
+
config: {
|
|
39
|
+
schema({ zod }) {
|
|
40
|
+
return zod.object({
|
|
41
|
+
name: zod.string(),
|
|
42
|
+
remotes: zod.array(
|
|
43
|
+
zod.object({
|
|
44
|
+
aliasName: zod.string().optional(),
|
|
45
|
+
// string 上没有 required
|
|
46
|
+
name: zod.string(),
|
|
47
|
+
entry: zod.string().optional(),
|
|
48
|
+
entries: zod.object({}).optional(),
|
|
49
|
+
keyResolver: zod.string().optional()
|
|
50
|
+
})
|
|
51
|
+
),
|
|
52
|
+
shared: zod.record(zod.any()),
|
|
53
|
+
library: zod.record(zod.any()),
|
|
54
|
+
remoteHash: zod.boolean()
|
|
55
|
+
}).partial();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
enableBy: api.EnableBy.config
|
|
59
|
+
});
|
|
60
|
+
api.modifyWebpackConfig(async (config, { webpack }) => {
|
|
61
|
+
const exposes = await constructExposes();
|
|
62
|
+
const remotes = formatRemotes();
|
|
63
|
+
const shared = getShared();
|
|
64
|
+
if (isEmpty(remotes) && isEmpty(exposes)) {
|
|
65
|
+
api.logger.warn(
|
|
66
|
+
"ModuleFederation exposes and remotes are empty, plugin will not work"
|
|
67
|
+
);
|
|
68
|
+
return config;
|
|
69
|
+
}
|
|
70
|
+
if (!isEmpty(remotes)) {
|
|
71
|
+
if (api.env === "production" || !api.config.mfsu) {
|
|
72
|
+
changeUmiEntry(config);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let name = "_";
|
|
76
|
+
if (!isEmpty(exposes)) {
|
|
77
|
+
name = mfName();
|
|
78
|
+
addMFEntry(
|
|
79
|
+
config,
|
|
80
|
+
name,
|
|
81
|
+
(0, import_path.join)(api.paths.absTmpPath, "plugin-mf", mfSetupPathFileName)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const useHash = typeof api.config.mf.remoteHash === "boolean" ? api.config.mf.remoteHash : api.config.hash && api.env !== "development";
|
|
85
|
+
const mfConfig = {
|
|
86
|
+
name,
|
|
87
|
+
remotes,
|
|
88
|
+
filename: useHash ? "remote.[contenthash:8].js" : "remote.js",
|
|
89
|
+
exposes,
|
|
90
|
+
shared,
|
|
91
|
+
library: api.config.mf.library
|
|
92
|
+
};
|
|
93
|
+
const { ModuleFederationPlugin } = webpack.container;
|
|
94
|
+
config.plugins.push(new ModuleFederationPlugin(mfConfig));
|
|
95
|
+
api.logger.debug(
|
|
96
|
+
`ModuleFederationPlugin is enabled with config ${JSON.stringify(
|
|
97
|
+
mfConfig
|
|
98
|
+
)}`
|
|
99
|
+
);
|
|
100
|
+
return config;
|
|
101
|
+
});
|
|
102
|
+
api.modifyDefaultConfig(async (memo) => {
|
|
103
|
+
if (memo.mfsu) {
|
|
104
|
+
const exposes = await constructExposes();
|
|
105
|
+
if (!isEmpty(exposes)) {
|
|
106
|
+
memo.mfsu.remoteName = mfName();
|
|
107
|
+
memo.mfsu.mfName = `mf_${memo.mfsu.remoteName}`;
|
|
108
|
+
}
|
|
109
|
+
const remotes = formatRemotes();
|
|
110
|
+
memo.mfsu.remoteAliases = Object.keys(remotes);
|
|
111
|
+
memo.mfsu.shared = getShared();
|
|
112
|
+
}
|
|
113
|
+
return memo;
|
|
114
|
+
});
|
|
115
|
+
api.onGenerateFiles(() => {
|
|
116
|
+
api.writeTmpFile({
|
|
117
|
+
// ref https://webpack.js.org/concepts/module-federation/#infer-publicpath-from-script
|
|
118
|
+
content: `/* infer remote public */;
|
|
119
|
+
__webpack_public_path__ = document.currentScript.src + '/../';`,
|
|
120
|
+
path: mfSetupPathFileName
|
|
121
|
+
});
|
|
122
|
+
const { remotes = [] } = api.config.mf;
|
|
123
|
+
api.writeTmpFile({
|
|
124
|
+
path: "index.tsx",
|
|
125
|
+
context: {
|
|
126
|
+
remoteCodeString: (0, import_mfUtils.toRemotesCodeString)(remotes)
|
|
127
|
+
},
|
|
128
|
+
tplPath: (0, import_plugin_utils.winPath)((0, import_path.join)(MF_TEMPLATES_DIR, "runtime.ts.tpl"))
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
api.register({
|
|
132
|
+
key: "onGenerateFiles",
|
|
133
|
+
// ensure after generate tpm files
|
|
134
|
+
stage: 10001,
|
|
135
|
+
fn: async () => {
|
|
136
|
+
if (api.env === "development" && api.config.mfsu) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const entry = (0, import_path.join)(api.paths.absTmpPath, "umi.ts");
|
|
140
|
+
const content = (0, import_fs.readFileSync)(
|
|
141
|
+
(0, import_path.join)(api.paths.absTmpPath, "umi.ts"),
|
|
142
|
+
"utf-8"
|
|
143
|
+
);
|
|
144
|
+
const [_imports, exports] = await (0, import_bundler_utils.parseModule)({ content, path: entry });
|
|
145
|
+
const mfEntryContent = [];
|
|
146
|
+
let hasDefaultExport = false;
|
|
147
|
+
if (exports.length) {
|
|
148
|
+
mfEntryContent.push(
|
|
149
|
+
`const umiExports = await import('${(0, import_plugin_utils.winPath)(entry)}')`
|
|
150
|
+
);
|
|
151
|
+
for (const exportName of exports) {
|
|
152
|
+
if (exportName === "default") {
|
|
153
|
+
hasDefaultExport = true;
|
|
154
|
+
mfEntryContent.push(`export default umiExports.${exportName}`);
|
|
155
|
+
} else {
|
|
156
|
+
mfEntryContent.push(
|
|
157
|
+
`export const ${exportName} = umiExports.${exportName}`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
mfEntryContent.push(`import('${(0, import_plugin_utils.winPath)(entry)}')`);
|
|
163
|
+
}
|
|
164
|
+
if (!hasDefaultExport) {
|
|
165
|
+
mfEntryContent.push("export default 1");
|
|
166
|
+
}
|
|
167
|
+
api.writeTmpFile({
|
|
168
|
+
content: mfEntryContent.join("\n"),
|
|
169
|
+
path: mfAsyncEntryFileName
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
function formatRemotes() {
|
|
174
|
+
const { remotes = [] } = api.userConfig.mf;
|
|
175
|
+
const memo = {};
|
|
176
|
+
remotes.forEach((remote) => {
|
|
177
|
+
const aliasName = remote.aliasName || remote.name;
|
|
178
|
+
const r = formatRemote(remote);
|
|
179
|
+
if (memo[aliasName]) {
|
|
180
|
+
return api.logger.error(
|
|
181
|
+
`${aliasName} already set as ${memo[aliasName]}, new value ${r} will be ignored`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
memo[aliasName] = r;
|
|
185
|
+
});
|
|
186
|
+
return memo;
|
|
187
|
+
}
|
|
188
|
+
function formatRemote(remote) {
|
|
189
|
+
if (remote.entry) {
|
|
190
|
+
return `${remote.name}@${remote.entry}`;
|
|
191
|
+
}
|
|
192
|
+
if (remote.entries && remote.keyResolver) {
|
|
193
|
+
const dynamicUrl = `promise new Promise(resolve => {
|
|
194
|
+
const entries = ${JSON.stringify(remote.entries)};
|
|
195
|
+
const key = ${remote.keyResolver};
|
|
196
|
+
|
|
197
|
+
const remoteUrlWithVersion = entries[key];
|
|
198
|
+
const script = document.createElement('script')
|
|
199
|
+
script.src = remoteUrlWithVersion
|
|
200
|
+
script.onload = () => {
|
|
201
|
+
// the injected script has loaded and is available on window
|
|
202
|
+
// we can now resolve this Promise
|
|
203
|
+
const proxy = {
|
|
204
|
+
get: (request) => window.${remote.name}.get(request),
|
|
205
|
+
init: (arg) => {
|
|
206
|
+
try {
|
|
207
|
+
return window.${remote.name}.init(arg)
|
|
208
|
+
} catch(e) {
|
|
209
|
+
console.log('remote container already initialized')
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
resolve(proxy)
|
|
214
|
+
}
|
|
215
|
+
// inject this script with the src set to the versioned remoteEntry.js
|
|
216
|
+
document.head.appendChild(script);
|
|
217
|
+
})
|
|
218
|
+
`;
|
|
219
|
+
return dynamicUrl;
|
|
220
|
+
} else {
|
|
221
|
+
api.logger.error("you should provider entry or entries and keyResolver");
|
|
222
|
+
throw Error("Wrong MF#remotes config");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function constructExposes() {
|
|
226
|
+
const exposes = {};
|
|
227
|
+
const exposesPath = (0, import_path.join)(api.paths.absSrcPath, "exposes");
|
|
228
|
+
if (!(0, import_fs.existsSync)(exposesPath)) {
|
|
229
|
+
return exposes;
|
|
230
|
+
}
|
|
231
|
+
const dir = (0, import_fs.opendirSync)(exposesPath);
|
|
232
|
+
for await (const dirent of dir) {
|
|
233
|
+
if (dirent.isDirectory()) {
|
|
234
|
+
exposes["./" + dirent.name] = (0, import_plugin_utils.winPath)(
|
|
235
|
+
(0, import_path.join)(api.paths.absSrcPath, "exposes", dirent.name)
|
|
236
|
+
);
|
|
237
|
+
} else {
|
|
238
|
+
api.logger.warn(
|
|
239
|
+
`${dirent.name} is not a directory, ignore in ModuleFederation expose`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return exposes;
|
|
244
|
+
}
|
|
245
|
+
function mfName() {
|
|
246
|
+
const name = api.userConfig.mf.name;
|
|
247
|
+
if (!name) {
|
|
248
|
+
api.logger.warn(
|
|
249
|
+
`module federation name is not defined , "unNamedMF" will be used`
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
if (!isValidIdentifyName(name)) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`module federation name '${name}' is not valid javascript identifier.`
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return name || "unNamedMF";
|
|
258
|
+
}
|
|
259
|
+
function getShared() {
|
|
260
|
+
const { shared = {} } = api.userConfig.mf;
|
|
261
|
+
return shared;
|
|
262
|
+
}
|
|
263
|
+
function changeUmiEntry(config) {
|
|
264
|
+
const { entry } = config;
|
|
265
|
+
const asyncEntryPath = (0, import_plugin_utils.winPath)(
|
|
266
|
+
(0, import_path.join)(api.paths.absTmpPath, "plugin-mf", mfAsyncEntryFileName)
|
|
267
|
+
);
|
|
268
|
+
if (entry.umi) {
|
|
269
|
+
if (typeof entry.umi === "string") {
|
|
270
|
+
entry.umi = asyncEntryPath;
|
|
271
|
+
} else if (Array.isArray(entry.umi)) {
|
|
272
|
+
const i = entry.umi.findIndex((f) => f.endsWith("umi.ts"));
|
|
273
|
+
if (i >= 0) {
|
|
274
|
+
entry.umi[i] = asyncEntryPath;
|
|
275
|
+
} else {
|
|
276
|
+
api.logger.info(
|
|
277
|
+
`umi.ts not found in entry.umi ${JSON.stringify(entry.umi)}`
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
api.logger.warn("umi entry not found");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function addMFEntry(config, mfName2, path) {
|
|
286
|
+
config.entry[mfName2] = path;
|
|
287
|
+
}
|
|
288
|
+
function isValidIdentifyName(name) {
|
|
289
|
+
const reservedKeywords = [
|
|
290
|
+
"abstract",
|
|
291
|
+
"await",
|
|
292
|
+
"boolean",
|
|
293
|
+
"break",
|
|
294
|
+
"byte",
|
|
295
|
+
"case",
|
|
296
|
+
"catch",
|
|
297
|
+
"char",
|
|
298
|
+
"class",
|
|
299
|
+
"const",
|
|
300
|
+
"continue",
|
|
301
|
+
"debugger",
|
|
302
|
+
"default",
|
|
303
|
+
"delete",
|
|
304
|
+
"do",
|
|
305
|
+
"double",
|
|
306
|
+
"else",
|
|
307
|
+
"enum",
|
|
308
|
+
"export",
|
|
309
|
+
"extends",
|
|
310
|
+
"false",
|
|
311
|
+
"final",
|
|
312
|
+
"finally",
|
|
313
|
+
"float",
|
|
314
|
+
"for",
|
|
315
|
+
"function",
|
|
316
|
+
"goto",
|
|
317
|
+
"if",
|
|
318
|
+
"implements",
|
|
319
|
+
"import",
|
|
320
|
+
"in",
|
|
321
|
+
"instanceof",
|
|
322
|
+
"int",
|
|
323
|
+
"interface",
|
|
324
|
+
"let",
|
|
325
|
+
"long",
|
|
326
|
+
"native",
|
|
327
|
+
"new",
|
|
328
|
+
"null",
|
|
329
|
+
"package",
|
|
330
|
+
"private",
|
|
331
|
+
"protected",
|
|
332
|
+
"public",
|
|
333
|
+
"return",
|
|
334
|
+
"short",
|
|
335
|
+
"static",
|
|
336
|
+
"super",
|
|
337
|
+
"switch",
|
|
338
|
+
"synchronized",
|
|
339
|
+
"this",
|
|
340
|
+
"throw",
|
|
341
|
+
"transient",
|
|
342
|
+
"true",
|
|
343
|
+
"try",
|
|
344
|
+
"typeof",
|
|
345
|
+
"var",
|
|
346
|
+
"void",
|
|
347
|
+
"volatile",
|
|
348
|
+
"while",
|
|
349
|
+
"with",
|
|
350
|
+
"yield"
|
|
351
|
+
];
|
|
352
|
+
const regexIdentifierName = /^(?:[$_\p{ID_Start}])(?:[$_\u200C\u200D\p{ID_Continue}])*$/u;
|
|
353
|
+
if (reservedKeywords.includes(name) || !regexIdentifierName.test(name)) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
}
|
package/dist/model.d.ts
CHANGED