@empjs/bridge-vue3 0.0.3

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 ADDED
@@ -0,0 +1,143 @@
1
+ # EMP Bridge React
2
+
3
+ EMP Bridge React 是一个用于跨 React 版本组件通信的桥接工具,它解决了不同 React 版本之间组件共享和通信的问题。
4
+
5
+ ## 功能特点
6
+
7
+ - 支持不同 React 版本(16/17/18/19)之间的组件共享
8
+ - 提供简单的 API 用于生产者和消费者之间的通信
9
+ - 自动处理 React 不同版本的渲染和卸载方法差异
10
+ - 支持异步加载组件
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ # 使用 npm
16
+ npm install @empjs/bridge-react
17
+
18
+ # 使用 yarn
19
+ yarn add @empjs/bridge-react
20
+
21
+ # 使用 pnpm
22
+ pnpm add @empjs/bridge-react
23
+ ```
24
+
25
+ ## 基本用法
26
+
27
+ ### 生产者(导出组件的应用)
28
+
29
+ ```tsx
30
+ // 在 React 16/17 应用中
31
+ import React from 'react';
32
+ import { createBridgeComponent } from '@empjs/bridge-react';
33
+
34
+ // 创建要共享的组件
35
+ const MyComponent = (props) => {
36
+ return <div>Hello from React 16/17! {props.message}</div>;
37
+ };
38
+
39
+ // 导出桥接组件
40
+ export default createBridgeComponent(MyComponent, {
41
+ React,
42
+ ReactDOM: require('react-dom'),
43
+ // React 18+ 才有 createRoot
44
+ // createRoot: require('react-dom/client').createRoot
45
+ });
46
+ ```
47
+
48
+ ### 消费者(使用组件的应用)
49
+
50
+ ```tsx
51
+ // 在 React 18/19 应用中
52
+ import React from 'react';
53
+ import { createRemoteAppComponent } from '@empjs/bridge-react';
54
+
55
+ // 导入远程组件(可以是动态导入)
56
+ import RemoteComponent from 'remote-app/MyComponent';
57
+
58
+ // 创建可在当前 React 版本中使用的组件
59
+ const BridgedComponent = createRemoteAppComponent(
60
+ RemoteComponent,
61
+ {
62
+ React,
63
+ ReactDOM: require('react-dom'),
64
+ createRoot: require('react-dom/client').createRoot
65
+ },
66
+ {
67
+ onError: (error) => console.error('Failed to load component:', error)
68
+ }
69
+ );
70
+
71
+ // 在应用中使用
72
+ function App() {
73
+ return (
74
+ <div>
75
+ <h1>My App (React 18/19)</h1>
76
+ <BridgedComponent message="Passed from React 18/19" />
77
+ </div>
78
+ );
79
+ }
80
+ ```
81
+
82
+ ## API 参考
83
+
84
+ ### createBridgeComponent
85
+
86
+ 用于生产者包装应用级别导出模块。
87
+
88
+ ```typescript
89
+ function createBridgeComponent(
90
+ Component: React.ComponentType<any>,
91
+ options: {
92
+ React: any;
93
+ ReactDOM: any;
94
+ createRoot?: Function;
95
+ }
96
+ ): BridgeProvider
97
+ ```
98
+
99
+ 参数:
100
+ - `Component`: 要导出的 React 组件
101
+ - `options`: React 相关配置
102
+ - `React`: React 实例
103
+ - `ReactDOM`: ReactDOM 实例
104
+ - `createRoot`: (可选) React 18+ 的 createRoot 方法
105
+
106
+ ### createRemoteAppComponent
107
+
108
+ 用于消费者加载应用级别模块。
109
+
110
+ ```typescript
111
+ function createRemoteAppComponent(
112
+ component: ComponentProvider,
113
+ reactOptions: {
114
+ React: any;
115
+ ReactDOM: any;
116
+ createRoot?: Function;
117
+ },
118
+ options?: {
119
+ onError?: (error: Error) => void;
120
+ }
121
+ ): React.ComponentType<any>
122
+ ```
123
+
124
+ 参数:
125
+ - `component`: 组件提供者函数,可以是同步或异步的
126
+ - `reactOptions`: 当前应用的 React 相关配置
127
+ - `React`: React 实例
128
+ - `ReactDOM`: ReactDOM 实例
129
+ - `createRoot`: (可选) React 18+ 的 createRoot 方法
130
+ - `options`: (可选) 额外配置
131
+ - `onError`: 错误处理回调函数
132
+
133
+ ## 使用场景
134
+
135
+ 1. 微前端架构中不同 React 版本的应用集成
136
+ 2. 逐步升级大型 React 应用时的版本兼容
137
+ 3. 共享组件库到不同 React 版本的项目中
138
+
139
+ ## 注意事项
140
+
141
+ - 确保正确提供对应版本的 React 和 ReactDOM 实例
142
+ - 对于 React 18+,需要提供 createRoot 方法
143
+ - 组件间通信仅限于 props 传递,不支持 Context API 跨版本共享
package/dist/index.cjs ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ createBridgeComponent: ()=>createBridgeComponent,
28
+ createRemoteAppComponent: ()=>createRemoteAppComponent,
29
+ default: ()=>__WEBPACK_DEFAULT_EXPORT__
30
+ });
31
+ function createBridgeComponent(Component, options) {
32
+ const { createApp } = options.Vue;
33
+ return function() {
34
+ const appMap = new Map();
35
+ const render = (dom, props)=>{
36
+ try {
37
+ const existingApp = appMap.get(dom);
38
+ if (existingApp) {
39
+ if (props) Object.keys(props).forEach((key)=>{
40
+ existingApp._instance.props[key] = props[key];
41
+ });
42
+ } else {
43
+ const app = createApp(Component, props || {});
44
+ if (options.plugin) options.plugin(app);
45
+ app.mount(dom);
46
+ appMap.set(dom, app);
47
+ }
48
+ } catch (error) {
49
+ console.error('[EMP-ERROR] Failed to render/update Vue component', error);
50
+ throw error;
51
+ }
52
+ };
53
+ const destroy = (dom)=>{
54
+ const app = appMap.get(dom);
55
+ if (!app) return;
56
+ try {
57
+ app.unmount();
58
+ appMap.delete(dom);
59
+ } catch (error) {
60
+ console.error('[EMP-ERROR] Failed to unmount Vue component', error);
61
+ }
62
+ };
63
+ return {
64
+ render,
65
+ destroy
66
+ };
67
+ };
68
+ }
69
+ function createRemoteAppComponent(component, vueOptions, options = {}) {
70
+ if (!component) throw new Error('createRemoteAppComponent: component parameter cannot be empty');
71
+ const { Vue } = vueOptions;
72
+ return defineComponent({
73
+ name: 'RemoteAppComponent',
74
+ props: {},
75
+ setup () {
76
+ const provider = ref(null);
77
+ const providerInfo = ref(null);
78
+ const isMounted = ref(false);
79
+ const containerRef = ref(null);
80
+ const loadComponent = async ()=>{
81
+ try {
82
+ if ('function' == typeof component) {
83
+ const result = component();
84
+ if (result instanceof Promise) {
85
+ const module = await result;
86
+ providerInfo.value = module.default;
87
+ } else providerInfo.value = component;
88
+ }
89
+ if (isMounted.value && containerRef.value) renderComponent();
90
+ } catch (error) {
91
+ if (options.onError) options.onError(error);
92
+ console.error('[EMP-ERROR] Failed to load component', error);
93
+ }
94
+ };
95
+ const renderComponent = ()=>{
96
+ if (!providerInfo.value || !containerRef.value) return;
97
+ try {
98
+ if (!provider.value) provider.value = providerInfo.value();
99
+ provider.value.render(containerRef.value, getCurrentInstance()?.props || {});
100
+ } catch (error) {
101
+ console.error('[EMP-ERROR] Failed to render component', error);
102
+ }
103
+ };
104
+ const unmountComponent = ()=>{
105
+ if (provider.value && containerRef.value) try {
106
+ provider.value.destroy(containerRef.value);
107
+ provider.value = null;
108
+ } catch (error) {
109
+ console.error('[EMP-ERROR] Failed to unmount component', error);
110
+ }
111
+ };
112
+ onMounted(()=>{
113
+ isMounted.value = true;
114
+ if (providerInfo.value) renderComponent();
115
+ });
116
+ onUpdated(()=>{
117
+ if (provider.value && containerRef.value) provider.value.render(containerRef.value, getCurrentInstance()?.props || {});
118
+ });
119
+ onBeforeUnmount(()=>{
120
+ isMounted.value = false;
121
+ const cleanup = ()=>unmountComponent();
122
+ if ('undefined' != typeof window && window.requestAnimationFrame) window.requestAnimationFrame(cleanup);
123
+ else setTimeout(cleanup, 0);
124
+ });
125
+ loadComponent();
126
+ return {
127
+ containerRef
128
+ };
129
+ },
130
+ render () {
131
+ return Vue.h('div', {
132
+ ref: 'containerRef'
133
+ });
134
+ }
135
+ });
136
+ }
137
+ function defineComponent(options) {
138
+ return options;
139
+ }
140
+ function ref(initialValue) {
141
+ return {
142
+ value: initialValue || null
143
+ };
144
+ }
145
+ function onMounted(fn) {}
146
+ function onUpdated(fn) {}
147
+ function onBeforeUnmount(fn) {}
148
+ function getCurrentInstance() {
149
+ return null;
150
+ }
151
+ const __WEBPACK_DEFAULT_EXPORT__ = {
152
+ createBridgeComponent,
153
+ createRemoteAppComponent
154
+ };
155
+ exports.createBridgeComponent = __webpack_exports__.createBridgeComponent;
156
+ exports.createRemoteAppComponent = __webpack_exports__.createRemoteAppComponent;
157
+ exports["default"] = __webpack_exports__["default"];
158
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
159
+ "createBridgeComponent",
160
+ "createRemoteAppComponent",
161
+ "default"
162
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
163
+ Object.defineProperty(exports, '__esModule', {
164
+ value: true
165
+ });
166
+
167
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["webpack://@empjs/bridge-vue3/webpack/runtime/define_property_getters","webpack://@empjs/bridge-vue3/webpack/runtime/has_own_property","webpack://@empjs/bridge-vue3/webpack/runtime/make_namespace_object","webpack://@empjs/bridge-vue3/./src/index.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","// Type definitions\nexport interface BridgeProviderReturn {\n render: (dom: HTMLElement, props?: Record<string, any>) => void\n destroy: (dom: HTMLElement) => void\n}\n\nexport type BridgeProvider = () => BridgeProviderReturn\nexport type AsyncBridgeProvider = () => Promise<{default: BridgeProvider}>\nexport type ComponentProvider = BridgeProvider | AsyncBridgeProvider\n\ninterface Vue3Options {\n Vue?: any\n plugin?: (app: any) => void\n}\n\n/**\n * Create bridge component - for producer to wrap application-level export modules\n */\nexport function createBridgeComponent(Component: any, options: Vue3Options): BridgeProvider {\n const {createApp} = options.Vue\n\n return function (): BridgeProviderReturn {\n const appMap = new Map<HTMLElement, any>()\n\n const render = (dom: HTMLElement, props?: Record<string, any>): void => {\n try {\n const existingApp = appMap.get(dom)\n\n if (existingApp) {\n // Update props for existing app\n if (props) {\n Object.keys(props).forEach(key => {\n existingApp._instance.props[key] = props[key]\n })\n }\n } else {\n // Create new app instance\n const app = createApp(Component, props || {})\n\n // 使用自定义插件(如果提供)\n if (options.plugin) {\n options.plugin(app)\n }\n\n app.mount(dom)\n appMap.set(dom, app)\n }\n } catch (error) {\n console.error('[EMP-ERROR] Failed to render/update Vue component', error)\n throw error\n }\n }\n\n const destroy = (dom: HTMLElement): void => {\n const app = appMap.get(dom)\n if (!app) return\n\n try {\n app.unmount()\n appMap.delete(dom)\n } catch (error) {\n console.error('[EMP-ERROR] Failed to unmount Vue component', error)\n }\n }\n\n return {render, destroy}\n }\n}\n\n/**\n * Create remote app component - for consumer to load application-level modules\n */\nexport function createRemoteAppComponent(\n component: ComponentProvider,\n vueOptions: Vue3Options,\n options: {onError?: (error: Error) => void} = {},\n): any {\n if (!component) {\n throw new Error('createRemoteAppComponent: component parameter cannot be empty')\n }\n\n const {Vue} = vueOptions\n\n return defineComponent({\n name: 'RemoteAppComponent',\n props: {},\n setup() {\n const provider = ref<BridgeProviderReturn | null>(null)\n const providerInfo = ref<BridgeProvider | null>(null)\n const isMounted = ref(false)\n const containerRef = ref<HTMLElement | null>(null)\n\n const loadComponent = async () => {\n try {\n if (typeof component === 'function') {\n const result = component()\n\n if (result instanceof Promise) {\n const module = await result\n providerInfo.value = module.default\n } else {\n providerInfo.value = component as BridgeProvider\n }\n }\n\n if (isMounted.value && containerRef.value) {\n renderComponent()\n }\n } catch (error) {\n if (options.onError) options.onError(error as Error)\n console.error('[EMP-ERROR] Failed to load component', error)\n }\n }\n\n const renderComponent = () => {\n if (!providerInfo.value || !containerRef.value) return\n\n try {\n if (!provider.value) {\n provider.value = providerInfo.value()\n }\n provider.value.render(containerRef.value, getCurrentInstance()?.props || {})\n } catch (error) {\n console.error('[EMP-ERROR] Failed to render component', error)\n }\n }\n\n const unmountComponent = () => {\n if (provider.value && containerRef.value) {\n try {\n provider.value.destroy(containerRef.value)\n provider.value = null\n } catch (error) {\n console.error('[EMP-ERROR] Failed to unmount component', error)\n }\n }\n }\n\n onMounted(() => {\n isMounted.value = true\n if (providerInfo.value) renderComponent()\n })\n\n onUpdated(() => {\n if (provider.value && containerRef.value) {\n provider.value.render(containerRef.value, getCurrentInstance()?.props || {})\n }\n })\n\n onBeforeUnmount(() => {\n isMounted.value = false\n const cleanup = () => unmountComponent()\n\n if (typeof window !== 'undefined' && window.requestAnimationFrame) {\n window.requestAnimationFrame(cleanup)\n } else {\n setTimeout(cleanup, 0)\n }\n })\n\n // 立即加载组件\n loadComponent()\n\n return {\n containerRef,\n }\n },\n render() {\n return Vue.h('div', {ref: 'containerRef'})\n },\n })\n}\n\n// 添加必要的导入声明\nfunction defineComponent(options: any): any {\n return options\n}\n\nfunction ref<T>(initialValue?: T): {value: T | null} {\n return {value: initialValue || null}\n}\n\nfunction onMounted(fn: () => void): void {}\nfunction onUpdated(fn: () => void): void {}\nfunction onBeforeUnmount(fn: () => void): void {}\nfunction getCurrentInstance(): {props: Record<string, any>} | null {\n return null\n}\n\nexport default {\n createBridgeComponent,\n createRemoteAppComponent,\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","createBridgeComponent","Component","options","createApp","appMap","Map","render","dom","props","existingApp","app","error","console","destroy","createRemoteAppComponent","component","vueOptions","Error","Vue","defineComponent","provider","ref","providerInfo","isMounted","containerRef","loadComponent","result","Promise","module","renderComponent","getCurrentInstance","unmountComponent","onMounted","onUpdated","onBeforeUnmount","cleanup","window","setTimeout","initialValue","fn"],"mappings":";;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFL,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOM,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D;;;;;;;;;ACYO,SAASI,sBAAsBC,SAAc,EAAEC,OAAoB;IACxE,MAAM,EAACC,SAAS,EAAC,GAAGD,QAAQ,GAAG;IAE/B,OAAO;QACL,MAAME,SAAS,IAAIC;QAEnB,MAAMC,SAAS,CAACC,KAAkBC;YAChC,IAAI;gBACF,MAAMC,cAAcL,OAAO,GAAG,CAACG;gBAE/B,IAAIE,aAEF;oBAAA,IAAID,OACFZ,OAAO,IAAI,CAACY,OAAO,OAAO,CAACb,CAAAA;wBACzBc,YAAY,SAAS,CAAC,KAAK,CAACd,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AAO;QAAC,OAAOA,gBAAgB;IAAI;AACrC;AAEA,SAASN,UAAUO,EAAc,GAAS;AAC1C,SAASN,UAAUM,EAAc,GAAS;AAC1C,SAASL,gBAAgBK,EAAc,GAAS;AAChD,SAAST;IACP,OAAO;AACT;AAEA,mCAAe;IACb9B;IACAc;AACF"}
@@ -0,0 +1,28 @@
1
+ export interface BridgeProviderReturn {
2
+ render: (dom: HTMLElement, props?: Record<string, any>) => void;
3
+ destroy: (dom: HTMLElement) => void;
4
+ }
5
+ export type BridgeProvider = () => BridgeProviderReturn;
6
+ export type AsyncBridgeProvider = () => Promise<{
7
+ default: BridgeProvider;
8
+ }>;
9
+ export type ComponentProvider = BridgeProvider | AsyncBridgeProvider;
10
+ interface Vue3Options {
11
+ Vue?: any;
12
+ plugin?: (app: any) => void;
13
+ }
14
+ /**
15
+ * Create bridge component - for producer to wrap application-level export modules
16
+ */
17
+ export declare function createBridgeComponent(Component: any, options: Vue3Options): BridgeProvider;
18
+ /**
19
+ * Create remote app component - for consumer to load application-level modules
20
+ */
21
+ export declare function createRemoteAppComponent(component: ComponentProvider, vueOptions: Vue3Options, options?: {
22
+ onError?: (error: Error) => void;
23
+ }): any;
24
+ declare const _default: {
25
+ createBridgeComponent: typeof createBridgeComponent;
26
+ createRemoteAppComponent: typeof createRemoteAppComponent;
27
+ };
28
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,127 @@
1
+ function createBridgeComponent(Component, options) {
2
+ const { createApp } = options.Vue;
3
+ return function() {
4
+ const appMap = new Map();
5
+ const render = (dom, props)=>{
6
+ try {
7
+ const existingApp = appMap.get(dom);
8
+ if (existingApp) {
9
+ if (props) Object.keys(props).forEach((key)=>{
10
+ existingApp._instance.props[key] = props[key];
11
+ });
12
+ } else {
13
+ const app = createApp(Component, props || {});
14
+ if (options.plugin) options.plugin(app);
15
+ app.mount(dom);
16
+ appMap.set(dom, app);
17
+ }
18
+ } catch (error) {
19
+ console.error('[EMP-ERROR] Failed to render/update Vue component', error);
20
+ throw error;
21
+ }
22
+ };
23
+ const destroy = (dom)=>{
24
+ const app = appMap.get(dom);
25
+ if (!app) return;
26
+ try {
27
+ app.unmount();
28
+ appMap.delete(dom);
29
+ } catch (error) {
30
+ console.error('[EMP-ERROR] Failed to unmount Vue component', error);
31
+ }
32
+ };
33
+ return {
34
+ render,
35
+ destroy
36
+ };
37
+ };
38
+ }
39
+ function createRemoteAppComponent(component, vueOptions, options = {}) {
40
+ if (!component) throw new Error('createRemoteAppComponent: component parameter cannot be empty');
41
+ const { Vue } = vueOptions;
42
+ return defineComponent({
43
+ name: 'RemoteAppComponent',
44
+ props: {},
45
+ setup () {
46
+ const provider = ref(null);
47
+ const providerInfo = ref(null);
48
+ const isMounted = ref(false);
49
+ const containerRef = ref(null);
50
+ const loadComponent = async ()=>{
51
+ try {
52
+ if ('function' == typeof component) {
53
+ const result = component();
54
+ if (result instanceof Promise) {
55
+ const module = await result;
56
+ providerInfo.value = module.default;
57
+ } else providerInfo.value = component;
58
+ }
59
+ if (isMounted.value && containerRef.value) renderComponent();
60
+ } catch (error) {
61
+ if (options.onError) options.onError(error);
62
+ console.error('[EMP-ERROR] Failed to load component', error);
63
+ }
64
+ };
65
+ const renderComponent = ()=>{
66
+ if (!providerInfo.value || !containerRef.value) return;
67
+ try {
68
+ if (!provider.value) provider.value = providerInfo.value();
69
+ provider.value.render(containerRef.value, getCurrentInstance()?.props || {});
70
+ } catch (error) {
71
+ console.error('[EMP-ERROR] Failed to render component', error);
72
+ }
73
+ };
74
+ const unmountComponent = ()=>{
75
+ if (provider.value && containerRef.value) try {
76
+ provider.value.destroy(containerRef.value);
77
+ provider.value = null;
78
+ } catch (error) {
79
+ console.error('[EMP-ERROR] Failed to unmount component', error);
80
+ }
81
+ };
82
+ onMounted(()=>{
83
+ isMounted.value = true;
84
+ if (providerInfo.value) renderComponent();
85
+ });
86
+ onUpdated(()=>{
87
+ if (provider.value && containerRef.value) provider.value.render(containerRef.value, getCurrentInstance()?.props || {});
88
+ });
89
+ onBeforeUnmount(()=>{
90
+ isMounted.value = false;
91
+ const cleanup = ()=>unmountComponent();
92
+ if ('undefined' != typeof window && window.requestAnimationFrame) window.requestAnimationFrame(cleanup);
93
+ else setTimeout(cleanup, 0);
94
+ });
95
+ loadComponent();
96
+ return {
97
+ containerRef
98
+ };
99
+ },
100
+ render () {
101
+ return Vue.h('div', {
102
+ ref: 'containerRef'
103
+ });
104
+ }
105
+ });
106
+ }
107
+ function defineComponent(options) {
108
+ return options;
109
+ }
110
+ function ref(initialValue) {
111
+ return {
112
+ value: initialValue || null
113
+ };
114
+ }
115
+ function onMounted(fn) {}
116
+ function onUpdated(fn) {}
117
+ function onBeforeUnmount(fn) {}
118
+ function getCurrentInstance() {
119
+ return null;
120
+ }
121
+ const src = {
122
+ createBridgeComponent,
123
+ createRemoteAppComponent
124
+ };
125
+ export { createBridgeComponent, createRemoteAppComponent, src as default };
126
+
127
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["webpack://@empjs/bridge-vue3/./src/index.ts"],"sourcesContent":["// Type definitions\nexport interface BridgeProviderReturn {\n render: (dom: HTMLElement, props?: Record<string, any>) => void\n destroy: (dom: HTMLElement) => void\n}\n\nexport type BridgeProvider = () => BridgeProviderReturn\nexport type AsyncBridgeProvider = () => Promise<{default: BridgeProvider}>\nexport type ComponentProvider = BridgeProvider | AsyncBridgeProvider\n\ninterface Vue3Options {\n Vue?: any\n plugin?: (app: any) => void\n}\n\n/**\n * Create bridge component - for producer to wrap application-level export modules\n */\nexport function createBridgeComponent(Component: any, options: Vue3Options): BridgeProvider {\n const {createApp} = options.Vue\n\n return function (): BridgeProviderReturn {\n const appMap = new Map<HTMLElement, any>()\n\n const render = (dom: HTMLElement, props?: Record<string, any>): void => {\n try {\n const existingApp = appMap.get(dom)\n\n if (existingApp) {\n // Update props for existing app\n if (props) {\n Object.keys(props).forEach(key => {\n existingApp._instance.props[key] = props[key]\n })\n }\n } else {\n // Create new app instance\n const app = createApp(Component, props || {})\n\n // 使用自定义插件(如果提供)\n if (options.plugin) {\n options.plugin(app)\n }\n\n app.mount(dom)\n appMap.set(dom, app)\n }\n } catch (error) {\n console.error('[EMP-ERROR] Failed to render/update Vue component', error)\n throw error\n }\n }\n\n const destroy = (dom: HTMLElement): void => {\n const app = appMap.get(dom)\n if (!app) return\n\n try {\n app.unmount()\n appMap.delete(dom)\n } catch (error) {\n console.error('[EMP-ERROR] Failed to unmount Vue component', error)\n }\n }\n\n return {render, destroy}\n }\n}\n\n/**\n * Create remote app component - for consumer to load application-level modules\n */\nexport function createRemoteAppComponent(\n component: ComponentProvider,\n vueOptions: Vue3Options,\n options: {onError?: (error: Error) => void} = {},\n): any {\n if (!component) {\n throw new Error('createRemoteAppComponent: component parameter cannot be empty')\n }\n\n const {Vue} = vueOptions\n\n return defineComponent({\n name: 'RemoteAppComponent',\n props: {},\n setup() {\n const provider = ref<BridgeProviderReturn | null>(null)\n const providerInfo = ref<BridgeProvider | null>(null)\n const isMounted = ref(false)\n const containerRef = ref<HTMLElement | null>(null)\n\n const loadComponent = async () => {\n try {\n if (typeof component === 'function') {\n const result = component()\n\n if (result instanceof Promise) {\n const module = await result\n providerInfo.value = module.default\n } else {\n providerInfo.value = component as BridgeProvider\n }\n }\n\n if (isMounted.value && containerRef.value) {\n renderComponent()\n }\n } catch (error) {\n if (options.onError) options.onError(error as Error)\n console.error('[EMP-ERROR] Failed to load component', error)\n }\n }\n\n const renderComponent = () => {\n if (!providerInfo.value || !containerRef.value) return\n\n try {\n if (!provider.value) {\n provider.value = providerInfo.value()\n }\n provider.value.render(containerRef.value, getCurrentInstance()?.props || {})\n } catch (error) {\n console.error('[EMP-ERROR] Failed to render component', error)\n }\n }\n\n const unmountComponent = () => {\n if (provider.value && containerRef.value) {\n try {\n provider.value.destroy(containerRef.value)\n provider.value = null\n } catch (error) {\n console.error('[EMP-ERROR] Failed to unmount component', error)\n }\n }\n }\n\n onMounted(() => {\n isMounted.value = true\n if (providerInfo.value) renderComponent()\n })\n\n onUpdated(() => {\n if (provider.value && containerRef.value) {\n provider.value.render(containerRef.value, getCurrentInstance()?.props || {})\n }\n })\n\n onBeforeUnmount(() => {\n isMounted.value = false\n const cleanup = () => unmountComponent()\n\n if (typeof window !== 'undefined' && window.requestAnimationFrame) {\n window.requestAnimationFrame(cleanup)\n } else {\n setTimeout(cleanup, 0)\n }\n })\n\n // 立即加载组件\n loadComponent()\n\n return {\n containerRef,\n }\n },\n render() {\n return Vue.h('div', {ref: 'containerRef'})\n },\n })\n}\n\n// 添加必要的导入声明\nfunction defineComponent(options: any): any {\n return options\n}\n\nfunction ref<T>(initialValue?: T): {value: T | null} {\n return {value: initialValue || null}\n}\n\nfunction onMounted(fn: () => void): void {}\nfunction onUpdated(fn: () => void): void {}\nfunction onBeforeUnmount(fn: () => void): void {}\nfunction getCurrentInstance(): {props: Record<string, any>} | null {\n return null\n}\n\nexport default {\n createBridgeComponent,\n createRemoteAppComponent,\n}\n"],"names":["createBridgeComponent","Component","options","createApp","appMap","Map","render","dom","props","existingApp","Object","key","app","error","console","destroy","createRemoteAppComponent","component","vueOptions","Error","Vue","defineComponent","provider","ref","providerInfo","isMounted","containerRef","loadComponent","result","Promise","module","renderComponent","getCurrentInstance","unmountComponent","onMounted","onUpdated","onBeforeUnmount","cleanup","window","setTimeout","initialValue","fn"],"mappings":"AAkBO,SAASA,sBAAsBC,SAAc,EAAEC,OAAoB;IACxE,MAAM,EAACC,SAAS,EAAC,GAAGD,QAAQ,GAAG;IAE/B,OAAO;QACL,MAAME,SAAS,IAAIC;QAEnB,MAAMC,SAAS,CAACC,KAAkBC;YAChC,IAAI;gBACF,MAAMC,cAAcL,OAAO,GAAG,CAACG;gBAE/B,IAAIE,aAEF;oBAAA,IAAID,OACFE,OAAO,IAAI,CAACF,OAAO,OAAO,CAACG,CAAAA;wBACzBF,YAAY,SAAS,CAAC,KAAK,CAACE,IAAI,GAAGH,KAAK,CAACG,IAAI;oBAC/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;AAEA,SAASN,UAAUO,EAAc,GAAS;AAC1C,SAASN,UAAUM,EAAc,GAAS;AAC1C,SAASL,gBAAgBK,EAAc,GAAS;AAChD,SAAST;IACP,OAAO;AACT;AAEA,YAAe;IACbhC;IACAgB;AACF"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@empjs/bridge-vue3",
3
+ "version": "0.0.3",
4
+ "description": "Emp Bridge Vue v3",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "maintainers": [
11
+ "xuhongbin",
12
+ "ckken",
13
+ "doerme",
14
+ "ron0115"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/empjs/emp.git",
19
+ "directory": "packages/bridge-vue3"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "main": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "import": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "require": {
33
+ "types": "./dist/index.d.ts",
34
+ "default": "./dist/index.cjs"
35
+ }
36
+ }
37
+ },
38
+ "engines": {
39
+ "node": ">=16.0.0"
40
+ },
41
+ "author": "Ken",
42
+ "dependencies": {},
43
+ "devDependencies": {},
44
+ "scripts": {
45
+ "dev": "rslib build --watch --env-mode development",
46
+ "build": "rslib build"
47
+ }
48
+ }