@modern-js/plugin-garfish 2.54.6 → 2.55.0
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/cjs/cli/code.js +75 -0
- package/dist/cjs/cli/index.js +45 -123
- package/dist/cjs/cli/template.js +48 -0
- package/dist/cjs/cli/utils.js +7 -100
- package/dist/cjs/runtime/index.js +8 -0
- package/dist/cjs/runtime/plugin.js +9 -4
- package/dist/cjs/runtime/provider.js +79 -0
- package/dist/cjs/runtime/render.js +81 -0
- package/dist/esm/cli/code.js +80 -0
- package/dist/esm/cli/index.js +72 -136
- package/dist/esm/cli/template.js +20 -0
- package/dist/esm/cli/utils.js +4 -92
- package/dist/esm/runtime/index.js +6 -1
- package/dist/esm/runtime/plugin.js +6 -6
- package/dist/esm/runtime/provider.js +74 -0
- package/dist/esm/runtime/render.js +77 -0
- package/dist/esm-node/cli/code.js +40 -0
- package/dist/esm-node/cli/index.js +47 -125
- package/dist/esm-node/cli/template.js +24 -0
- package/dist/esm-node/cli/utils.js +6 -97
- package/dist/esm-node/runtime/index.js +6 -1
- package/dist/esm-node/runtime/plugin.js +5 -4
- package/dist/esm-node/runtime/provider.js +55 -0
- package/dist/esm-node/runtime/render.js +56 -0
- package/dist/types/cli/code.d.ts +7 -0
- package/dist/types/cli/index.d.ts +12 -5
- package/dist/types/cli/template.d.ts +11 -0
- package/dist/types/cli/utils.d.ts +1 -3
- package/dist/types/runtime/index.d.ts +3 -1
- package/dist/types/runtime/plugin.d.ts +2 -2
- package/dist/types/runtime/provider.d.ts +17 -0
- package/dist/types/runtime/render.d.ts +5 -0
- package/package.json +13 -23
- package/type.d.ts +8 -1
- package/dist/cjs/deps/index.js +0 -38
- package/dist/cjs/index.js +0 -40
- package/dist/esm/deps/index.js +0 -4
- package/dist/esm/index.js +0 -5
- package/dist/esm-node/deps/index.js +0 -4
- package/dist/esm-node/index.js +0 -5
- package/dist/types/deps/index.d.ts +0 -2
- package/dist/types/index.d.ts +0 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
+
import { createRoot } from "@modern-js/runtime/react";
|
|
6
|
+
import { createPortal, unmountComponentAtNode } from "react-dom";
|
|
7
|
+
import { garfishRender } from "./render";
|
|
8
|
+
function createProvider(id, customBootstrap) {
|
|
9
|
+
return function(param) {
|
|
10
|
+
var basename = param.basename, dom = param.dom;
|
|
11
|
+
var root = null;
|
|
12
|
+
return {
|
|
13
|
+
render: function render(param2) {
|
|
14
|
+
var basename2 = param2.basename, dom2 = param2.dom, props = param2.props, appName = param2.appName;
|
|
15
|
+
return _async_to_generator(function() {
|
|
16
|
+
return _ts_generator(this, function(_state) {
|
|
17
|
+
switch (_state.label) {
|
|
18
|
+
case 0:
|
|
19
|
+
return [
|
|
20
|
+
4,
|
|
21
|
+
garfishRender(id || "root", customBootstrap, {
|
|
22
|
+
basename: basename2,
|
|
23
|
+
dom: dom2,
|
|
24
|
+
props,
|
|
25
|
+
appName
|
|
26
|
+
})
|
|
27
|
+
];
|
|
28
|
+
case 1:
|
|
29
|
+
root = _state.sent();
|
|
30
|
+
return [
|
|
31
|
+
2
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
})();
|
|
36
|
+
},
|
|
37
|
+
destroy: function destroy(param2) {
|
|
38
|
+
var dom2 = param2.dom;
|
|
39
|
+
var node = dom2.querySelector("#".concat(id || "root")) || dom2;
|
|
40
|
+
if (node) {
|
|
41
|
+
if (process.env.IS_REACT18 === "true") {
|
|
42
|
+
root.unmount();
|
|
43
|
+
} else {
|
|
44
|
+
unmountComponentAtNode(node);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
// 兼容旧版本
|
|
49
|
+
SubModuleComponent: function(props) {
|
|
50
|
+
var ModernRoot = createRoot(null, {
|
|
51
|
+
router: {
|
|
52
|
+
basename
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
56
|
+
basename
|
|
57
|
+
}, props)), dom.querySelector("#".concat(id || "root")) || dom);
|
|
58
|
+
},
|
|
59
|
+
jupiter_submodule_app_key: function(props) {
|
|
60
|
+
var ModernRoot = createRoot(null, {
|
|
61
|
+
router: {
|
|
62
|
+
basename
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
66
|
+
basename
|
|
67
|
+
}, props)), dom.querySelector("#".concat(id || "root")) || dom);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export {
|
|
73
|
+
createProvider
|
|
74
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
+
import { render } from "@modern-js/runtime/browser";
|
|
6
|
+
import { createRoot } from "@modern-js/runtime/react";
|
|
7
|
+
function isRenderGarfish(params) {
|
|
8
|
+
var renderByGarfish = typeof __GARFISH_EXPORTS__ !== "undefined" || typeof window !== "undefined" && window.Garfish && window.Garfish.activeApps && window.Garfish.activeApps.some(function(app) {
|
|
9
|
+
var _app_appInfo;
|
|
10
|
+
return ((_app_appInfo = app.appInfo) === null || _app_appInfo === void 0 ? void 0 : _app_appInfo.name) === (params === null || params === void 0 ? void 0 : params.appName);
|
|
11
|
+
});
|
|
12
|
+
return renderByGarfish;
|
|
13
|
+
}
|
|
14
|
+
function canContinueRender(param) {
|
|
15
|
+
var dom = param.dom, appName = param.appName;
|
|
16
|
+
var renderByGarfish = isRenderGarfish({
|
|
17
|
+
appName
|
|
18
|
+
});
|
|
19
|
+
var renderByProvider = dom || appName;
|
|
20
|
+
if (renderByGarfish) {
|
|
21
|
+
if (renderByProvider) {
|
|
22
|
+
return true;
|
|
23
|
+
} else {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function garfishRender(mountId, customBootstrap, _params) {
|
|
31
|
+
return _garfishRender.apply(this, arguments);
|
|
32
|
+
}
|
|
33
|
+
function _garfishRender() {
|
|
34
|
+
_garfishRender = _async_to_generator(function(mountId, customBootstrap, _params) {
|
|
35
|
+
var _ref, basename, props, dom, appName, ModernRoot;
|
|
36
|
+
var _arguments = arguments;
|
|
37
|
+
return _ts_generator(this, function(_state) {
|
|
38
|
+
_ref = // eslint-disable-next-line prefer-rest-params
|
|
39
|
+
typeof _arguments[2] === "object" && _arguments[2] || {}, basename = _ref.basename, props = _ref.props, dom = _ref.dom, appName = _ref.appName;
|
|
40
|
+
if (canContinueRender({
|
|
41
|
+
dom,
|
|
42
|
+
appName
|
|
43
|
+
})) {
|
|
44
|
+
ModernRoot = createRoot(null, {
|
|
45
|
+
router: {
|
|
46
|
+
basename
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
if (customBootstrap) {
|
|
50
|
+
return [
|
|
51
|
+
2,
|
|
52
|
+
customBootstrap(ModernRoot, function() {
|
|
53
|
+
return render(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
54
|
+
basename
|
|
55
|
+
}, props)), dom || mountId);
|
|
56
|
+
})
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
return [
|
|
60
|
+
2,
|
|
61
|
+
render(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
62
|
+
basename
|
|
63
|
+
}, props)), dom || mountId)
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
return [
|
|
67
|
+
2,
|
|
68
|
+
null
|
|
69
|
+
];
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
return _garfishRender.apply(this, arguments);
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
garfishRender,
|
|
76
|
+
isRenderGarfish
|
|
77
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import * as template from "./template";
|
|
4
|
+
import { generateAsyncEntryCode } from "./utils";
|
|
5
|
+
const ENTRY_POINT_FILE_NAME = "index.jsx";
|
|
6
|
+
const ENTRY_BOOTSTRAP_FILE_NAME = "bootstrap.jsx";
|
|
7
|
+
const generateCode = async (entrypoints, appContext, config, appendEntryCode) => {
|
|
8
|
+
const { mountId } = config.html;
|
|
9
|
+
const { enableAsyncEntry } = config.source;
|
|
10
|
+
const { internalDirectory, internalSrcAlias, metaName, srcDirectory } = appContext;
|
|
11
|
+
await Promise.all(entrypoints.map(async (entrypoint) => {
|
|
12
|
+
const { entryName, isAutoMount, entry, customEntry, customBootstrap } = entrypoint;
|
|
13
|
+
const appendCode = await appendEntryCode({
|
|
14
|
+
entrypoint
|
|
15
|
+
});
|
|
16
|
+
if (isAutoMount) {
|
|
17
|
+
const indexCode = template.index({
|
|
18
|
+
srcDirectory,
|
|
19
|
+
internalSrcAlias,
|
|
20
|
+
metaName,
|
|
21
|
+
entry,
|
|
22
|
+
entryName,
|
|
23
|
+
customEntry,
|
|
24
|
+
customBootstrap,
|
|
25
|
+
mountId,
|
|
26
|
+
appendCode
|
|
27
|
+
});
|
|
28
|
+
const indexFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
|
|
29
|
+
fs.outputFileSync(indexFile, indexCode, "utf8");
|
|
30
|
+
if (enableAsyncEntry) {
|
|
31
|
+
const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
|
|
32
|
+
fs.outputFileSync(bootstrapFile, generateAsyncEntryCode(appendCode), "utf8");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}));
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
ENTRY_BOOTSTRAP_FILE_NAME,
|
|
39
|
+
generateCode
|
|
40
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
1
|
+
import { createRuntimeExportsUtils, getEntryOptions } from "@modern-js/utils";
|
|
2
|
+
import { createAsyncWorkflow } from "@modern-js/core";
|
|
2
3
|
import { logger } from "../util";
|
|
3
|
-
import { getRuntimeConfig,
|
|
4
|
+
import { getRuntimeConfig, setRuntimeConfig } from "./utils";
|
|
5
|
+
import { generateCode } from "./code";
|
|
4
6
|
const externals = {
|
|
5
7
|
"react-dom": "react-dom",
|
|
6
8
|
react: "react"
|
|
@@ -19,11 +21,33 @@ function getDefaultMicroFrontedConfig(microFrontend) {
|
|
|
19
21
|
...microFrontend
|
|
20
22
|
};
|
|
21
23
|
}
|
|
22
|
-
const
|
|
24
|
+
const appendEntryCode = createAsyncWorkflow();
|
|
25
|
+
const garfishPlugin = () => ({
|
|
23
26
|
name: "@modern-js/plugin-garfish",
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
pre: [
|
|
28
|
+
"@modern-js/runtime"
|
|
29
|
+
],
|
|
30
|
+
registerHook: {
|
|
31
|
+
appendEntryCode
|
|
32
|
+
},
|
|
33
|
+
setup: (api) => {
|
|
26
34
|
return {
|
|
35
|
+
_internalRuntimePlugins({ entrypoint, plugins }) {
|
|
36
|
+
const userConfig = api.useResolvedConfigContext();
|
|
37
|
+
const { packageName, metaName } = api.useAppContext();
|
|
38
|
+
const runtimeConfig = getEntryOptions(entrypoint.entryName, entrypoint.isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
39
|
+
if (runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.masterApp) {
|
|
40
|
+
plugins.push({
|
|
41
|
+
name: "garfish",
|
|
42
|
+
path: `@${metaName}/plugin-garfish/runtime`,
|
|
43
|
+
config: (runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.masterApp) || {}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
entrypoint,
|
|
48
|
+
plugins
|
|
49
|
+
};
|
|
50
|
+
},
|
|
27
51
|
resolvedConfig: async (config) => {
|
|
28
52
|
const { resolved } = config;
|
|
29
53
|
const { masterApp, router } = getRuntimeConfig(resolved);
|
|
@@ -34,7 +58,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
34
58
|
};
|
|
35
59
|
if (masterApp) {
|
|
36
60
|
var _useConfig_server, _router_historyOptions;
|
|
37
|
-
const useConfig = useConfigContext();
|
|
61
|
+
const useConfig = api.useConfigContext();
|
|
38
62
|
const baseUrl = useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.baseUrl;
|
|
39
63
|
if (Array.isArray(baseUrl)) {
|
|
40
64
|
throw new Error("Now Micro-Front-End mode dose not support multiple baseUrl, you can set it as a string");
|
|
@@ -55,10 +79,9 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
55
79
|
},
|
|
56
80
|
config() {
|
|
57
81
|
var _useConfig_output, _useConfig_deploy;
|
|
58
|
-
const useConfig = useConfigContext();
|
|
82
|
+
const useConfig = api.useConfigContext();
|
|
83
|
+
const { metaName, packageName } = api.useAppContext();
|
|
59
84
|
logger("useConfig", useConfig);
|
|
60
|
-
const config = useAppContext();
|
|
61
|
-
pluginsExportsUtils = createRuntimeExportsUtils(config.internalDirectory, "plugins");
|
|
62
85
|
let disableCssExtract = ((_useConfig_output = useConfig.output) === null || _useConfig_output === void 0 ? void 0 : _useConfig_output.disableCssExtract) || false;
|
|
63
86
|
if ((_useConfig_deploy = useConfig.deploy) === null || _useConfig_deploy === void 0 ? void 0 : _useConfig_deploy.microFrontend) {
|
|
64
87
|
var _useConfig_deploy1;
|
|
@@ -73,8 +96,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
73
96
|
},
|
|
74
97
|
source: {
|
|
75
98
|
alias: {
|
|
76
|
-
|
|
77
|
-
"@modern-js/runtime/garfish": "@modern-js/plugin-garfish/runtime"
|
|
99
|
+
[`@${metaName}/runtime/garfish`]: `@${metaName}/plugin-garfish/runtime`
|
|
78
100
|
}
|
|
79
101
|
},
|
|
80
102
|
tools: {
|
|
@@ -92,7 +114,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
92
114
|
}
|
|
93
115
|
]);
|
|
94
116
|
}
|
|
95
|
-
const resolveOptions = useResolvedConfigContext();
|
|
117
|
+
const resolveOptions = api.useResolvedConfigContext();
|
|
96
118
|
if (resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions_deploy = resolveOptions.deploy) === null || _resolveOptions_deploy === void 0 ? void 0 : _resolveOptions_deploy.microFrontend) {
|
|
97
119
|
var _resolveOptions_dev, _useConfig_dev, _resolveOptions_server, _resolveOptions_deploy1;
|
|
98
120
|
chain.output.libraryTarget("umd");
|
|
@@ -117,7 +139,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
117
139
|
}
|
|
118
140
|
const uniqueName = chain.output.get("uniqueName");
|
|
119
141
|
if (!uniqueName) {
|
|
120
|
-
chain.output.uniqueName(
|
|
142
|
+
chain.output.uniqueName(packageName);
|
|
121
143
|
}
|
|
122
144
|
const resolveConfig = chain.toConfig();
|
|
123
145
|
logger("bundlerConfig", {
|
|
@@ -132,125 +154,25 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
132
154
|
};
|
|
133
155
|
},
|
|
134
156
|
addRuntimeExports() {
|
|
135
|
-
const config = useResolvedConfigContext();
|
|
157
|
+
const config = api.useResolvedConfigContext();
|
|
136
158
|
const { masterApp } = getRuntimeConfig(config);
|
|
159
|
+
const { internalDirectory, metaName } = api.useAppContext();
|
|
160
|
+
const pluginsExportsUtils = createRuntimeExportsUtils(internalDirectory, "plugins");
|
|
137
161
|
if (masterApp) {
|
|
138
|
-
const addExportStatement = `export { default as garfish, default as masterApp } from '${
|
|
162
|
+
const addExportStatement = `export { default as garfish, default as masterApp } from '${metaName}/plugin-garfish/runtime'`;
|
|
139
163
|
logger("exportStatement", addExportStatement);
|
|
140
164
|
pluginsExportsUtils.addExport(addExportStatement);
|
|
141
165
|
}
|
|
142
|
-
const otherExportStatement = `export { hoistNonReactStatics } from '${pluginName}/deps'`;
|
|
143
|
-
logger("otherExportStatement", otherExportStatement);
|
|
144
|
-
pluginsExportsUtils.addExport(otherExportStatement);
|
|
145
|
-
},
|
|
146
|
-
modifyEntryImports({ entrypoint, imports }) {
|
|
147
|
-
const config = useResolvedConfigContext();
|
|
148
|
-
const { masterApp } = getRuntimeConfig(config);
|
|
149
|
-
if (masterApp) {
|
|
150
|
-
imports.push({
|
|
151
|
-
value: runtimePluginName,
|
|
152
|
-
specifiers: [
|
|
153
|
-
{
|
|
154
|
-
imported: "garfish"
|
|
155
|
-
}
|
|
156
|
-
]
|
|
157
|
-
});
|
|
158
|
-
imports.push({
|
|
159
|
-
value: runtimePluginName,
|
|
160
|
-
specifiers: [
|
|
161
|
-
{
|
|
162
|
-
imported: "masterApp"
|
|
163
|
-
}
|
|
164
|
-
]
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
imports.push({
|
|
168
|
-
value: runtimePluginName,
|
|
169
|
-
specifiers: [
|
|
170
|
-
{
|
|
171
|
-
imported: "hoistNonReactStatics"
|
|
172
|
-
}
|
|
173
|
-
]
|
|
174
|
-
});
|
|
175
|
-
imports.push({
|
|
176
|
-
value: "react-dom",
|
|
177
|
-
specifiers: [
|
|
178
|
-
{
|
|
179
|
-
imported: "unmountComponentAtNode"
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
imported: "createPortal"
|
|
183
|
-
}
|
|
184
|
-
]
|
|
185
|
-
});
|
|
186
|
-
return {
|
|
187
|
-
imports,
|
|
188
|
-
entrypoint
|
|
189
|
-
};
|
|
190
|
-
},
|
|
191
|
-
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
|
192
|
-
const config = useResolvedConfigContext();
|
|
193
|
-
const { masterApp } = getRuntimeConfig(config);
|
|
194
|
-
if (masterApp) {
|
|
195
|
-
logger("garfishPlugin options", masterApp);
|
|
196
|
-
plugins.push({
|
|
197
|
-
name: "garfish",
|
|
198
|
-
args: "masterApp",
|
|
199
|
-
options: masterApp === true ? JSON.stringify({}) : JSON.stringify(masterApp)
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
return {
|
|
203
|
-
entrypoint,
|
|
204
|
-
plugins
|
|
205
|
-
};
|
|
206
|
-
},
|
|
207
|
-
modifyEntryRenderFunction({ entrypoint, code }) {
|
|
208
|
-
var _config_deploy;
|
|
209
|
-
const config = useResolvedConfigContext();
|
|
210
|
-
if (!(config === null || config === void 0 ? void 0 : (_config_deploy = config.deploy) === null || _config_deploy === void 0 ? void 0 : _config_deploy.microFrontend)) {
|
|
211
|
-
return {
|
|
212
|
-
entrypoint,
|
|
213
|
-
code
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
const nCode = makeRenderFunction(code);
|
|
217
|
-
logger("makeRenderFunction", nCode);
|
|
218
|
-
return {
|
|
219
|
-
entrypoint,
|
|
220
|
-
code: nCode
|
|
221
|
-
};
|
|
222
|
-
},
|
|
223
|
-
modifyAsyncEntry({ entrypoint, code }) {
|
|
224
|
-
var _config_deploy, _config_source;
|
|
225
|
-
const config = useResolvedConfigContext();
|
|
226
|
-
let finalCode = code;
|
|
227
|
-
if ((config === null || config === void 0 ? void 0 : (_config_deploy = config.deploy) === null || _config_deploy === void 0 ? void 0 : _config_deploy.microFrontend) && (config === null || config === void 0 ? void 0 : (_config_source = config.source) === null || _config_source === void 0 ? void 0 : _config_source.enableAsyncEntry)) {
|
|
228
|
-
finalCode = generateAsyncEntry(code);
|
|
229
|
-
return {
|
|
230
|
-
entrypoint,
|
|
231
|
-
code: `${finalCode}`
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
return {
|
|
235
|
-
entrypoint,
|
|
236
|
-
code: finalCode
|
|
237
|
-
};
|
|
238
166
|
},
|
|
239
|
-
|
|
240
|
-
var
|
|
241
|
-
const
|
|
242
|
-
if (
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
exportStatement: exportStatementCode
|
|
248
|
-
};
|
|
167
|
+
async generateEntryCode({ entrypoints }) {
|
|
168
|
+
var _resolveOptions_deploy;
|
|
169
|
+
const resolveOptions = api.useResolvedConfigContext();
|
|
170
|
+
if (resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions_deploy = resolveOptions.deploy) === null || _resolveOptions_deploy === void 0 ? void 0 : _resolveOptions_deploy.microFrontend) {
|
|
171
|
+
const appContext = api.useAppContext();
|
|
172
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
173
|
+
const { appendEntryCode: appendEntryCode2 } = api.useHookRunners();
|
|
174
|
+
await generateCode(entrypoints, appContext, resolvedConfig, appendEntryCode2);
|
|
249
175
|
}
|
|
250
|
-
return {
|
|
251
|
-
entrypoint,
|
|
252
|
-
exportStatement
|
|
253
|
-
};
|
|
254
176
|
}
|
|
255
177
|
};
|
|
256
178
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { formatImportPath } from "@modern-js/utils";
|
|
2
|
+
const genRenderCode = ({ srcDirectory, internalSrcAlias, metaName, entry, customEntry, customBootstrap, mountId }) => customEntry ? `import '${entry.replace(srcDirectory, internalSrcAlias)}'
|
|
3
|
+
export * from '${entry.replace(srcDirectory, internalSrcAlias)}'` : `import { garfishRender, createProvider } from '@${metaName}/plugin-garfish/runtime';
|
|
4
|
+
|
|
5
|
+
${customBootstrap ? `import customBootstrap from '${formatImportPath(customBootstrap.replace(srcDirectory, internalSrcAlias))}';` : "let customBootstrap;"}
|
|
6
|
+
garfishRender('${mountId || "root"}', customBootstrap)
|
|
7
|
+
|
|
8
|
+
export const provider = createProvider(undefined, ${customBootstrap ? "customBootstrap" : void 0});
|
|
9
|
+
`;
|
|
10
|
+
const index = ({ srcDirectory, internalSrcAlias, metaName, entry, entryName, customEntry, customBootstrap, mountId, appendCode = [] }) => `import '@${metaName}/runtime/registry/${entryName}';
|
|
11
|
+
${genRenderCode({
|
|
12
|
+
srcDirectory,
|
|
13
|
+
internalSrcAlias,
|
|
14
|
+
metaName,
|
|
15
|
+
entry,
|
|
16
|
+
customEntry,
|
|
17
|
+
customBootstrap,
|
|
18
|
+
mountId
|
|
19
|
+
})}
|
|
20
|
+
${appendCode.join("\n")}
|
|
21
|
+
`;
|
|
22
|
+
export {
|
|
23
|
+
index
|
|
24
|
+
};
|
|
@@ -1,93 +1,3 @@
|
|
|
1
|
-
const makeProvider = () => `
|
|
2
|
-
export const provider = function ({basename, dom}) {
|
|
3
|
-
return {
|
|
4
|
-
render({basename, dom, props, appName}) {
|
|
5
|
-
render({ props, basename, dom, appName });
|
|
6
|
-
},
|
|
7
|
-
destroy({ dom }) {
|
|
8
|
-
const node = dom.querySelector('#' + MOUNT_ID) || dom;
|
|
9
|
-
|
|
10
|
-
if (node) {
|
|
11
|
-
if (IS_REACT18) {
|
|
12
|
-
root.unmount();
|
|
13
|
-
} else {
|
|
14
|
-
unmountComponentAtNode(node);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
SubModuleComponent: (props) => {
|
|
19
|
-
const SubApp = render({props, basename});
|
|
20
|
-
|
|
21
|
-
return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
|
|
22
|
-
},
|
|
23
|
-
jupiter_submodule_app_key: (props) => {
|
|
24
|
-
const SubApp = render({props, basename});
|
|
25
|
-
|
|
26
|
-
return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
|
|
32
|
-
__GARFISH_EXPORTS__.provider = provider;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function canContinueRender ({ dom, appName }) {
|
|
36
|
-
var renderByGarfish =
|
|
37
|
-
typeof __GARFISH_EXPORTS__ !== 'undefined'
|
|
38
|
-
|| typeof window !== 'undefined' && window.Garfish && window.Garfish.activeApps && window.Garfish.activeApps.some((app)=>app.appInfo.name === appName);
|
|
39
|
-
let renderByProvider = dom || appName;
|
|
40
|
-
if (renderByGarfish) {
|
|
41
|
-
// Runs in the Garfish environment and is rendered by the provider
|
|
42
|
-
if (renderByProvider) {
|
|
43
|
-
return true;
|
|
44
|
-
} else {
|
|
45
|
-
// Runs in the Garfish environment and is not rendered by the provider
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
// Running in a non-Garfish environment
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function generateRouterPlugin (basename,routerConfig) {
|
|
55
|
-
if (basename) {
|
|
56
|
-
routerConfig.originalBaseUrl = basename.replace(/^\\/*/, "/");
|
|
57
|
-
// for compatibility with react router v5
|
|
58
|
-
routerConfig.basename = basename;
|
|
59
|
-
if (routerConfig.supportHtml5History !== false) {
|
|
60
|
-
if (!routerConfig.historyOptions) {
|
|
61
|
-
routerConfig.historyOptions = {
|
|
62
|
-
basename: basename
|
|
63
|
-
};
|
|
64
|
-
} else {
|
|
65
|
-
routerConfig.historyOptions.basename = basename;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return router(routerConfig);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function generateRootDom ({ dom, props, basename }) {
|
|
73
|
-
const mountNode = dom ? (dom.querySelector('#' + MOUNT_ID) || dom) : document.getElementById(MOUNT_ID);
|
|
74
|
-
const mergedProps = {
|
|
75
|
-
...props,
|
|
76
|
-
basename,
|
|
77
|
-
}
|
|
78
|
-
return { mountNode, props: mergedProps }
|
|
79
|
-
}
|
|
80
|
-
`;
|
|
81
|
-
const makeRenderFunction = (code) => {
|
|
82
|
-
const inGarfishToRender = `
|
|
83
|
-
let { basename, props, dom, appName } = typeof arguments[0] === 'object' && arguments[0] || {};
|
|
84
|
-
if (!canContinueRender({ dom, appName })) return null;
|
|
85
|
-
const rootDomInfo = generateRootDom({dom, props, basename});
|
|
86
|
-
let mountNode = rootDomInfo.mountNode;
|
|
87
|
-
props = rootDomInfo.props;
|
|
88
|
-
`;
|
|
89
|
-
return inGarfishToRender + code.replace(`router(`, `generateRouterPlugin(basename,`).replace(/MOUNT_ID/g, "mountNode").replace(`createApp({`, "createApp({ props,").replace(`bootstrap(AppWrapper, mountNode, root`, "bootstrap(AppWrapper, mountNode, root = IS_REACT18 ? ReactDOM.createRoot(mountNode) : null").replace(`customBootstrap(AppWrapper`, "customBootstrap(AppWrapper, mountNode").replace(/customBootstrap\((.*)\)/g, "customBootstrap($1, props)");
|
|
90
|
-
};
|
|
91
1
|
function getRuntimeConfig(config) {
|
|
92
2
|
var _config_runtime;
|
|
93
3
|
if (config === null || config === void 0 ? void 0 : (_config_runtime = config.runtime) === null || _config_runtime === void 0 ? void 0 : _config_runtime.features) {
|
|
@@ -108,23 +18,22 @@ function setRuntimeConfig(config, key, value) {
|
|
|
108
18
|
}
|
|
109
19
|
return void 0;
|
|
110
20
|
}
|
|
111
|
-
const
|
|
112
|
-
const transformCode = code.replace(`import('./bootstrap.jsx');`, `if (!window.__GARFISH__) { import('./bootstrap.jsx'); }`);
|
|
21
|
+
const generateAsyncEntryCode = (appendCode = []) => {
|
|
113
22
|
return `
|
|
114
23
|
export const provider = async (...args) => {
|
|
115
|
-
const exports = await import('./
|
|
24
|
+
const exports = await import('./index.jsx');
|
|
116
25
|
return exports.provider.apply(null, args);
|
|
117
26
|
};
|
|
118
|
-
|
|
27
|
+
if (!window.__GARFISH__) { import('./index.jsx'); }
|
|
119
28
|
if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
|
|
120
29
|
__GARFISH_EXPORTS__.provider = provider;
|
|
121
30
|
}
|
|
31
|
+
|
|
32
|
+
${appendCode.join("\n")}
|
|
122
33
|
`;
|
|
123
34
|
};
|
|
124
35
|
export {
|
|
125
|
-
|
|
36
|
+
generateAsyncEntryCode,
|
|
126
37
|
getRuntimeConfig,
|
|
127
|
-
makeProvider,
|
|
128
|
-
makeRenderFunction,
|
|
129
38
|
setRuntimeConfig
|
|
130
39
|
};
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { default as default2 } from "./plugin";
|
|
1
|
+
import { default as default2, garfishPlugin } from "./plugin";
|
|
2
2
|
import { useModuleApps, useModuleApp } from "./useModuleApps";
|
|
3
3
|
import { default as default3, default as default4 } from "garfish";
|
|
4
|
+
import { garfishRender } from "./render";
|
|
5
|
+
import { createProvider } from "./provider";
|
|
4
6
|
export {
|
|
5
7
|
default3 as Garfish,
|
|
8
|
+
createProvider,
|
|
6
9
|
default2 as default,
|
|
7
10
|
default4 as garfish,
|
|
11
|
+
garfishPlugin,
|
|
12
|
+
garfishRender,
|
|
8
13
|
useModuleApp,
|
|
9
14
|
useModuleApps
|
|
10
15
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import GarfishInstance from "garfish";
|
|
3
3
|
import React from "react";
|
|
4
|
-
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
5
4
|
import { logger } from "../util";
|
|
6
5
|
import { GarfishProvider } from "./utils/Context";
|
|
7
6
|
import setExternal from "./utils/setExternal";
|
|
@@ -33,7 +32,7 @@ async function initOptions(manifest = {}, options) {
|
|
|
33
32
|
apps
|
|
34
33
|
};
|
|
35
34
|
}
|
|
36
|
-
|
|
35
|
+
const garfishPlugin = (config) => ({
|
|
37
36
|
name: "@modern-js/garfish-plugin",
|
|
38
37
|
setup: () => {
|
|
39
38
|
setExternal();
|
|
@@ -98,13 +97,15 @@ var plugin_default = (config) => ({
|
|
|
98
97
|
}
|
|
99
98
|
}
|
|
100
99
|
return next({
|
|
101
|
-
App:
|
|
100
|
+
App: GetMicroFrontendApp,
|
|
102
101
|
config: config2
|
|
103
102
|
});
|
|
104
103
|
}
|
|
105
104
|
};
|
|
106
105
|
}
|
|
107
106
|
});
|
|
107
|
+
var plugin_default = garfishPlugin;
|
|
108
108
|
export {
|
|
109
|
-
plugin_default as default
|
|
109
|
+
plugin_default as default,
|
|
110
|
+
garfishPlugin
|
|
110
111
|
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createRoot } from "@modern-js/runtime/react";
|
|
3
|
+
import { createPortal, unmountComponentAtNode } from "react-dom";
|
|
4
|
+
import { garfishRender } from "./render";
|
|
5
|
+
function createProvider(id, customBootstrap) {
|
|
6
|
+
return ({ basename, dom }) => {
|
|
7
|
+
let root = null;
|
|
8
|
+
return {
|
|
9
|
+
async render({ basename: basename2, dom: dom2, props, appName }) {
|
|
10
|
+
root = await garfishRender(id || "root", customBootstrap, {
|
|
11
|
+
basename: basename2,
|
|
12
|
+
dom: dom2,
|
|
13
|
+
props,
|
|
14
|
+
appName
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
destroy({ dom: dom2 }) {
|
|
18
|
+
const node = dom2.querySelector(`#${id || "root"}`) || dom2;
|
|
19
|
+
if (node) {
|
|
20
|
+
if (process.env.IS_REACT18 === "true") {
|
|
21
|
+
root.unmount();
|
|
22
|
+
} else {
|
|
23
|
+
unmountComponentAtNode(node);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
// 兼容旧版本
|
|
28
|
+
SubModuleComponent: (props) => {
|
|
29
|
+
const ModernRoot = createRoot(null, {
|
|
30
|
+
router: {
|
|
31
|
+
basename
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
35
|
+
basename,
|
|
36
|
+
...props
|
|
37
|
+
}), dom.querySelector(`#${id || "root"}`) || dom);
|
|
38
|
+
},
|
|
39
|
+
jupiter_submodule_app_key: (props) => {
|
|
40
|
+
const ModernRoot = createRoot(null, {
|
|
41
|
+
router: {
|
|
42
|
+
basename
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
46
|
+
basename,
|
|
47
|
+
...props
|
|
48
|
+
}), dom.querySelector(`#${id || "root"}`) || dom);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
createProvider
|
|
55
|
+
};
|