@umijs/plugins 4.0.13 → 4.0.16
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/antd.js +11 -1
- package/dist/layout.js +13 -2
- package/dist/mf.js +9 -0
- package/dist/qiankun/slave.js +88 -0
- package/dist/utils/mfUtils.d.ts +14 -0
- package/dist/utils/mfUtils.js +56 -0
- package/package.json +6 -4
- package/tpls/mf-runtime.ts.tpl +149 -0
package/dist/antd.js
CHANGED
|
@@ -45,12 +45,14 @@ var import_resolveProjectDep = require("./utils/resolveProjectDep");
|
|
|
45
45
|
var import_withTmpPath = require("./utils/withTmpPath");
|
|
46
46
|
var antd_default = (api) => {
|
|
47
47
|
let pkgPath;
|
|
48
|
+
let antdVersion = "4.0.0";
|
|
48
49
|
try {
|
|
49
50
|
pkgPath = (0, import_resolveProjectDep.resolveProjectDep)({
|
|
50
51
|
pkg: api.pkg,
|
|
51
52
|
cwd: api.cwd,
|
|
52
53
|
dep: "antd"
|
|
53
54
|
}) || (0, import_path.dirname)(require.resolve("antd/package.json"));
|
|
55
|
+
antdVersion = require(`${pkgPath}/package.json`).version;
|
|
54
56
|
} catch (e) {
|
|
55
57
|
}
|
|
56
58
|
api.describe({
|
|
@@ -94,6 +96,10 @@ var antd_default = (api) => {
|
|
|
94
96
|
if (antd.dayjs) {
|
|
95
97
|
memo.alias.moment = (0, import_path.dirname)(require.resolve("dayjs/package.json"));
|
|
96
98
|
}
|
|
99
|
+
if (antdVersion.startsWith("5")) {
|
|
100
|
+
const theme = require("@ant-design/antd-theme-variable");
|
|
101
|
+
memo.theme = __spreadValues(__spreadValues({}, theme), memo.theme);
|
|
102
|
+
}
|
|
97
103
|
if (antd.dark || antd.compact) {
|
|
98
104
|
const { getThemeVariables } = require("antd/dist/theme");
|
|
99
105
|
memo.theme = __spreadValues(__spreadValues({}, getThemeVariables(antd)), memo.theme);
|
|
@@ -105,6 +111,9 @@ var antd_default = (api) => {
|
|
|
105
111
|
});
|
|
106
112
|
api.addExtraBabelPlugins(() => {
|
|
107
113
|
const style = api.config.antd.style || "less";
|
|
114
|
+
if (antdVersion.startsWith("5")) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
108
117
|
return api.config.antd.import && !api.appData.vite ? [
|
|
109
118
|
[
|
|
110
119
|
require.resolve("babel-plugin-import"),
|
|
@@ -151,7 +160,8 @@ export function rootContainer(container) {
|
|
|
151
160
|
});
|
|
152
161
|
api.addEntryImportsAhead(() => {
|
|
153
162
|
const style = api.config.antd.style || "less";
|
|
154
|
-
|
|
163
|
+
const doNotImportLess = api.config.antd.import && !api.appData.vite || antdVersion.startsWith("5");
|
|
164
|
+
return doNotImportLess ? [] : [
|
|
155
165
|
{
|
|
156
166
|
source: style === "less" ? "antd/dist/antd.less" : "antd/dist/antd.css"
|
|
157
167
|
}
|
package/dist/layout.js
CHANGED
|
@@ -30,8 +30,19 @@ var import_fs = require("fs");
|
|
|
30
30
|
var import_path = require("path");
|
|
31
31
|
var import_umi = require("umi");
|
|
32
32
|
var import_plugin_utils = require("umi/plugin-utils");
|
|
33
|
+
var import_resolveProjectDep = require("./utils/resolveProjectDep");
|
|
33
34
|
var import_withTmpPath = require("./utils/withTmpPath");
|
|
34
35
|
var layout_default = (api) => {
|
|
36
|
+
let antdVersion = "4.0.0";
|
|
37
|
+
try {
|
|
38
|
+
const pkgPath2 = (0, import_resolveProjectDep.resolveProjectDep)({
|
|
39
|
+
pkg: api.pkg,
|
|
40
|
+
cwd: api.cwd,
|
|
41
|
+
dep: "antd"
|
|
42
|
+
}) || (0, import_path.dirname)(require.resolve("antd/package.json"));
|
|
43
|
+
antdVersion = require(`${pkgPath2}/package.json`).version;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
}
|
|
35
46
|
api.describe({
|
|
36
47
|
key: "layout",
|
|
37
48
|
config: {
|
|
@@ -427,7 +438,7 @@ export function getRightRenderContent (opts: {
|
|
|
427
438
|
api.writeTmpFile({
|
|
428
439
|
path: "Layout.less",
|
|
429
440
|
content: `
|
|
430
|
-
@import '~antd/es/style/themes/default.less';
|
|
441
|
+
${antdVersion.startsWith("5") ? "" : "@import '~antd/es/style/themes/default.less';"}
|
|
431
442
|
@pro-header-hover-bg: rgba(0, 0, 0, 0.025);
|
|
432
443
|
@media screen and (max-width: @screen-xs) {
|
|
433
444
|
// \u5728\u5C0F\u5C4F\u5E55\u7684\u65F6\u5019\u53EF\u4EE5\u6709\u66F4\u597D\u7684\u4F53\u9A8C
|
|
@@ -480,7 +491,7 @@ export function getRightRenderContent (opts: {
|
|
|
480
491
|
.umi-plugin-layout-name {
|
|
481
492
|
margin-left: 8px;
|
|
482
493
|
}
|
|
483
|
-
|
|
494
|
+
`
|
|
484
495
|
});
|
|
485
496
|
api.writeTmpFile({
|
|
486
497
|
path: "Logo.tsx",
|
package/dist/mf.js
CHANGED
|
@@ -25,6 +25,7 @@ module.exports = __toCommonJS(mf_exports);
|
|
|
25
25
|
var import_fs = require("fs");
|
|
26
26
|
var import_path = require("path");
|
|
27
27
|
var import_plugin_utils = require("umi/plugin-utils");
|
|
28
|
+
var import_mfUtils = require("./utils/mfUtils");
|
|
28
29
|
var { isEmpty } = import_plugin_utils.lodash;
|
|
29
30
|
var mfSetupPathFileName = "_mf_setup-public-path.js";
|
|
30
31
|
var mfAsyncEntryFileName = "asyncEntry.ts";
|
|
@@ -86,6 +87,14 @@ function mf(api) {
|
|
|
86
87
|
__webpack_public_path__ = document.currentScript.src + '/../';`,
|
|
87
88
|
path: mfSetupPathFileName
|
|
88
89
|
});
|
|
90
|
+
const { remotes = [] } = api.config.mf;
|
|
91
|
+
api.writeTmpFile({
|
|
92
|
+
path: "index.tsx",
|
|
93
|
+
context: {
|
|
94
|
+
remoteCodeString: (0, import_mfUtils.toRemotesCodeString)(remotes)
|
|
95
|
+
},
|
|
96
|
+
tplPath: (0, import_path.join)(__dirname, "../tpls/mf-runtime.ts.tpl")
|
|
97
|
+
});
|
|
89
98
|
if (api.env === "development" && api.config.mfsu) {
|
|
90
99
|
return;
|
|
91
100
|
}
|
package/dist/qiankun/slave.js
CHANGED
|
@@ -42,6 +42,8 @@ __export(slave_exports, {
|
|
|
42
42
|
default: () => slave_default
|
|
43
43
|
});
|
|
44
44
|
module.exports = __toCommonJS(slave_exports);
|
|
45
|
+
var import_http_proxy_middleware = require("@umijs/bundler-webpack/compiled/http-proxy-middleware");
|
|
46
|
+
var import_utils = require("@umijs/utils");
|
|
45
47
|
var import_assert = __toESM(require("assert"));
|
|
46
48
|
var import_fs = require("fs");
|
|
47
49
|
var import_path = require("path");
|
|
@@ -49,6 +51,42 @@ var import_umi = require("umi");
|
|
|
49
51
|
var import_plugin_utils = require("umi/plugin-utils");
|
|
50
52
|
var import_withTmpPath = require("../utils/withTmpPath");
|
|
51
53
|
var import_constants = require("./constants");
|
|
54
|
+
function getCurrentLocalDevServerEntry(api, req) {
|
|
55
|
+
const port = api.appData.port;
|
|
56
|
+
const hostname = req.hostname;
|
|
57
|
+
const protocol = req.protocol;
|
|
58
|
+
return `${protocol}://${hostname}${port ? ":" : ""}${port}/local-dev-server`;
|
|
59
|
+
}
|
|
60
|
+
function handleOriginalHtml(api, microAppEntry, originalHtml) {
|
|
61
|
+
const appName = api.pkg.name;
|
|
62
|
+
(0, import_assert.default)(appName, "[@umijs/plugin-qiankun]: You should have name in package.json");
|
|
63
|
+
const $ = import_utils.cheerio.load(originalHtml);
|
|
64
|
+
$("head").prepend(`<script type="extra-qiankun-config">${JSON.stringify({
|
|
65
|
+
master: {
|
|
66
|
+
apps: [
|
|
67
|
+
{
|
|
68
|
+
name: appName,
|
|
69
|
+
entry: microAppEntry,
|
|
70
|
+
extraSource: microAppEntry
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
routes: [
|
|
74
|
+
{
|
|
75
|
+
microApp: appName,
|
|
76
|
+
name: appName,
|
|
77
|
+
path: "/" + appName,
|
|
78
|
+
extraSource: microAppEntry
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
prefetch: false
|
|
82
|
+
}
|
|
83
|
+
})}<\/script>`);
|
|
84
|
+
return api.applyPlugins({
|
|
85
|
+
key: "modifyMasterHTML",
|
|
86
|
+
type: api.ApplyPluginsType.modify,
|
|
87
|
+
initialValue: $.html()
|
|
88
|
+
});
|
|
89
|
+
}
|
|
52
90
|
function isSlaveEnable(opts) {
|
|
53
91
|
var _a, _b;
|
|
54
92
|
const slaveCfg = (_b = (_a = opts.userConfig) == null ? void 0 : _a.qiankun) == null ? void 0 : _b.slave;
|
|
@@ -194,6 +232,56 @@ export { connectMaster } from './connectMaster';
|
|
|
194
232
|
},
|
|
195
233
|
before: "model"
|
|
196
234
|
});
|
|
235
|
+
api.addMiddlewares(async () => {
|
|
236
|
+
return async (req, res, next) => {
|
|
237
|
+
var _a;
|
|
238
|
+
const qiankunConfig = api.config.qiankun || {};
|
|
239
|
+
const masterEntry = (_a = qiankunConfig.slave) == null ? void 0 : _a.masterEntry;
|
|
240
|
+
const isMasterApp = qiankunConfig.master.enable;
|
|
241
|
+
const { proxyToMasterEnabled } = await api.applyPlugins({
|
|
242
|
+
key: "shouldProxyToMaster",
|
|
243
|
+
type: api.ApplyPluginsType.modify,
|
|
244
|
+
initialValue: { proxyToMasterEnabled: true, req }
|
|
245
|
+
}) ?? {};
|
|
246
|
+
if (masterEntry && proxyToMasterEnabled) {
|
|
247
|
+
if (isMasterApp) {
|
|
248
|
+
api.logger.warn("You should not set masterEntry in master app ");
|
|
249
|
+
return next();
|
|
250
|
+
}
|
|
251
|
+
return (0, import_http_proxy_middleware.createProxyMiddleware)((pathname) => pathname !== "/local-dev-server", {
|
|
252
|
+
target: masterEntry,
|
|
253
|
+
secure: false,
|
|
254
|
+
ignorePath: false,
|
|
255
|
+
followRedirects: false,
|
|
256
|
+
changeOrigin: true,
|
|
257
|
+
selfHandleResponse: true,
|
|
258
|
+
onProxyRes: (0, import_http_proxy_middleware.responseInterceptor)(async (responseBuffer, proxyRes, req2, res2) => {
|
|
259
|
+
if (proxyRes.statusCode === 302) {
|
|
260
|
+
const hostname = req2.hostname;
|
|
261
|
+
const port = process.env.PORT;
|
|
262
|
+
const goto = `${hostname}:${port}`;
|
|
263
|
+
const redirectUrl = proxyRes.headers.location.replace(encodeURIComponent(new URL(masterEntry).hostname), encodeURIComponent(goto)) || masterEntry;
|
|
264
|
+
const redirectMessage = `[@umijs/plugin-qiankun]: redirect to ${redirectUrl}`;
|
|
265
|
+
api.logger.info(redirectMessage);
|
|
266
|
+
res2.statusCode = 302;
|
|
267
|
+
res2.setHeader("location", redirectUrl);
|
|
268
|
+
return redirectMessage;
|
|
269
|
+
}
|
|
270
|
+
const microAppEntry = getCurrentLocalDevServerEntry(api, req2);
|
|
271
|
+
const originalHtml = responseBuffer.toString("utf8");
|
|
272
|
+
const html = handleOriginalHtml(api, microAppEntry, originalHtml);
|
|
273
|
+
return html;
|
|
274
|
+
}),
|
|
275
|
+
onError(err, _, res2) {
|
|
276
|
+
api.logger.error(err);
|
|
277
|
+
res2.set("content-type", "text/plain; charset=UTF-8");
|
|
278
|
+
res2.end(`[@umijs/plugin-qiankun] \u4EE3\u7406\u5230 ${masterEntry} \u65F6\u51FA\u9519\u4E86\uFF0C\u8BF7\u5C1D\u8BD5 ${masterEntry} \u662F\u5426\u662F\u53EF\u4EE5\u6B63\u5E38\u8BBF\u95EE\u7684\uFF0C\u7136\u540E\u91CD\u65B0\u542F\u52A8\u9879\u76EE\u8BD5\u8BD5\u3002(\u6CE8\u610F\u5982\u679C\u51FA\u73B0\u8DE8\u57DF\u95EE\u9898\uFF0C\u8BF7\u4FEE\u6539\u672C\u5730 host \uFF0C\u901A\u8FC7\u4E00\u4E2A\u548C\u4E3B\u5E94\u7528\u76F8\u540C\u7684\u4E00\u7EA7\u57DF\u540D\u7684\u57DF\u540D\u6765\u8BBF\u95EE 127.0.0.1)`);
|
|
279
|
+
}
|
|
280
|
+
})(req, res, next);
|
|
281
|
+
}
|
|
282
|
+
return next();
|
|
283
|
+
};
|
|
284
|
+
});
|
|
197
285
|
};
|
|
198
286
|
// Annotate the CommonJS export names for ESM import in node:
|
|
199
287
|
0 && (module.exports = {});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare type SimpleRemote = {
|
|
2
|
+
entry: string;
|
|
3
|
+
name: string;
|
|
4
|
+
aliasName?: string;
|
|
5
|
+
};
|
|
6
|
+
declare type RemoteEntries = {
|
|
7
|
+
name: string;
|
|
8
|
+
entries: object;
|
|
9
|
+
keyResolver: string;
|
|
10
|
+
aliasName?: string;
|
|
11
|
+
};
|
|
12
|
+
declare type Remote = SimpleRemote | RemoteEntries;
|
|
13
|
+
export declare function toRemotesCodeString(remotes: Remote[]): string;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
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/utils/mfUtils.ts
|
|
20
|
+
var mfUtils_exports = {};
|
|
21
|
+
__export(mfUtils_exports, {
|
|
22
|
+
toRemotesCodeString: () => toRemotesCodeString
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(mfUtils_exports);
|
|
25
|
+
function isSimpleRemote(r) {
|
|
26
|
+
return r.entry;
|
|
27
|
+
}
|
|
28
|
+
function isRemoteEntries(r) {
|
|
29
|
+
return r.entries && r.keyResolver;
|
|
30
|
+
}
|
|
31
|
+
function toRemotesCodeString(remotes) {
|
|
32
|
+
const res = [];
|
|
33
|
+
for (const r of remotes) {
|
|
34
|
+
const aliasName = r.aliasName || r.name;
|
|
35
|
+
const remoteName = r.name;
|
|
36
|
+
if (isSimpleRemote(r)) {
|
|
37
|
+
res.push(`${aliasName}: {
|
|
38
|
+
aliasName: "${aliasName}",
|
|
39
|
+
remoteName: "${remoteName}",
|
|
40
|
+
entry: "${r.entry}"
|
|
41
|
+
}`);
|
|
42
|
+
}
|
|
43
|
+
if (isRemoteEntries(r)) {
|
|
44
|
+
res.push(`${aliasName}: {
|
|
45
|
+
aliasName: "${aliasName}",
|
|
46
|
+
remoteName: "${remoteName}",
|
|
47
|
+
entry: (${JSON.stringify(r.entries)})[${r.keyResolver}]
|
|
48
|
+
}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return res.join(",\n");
|
|
52
|
+
}
|
|
53
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
+
0 && (module.exports = {
|
|
55
|
+
toRemotesCodeString
|
|
56
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/plugins",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.16",
|
|
4
4
|
"description": "@umijs/plugins",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi/tree/master/packages/plugins#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi/issues",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"types": "dist/index.d.ts",
|
|
14
14
|
"files": [
|
|
15
15
|
"dist",
|
|
16
|
-
"libs"
|
|
16
|
+
"libs",
|
|
17
|
+
"tpls"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
19
20
|
"build": "umi-scripts father build",
|
|
@@ -23,9 +24,10 @@
|
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
26
|
"@ahooksjs/use-request": "^2.0.0",
|
|
27
|
+
"@ant-design/antd-theme-variable": "^1.0.0",
|
|
26
28
|
"@ant-design/icons": "^4.7.0",
|
|
27
29
|
"@ant-design/pro-layout": "^7.0.1-beta.28",
|
|
28
|
-
"@umijs/bundler-utils": "4.0.
|
|
30
|
+
"@umijs/bundler-utils": "4.0.16",
|
|
29
31
|
"antd-dayjs-webpack-plugin": "^1.0.6",
|
|
30
32
|
"axios": "^0.27.2",
|
|
31
33
|
"babel-plugin-import": "^1.13.5",
|
|
@@ -44,7 +46,7 @@
|
|
|
44
46
|
"warning": "^4.0.3"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
|
-
"umi": "4.0.
|
|
49
|
+
"umi": "4.0.16"
|
|
48
50
|
},
|
|
49
51
|
"publishConfig": {
|
|
50
52
|
"access": "public"
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { lazy, Suspense } from 'react'
|
|
2
|
+
import type { Component, ReactNode } from 'react'
|
|
3
|
+
const remotes = {
|
|
4
|
+
{{{ remoteCodeString }}}
|
|
5
|
+
};
|
|
6
|
+
const scriptLoadedMap: Record<string, Promise<void> | 0 | undefined> = {};
|
|
7
|
+
|
|
8
|
+
type MFModuleImportRequest = { entry: string; remoteName: string; moduleName: string; }
|
|
9
|
+
type MFModuleRegisterRequest = { entry: string; remoteName: string; aliasName?:string; }
|
|
10
|
+
|
|
11
|
+
export async function rawMfImport(opts: MFModuleImportRequest) {
|
|
12
|
+
await loadRemoteScriptWithCache(opts.remoteName, opts.entry);
|
|
13
|
+
return getRemoteModule(opts.remoteName, opts.moduleName);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function registerMfRemote (opts: MFModuleRegisterRequest) {
|
|
17
|
+
const aliasName = opts.aliasName || opts.remoteName;
|
|
18
|
+
if( remotes[aliasName] ){
|
|
19
|
+
return console.warn(`registerMfRemote: ${aliasName} is already registered as`, remotes[aliasName]);
|
|
20
|
+
}
|
|
21
|
+
remotes[aliasName] ={
|
|
22
|
+
...opts,
|
|
23
|
+
aliasName,
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function safeMfImport(moduleSpecifier: any, fallback: any) {
|
|
28
|
+
try {
|
|
29
|
+
const i = moduleSpecifier.indexOf('/');
|
|
30
|
+
if (i < 0) {
|
|
31
|
+
console.error(
|
|
32
|
+
`safeMfImport: bad Module Name ${moduleSpecifier}, should match pattern 'remote/moduleName'`,
|
|
33
|
+
moduleSpecifier,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return fallback;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const aliasName = moduleSpecifier.slice(0, i);
|
|
40
|
+
const module = moduleSpecifier.slice(i + 1);
|
|
41
|
+
const entry = remotes[aliasName]?.entry;
|
|
42
|
+
const remoteName = remotes[aliasName]?.remoteName;
|
|
43
|
+
|
|
44
|
+
if(!entry){
|
|
45
|
+
console.error(
|
|
46
|
+
`safeMfImport: bad Module Name ${moduleSpecifier}, no remote for "aliasName"`,
|
|
47
|
+
moduleSpecifier,
|
|
48
|
+
);
|
|
49
|
+
return fallback;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
await loadRemoteScriptWithCache(remoteName, entry);
|
|
53
|
+
|
|
54
|
+
return getRemoteModule(remoteName, module);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error('safeMfImport: Module', moduleSpecifier, 'failed', e);
|
|
57
|
+
return fallback;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function loadScript(url): Promise<void> {
|
|
62
|
+
const element = document.createElement('script');
|
|
63
|
+
element.src = url;
|
|
64
|
+
element.type = 'text/javascript';
|
|
65
|
+
element.async = true;
|
|
66
|
+
|
|
67
|
+
const loadScriptQ = new Promise<void>((resolve, reject) => {
|
|
68
|
+
element.onload = () => {
|
|
69
|
+
document.head.removeChild(element);
|
|
70
|
+
resolve();
|
|
71
|
+
};
|
|
72
|
+
element.onerror = (e) => {
|
|
73
|
+
document.head.removeChild(element);
|
|
74
|
+
reject(e);
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
document.head.appendChild(element);
|
|
78
|
+
|
|
79
|
+
return loadScriptQ;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function getRemoteModule(remoteName:string, moduleName: string): any {
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
await __webpack_init_sharing__('default');
|
|
85
|
+
const container = window[remoteName]; // or get the container somewhere else
|
|
86
|
+
// Initialize the container, it may provide shared modules
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
await container.init(__webpack_share_scopes__.default);
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
const factory = await window[remoteName].get(`./${moduleName}`);
|
|
91
|
+
return factory();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function loadRemoteScriptWithCache(remoteName:string, url: string): Promise<void> {
|
|
95
|
+
|
|
96
|
+
const loadCache = scriptLoadedMap[remoteName];
|
|
97
|
+
let scriptLoadQ: Promise<void> | null = null;
|
|
98
|
+
|
|
99
|
+
if(loadCache === 0){
|
|
100
|
+
// script Already loaded
|
|
101
|
+
return;
|
|
102
|
+
} else if(loadCache) {
|
|
103
|
+
await loadCache
|
|
104
|
+
}else{
|
|
105
|
+
let p = loadScript(url).then(()=>{
|
|
106
|
+
scriptLoadedMap[remoteName] = 0;
|
|
107
|
+
},(e)=>{
|
|
108
|
+
scriptLoadedMap[remoteName] = undefined;
|
|
109
|
+
throw e;
|
|
110
|
+
});
|
|
111
|
+
scriptLoadedMap[remoteName] = p;
|
|
112
|
+
await p;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type SafeRemoteComponentOpts ={
|
|
117
|
+
moduleSpecifier:string;
|
|
118
|
+
fallbackComponent: ComponentType<any>;
|
|
119
|
+
loadingElement: ReactNode
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function safeRemoteComponent<T extends ComponentType<any>>(opts: SafeRemoteComponentOpts): T {
|
|
123
|
+
const Lazy = lazy<T>(()=>safeMfImport(opts.moduleSpecifier, { default: opts.fallbackComponent }));
|
|
124
|
+
return (props)=> (<Suspense fallback={opts.loadingElement}>
|
|
125
|
+
<Lazy {...props} />
|
|
126
|
+
</Suspense>)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
type RawRemoteComponentOpts ={
|
|
130
|
+
mfConfig:{
|
|
131
|
+
entry:string;
|
|
132
|
+
remoteName: string;
|
|
133
|
+
moduleName: string;
|
|
134
|
+
};
|
|
135
|
+
fallbackComponent: ComponentType<any>;
|
|
136
|
+
loadingElement: ReactNode;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function safeRemoteComponentWithMfConfig<T extends ComponentType<any>>(opts: RawRemoteComponentOpts): T {
|
|
140
|
+
const Lazy = lazy<T>(()=>{
|
|
141
|
+
return rawMfImport(opts.mfConfig)
|
|
142
|
+
.catch(()=>{
|
|
143
|
+
return { default: opts.fallbackComponent };
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
return (props)=> (<Suspense fallback={opts.loadingElement}>
|
|
147
|
+
<Lazy {...props} />
|
|
148
|
+
</Suspense>)
|
|
149
|
+
}
|