Skip to content

Commit b28891d

Browse files
authored
🐛 invalid web module fix update web content (#13)
1 parent 420a517 commit b28891d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

.changeset/rude-bags-care.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vsit": patch
3+
---
4+
5+
fix edit web content not working

packages/vsit/src/plugins/vsit/index.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
} from '@/common/utils'
1919

2020
import type { AsyncReturnType } from 'type-fest'
21-
import type { Plugin } from 'vite'
21+
import type { Plugin, ViteDevServer } from 'vite'
22+
23+
const invalid = async (moduleName: string, server: ViteDevServer) => {
24+
const module = await server.moduleGraph.getModuleByUrl(moduleName)
25+
module && server.moduleGraph.invalidateModule(module)
26+
}
2227

2328
export const PluginVit = (): Plugin[] => {
2429
let content = ''
@@ -45,6 +50,8 @@ export const PluginVit = (): Plugin[] => {
4550
return
4651
}
4752
if (url.pathname === '/fake-web-file' && req.method === 'GET') {
53+
// Invalid before transformRequest, make sure transformRequest get latest content from user
54+
await invalid(VIRUTAL_WEB_ID, server)
4855
webContent = (await server.transformRequest(VIRUTAL_WEB_ID, { ssr: false }))?.code ?? ''
4956
res.setHeader('Content-Type', 'text/javascript')
5057
res.end(webContent)
@@ -71,7 +78,7 @@ export const PluginVit = (): Plugin[] => {
7178
} else {
7279
res.end('ok')
7380
}
74-
module && server.moduleGraph.invalidateModule(module)
81+
invalid(VIRUTAL_NODE_ID, server)
7582
} catch (e) {
7683
console.error(e)
7784
}

0 commit comments

Comments
 (0)