Skip to content

Commit

Permalink
fix: save original source descriptor in cache to compare on hmr, vite…
Browse files Browse the repository at this point in the history
  • Loading branch information
rashfael committed Jul 27, 2023
1 parent ec58d00 commit bd77219
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/plugin-vue/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare module 'vue/compiler-sfc' {
interface SFCDescriptor {
id: string
original?: SFCDescriptor
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const directRequestRE = /(?:\?|&)direct\b/
* Vite-specific HMR handling
*/
export async function handleHotUpdate(
{ file, modules, read }: HmrContext,
{ file, modules, read, server }: HmrContext,
options: ResolvedOptions,
): Promise<ModuleNode[] | void> {
const prevDescriptor = getDescriptor(file, options, false)
Expand All @@ -42,7 +42,10 @@ export async function handleHotUpdate(
const mainModule = getMainModule(modules)
const templateModule = modules.find((m) => /type=template/.test(m.url))

const scriptChanged = hasScriptChanged(prevDescriptor, descriptor)
const scriptChanged = hasScriptChanged(
prevDescriptor.original ? prevDescriptor.original : prevDescriptor,
descriptor,
)
if (scriptChanged) {
affectedModules.add(getScriptModule(modules) || mainModule)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export async function transformMain(

// prev descriptor is only set and used for hmr
const prevDescriptor = getPrevDescriptor(filename)
const { descriptor, errors } = createDescriptor(filename, code, options)
const { descriptor, errors } = createDescriptor(
filename,
code,
options,
pluginContext.originalCode,
)

if (errors.length) {
errors.forEach((error) =>
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createDescriptor(
filename: string,
source: string,
{ root, isProduction, sourceMap, compiler }: ResolvedOptions,
originalSource?: string,
): SFCParseResult {
const { descriptor, errors } = compiler.parse(source, {
filename,
Expand All @@ -29,6 +30,14 @@ export function createDescriptor(
const normalizedPath = slash(path.normalize(path.relative(root, filename)))
descriptor.id = getHash(normalizedPath + (isProduction ? source : ''))

if (originalSource && source !== originalSource) {
const { descriptor: originalDescriptor } = compiler.parse(originalSource, {
filename,
sourceMap,
})
descriptor.original = originalDescriptor
}

cache.set(filename, descriptor)
return { descriptor, errors }
}
Expand Down

0 comments on commit bd77219

Please # to comment.