8000 fix(hmr): should reload if relies file changed after re-render · unplugin/unplugin-vue@da26e52 · GitHub
[go: up one dir, main page]

Skip to content

Commit da26e52

Browse files
committed
fix(hmr): should reload if relies file changed after re-render
ref: vitejs/vite-plugin-vue@62b17f3
1 parent 0f2d3eb commit da26e52

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/core/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ export const plugin = createUnplugin<Options | undefined, false>(
227227
vite: {
228228
api,
229229
handleHotUpdate(ctx) {
230+
ctx.server.ws.send({
231+
type: 'custom',
232+
event: 'file-changed',
233+
data: { file: normalizePath(ctx.file) },
234+
})
235+
230236
if (options.value.compiler.invalidateTypeCache) {
231237
options.value.compiler.invalidateTypeCache(ctx.file)
232238
}

src/core/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,19 @@ export async function transformMain(
149149
`_sfc_main.__hmrId = ${JSON.stringify(descriptor.id)}`,
150150
`typeof __VUE_HMR_RUNTIME__ !== 'undefined' && ` +
151151
`__VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)`,
152+
`import.meta.hot.on('file-changed', ({ file }) => {`,
153+
` __VUE_HMR_RUNTIME__.CHANGED_FILE = file`,
154+
`})`,
152155
)
153156
// check if the template is the only thing that changed
154157
if (prevDescriptor && isOnlyTemplateChanged(prevDescriptor, descriptor)) {
155-
output.push(`export const _rerender_only = true`)
158+
// #7 only consider re-render if the HMR is triggered by the current component,
159+
// otherwise reload. Due to vite will cache the transform result. If the HMR
160+
// is triggered by other files that the current component relies on, a reload
161+
// is required.
162+
output.push(
163+
`export const _rerender_only = __VUE_HMR_RUNTIME__.CHANGED_FILE === ${JSON.stringify(normalizePath(filename))}`,
164+
)
156165
}
157166
output.push(
158167
`import.meta.hot.accept(mod => {`,

0 commit comments

Comments
 (0)
0