Skip to content

Commit db3f57a

Browse files
committed
fix(hmr): fix hmr when global mixins are used
fix #4174
1 parent fe58bae commit db3f57a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: packages/runtime-core/__tests__/hmr.spec.ts

+41-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import {
1111
nextTick
1212
} from '@vue/runtime-test'
1313
import * as runtimeTest from '@vue/runtime-test'
14+
import { registerRuntimeCompiler, createApp } from '@vue/runtime-test'
1415
import { baseCompile } from '@vue/compiler-core'
1516

1617
declare var __VUE_HMR_RUNTIME__: HMRRuntime
1718
const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
1819

19-
runtimeTest.registerRuntimeCompiler(compileToFunction)
20+
registerRuntimeCompiler(compileToFunction)
2021

2122
function compileToFunction(template: string) {
2223
const { code } = baseCompile(template)
@@ -395,4 +396,43 @@ describe('hot module replacement', () => {
395396
`<div style={}><div>1</div><div>2</div></div>`
396397
)
397398
})
399+
400+
// #4174
401+
test('with global mixins', async () => {
402+
const childId = 'hmr-global-mixin'
403+
const createSpy1 = jest.fn()
404+
const createSpy2 = jest.fn()
405+
406+
const Child: ComponentOptions = {
407+
__hmrId: childId,
408+
created: createSpy1,
409+
render() {
410+
return h('div')
411+
}
412+
}
413+
createRecord(childId, Child)
414+
415+
const Parent: ComponentOptions = {
416+
render: () => h(Child)
417+
}
418+
419+
const app = createApp(Parent)
420+
app.mixin({})
421+
422+
const root = nodeOps.createElement('div')
423+
app.mount(root)
424+
expect(createSpy1).toHaveBeenCalledTimes(1)
425+
expect(createSpy2).toHaveBeenCalledTimes(0)
426+
427+
reload(childId, {
428+
__hmrId: childId,
429+
created: createSpy2,
430+
render() {
431+
return h('div')
432+
}
433+
})
434+
await nextTick()
435+
expect(createSpy1).toHaveBeenCalledTimes(1)
436+
expect(createSpy2).toHaveBeenCalledTimes(1)
437+
})
398438
})

Diff for: packages/runtime-core/src/hmr.ts

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ function reload(id: string, newComp: ComponentOptions | ClassComponent) {
130130
}
131131

132132
Array.from(instances).forEach(instance => {
133+
// invalidate options resolution cache
134+
instance.appContext.optionsCache.delete(instance.type as any)
135+
133136
if (instance.parent) {
134137
// 4. Force the parent instance to re-render. This will cause all updated
135138
// components to be unmounted and re-mounted. Queue the update so that we

0 commit comments

Comments
 (0)