@modern-js/plugin-garfish 2.54.6 → 2.56.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 +47 -123
- package/dist/cjs/cli/template.js +52 -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 +92 -0
- package/dist/cjs/{deps/index.js → runtime/utils.js} +8 -16
- package/dist/esm/cli/code.js +80 -0
- package/dist/esm/cli/index.js +73 -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 +113 -0
- package/dist/esm/runtime/utils.js +6 -0
- package/dist/esm-node/cli/code.js +40 -0
- package/dist/esm-node/cli/index.js +49 -125
- package/dist/esm-node/cli/template.js +28 -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 +68 -0
- package/dist/esm-node/runtime/utils.js +6 -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 +23 -0
- package/dist/types/runtime/utils.d.ts +1 -0
- package/package.json +13 -23
- package/type.d.ts +8 -1
- 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,113 @@
|
|
|
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 "@meta/runtime/react";
|
|
6
|
+
import { render } from "@meta/runtime/browser";
|
|
7
|
+
import { createPortal, unmountComponentAtNode } from "react-dom";
|
|
8
|
+
function createProvider(id) {
|
|
9
|
+
var _ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, customBootstrap = _ref.customBootstrap, beforeRender = _ref.beforeRender;
|
|
10
|
+
return function(param) {
|
|
11
|
+
var basename = param.basename, dom = param.dom;
|
|
12
|
+
var root = null;
|
|
13
|
+
return {
|
|
14
|
+
render: function render1(param2) {
|
|
15
|
+
var basename2 = param2.basename, dom2 = param2.dom, props = param2.props;
|
|
16
|
+
return _async_to_generator(function() {
|
|
17
|
+
var ModernRoot;
|
|
18
|
+
return _ts_generator(this, function(_state) {
|
|
19
|
+
switch (_state.label) {
|
|
20
|
+
case 0:
|
|
21
|
+
ModernRoot = createRoot(null, {
|
|
22
|
+
router: {
|
|
23
|
+
basename: basename2
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
if (!customBootstrap)
|
|
27
|
+
return [
|
|
28
|
+
3,
|
|
29
|
+
2
|
|
30
|
+
];
|
|
31
|
+
return [
|
|
32
|
+
4,
|
|
33
|
+
customBootstrap(ModernRoot, function() {
|
|
34
|
+
return render(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
35
|
+
basename: basename2
|
|
36
|
+
}, props)), dom2);
|
|
37
|
+
})
|
|
38
|
+
];
|
|
39
|
+
case 1:
|
|
40
|
+
root = _state.sent();
|
|
41
|
+
return [
|
|
42
|
+
3,
|
|
43
|
+
6
|
|
44
|
+
];
|
|
45
|
+
case 2:
|
|
46
|
+
if (!beforeRender)
|
|
47
|
+
return [
|
|
48
|
+
3,
|
|
49
|
+
4
|
|
50
|
+
];
|
|
51
|
+
return [
|
|
52
|
+
4,
|
|
53
|
+
beforeRender(ModernRoot)
|
|
54
|
+
];
|
|
55
|
+
case 3:
|
|
56
|
+
_state.sent();
|
|
57
|
+
_state.label = 4;
|
|
58
|
+
case 4:
|
|
59
|
+
return [
|
|
60
|
+
4,
|
|
61
|
+
render(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
62
|
+
basename: basename2
|
|
63
|
+
}, props)), dom2)
|
|
64
|
+
];
|
|
65
|
+
case 5:
|
|
66
|
+
root = _state.sent();
|
|
67
|
+
_state.label = 6;
|
|
68
|
+
case 6:
|
|
69
|
+
return [
|
|
70
|
+
2
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
})();
|
|
75
|
+
},
|
|
76
|
+
destroy: function destroy(param2) {
|
|
77
|
+
var dom2 = param2.dom;
|
|
78
|
+
var node = dom2.querySelector("#".concat(id || "root")) || dom2;
|
|
79
|
+
if (node) {
|
|
80
|
+
if (process.env.IS_REACT18 === "true") {
|
|
81
|
+
root.unmount();
|
|
82
|
+
} else {
|
|
83
|
+
unmountComponentAtNode(node);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
// 兼容旧版本
|
|
88
|
+
SubModuleComponent: function(props) {
|
|
89
|
+
var ModernRoot = createRoot(null, {
|
|
90
|
+
router: {
|
|
91
|
+
basename
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
95
|
+
basename
|
|
96
|
+
}, props)), dom.querySelector("#".concat(id || "root")) || dom);
|
|
97
|
+
},
|
|
98
|
+
jupiter_submodule_app_key: function(props) {
|
|
99
|
+
var ModernRoot = createRoot(null, {
|
|
100
|
+
router: {
|
|
101
|
+
basename
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, _object_spread({
|
|
105
|
+
basename
|
|
106
|
+
}, props)), dom.querySelector("#".concat(id || "root")) || dom);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export {
|
|
112
|
+
createProvider
|
|
113
|
+
};
|
|
@@ -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,9 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
73
96
|
},
|
|
74
97
|
source: {
|
|
75
98
|
alias: {
|
|
76
|
-
|
|
77
|
-
"@
|
|
99
|
+
[`@${metaName}/runtime/garfish`]: `@${metaName}/plugin-garfish/runtime`,
|
|
100
|
+
"@meta/runtime/browser": "@modern-js/runtime/browser",
|
|
101
|
+
"@meta/runtime/react": "@modern-js/runtime/react"
|
|
78
102
|
}
|
|
79
103
|
},
|
|
80
104
|
tools: {
|
|
@@ -92,7 +116,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
92
116
|
}
|
|
93
117
|
]);
|
|
94
118
|
}
|
|
95
|
-
const resolveOptions = useResolvedConfigContext();
|
|
119
|
+
const resolveOptions = api.useResolvedConfigContext();
|
|
96
120
|
if (resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions_deploy = resolveOptions.deploy) === null || _resolveOptions_deploy === void 0 ? void 0 : _resolveOptions_deploy.microFrontend) {
|
|
97
121
|
var _resolveOptions_dev, _useConfig_dev, _resolveOptions_server, _resolveOptions_deploy1;
|
|
98
122
|
chain.output.libraryTarget("umd");
|
|
@@ -117,7 +141,7 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
117
141
|
}
|
|
118
142
|
const uniqueName = chain.output.get("uniqueName");
|
|
119
143
|
if (!uniqueName) {
|
|
120
|
-
chain.output.uniqueName(
|
|
144
|
+
chain.output.uniqueName(packageName);
|
|
121
145
|
}
|
|
122
146
|
const resolveConfig = chain.toConfig();
|
|
123
147
|
logger("bundlerConfig", {
|
|
@@ -132,125 +156,25 @@ const garfishPlugin = ({ pluginName = "@modern-js/plugin-garfish", runtimePlugin
|
|
|
132
156
|
};
|
|
133
157
|
},
|
|
134
158
|
addRuntimeExports() {
|
|
135
|
-
const config = useResolvedConfigContext();
|
|
159
|
+
const config = api.useResolvedConfigContext();
|
|
136
160
|
const { masterApp } = getRuntimeConfig(config);
|
|
161
|
+
const { internalDirectory, metaName } = api.useAppContext();
|
|
162
|
+
const pluginsExportsUtils = createRuntimeExportsUtils(internalDirectory, "plugins");
|
|
137
163
|
if (masterApp) {
|
|
138
|
-
const addExportStatement = `export { default as garfish, default as masterApp } from '${
|
|
164
|
+
const addExportStatement = `export { default as garfish, default as masterApp } from '${metaName}/plugin-garfish/runtime'`;
|
|
139
165
|
logger("exportStatement", addExportStatement);
|
|
140
166
|
pluginsExportsUtils.addExport(addExportStatement);
|
|
141
167
|
}
|
|
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
168
|
},
|
|
239
|
-
|
|
240
|
-
var
|
|
241
|
-
const
|
|
242
|
-
if (
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
exportStatement: exportStatementCode
|
|
248
|
-
};
|
|
169
|
+
async generateEntryCode({ entrypoints }) {
|
|
170
|
+
var _resolveOptions_deploy;
|
|
171
|
+
const resolveOptions = api.useResolvedConfigContext();
|
|
172
|
+
if (resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions_deploy = resolveOptions.deploy) === null || _resolveOptions_deploy === void 0 ? void 0 : _resolveOptions_deploy.microFrontend) {
|
|
173
|
+
const appContext = api.useAppContext();
|
|
174
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
175
|
+
const { appendEntryCode: appendEntryCode2 } = api.useHookRunners();
|
|
176
|
+
await generateCode(entrypoints, appContext, resolvedConfig, appendEntryCode2);
|
|
249
177
|
}
|
|
250
|
-
return {
|
|
251
|
-
entrypoint,
|
|
252
|
-
exportStatement
|
|
253
|
-
};
|
|
254
178
|
}
|
|
255
179
|
};
|
|
256
180
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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 { createRoot } from '@${metaName}/runtime/react';
|
|
4
|
+
import { render } from '@${metaName}/runtime/browser';
|
|
5
|
+
import { isRenderGarfish, createProvider } from '@${metaName}/plugin-garfish/runtime';
|
|
6
|
+
${customBootstrap ? `import customBootstrap from '${formatImportPath(customBootstrap.replace(srcDirectory, internalSrcAlias))}';` : "let customBootstrap;"}
|
|
7
|
+
if (!isRenderGarfish()) {
|
|
8
|
+
const ModernRoot = createRoot();
|
|
9
|
+
${customBootstrap ? `customBootstrap(ModernRoot, () => render(<ModernRoot />, '${mountId || "root"}'));` : `render(<ModernRoot />, '${mountId || "root"}');`};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const provider = createProvider('${mountId || "root"}', { customBootstrap });
|
|
13
|
+
`;
|
|
14
|
+
const index = ({ srcDirectory, internalSrcAlias, metaName, entry, entryName, customEntry, customBootstrap, mountId, appendCode = [] }) => `import '@${metaName}/runtime/registry/${entryName}';
|
|
15
|
+
${genRenderCode({
|
|
16
|
+
srcDirectory,
|
|
17
|
+
internalSrcAlias,
|
|
18
|
+
metaName,
|
|
19
|
+
entry,
|
|
20
|
+
customEntry,
|
|
21
|
+
customBootstrap,
|
|
22
|
+
mountId
|
|
23
|
+
})}
|
|
24
|
+
${appendCode.join("\n")}
|
|
25
|
+
`;
|
|
26
|
+
export {
|
|
27
|
+
index
|
|
28
|
+
};
|
|
@@ -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 { isRenderGarfish } from "./utils";
|
|
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
|
+
isRenderGarfish,
|
|
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,68 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createRoot } from "@meta/runtime/react";
|
|
3
|
+
import { render } from "@meta/runtime/browser";
|
|
4
|
+
import { createPortal, unmountComponentAtNode } from "react-dom";
|
|
5
|
+
function createProvider(id, { customBootstrap, beforeRender } = {}) {
|
|
6
|
+
return ({ basename, dom }) => {
|
|
7
|
+
let root = null;
|
|
8
|
+
return {
|
|
9
|
+
async render({ basename: basename2, dom: dom2, props }) {
|
|
10
|
+
const ModernRoot = createRoot(null, {
|
|
11
|
+
router: {
|
|
12
|
+
basename: basename2
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
if (customBootstrap) {
|
|
16
|
+
root = await customBootstrap(ModernRoot, () => render(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
17
|
+
basename: basename2,
|
|
18
|
+
...props
|
|
19
|
+
}), dom2));
|
|
20
|
+
} else {
|
|
21
|
+
if (beforeRender) {
|
|
22
|
+
await beforeRender(ModernRoot);
|
|
23
|
+
}
|
|
24
|
+
root = await render(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
25
|
+
basename: basename2,
|
|
26
|
+
...props
|
|
27
|
+
}), dom2);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
destroy({ dom: dom2 }) {
|
|
31
|
+
const node = dom2.querySelector(`#${id || "root"}`) || dom2;
|
|
32
|
+
if (node) {
|
|
33
|
+
if (process.env.IS_REACT18 === "true") {
|
|
34
|
+
root.unmount();
|
|
35
|
+
} else {
|
|
36
|
+
unmountComponentAtNode(node);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
// 兼容旧版本
|
|
41
|
+
SubModuleComponent: (props) => {
|
|
42
|
+
const ModernRoot = createRoot(null, {
|
|
43
|
+
router: {
|
|
44
|
+
basename
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
48
|
+
basename,
|
|
49
|
+
...props
|
|
50
|
+
}), dom.querySelector(`#${id || "root"}`) || dom);
|
|
51
|
+
},
|
|
52
|
+
jupiter_submodule_app_key: (props) => {
|
|
53
|
+
const ModernRoot = createRoot(null, {
|
|
54
|
+
router: {
|
|
55
|
+
basename
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return /* @__PURE__ */ createPortal(/* @__PURE__ */ _jsx(ModernRoot, {
|
|
59
|
+
basename,
|
|
60
|
+
...props
|
|
61
|
+
}), dom.querySelector(`#${id || "root"}`) || dom);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
createProvider
|
|
68
|
+
};
|