Skip to content

Commit

Permalink
fix(plugin-vue): avoid throwing on never requested file
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 29, 2020
1 parent ce4d714 commit 48a24c1
Show file tree
Hide file tree
Showing 3 changed files with 10 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: () => string | Promise<string>,
server: ViteDevServer
): Promise<ModuleNode[] | void> {
const prevDescriptor = getDescriptor(file)
const prevDescriptor = getDescriptor(file, false)
if (!prevDescriptor) {
// file hasn't been requested yet (e.g. async component)
return
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-vue/src/index.ts
Expand Up @@ -107,7 +107,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
if (query.src) {
return fs.readFileSync(filename, 'utf-8')
}
const descriptor = getDescriptor(filename)
const descriptor = getDescriptor(filename)!
let block: SFCBlock | null | undefined
if (query.type === 'script') {
// handle <scrip> + <script setup> merge via compileScript()
Expand Down Expand Up @@ -139,7 +139,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
return transformMain(code, filename, options, this)
} else {
// sub block request
const descriptor = getDescriptor(filename)
const descriptor = getDescriptor(filename)!
if (query.type === 'template') {
return transformTemplateAsModule(code, descriptor, options, this)
} else if (query.type === 'style') {
Expand Down
12 changes: 7 additions & 5 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Expand Up @@ -34,14 +34,16 @@ export function setPrevDescriptor(filename: string, entry: SFCDescriptor) {
prevCache.set(filename, entry)
}

export function getDescriptor(filename: string) {
export function getDescriptor(filename: string, errorOnMissing = true) {
if (cache.has(filename)) {
return cache.get(filename)!
}
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 (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.`
)
}
}

export function setDescriptor(filename: string, entry: SFCDescriptor) {
Expand Down

0 comments on commit 48a24c1

Please sign in to comment.