Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 29bbe7a

Browse files
committed
wip: reused compiled script on template hmr
1 parent 53b0e40 commit 29bbe7a

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/handleHotUpdate.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs'
22
import _debug from 'debug'
33
import { parse, SFCBlock } from '@vue/compiler-sfc'
44
import { getDescriptor, setDescriptor } from './utils/descriptorCache'
5+
import { getResolvedScript, setResolvedScript } from './script'
56

67
const debug = _debug('vite:hmr')
78

@@ -50,6 +51,11 @@ export async function handleHotUpdate(file: string, modules: any[]) {
5051
}
5152

5253
if (!isEqualBlock(descriptor.template, prevDescriptor.template)) {
54+
// when a <script setup> component's template changes, it will need correct
55+
// binding metadata. However, when reloading the template alone the binding
56+
// metadata will not be available since the script part isn't loaded.
57+
// in this case, reuse the compiled script from previous descriptor.
58+
setResolvedScript(descriptor, getResolvedScript(prevDescriptor)!)
5359
needRerender = true
5460
}
5561

@@ -75,13 +81,6 @@ export async function handleHotUpdate(file: string, modules: any[]) {
7581
const prev = prevStyles[i]
7682
const next = nextStyles[i]
7783
if (!prev || !isEqualBlock(prev, next)) {
78-
// css modules update causes a reload because the $style object is changed
79-
// and it may be used in JS.
80-
// if (prev.module != null || next.module != null) {
81-
// return modules.filter(
82-
// (m) => !/type=/.test(m.id) || /type=script/.test(m.id)
83-
// )
84-
// }
8584
didUpdateStyle = true
8685
filteredModules.push(modules.find((m) => m.id.includes(`index=${i}`)))
8786
}

src/script.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ const serverCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
1010

1111
export function getResolvedScript(
1212
descriptor: SFCDescriptor,
13-
isServer: boolean
13+
isServer = false
1414
): SFCScriptBlock | null | undefined {
1515
return (isServer ? serverCache : clientCache).get(descriptor)
1616
}
1717

18+
export function setResolvedScript(
19+
descriptor: SFCDescriptor,
20+
script: SFCScriptBlock,
21+
isServer = false
22+
) {
23+
;(isServer ? serverCache : clientCache).set(descriptor, script)
24+
}
25+
1826
export function resolveScript(
1927
descriptor: SFCDescriptor,
2028
scopeId: string,

src/utils/descriptorCache.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ export function getDescriptor(id: string) {
1010
if (cache.has(id)) {
1111
return cache.get(id)!
1212
}
13-
1413
throw new Error(`${id} is not parsed yet`)
1514
}

0 commit comments

Comments
 (0)