@lemon-fe/vite-plugin-micro-frontend 2.0.0-beta.3 → 2.0.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/README.md +54 -54
- package/dist/index.d.ts +3 -7
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime-app.d.ts +37 -0
- package/dist/runtime-app.js +82 -0
- package/dist/runtime-app.js.map +1 -0
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +2 -2
- package/package.json +8 -9
- package/dist/runtime-bridge.d.ts +0 -65
- package/dist/runtime-bridge.js +0 -96
- package/dist/runtime-bridge.js.map +0 -1
|
@@ -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,WAAW,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,EACxC;AAC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E,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;;;;"}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/** @deprecated 请优先使用 runtime/dynamic 或 runtime/
|
|
2
|
-
export * from './runtime-
|
|
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/
|
|
2
|
-
export * from './runtime-
|
|
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
|
|
4
|
-
"description": "基于 Module Federation Runtime 与 React
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "基于 Module Federation Runtime 与 React 组件的 Vite 微前端插件集合",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"types": "./dist/runtime-dynamic.d.ts",
|
|
22
22
|
"default": "./dist/runtime-dynamic.js"
|
|
23
23
|
},
|
|
24
|
-
"./runtime/
|
|
25
|
-
"types": "./dist/runtime-
|
|
26
|
-
"default": "./dist/runtime-
|
|
24
|
+
"./runtime/app": {
|
|
25
|
+
"types": "./dist/runtime-app.d.ts",
|
|
26
|
+
"default": "./dist/runtime-app.js"
|
|
27
27
|
},
|
|
28
28
|
"./runtime/federation": {
|
|
29
29
|
"types": "./dist/runtime-federation.d.ts",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"runtime/dynamic": [
|
|
43
43
|
"./dist/runtime-dynamic.d.ts"
|
|
44
44
|
],
|
|
45
|
-
"runtime/
|
|
46
|
-
"./dist/runtime-
|
|
45
|
+
"runtime/app": [
|
|
46
|
+
"./dist/runtime-app.d.ts"
|
|
47
47
|
],
|
|
48
48
|
"runtime/federation": [
|
|
49
49
|
"./dist/runtime-federation.d.ts"
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"vite-plugin",
|
|
65
65
|
"micro-frontend",
|
|
66
66
|
"module-federation",
|
|
67
|
-
"react
|
|
67
|
+
"react",
|
|
68
68
|
"routes"
|
|
69
69
|
],
|
|
70
70
|
"author": "",
|
|
@@ -80,7 +80,6 @@
|
|
|
80
80
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@module-federation/bridge-react": "2.9.0",
|
|
84
83
|
"@module-federation/runtime": "2.9.0",
|
|
85
84
|
"@module-federation/vite": "1.21.4",
|
|
86
85
|
"prettier": "^3.5.3",
|
package/dist/runtime-bridge.d.ts
DELETED
|
@@ -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 };
|
package/dist/runtime-bridge.js
DELETED
|
@@ -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":";;;;A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