@module-federation/bridge-vue3 0.0.0-next-20240619092013

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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @module-federation/bridge-vue3
2
+
3
+ ## 0.0.0-next-20240619092013
4
+
5
+ ### Patch Changes
6
+
7
+ - @module-federation/bridge-shared@0.0.0-next-20240619092013
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 ScriptedAlchemy LLC (Zack Jackson) Zhou Shaw (zhouxiao)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Vue 3 + TypeScript + Vite
2
+
3
+ This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4
+
5
+ ## Recommended Setup
6
+
7
+ - [VS Code](https://code.visualstudio.com/) + [Vue - Official](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (previously Volar) and disable Vetur
8
+
9
+ - Use [vue-tsc](https://github.com/vuejs/language-tools/tree/master/packages/tsc) for performing the same type checking from the command line, or for generating d.ts files for SFCs.
package/index.html ADDED
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Vite + Vue + TS</title>
8
+ </head>
9
+ <body>
10
+ <div id="app"></div>
11
+ <script type="module" src="/src/main.ts"></script>
12
+ </body>
13
+ </html>
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@module-federation/bridge-vue3",
3
+ "author": "zhouxiao <codingzx@gmail.com>",
4
+ "version": "0.0.0-next-20240619092013",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "./dist/index.umd.js",
10
+ "module": "./dist/index.es.js",
11
+ "types": "./dist/index.d.ts",
12
+ "peerDependencies": {
13
+ "vue": "=3",
14
+ "vue-router": "=3"
15
+ },
16
+ "dependencies": {
17
+ "@module-federation/bridge-shared": "0.0.0-next-20240619092013"
18
+ },
19
+ "devDependencies": {
20
+ "@vitejs/plugin-vue": "^5.0.4",
21
+ "@vitejs/plugin-vue-jsx": "^4.0.0",
22
+ "typescript": "^5.2.2",
23
+ "vite": "^5.2.0",
24
+ "vite-plugin-dts": "^3.9.1",
25
+ "vue": "^3.4.21",
26
+ "vue-router": "4.3.2",
27
+ "vue-tsc": "^2.0.6"
28
+ },
29
+ "scripts": {
30
+ "dev": "vite",
31
+ "build": "vite build",
32
+ "preview": "vite preview"
33
+ }
34
+ }
package/project.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "vue3-bridge",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "packages/bridge/vue3-bridge/src",
5
+ "projectType": "library",
6
+ "targets": {
7
+ "build": {
8
+ "executor": "nx:run-commands",
9
+ "options": {
10
+ "commands": ["npm run build --prefix packages/bridge/vue3-bridge"]
11
+ }
12
+ },
13
+ "test": {
14
+ "executor": "nx:run-commands",
15
+ "options": {
16
+ "commands": ["npm run test --prefix packages/bridge/vue3-bridge"]
17
+ }
18
+ }
19
+ },
20
+ "tags": ["type:pkg"]
21
+ }
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
package/src/App.vue ADDED
@@ -0,0 +1,30 @@
1
+ <script setup lang="ts">
2
+ import HelloWorld from './components/HelloWorld.vue';
3
+ </script>
4
+
5
+ <template>
6
+ <div>
7
+ <a href="https://vitejs.dev" target="_blank">
8
+ <img src="/vite.svg" class="logo" alt="Vite logo" />
9
+ </a>
10
+ <a href="https://vuejs.org/" target="_blank">
11
+ <img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
12
+ </a>
13
+ </div>
14
+ <HelloWorld msg="Vite + Vue" />
15
+ </template>
16
+
17
+ <style scoped>
18
+ .logo {
19
+ height: 6em;
20
+ padding: 1.5em;
21
+ will-change: filter;
22
+ transition: filter 300ms;
23
+ }
24
+ .logo:hover {
25
+ filter: drop-shadow(0 0 2em #646cffaa);
26
+ }
27
+ .logo.vue:hover {
28
+ filter: drop-shadow(0 0 2em #42b883aa);
29
+ }
30
+ </style>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
@@ -0,0 +1,38 @@
1
+ <script setup lang="ts">
2
+ import { ref } from 'vue';
3
+
4
+ defineProps<{ msg: string }>();
5
+
6
+ const count = ref(0);
7
+ </script>
8
+
9
+ <template>
10
+ <h1>{{ msg }}</h1>
11
+
12
+ <div class="card">
13
+ <button type="button" @click="count++">count is {{ count }}</button>
14
+ <p>
15
+ Edit
16
+ <code>components/HelloWorld.vue</code> to test HMR
17
+ </p>
18
+ </div>
19
+
20
+ <p>
21
+ Check out
22
+ <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
23
+ >create-vue</a
24
+ >, the official Vue + Vite starter
25
+ </p>
26
+ <p>
27
+ Install
28
+ <a href="https://github.com/vuejs/language-tools" target="_blank">Volar</a>
29
+ in your IDE for a better DX
30
+ </p>
31
+ <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
32
+ </template>
33
+
34
+ <style scoped>
35
+ .read-the-docs {
36
+ color: #888;
37
+ }
38
+ </style>
package/src/create.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { defineAsyncComponent, h } from 'vue';
2
+ import RemoteApp from './remoteApp.jsx';
3
+ import { LoggerInstance } from './utils.js';
4
+ import { useRoute } from 'vue-router';
5
+
6
+ declare const __APP_VERSION__: string;
7
+
8
+ export function createRemoteComponent(
9
+ lazyComponent: () => Promise<any>,
10
+ info?: { export: string },
11
+ ) {
12
+ return defineAsyncComponent({
13
+ __APP_VERSION__,
14
+ //@ts-ignore
15
+ loader: async () => {
16
+ const route = useRoute();
17
+
18
+ let basename = '/';
19
+ const matchPath = route.matched[0]?.path;
20
+ if (matchPath) {
21
+ if (matchPath.endsWith('/:pathMatch(.*)*')) {
22
+ basename = matchPath.replace('/:pathMatch(.*)*', '');
23
+ } else {
24
+ basename = route.matched[0].path;
25
+ }
26
+ }
27
+
28
+ const exportName = info?.export || 'default';
29
+ LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
30
+ basename,
31
+ lazyComponent,
32
+ exportName,
33
+ });
34
+
35
+ const module: any = await lazyComponent();
36
+ const moduleName = module && module[Symbol.for('mf_module_id')];
37
+ const exportFn = module[exportName];
38
+
39
+ LoggerInstance.log(
40
+ `createRemoteComponent LazyComponent loadRemote info >>>`,
41
+ { name: moduleName, module, exportName, basename, route },
42
+ );
43
+
44
+ if (exportName in module && typeof exportFn === 'function') {
45
+ return {
46
+ render() {
47
+ return h(RemoteApp, {
48
+ moduleName,
49
+ ...info,
50
+ providerInfo: exportFn,
51
+ basename,
52
+ });
53
+ },
54
+ };
55
+ }
56
+ throw new Error('module not found');
57
+ },
58
+ loadingComponent: {
59
+ template: '<div>Loading...</div>',
60
+ },
61
+ errorComponent: {
62
+ template: '<div>Error loading component</div>',
63
+ },
64
+ delay: 200,
65
+ timeout: 3000,
66
+ });
67
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { createBridgeComponent } from './provider';
2
+ export { createRemoteComponent } from './create';
3
+ export type { RenderFnParams } from '@module-federation/bridge-shared';
@@ -0,0 +1,53 @@
1
+ import * as Vue from 'vue';
2
+ import * as VueRouter from 'vue-router';
3
+ import { RenderFnParams } from '@module-federation/bridge-shared';
4
+ import { LoggerInstance } from './utils';
5
+
6
+ declare const __APP_VERSION__: string;
7
+
8
+ export function createBridgeComponent(bridgeInfo: any) {
9
+ const rootMap = new Map();
10
+ return () => {
11
+ return {
12
+ __APP_VERSION__,
13
+ render(info: RenderFnParams) {
14
+ LoggerInstance.log(`createBridgeComponent render Info`, info);
15
+ const app = Vue.createApp(bridgeInfo.rootComponent);
16
+ rootMap.set(info.dom, app);
17
+ const appOptions = bridgeInfo.appOptions({
18
+ basename: info.basename,
19
+ memoryRoute: info.memoryRoute,
20
+ });
21
+
22
+ const history = info.memoryRoute
23
+ ? VueRouter.createMemoryHistory(info.basename)
24
+ : VueRouter.createWebHistory(info.basename);
25
+ const router = VueRouter.createRouter({
26
+ ...appOptions.router.options,
27
+ history,
28
+ routes: appOptions.router.getRoutes(),
29
+ });
30
+
31
+ LoggerInstance.log(`createBridgeComponent render router info>>>`, {
32
+ name: info.name,
33
+ router,
34
+ });
35
+ // memory route Initializes the route
36
+ if (info.memoryRoute) {
37
+ router.push(info.memoryRoute.entryPath).then(() => {
38
+ app.use(router);
39
+ app.mount(info.dom);
40
+ });
41
+ } else {
42
+ app.use(router);
43
+ app.mount(info.dom);
44
+ }
45
+ },
46
+ destroy(info: { dom: HTMLElement }) {
47
+ LoggerInstance.log(`createBridgeComponent destroy Info`, info);
48
+ const root = rootMap.get(info?.dom);
49
+ root?.unmount();
50
+ },
51
+ };
52
+ };
53
+ }
@@ -0,0 +1,69 @@
1
+ import { ref, onMounted, onBeforeUnmount, watch, defineComponent } from 'vue';
2
+ import { useRoute } from 'vue-router';
3
+ import { LoggerInstance } from './utils';
4
+ import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
5
+
6
+ export default defineComponent({
7
+ name: 'RemoteApp',
8
+ props: {
9
+ moduleName: String,
10
+ basename: String,
11
+ memoryRoute: Object,
12
+ providerInfo: Function,
13
+ },
14
+ setup(props) {
15
+ const rootRef = ref(null);
16
+ const providerInfoRef = ref(null);
17
+ const pathname = ref('');
18
+ const route = useRoute();
19
+
20
+ const renderComponent = () => {
21
+ const providerReturn = props.providerInfo?.();
22
+ providerInfoRef.value = providerReturn;
23
+ const renderProps = {
24
+ name: props.moduleName,
25
+ dom: rootRef.value,
26
+ basename: props.basename,
27
+ memoryRoute: props.memoryRoute,
28
+ };
29
+ LoggerInstance.log(
30
+ `createRemoteComponent LazyComponent render >>>`,
31
+ renderProps,
32
+ );
33
+ providerReturn.render(renderProps);
34
+ };
35
+
36
+ const watchStopHandle = watch(
37
+ () => route.path,
38
+ (newPath) => {
39
+ if (newPath !== route.path) {
40
+ renderComponent();
41
+ }
42
+
43
+ // dispatchPopstateEnv
44
+ if (pathname.value !== '' && pathname.value !== newPath) {
45
+ LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
46
+ ...props,
47
+ pathname: route.path,
48
+ });
49
+ dispatchPopstateEnv();
50
+ }
51
+ pathname.value = newPath;
52
+ },
53
+ );
54
+
55
+ onMounted(() => {
56
+ renderComponent();
57
+ });
58
+
59
+ onBeforeUnmount(() => {
60
+ LoggerInstance.log(`createRemoteComponent LazyComponent destroy >>>`, {
61
+ ...props,
62
+ });
63
+ watchStopHandle();
64
+ (providerInfoRef.value as any)?.destroy({ dom: rootRef.value });
65
+ });
66
+
67
+ return () => <div ref={rootRef}></div>;
68
+ },
69
+ });
package/src/style.css ADDED
@@ -0,0 +1,79 @@
1
+ :root {
2
+ font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3
+ line-height: 1.5;
4
+ font-weight: 400;
5
+
6
+ color-scheme: light dark;
7
+ color: rgba(255, 255, 255, 0.87);
8
+ background-color: #242424;
9
+
10
+ font-synthesis: none;
11
+ text-rendering: optimizeLegibility;
12
+ -webkit-font-smoothing: antialiased;
13
+ -moz-osx-font-smoothing: grayscale;
14
+ }
15
+
16
+ a {
17
+ font-weight: 500;
18
+ color: #646cff;
19
+ text-decoration: inherit;
20
+ }
21
+ a:hover {
22
+ color: #535bf2;
23
+ }
24
+
25
+ body {
26
+ margin: 0;
27
+ display: flex;
28
+ place-items: center;
29
+ min-width: 320px;
30
+ min-height: 100vh;
31
+ }
32
+
33
+ h1 {
34
+ font-size: 3.2em;
35
+ line-height: 1.1;
36
+ }
37
+
38
+ button {
39
+ border-radius: 8px;
40
+ border: 1px solid transparent;
41
+ padding: 0.6em 1.2em;
42
+ font-size: 1em;
43
+ font-weight: 500;
44
+ font-family: inherit;
45
+ background-color: #1a1a1a;
46
+ cursor: pointer;
47
+ transition: border-color 0.25s;
48
+ }
49
+ button:hover {
50
+ border-color: #646cff;
51
+ }
52
+ button:focus,
53
+ button:focus-visible {
54
+ outline: 4px auto -webkit-focus-ring-color;
55
+ }
56
+
57
+ .card {
58
+ padding: 2em;
59
+ }
60
+
61
+ #app {
62
+ max-width: 1280px;
63
+ margin: 0 auto;
64
+ padding: 2rem;
65
+ text-align: center;
66
+ }
67
+
68
+ @media (prefers-color-scheme: light) {
69
+ :root {
70
+ color: #213547;
71
+ background-color: #ffffff;
72
+ }
73
+ a:hover {
74
+ color: #747bff;
75
+ }
76
+ button {
77
+ background-color: #f9f9f9;
78
+ }
79
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Logger } from '@module-federation/bridge-shared';
2
+
3
+ export const LoggerInstance = new Logger('vue3-bridge');
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/tsconfig.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "compilerOptions": {
3
+ "useDefineForClassFields": true,
4
+
5
+ /* Bundler mode */
6
+ "allowImportingTsExtensions": true,
7
+ "resolveJsonModule": true,
8
+ "isolatedModules": true,
9
+ "jsxImportSource": "vue",
10
+
11
+ /* Linting */
12
+ "noUnusedLocals": true,
13
+ "noUnusedParameters": true,
14
+ "noFallthroughCasesInSwitch": true,
15
+ "declaration": true,
16
+ "emitDeclarationOnly": true,
17
+ "outDir": "dist",
18
+ "skipLibCheck": true,
19
+ "strict": true,
20
+ "moduleResolution": "node",
21
+ "target": "esnext",
22
+ "module": "esnext",
23
+ "lib": ["esnext", "dom"],
24
+ "jsx": "preserve",
25
+ "esModuleInterop": true,
26
+ "allowSyntheticDefaultImports": true,
27
+ "sourceMap": true,
28
+ "baseUrl": ".",
29
+ "paths": {
30
+ "@/*": ["src/*"]
31
+ }
32
+ },
33
+ "include": [
34
+ "src/**/*.ts",
35
+ "src/**/*.tsx",
36
+ "src/**/*.vue",
37
+ "src/remoteApp.tsx",
38
+ "src/create.ts"
39
+ ],
40
+ "references": [{ "path": "./tsconfig.node.json" }]
41
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "composite": true,
4
+ "skipLibCheck": true,
5
+ "module": "ESNext",
6
+ "moduleResolution": "bundler",
7
+ "allowSyntheticDefaultImports": true,
8
+ "strict": true
9
+ },
10
+ "include": ["vite.config.ts"]
11
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { defineConfig } from 'vite';
2
+ import vue from '@vitejs/plugin-vue';
3
+ import path from 'path';
4
+ import dts from 'vite-plugin-dts';
5
+ import vueJsx from '@vitejs/plugin-vue-jsx';
6
+ import packageJson from './package.json';
7
+
8
+ export default defineConfig({
9
+ plugins: [
10
+ vue(),
11
+ dts({
12
+ rollupTypes: true,
13
+ bundledPackages: ['@module-federation/bridge-shared'],
14
+ }),
15
+ vueJsx(),
16
+ ],
17
+ build: {
18
+ lib: {
19
+ entry: path.resolve(__dirname, 'src/index.ts'),
20
+ formats: ['cjs', 'es'],
21
+ fileName: (format) => `index.${format}.js`,
22
+ },
23
+ rollupOptions: {
24
+ external: ['vue', 'vue-router'],
25
+ },
26
+ },
27
+ define: {
28
+ __APP_VERSION__: JSON.stringify(packageJson.version),
29
+ },
30
+ });