@lemon-fe/vite-plugin-micro-frontend 2.0.0-beta.3 → 2.0.1

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.
@@ -0,0 +1,37 @@
1
+ import * as react from 'react';
2
+ import { ReactNode, ComponentType } from 'react';
3
+ import { useHistory } from 'react-router-dom';
4
+
5
+ type AppHistory = ReturnType<typeof useHistory>;
6
+ /** 普通组件微应用接收的宿主能力。 */
7
+ interface MicroFrontendProps {
8
+ /** 应用路由前缀,例如 /wms。 */
9
+ basename?: string;
10
+ /** 向基座登记当前应用的错误监控信息。 */
11
+ setSentryMetadata?: (metadata: {
12
+ dsn: string;
13
+ release: string;
14
+ }) => void;
15
+ }
16
+ /** 复用宿主 history,仅转换应用绝对路径的前缀,不创建浏览器 history。 */
17
+ declare function createScopedHistory(originHistory: AppHistory, basename: string): AppHistory;
18
+ interface AppRouterProps {
19
+ /** 应用路由前缀。 */
20
+ basename: string;
21
+ /** 应用组件树。 */
22
+ children?: ReactNode;
23
+ }
24
+ declare function AppRouter({ basename, children }: AppRouterProps): react.CElement<{
25
+ history: any;
26
+ }, react.Component<{
27
+ history: any;
28
+ }, any, any>>;
29
+ /** 为插件生成的 ./app 提供普通 React 组件与应用路由前缀。 */
30
+ declare function createMicroFrontendApp<Props extends MicroFrontendProps = MicroFrontendProps>(options: {
31
+ rootComponent: ComponentType<Props>;
32
+ basename: string;
33
+ mountElementId?: string;
34
+ }): ComponentType<Props>;
35
+
36
+ export { AppRouter, createMicroFrontendApp, createScopedHistory };
37
+ export type { AppRouterProps, MicroFrontendProps };
@@ -0,0 +1,82 @@
1
+ import { useMemo, createElement } from 'react';
2
+ import { useHistory, Router } from 'react-router-dom';
3
+
4
+ function hasBasename(pathname, basename) {
5
+ const path = pathname.split(/[?#]/)[0];
6
+ return basename === '' || path === basename || path.startsWith(`${basename}/`);
7
+ }
8
+ function addBasename(location, basename) {
9
+ if (!basename)
10
+ return location;
11
+ if (typeof location === 'string') {
12
+ // 相对路径、query 和 hash 交给宿主 history 按当前地址解析。
13
+ if (location !== '' && !location.startsWith('/'))
14
+ return location;
15
+ return hasBasename(location, basename) ? location : `${basename}${location || '/'}`;
16
+ }
17
+ if (location.pathname === undefined ||
18
+ (location.pathname !== '' && !location.pathname.startsWith('/')) ||
19
+ hasBasename(location.pathname, basename)) {
20
+ return location;
21
+ }
22
+ return { ...location, pathname: `${basename}${location.pathname || '/'}` };
23
+ }
24
+ function removeBasename(location, basename) {
25
+ if (!basename || !hasBasename(location.pathname, basename))
26
+ return location;
27
+ return { ...location, pathname: location.pathname.slice(basename.length) || '/' };
28
+ }
29
+ /** 复用宿主 history,仅转换应用绝对路径的前缀,不创建浏览器 history。 */
30
+ function createScopedHistory(originHistory, basename) {
31
+ const prefix = !basename || basename === '/' ? '' : `/${basename.replace(/^\/+|\/+$/g, '')}`;
32
+ return {
33
+ get length() {
34
+ return originHistory.length;
35
+ },
36
+ get action() {
37
+ return originHistory.action;
38
+ },
39
+ get location() {
40
+ return removeBasename(originHistory.location, prefix);
41
+ },
42
+ createHref(location) {
43
+ return originHistory.createHref(addBasename(location, prefix));
44
+ },
45
+ push(location, state) {
46
+ originHistory.push(addBasename(location, prefix), state);
47
+ },
48
+ replace(location, state) {
49
+ originHistory.replace(addBasename(location, prefix), state);
50
+ },
51
+ go: originHistory.go,
52
+ goBack: originHistory.goBack,
53
+ goForward: originHistory.goForward,
54
+ listen(listener) {
55
+ return originHistory.listen((location, action) => {
56
+ listener(removeBasename(location, prefix), action);
57
+ });
58
+ },
59
+ block(prompt) {
60
+ if (typeof prompt !== 'function')
61
+ return originHistory.block(prompt);
62
+ return originHistory.block((location, action) => prompt(removeBasename(location, prefix), action));
63
+ },
64
+ };
65
+ }
66
+ function AppRouter({ basename, children }) {
67
+ const history = useHistory();
68
+ const scopedHistory = useMemo(() => createScopedHistory(history, basename), [history, basename]);
69
+ return createElement(Router, { history: scopedHistory }, children);
70
+ }
71
+ /** 为插件生成的 ./app 提供普通 React 组件与应用路由前缀。 */
72
+ function createMicroFrontendApp(options) {
73
+ return function MicroFrontendApp(props) {
74
+ const content = createElement(options.rootComponent, props);
75
+ return createElement(AppRouter, { basename: props.basename ?? options.basename }, options.mountElementId
76
+ ? createElement('div', { id: options.mountElementId, style: { width: '100%', height: '100%' } }, content)
77
+ : content);
78
+ };
79
+ }
80
+
81
+ export { AppRouter, createMicroFrontendApp, createScopedHistory };
82
+ //# sourceMappingURL=runtime-app.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-app.js","sources":["../src/runtime/app.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAeA,SAAS,WAAW,CAAC,QAAgB,EAAE,QAAgB,EAAA;IACrD,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACtC,IAAA,OAAO,QAAQ,KAAK,EAAE,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,UAAU,CAAC,CAAA,EAAG,QAAQ,CAAA,CAAA,CAAG,CAAC;AAChF;AAOA,SAAS,WAAW,CAAC,QAA+B,EAAE,QAAgB,EAAA;AACpE,IAAA,IAAI,CAAC,QAAQ;AAAE,QAAA,OAAO,QAAQ;AAC9B,IAAA,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;;QAEhC,IAAI,QAAQ,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;AAAE,YAAA,OAAO,QAAQ;QACjE,OAAO,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,GAAG,QAAQ,GAAG,GAAG,QAAQ,CAAA,EAAG,QAAQ,IAAI,GAAG,EAAE;IACrF;AACA,IAAA,IACE,QAAQ,CAAC,QAAQ,KAAK,SAAS;AAC/B,SAAC,QAAQ,CAAC,QAAQ,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAChE,WA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AC3C,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,aAAa,EAAE,KAAK,CAAC;AAC3D,QAAA,OAAO,aAAa,CAClB,SAAS,EACT,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,EAChD,OAAO,CAAC;cACJ,aAAa,CACX,KAAK,EACL,EAAE,EAAE,EAAE,OAAO,CAAC,cAAc,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EACxE,OAAO;cAET,OAAO,CACZ;AACH,IAAA,CAAC;AACH;;;;"}
@@ -1,5 +1,33 @@
1
1
  export { loadRemote } from '@module-federation/runtime';
2
2
 
3
+ interface DevelopmentApplication {
4
+ name: string;
5
+ entry: string;
6
+ module: string;
7
+ }
8
+ interface LoadDevelopmentRootOptions {
9
+ appName: string;
10
+ /** 消费应用的 MF name,默认由 appName 推导。 */
11
+ federationName?: string;
12
+ }
13
+ interface BaseRootProps {
14
+ localApplications: Record<string, DevelopmentApplication>;
15
+ }
16
+ type BaseRootComponent = (props: BaseRootProps) => unknown;
17
+ interface BaseRoot {
18
+ BaseRoot: BaseRootComponent;
19
+ createElement(component: BaseRootComponent, props: BaseRootProps): unknown;
20
+ createRenderer(container: HTMLElement): {
21
+ render(element: unknown): void;
22
+ unmount(): void;
23
+ };
24
+ }
25
+ interface DevelopmentRoot extends BaseRoot {
26
+ props: BaseRootProps;
27
+ }
28
+ /** 加载基座根组件,并提供本地应用的联邦入口。 */
29
+ declare function loadDevelopmentRoot({ appName, federationName, }: LoadDevelopmentRootOptions): Promise<DevelopmentRoot>;
30
+
3
31
  /** 保留直接使用官方默认实例的兼容入口。生成器使用下面的实例绑定加载器。 */
4
32
 
5
33
  interface RemoteLoaderOptions {
@@ -15,5 +43,5 @@ interface RemoteLoaderOptions {
15
43
  /** 动态加载不会经过 Vite 的静态 remote import 转换,必须显式注册到消费应用的实例。 */
16
44
  declare function createRemoteLoader(options: RemoteLoaderOptions): <T>(moduleSpecifier: string) => Promise<T | null>;
17
45
 
18
- export { createRemoteLoader };
19
- export type { RemoteLoaderOptions };
46
+ export { createRemoteLoader, loadDevelopmentRoot };
47
+ export type { DevelopmentApplication, LoadDevelopmentRootOptions, RemoteLoaderOptions };
@@ -1,6 +1,39 @@
1
- import { getInstance } from '@module-federation/runtime';
1
+ import { createInstance, getInstance } from '@module-federation/runtime';
2
2
  export { loadRemote } from '@module-federation/runtime';
3
3
 
4
+ /** 加载基座根组件,并提供本地应用的联邦入口。 */
5
+ async function loadDevelopmentRoot({ appName, federationName, }) {
6
+ const { basePath, remoteName, gatewayOrigin } = __MICRO_FRONTEND_DEVELOPMENT_SHELL__;
7
+ const localName = federationName || appName.replace(/-/g, '_');
8
+ const origin = window.location.origin;
9
+ const baseURL = `${origin}${basePath}`;
10
+ const shell = createInstance({
11
+ name: `${localName}_development_shell`,
12
+ remotes: [{ name: remoteName, entry: `${baseURL}/remote.js`, type: 'module' }],
13
+ shareStrategy: 'loaded-first',
14
+ });
15
+ const root = await shell.loadRemote(`${remoteName}/root`);
16
+ if (!root?.loadBaseRoot) {
17
+ throw new Error('目标基座未提供 root/loadBaseRoot,请先部署新版基座');
18
+ }
19
+ const base = await root.loadBaseRoot({
20
+ baseURL,
21
+ gatewayBaseURL: gatewayOrigin,
22
+ reactScripts: {
23
+ react: '/@micro-frontend/host-react.js',
24
+ reactDOM: '/@micro-frontend/host-react-dom.js',
25
+ },
26
+ });
27
+ return {
28
+ ...base,
29
+ props: {
30
+ localApplications: {
31
+ [appName]: { name: localName, entry: `${origin}/remote.js`, module: `${localName}/app` },
32
+ },
33
+ },
34
+ };
35
+ }
36
+
4
37
  /** 动态加载不会经过 Vite 的静态 remote import 转换,必须显式注册到消费应用的实例。 */
5
38
  function createRemoteLoader(options) {
6
39
  const registeredInstances = new WeakSet();
@@ -32,5 +65,5 @@ function createRemoteLoader(options) {
32
65
  };
33
66
  }
34
67
 
35
- export { createRemoteLoader };
68
+ export { createRemoteLoader, loadDevelopmentRoot };
36
69
  //# sourceMappingURL=runtime-federation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime-federation.js","sources":["../src/runtime/federation.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAqBA;AACM,SAAU,kBAAkB,CAAC,OAA4B,EAAA;AAC7D,IAAA,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAAoB;IAE3D,OAAO,SAAS,oBAAoB,CAAI,eAAuB,EAAA;AAC7D,QAAA,MAAM,QAAQ,GAAG,OAAO,CAAC;AACvB,cAAE,WAAW,CAAC,SAAS,IAAI,SAAS,CAAC,IAAI,KAAK,OAAO,CAAC,QAAQ;cAC5D,WAAW,EAAE;QACjB,IAAI,CAAC,QAAQ,EAAE;YACb,MAAM,IAAI,KAAK,CAAC,CAAA,UAAA,EAAa,OAAO,CAAC,QAAQ,IAAI,WAAW,CAAA,iBAAA,CAAmB,CAAC;QAClF;QAEA,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YACtC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,KAAI;gBACtE,MAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK;sBAC1C,MAAM,CAAC;AACT,sBAAE,IAAI,GAAG,CACL,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,CAAA,CAAE,EAChE,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,QAAQ,CAAC,MAAM,CAC9C,CAAC,IAAI;gBACV,OAAO;AACL,oBAAA,IAAI,EAAE,KAAK;oBACX,KAAK;oBACL,IAAI,EAAE,MAAM,CAAC,UAAU,GAAG,QAAQ,GAAG,KAAK;oBAC1C,eAAe,EAAE,MAAM,CAAC,UAAU;AAClC,oBAAA,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,SAAS;iBAC5C;AACH,YAAA,CAAC,CAAC;;AAEF,YAAA,QAAQ,CAAC,eAAe,CAAC,OAAO,CAAC;AACjC,YAAA,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnC;AAEA,QAAA,OAAO,QAAQ,CAAC,UAAU,CAAI,eAAe,CAAC;AAChD,IAAA,CAAC;AACH;;;;"}
1
+ {"version":3,"file":"runtime-federation.js","sources":["../src/runtime/development.ts","../src/runtime/federation.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;AA4CA;AACO,eAAe,mBAAmB,CAAC,EACxC,OAAO,EACP,cAAc,GACa,EAAA;IAC3B,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,GAAG,oCAAoC;AACpF,IAAA,MAAM,SAAS,GAAG,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC;AAC9D,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM;AACrC,IAAA,MAAM,OAAO,GAAG,CAAA,EAAG,MAAM,CAAA,EAAG,QAAQ,EAAE;IACtC,MAAM,KAAK,GAAG,cAAc,CAAC;QAC3B,IAAI,EAAE,CAAA,EAAG,SAAS,CAAA,kBAAA,CAAoB;AACtC,QAAA,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAA,EAAG,OAAO,CAAA,UAAA,CAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;AAC9E,QAAA,aAAa,EAAE,cAAc;AAC9B,KAAA,CAAC;IACF,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,UAAU,CAAiB,CAAA,EAAG,UAAU,CAAA,KAAA,CAAO,CAAC;AACzE,IAAA,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE;AACvB,QAAA,MAAM,IAAI,KAAK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sBAAE,IAAI,GAAG,CACL,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,CAAA,CAAE,EAChE,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,QAAQ,CAAC,MAAM,CAC9C,CAAC,IAAI;gBACV,OAAO;AACL,oBAAA,IAAI,EAAE,KAAK;oBACX,KAAK;oBACL,IAAI,EAAE,MAAM,CAAC,UAAU,GAAG,QAAQ,GAAG,KAAK;oBAC1C,eAAe,EAAE,MAAM,CAAC,UAAU;AAClC,oBAAA,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,SAAS;iBAC5C;AACH,YAAA,CAAC,CAAC;;AAEF,YAAA,QAAQ,CAAC,eAAe,CAAC,OAAO,CAAC;AACjC,YAAA,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnC;AAEA,QAAA,OAAO,QAAQ,CAAC,UAAU,CAAI,eAAe,CAAC;AAChD,IAAA,CAAC;AACH;;;;"}
package/dist/runtime.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- /** @deprecated 请优先使用 runtime/dynamic 或 runtime/bridge 子路径 */
2
- export * from './runtime-bridge';
1
+ /** @deprecated 请优先使用 runtime/dynamic 或 runtime/app 子路径 */
2
+ export * from './runtime-app';
3
3
  export * from './runtime-dynamic';
package/dist/runtime.js CHANGED
@@ -1,3 +1,3 @@
1
- /** @deprecated 请优先使用 runtime/dynamic 或 runtime/bridge 子路径 */
2
- export * from './runtime-bridge.js';
1
+ /** @deprecated 请优先使用 runtime/dynamic 或 runtime/app 子路径 */
2
+ export * from './runtime-app.js';
3
3
  export * from './runtime-dynamic.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lemon-fe/vite-plugin-micro-frontend",
3
- "version": "2.0.0-beta.3",
4
- "description": "基于 Module Federation Runtime 与 React Bridge 的 Vite 微前端插件集合",
3
+ "version": "2.0.1",
4
+ "description": "基于 Module Federation Runtime 与 React 组件的 Vite 微前端插件集合",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -9,44 +9,24 @@
9
9
  "mf": "./dist/cli.js"
10
10
  },
11
11
  "exports": {
12
- ".": {
13
- "types": "./dist/index.d.ts",
14
- "default": "./dist/index.js"
15
- },
16
- "./runtime": {
17
- "types": "./dist/runtime.d.ts",
18
- "default": "./dist/runtime.js"
19
- },
20
- "./runtime/dynamic": {
21
- "types": "./dist/runtime-dynamic.d.ts",
22
- "default": "./dist/runtime-dynamic.js"
23
- },
24
- "./runtime/bridge": {
25
- "types": "./dist/runtime-bridge.d.ts",
26
- "default": "./dist/runtime-bridge.js"
27
- },
28
- "./runtime/federation": {
29
- "types": "./dist/runtime-federation.d.ts",
30
- "default": "./dist/runtime-federation.js"
31
- },
32
- "./runtime/react-refresh-init": {
33
- "types": "./dist/runtime-react-refresh-init.d.ts",
34
- "default": "./dist/runtime-react-refresh-init.js"
35
- }
12
+ ".": "./dist/index.js",
13
+ "./runtime": "./dist/runtime.js",
14
+ "./runtime/dynamic": "./dist/runtime-dynamic.js",
15
+ "./runtime/app": "./dist/runtime-app.js",
16
+ "./runtime/federation": "./dist/runtime-federation.js",
17
+ "./runtime/development": "./dist/runtime-federation.js",
18
+ "./runtime/react-refresh-init": "./dist/runtime-react-refresh-init.js"
36
19
  },
37
20
  "typesVersions": {
38
21
  "*": {
39
22
  "runtime": [
40
23
  "./dist/runtime.d.ts"
41
24
  ],
42
- "runtime/dynamic": [
43
- "./dist/runtime-dynamic.d.ts"
44
- ],
45
- "runtime/bridge": [
46
- "./dist/runtime-bridge.d.ts"
47
- ],
48
- "runtime/federation": [
25
+ "runtime/development": [
49
26
  "./dist/runtime-federation.d.ts"
27
+ ],
28
+ "runtime/*": [
29
+ "./dist/runtime-*.d.ts"
50
30
  ]
51
31
  }
52
32
  },
@@ -64,7 +44,7 @@
64
44
  "vite-plugin",
65
45
  "micro-frontend",
66
46
  "module-federation",
67
- "react-bridge",
47
+ "react",
68
48
  "routes"
69
49
  ],
70
50
  "author": "",
@@ -80,7 +60,6 @@
80
60
  "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
81
61
  },
82
62
  "dependencies": {
83
- "@module-federation/bridge-react": "2.9.0",
84
63
  "@module-federation/runtime": "2.9.0",
85
64
  "@module-federation/vite": "1.21.4",
86
65
  "prettier": "^3.5.3",
@@ -1,65 +0,0 @@
1
- import * as _module_federation_bridge_react from '@module-federation/bridge-react';
2
- import { ComponentType } from 'react';
3
-
4
- interface MicroFrontendLocation {
5
- pathname: string;
6
- search: string;
7
- hash: string;
8
- state?: unknown;
9
- }
10
- type RouterTo = string | {
11
- pathname?: string;
12
- search?: string;
13
- hash?: string;
14
- state?: unknown;
15
- };
16
- interface MicroFrontendRouterContext {
17
- location: MicroFrontendLocation;
18
- history: {
19
- action?: string;
20
- length?: number;
21
- location: MicroFrontendLocation;
22
- createHref: (to: RouterTo) => string;
23
- push: (to: RouterTo, state?: unknown) => unknown;
24
- replace: (to: RouterTo, state?: unknown) => unknown;
25
- go: (n: number) => unknown;
26
- goBack: () => unknown;
27
- goForward: () => unknown;
28
- listen: (listener: (location: MicroFrontendLocation, action?: string) => void) => () => void;
29
- block?: (prompt: (location: MicroFrontendLocation, action?: string) => unknown) => () => void;
30
- };
31
- match?: unknown;
32
- staticContext?: unknown;
33
- }
34
- interface MicroFrontendProps {
35
- container?: HTMLElement;
36
- basename?: string;
37
- mountElementId?: string;
38
- /** 基座 Router 的 history/location;由 Bridge provider 在微应用 Root 内自动注入 Router。 */
39
- routerContext?: MicroFrontendRouterContext;
40
- [key: string]: unknown;
41
- }
42
- interface MicroFrontendLifeCycle {
43
- renderContainer: (props: MicroFrontendProps) => void | Promise<void>;
44
- bootstrap?: () => void | Promise<void>;
45
- mount?: (props: MicroFrontendProps) => void | Promise<void>;
46
- unmount?: (props: MicroFrontendProps) => void | Promise<void>;
47
- update?: (props: MicroFrontendProps) => void | Promise<void>;
48
- }
49
- interface CreateMicroFrontendBridgeProviderOptions {
50
- appName: string;
51
- mountElementId: string;
52
- }
53
- interface CreateMicroFrontendRootBridgeProviderOptions<Props extends MicroFrontendProps = MicroFrontendProps> {
54
- rootComponent: ComponentType<Props>;
55
- /** 应用在 Bridge 容器内使用的稳定根节点 id。 */
56
- mountElementId?: string;
57
- }
58
- /** 为组件式应用创建官方 React 18 Bridge provider。 */
59
- declare function createMicroFrontendRootBridgeProvider<Props extends MicroFrontendProps = MicroFrontendProps>(options: CreateMicroFrontendRootBridgeProviderOptions<Props>): () => {
60
- render(info: _module_federation_bridge_react.RenderParams): Promise<void>;
61
- destroy(info: _module_federation_bridge_react.DestroyParams): void;
62
- };
63
-
64
- export { createMicroFrontendRootBridgeProvider };
65
- export type { CreateMicroFrontendBridgeProviderOptions, CreateMicroFrontendRootBridgeProviderOptions, MicroFrontendLifeCycle, MicroFrontendLocation, MicroFrontendProps, MicroFrontendRouterContext };
@@ -1,96 +0,0 @@
1
- import { createElement, useMemo } from 'react';
2
- import { createBridgeComponent } from '@module-federation/bridge-react/v18';
3
- import { Router, BrowserRouter } from 'react-router-dom';
4
-
5
- /**
6
- * Module Federation React Bridge 运行时。
7
- * 这是浏览器入口,不得引入 Node.js API。
8
- */
9
- function stripRouterBasename(pathname, basename) {
10
- if (!basename || basename === '/')
11
- return pathname;
12
- const normalizedBasename = `/${basename.replace(/^\/+|\/+$/g, '')}`;
13
- if (pathname === normalizedBasename)
14
- return '/';
15
- if (pathname.startsWith(`${normalizedBasename}/`)) {
16
- return pathname.slice(normalizedBasename.length) || '/';
17
- }
18
- return pathname;
19
- }
20
- function scopeRouterBasename(pathname, basename) {
21
- // 相对路径交给基座 history 按当前地址解析,提前补 basename 会把它变成绝对路径。
22
- if (!basename || basename === '/' || (pathname !== '' && !pathname.startsWith('/'))) {
23
- return pathname;
24
- }
25
- const normalizedBasename = `/${basename.replace(/^\/+|\/+$/g, '')}`;
26
- if (pathname === normalizedBasename ||
27
- pathname.startsWith(`${normalizedBasename}/`) ||
28
- pathname.startsWith(`${normalizedBasename}?`) ||
29
- pathname.startsWith(`${normalizedBasename}#`)) {
30
- return pathname;
31
- }
32
- return `${normalizedBasename}${pathname || '/'}`;
33
- }
34
- function adaptRouterContext(context, basename) {
35
- const { history, location } = context;
36
- const unscope = (nextLocation) => ({
37
- ...nextLocation,
38
- pathname: stripRouterBasename(nextLocation.pathname, basename),
39
- });
40
- const toHostLocation = (to) => typeof to === 'string'
41
- ? scopeRouterBasename(to, basename)
42
- : typeof to.pathname === 'string'
43
- ? { ...to, pathname: scopeRouterBasename(to.pathname, basename) }
44
- : to;
45
- const scopedLocation = unscope(location);
46
- const scopedHistory = {
47
- ...history,
48
- get action() {
49
- return history.action;
50
- },
51
- get length() {
52
- return history.length;
53
- },
54
- get location() {
55
- return unscope(history.location);
56
- },
57
- createHref: (to) => history.createHref(toHostLocation(to)),
58
- push: (to, state) => history.push(toHostLocation(to), state),
59
- replace: (to, state) => history.replace(toHostLocation(to), state),
60
- listen: (listener) => history.listen((nextLocation, action) => listener(unscope(nextLocation), action)),
61
- ...(history.block
62
- ? {
63
- block: (prompt) => history.block?.((nextLocation, action) => prompt(unscope(nextLocation), action)) ??
64
- (() => { }),
65
- }
66
- : {}),
67
- };
68
- return { ...context, location: scopedLocation, history: scopedHistory };
69
- }
70
- function HostRouterProvider(props) {
71
- const scopedContext = useMemo(() => props.routerContext ? adaptRouterContext(props.routerContext, props.basename) : undefined, [props.basename, props.routerContext]);
72
- if (scopedContext) {
73
- return createElement(Router, { history: scopedContext.history }, props.children);
74
- }
75
- // SSR/tests have no browser history. The host path is only available when mounted by the base.
76
- if (typeof window === 'undefined')
77
- return props.children;
78
- return createElement(BrowserRouter, undefined, props.children);
79
- }
80
- /** 为组件式应用创建官方 React 18 Bridge provider。 */
81
- function createMicroFrontendRootBridgeProvider(options) {
82
- const RootComponent = options.rootComponent;
83
- const BridgeRoot = props => {
84
- const content = createElement(RootComponent, props);
85
- const routedContent = createElement(HostRouterProvider, { basename: props.basename, routerContext: props.routerContext }, content);
86
- return options.mountElementId
87
- ? createElement('div', { id: options.mountElementId }, routedContent)
88
- : routedContent;
89
- };
90
- return createBridgeComponent({
91
- rootComponent: BridgeRoot,
92
- });
93
- }
94
-
95
- export { createMicroFrontendRootBridgeProvider };
96
- //# sourceMappingURL=runtime-bridge.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-bridge.js","sources":["../src/runtime/bridge.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAAA;;;AAGG;AAiDH,SAAS,mBAAmB,CAAC,QAAgB,EAAE,QAAiB,EAAA;AAC9D,IAAA,IAAI,CAAC,QAAQ,IAAI,QAAQ,KAAK,GAAG;AAAE,QAAA,OAAO,QAAQ;AAElD,IAAA,MAAM,kBAAkB,GAAG,CAAA,CAAA,EAAI,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA,CAAE;IACnE,IAAI,QAAQ,KAAK,kBAAkB;AAAE,QAAA,OAAO,GAAG;IAC/C,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,kBAAkB,CAAA,CAAA,CAAG,CAAC,EAAE;QACjD,OAAO,QAAQ,CAAC,KAAK,CAAC,kBAAkB,CAAC,MAAM,CAAC,IAAI,GAAG;IACzD;AAEA,IAAA,OAAO,QAAQ;AACjB;AAEA,SAAS,mBAAmB,CAAC,QAAgB,EAAE,QAAiB,EAAA;;IAE9D,IAAI,CAAC,QAAQ,IAAI,QAAQ,KAAK,GAAG,KAAK,QAAQ,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE;AACnF,QAAA,OAAO,QAAQ;IACjB;AACA,IAAA,MAAM,kBAAkB,GAAG,CAAA,CAAA,EAAI,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA,CAAE;IACnE,IACE,QAAQ,KAAK,k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