Skip to content

Commit 662c262

Browse files
authored
feat: make module update reactive by rpc functions (#104)
1 parent 549eec8 commit 662c262

File tree

11 files changed

+106
-10
lines changed

11 files changed

+106
-10
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"debug": "^4.3.4",
8686
"error-stack-parser-es": "^0.1.1",
8787
"fs-extra": "^11.1.1",
88+
"lodash-es": "^4.17.21",
8889
"open": "^9.1.0",
8990
"picocolors": "^1.0.0",
9091
"sirv": "^2.0.3"
@@ -98,6 +99,7 @@
9899
"@types/debug": "^4.1.12",
99100
"@types/diff-match-patch": "^1.0.36",
100101
"@types/fs-extra": "^11.0.4",
102+
"@types/lodash-es": "^4.17.12",
101103
"@types/node": "^20.10.0",
102104
"@unocss/eslint-config": "^0.57.7",
103105
"@unocss/eslint-plugin": "^0.57.7",

playground/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"build-inspect": "serve .vite-inspect"
1111
},
1212
"dependencies": {
13-
"vue": "^3.3.8"
13+
"vue": "^3.3.8",
14+
"vue-router": "^4.2.5"
1415
},
1516
"devDependencies": {
1617
"@vitejs/plugin-vue": "^4.5.0",

playground/src/App.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<script setup lang="ts">
2-
// This starter template is using Vue 3 <script setup> SFCs
3-
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
4-
import HelloWorld from './components/HelloWorld.vue'
2+
import { RouterLink, RouterView } from 'vue-router'
53
</script>
64

75
<template>
8-
<HelloWorld msg="Vite + Vue" />
6+
<main>
7+
<nav>
8+
<RouterLink to="/">
9+
Home
10+
</RouterLink>
11+
<RouterLink to="/other">
12+
Other
13+
</RouterLink>
14+
</nav>
15+
16+
<RouterView />
17+
</main>
918
</template>

playground/src/main.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createApp } from 'vue'
22
import './style.css'
33
import App from './App.vue'
4+
import router from './router'
45

5-
createApp(App).mount('#app')
6+
const app = createApp(App)
7+
app.use(router)
8+
app.mount('#app')

playground/src/router/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createRouter, createWebHashHistory } from 'vue-router'
2+
import Home from '../views/Home.vue'
3+
4+
const router = createRouter({
5+
history: createWebHashHistory(),
6+
routes: [
7+
{
8+
name: 'home',
9+
path: '/',
10+
component: Home,
11+
},
12+
{
13+
name: 'other',
14+
path: '/other',
15+
component: () => import('../views/Other.vue'),
16+
},
17+
],
18+
})
19+
20+
export default router

playground/src/views/Home.vue

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
// This starter template is using Vue 3 <script setup> SFCs
3+
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
4+
import HelloWorld from '../components/HelloWorld.vue'
5+
</script>
6+
7+
<template>
8+
<HelloWorld msg="Vite + Vue" />
9+
</template>

playground/src/views/Other.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>Other</div>
3+
</template>

pnpm-lock.yaml

+32-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/logic/rpc.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createRPCClient } from 'vite-dev-rpc'
22
import type { BirpcReturn } from 'birpc'
33
import { createHotContext } from 'vite-hot-client'
44
import type { ModuleTransformInfo, RPCFunctions } from '../../types'
5+
import { refetch } from './state'
56

67
export const isStaticMode = document.body.getAttribute('data-vite-inspect-mode') === 'BUILD'
78

@@ -31,9 +32,14 @@ function createStaticRpcClient(): RPCFunctions {
3132
return {}
3233
},
3334
getIdInfo,
35+
moduleUpdated() {},
3436
}
3537
}
3638

3739
export const rpc = isStaticMode
3840
? createStaticRpcClient() as BirpcReturn<RPCFunctions>
39-
: createRPCClient<RPCFunctions>('vite-plugin-inspect', (await createHotContext('/___', `${location.pathname.split('/__inspect')[0] || ''}/`.replace(/\/\//g, '/')))!)
41+
: createRPCClient<RPCFunctions, Pick<RPCFunctions, 'moduleUpdated'>>('vite-plugin-inspect', (await createHotContext('/___', `${location.pathname.split('/__inspect')[0] || ''}/`.replace(/\/\//g, '/')))!, {
42+
moduleUpdated() {
43+
refetch()
44+
},
45+
})

src/node/index.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Connect, Plugin, ResolvedConfig, ViteDevServer } from 'vite'
33
import sirv from 'sirv'
44
import { createRPCServer } from 'vite-dev-rpc'
55
import c from 'picocolors'
6+
import { debounce } from 'lodash-es'
67
import type { HMRData, RPCFunctions } from '../types'
78
import { DIR_CLIENT } from '../dir'
89
import type { Options } from './options'
@@ -121,16 +122,26 @@ export default function PluginInspect(options: Options = {}): Plugin {
121122
dev: true,
122123
}))
123124

124-
const rpcFunctions = {
125+
const rpcFunctions: RPCFunctions = {
125126
list: () => ctx.getList(server),
126127
getIdInfo,
127128
getPluginMetrics: (ssr = false) => ctx.getPluginMetrics(ssr),
128129
getServerMetrics,
129130
resolveId: (id: string, ssr = false) => ctx.resolveId(id, ssr),
130131
clear: clearId,
132+
moduleUpdated: () => {},
131133
}
132134

133-
createRPCServer<RPCFunctions>('vite-plugin-inspect', server.ws, rpcFunctions)
135+
const rpcServer = createRPCServer<RPCFunctions>('vite-plugin-inspect', server.ws, rpcFunctions)
136+
137+
const debouncedModuleUpdated = debounce(() => {
138+
rpcServer.moduleUpdated.asEvent()
139+
}, 100)
140+
141+
server.middlewares.use((req, res, next) => {
142+
debouncedModuleUpdated()
143+
next()
144+
})
134145

135146
function getServerMetrics() {
136147
return serverPerf || {}

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export interface RPCFunctions {
6868
clear(id: string, ssr: boolean): Awaitable<void>
6969
getPluginMetrics(ssr: boolean): Awaitable<PluginMetricInfo[]>
7070
getServerMetrics(): Awaitable<Record<string, Record<string, { name: string, self: number, total: number }[]>>>
71+
moduleUpdated(): void
7172
}
7273

7374
export interface HMRData {

0 commit comments

Comments
 (0)