})
test('createRecord', () => {
- expect(createRecord('test1')).toBe(true)
+ expect(createRecord('test1', {})).toBe(true)
// if id has already been created, should return false
- expect(createRecord('test1')).toBe(false)
+ expect(createRecord('test1', {})).toBe(false)
})
test('rerender', async () => {
__hmrId: childId,
render: compileToFunction(`<div><slot/></div>`)
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
__hmrId: parentId,
`<div @click="count++">{{ count }}<Child>{{ count }}</Child></div>`
)
}
- createRecord(parentId)
+ createRecord(parentId, Parent)
render(h(Parent), root)
expect(serializeInner(root)).toBe(`<div>0<div>0</div></div>`)
unmounted: unmountSpy,
render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
render: () => h(Child)
render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
}
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
render: () => h(Child)
},
render: compileToFunction(template)
}
- createRecord(id)
+ createRecord(id, Comp)
render(h(Comp), root)
expect(serializeInner(root)).toBe(
},
render: compileToFunction(`<div>{{ msg }}</div>`)
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
__hmrId: parentId,
components: { Child },
render: compileToFunction(`<Child msg="foo" />`)
}
- createRecord(parentId)
+ createRecord(parentId, Parent)
render(h(Parent), root)
expect(serializeInner(root)).toBe(`<div>foo</div>`)
__hmrId: childId,
render: compileToFunction(`<div>child</div>`)
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
__hmrId: parentId,
components: { Child },
render: compileToFunction(`<Child class="test" />`)
}
- createRecord(parentId)
+ createRecord(parentId, Parent)
render(h(Parent), root)
expect(serializeInner(root)).toBe(`<div class="test">child</div>`)
__hmrId: childId,
render: compileToFunction(`<div>child</div>`)
}
- createRecord(childId)
+ createRecord(childId, Child)
const components: ComponentOptions[] = []
}
}
- createRecord(parentId)
+ createRecord(parentId, parentComp)
}
const last = components[components.length - 1]
</Child>
`)
}
- createRecord(parentId)
+ createRecord(parentId, Parent)
render(h(Parent), root)
expect(serializeInner(root)).toBe(
return h('div')
}
}
- createRecord(childId)
+ createRecord(childId, Child)
const Parent: ComponentOptions = {
render: () => h(Child)
expect(createSpy1).toHaveBeenCalledTimes(1)
expect(createSpy2).toHaveBeenCalledTimes(1)
})
+
+ // #4757
+ test('rerender for component that has no active instance yet', () => {
+ const id = 'no-active-instance-rerender'
+ const Foo: ComponentOptions = {
+ __hmrId: id,
+ render: () => 'foo'
+ }
+
+ createRecord(id, Foo)
+ rerender(id, () => 'bar')
+
+ const root = nodeOps.createElement('div')
+ render(h(Foo), root)
+ expect(serializeInner(root)).toBe('bar')
+ })
+
+ test('reload for component that has no active instance yet', () => {
+ const id = 'no-active-instance-reload'
+ const Foo: ComponentOptions = {
+ __hmrId: id,
+ render: () => 'foo'
+ }
+
+ createRecord(id, Foo)
+ reload(id, {
+ __hmrId: id,
+ render: () => 'bar'
+ })
+
+ const root = nodeOps.createElement('div')
+ render(h(Foo), root)
+ expect(serializeInner(root)).toBe('bar')
+ })
})
import { queueJob, queuePostFlushCb } from './scheduler'
import { extend, getGlobalThis } from '@vue/shared'
+type HMRComponent = ComponentOptions | ClassComponent
+
export let isHmrUpdating = false
export const hmrDirtyComponents = new Set<ConcreteComponent>()
} as HMRRuntime
}
-const map: Map<string, Set<ComponentInternalInstance>> = new Map()
+const map: Map<
+ string,
+ {
+ // the initial component definition is recorded on import - this allows us
+ // to apply hot updates to the component even when there are no actively
+ // rendered instance.
+ initialDef: ComponentOptions
+ instances: Set<ComponentInternalInstance>
+ }
+> = new Map()
export function registerHMR(instance: ComponentInternalInstance) {
const id = instance.type.__hmrId!
let record = map.get(id)
if (!record) {
- createRecord(id)
+ createRecord(id, instance.type as HMRComponent)
record = map.get(id)!
}
- record.add(instance)
+ record.instances.add(instance)
}
export function unregisterHMR(instance: ComponentInternalInstance) {
- map.get(instance.type.__hmrId!)!.delete(instance)
+ map.get(instance.type.__hmrId!)!.instances.delete(instance)
}
-function createRecord(id: string): boolean {
+function createRecord(id: string, initialDef: HMRComponent): boolean {
if (map.has(id)) {
return false
}
- map.set(id, new Set())
+ map.set(id, {
+ initialDef: normalizeClassComponent(initialDef),
+ instances: new Set()
+ })
return true
}
-type HMRComponent = ComponentOptions | ClassComponent
-
function normalizeClassComponent(component: HMRComponent): ComponentOptions {
return isClassComponent(component) ? component.__vccOpts : component
}
if (!record) {
return
}
+
+ // update initial record (for not-yet-rendered component)
+ record.initialDef.render = newRender
+
// Create a snapshot which avoids the set being mutated during updates
- ;[...record].forEach(instance => {
+ ;[...record.instances].forEach(instance => {
if (newRender) {
instance.render = newRender as InternalRenderFunction
normalizeClassComponent(instance.type as HMRComponent).render = newRender
if (!record) return
newComp = normalizeClassComponent(newComp)
+ // update initial def (for not-yet-rendered components)
+ updateComponentDef(record.initialDef, newComp)
// create a snapshot which avoids the set being mutated during updates
- const instances = [...record]
+ const instances = [...record.instances]
for (const instance of instances) {
const oldComp = normalizeClassComponent(instance.type as HMRComponent)
if (!hmrDirtyComponents.has(oldComp)) {
// 1. Update existing comp definition to match new one
- extend(oldComp, newComp)
- for (const key in oldComp) {
- if (key !== '__file' && !(key in newComp)) {
- delete (oldComp as any)[key]
- }
+ if (oldComp !== record.initialDef) {
+ updateComponentDef(oldComp, newComp)
}
// 2. mark definition dirty. This forces the renderer to replace the
// component on patch.
})
}
+function updateComponentDef(
+ oldComp: ComponentOptions,
+ newComp: ComponentOptions
+) {
+ extend(oldComp, newComp)
+ for (const key in oldComp) {
+ if (key !== '__file' && !(key in newComp)) {
+ delete (oldComp as any)[key]
+ }
+ }
+}
+
function tryWrap(fn: (id: string, arg: any) => any): Function {
return (id: string, arg: any) => {
try {