Skip to content

Commit

Permalink
fix(plugin-vue): ensure descriptor in case main request is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 8, 2021
1 parent 38de2c9 commit 85612fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/handleHotUpdate.ts
Expand Up @@ -19,7 +19,7 @@ export async function handleHotUpdate({
read,
server
}: HmrContext): Promise<ModuleNode[] | void> {
const prevDescriptor = getDescriptor(file, false)
const prevDescriptor = getDescriptor(file, server.config.root, false, false)
if (!prevDescriptor) {
// file hasn't been requested yet (e.g. async component)
return
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-vue/src/index.ts
Expand Up @@ -167,7 +167,11 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
if (query.src) {
return fs.readFileSync(filename, 'utf-8')
}
const descriptor = getDescriptor(filename)!
const descriptor = getDescriptor(
filename,
options.root,
options.isProduction
)!
let block: SFCBlock | null | undefined
if (query.type === 'script') {
// handle <scrip> + <script setup> merge via compileScript()
Expand Down Expand Up @@ -219,7 +223,11 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
)
} else {
// sub block request
const descriptor = getDescriptor(filename)!
const descriptor = getDescriptor(
filename,
options.root,
options.isProduction
)!
if (query.type === 'template') {
return transformTemplateAsModule(code, descriptor, options, this, ssr)
} else if (query.type === 'style') {
Expand Down
19 changes: 14 additions & 5 deletions packages/plugin-vue/src/utils/descriptorCache.ts
@@ -1,3 +1,4 @@
import fs from 'fs'
import path from 'path'
import slash from 'slash'
import hash from 'hash-sum'
Expand Down Expand Up @@ -45,16 +46,24 @@ export function setPrevDescriptor(

export function getDescriptor(
filename: string,
errorOnMissing = true
root: string,
isProduction: boolean | undefined,
createIfNotFound = true
): SFCDescriptor | undefined {
if (cache.has(filename)) {
return cache.get(filename)!
}
if (errorOnMissing) {
throw new Error(
`${filename} has no corresponding SFC entry in the cache. ` +
`This is a @vitejs/plugin-vue internal error, please open an issue.`
if (createIfNotFound) {
const { descriptor, errors } = createDescriptor(
filename,
fs.readFileSync(filename, 'utf-8'),
root,
isProduction
)
if (errors) {
throw errors[0]
}
return descriptor
}
}

Expand Down

0 comments on commit 85612fe

Please sign in to comment.