Skip to content

Commit 743b731

Browse files
committed
fix: handle main file src prefix when setting files + avoid infinite loop due to state.error push
1 parent a9674f5 commit 743b731

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/store.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,15 @@ export class ReplStore implements Store {
246246
}
247247

248248
addFile(fileOrFilename: string | File): void {
249-
const file =
250-
typeof fileOrFilename === 'string'
251-
? new File(
252-
fileOrFilename,
253-
fileOrFilename.endsWith('.vue') ? newSFCCode : ''
254-
)
255-
: fileOrFilename
249+
let file: File
250+
if (typeof fileOrFilename === 'string') {
251+
file = new File(
252+
fileOrFilename,
253+
fileOrFilename.endsWith('.vue') ? newSFCCode : ''
254+
)
255+
} else {
256+
file = fileOrFilename
257+
}
256258
this.state.files[file.filename] = file
257259
if (!file.hidden) this.setActive(file.filename)
258260
}
@@ -342,14 +344,15 @@ export class ReplStore implements Store {
342344
for (const filename in newFiles) {
343345
setFile(files, filename, newFiles[filename])
344346
}
345-
this.state.errors = []
347+
const errors = []
346348
for (const file in files) {
347-
this.state.errors.push(...(await compileFile(this, files[file])))
349+
errors.push(...(await compileFile(this, files[file])))
348350
}
349-
this.state.mainFile = mainFile
351+
this.state.errors = errors
352+
this.state.mainFile = addSrcPrefix(mainFile)
350353
this.state.files = files
351354
this.initImportMap()
352-
this.setActive(mainFile)
355+
this.setActive(this.state.mainFile)
353356
this.forceSandboxReset()
354357
}
355358

@@ -470,15 +473,18 @@ function setFile(
470473
) {
471474
// prefix user files with src/
472475
// for cleaner Volar path completion when using Monaco editor
473-
const normalized =
474-
filename !== importMapFile &&
475-
filename !== tsconfigFile &&
476-
!filename.startsWith('src/')
477-
? `src/${filename}`
478-
: filename
476+
const normalized = addSrcPrefix(filename)
479477
files[normalized] = new File(normalized, content)
480478
}
481479

480+
function addSrcPrefix(file: string) {
481+
return file === importMapFile ||
482+
file === tsconfigFile ||
483+
file.startsWith('src/')
484+
? file
485+
: `src/${file}`
486+
}
487+
482488
function fixURL(url: string) {
483489
return url.replace('https://sfc.vuejs', 'https://play.vuejs')
484490
}

0 commit comments

Comments
 (0)