Skip to content

Commit

Permalink
fix: ensure code is finished compiling before returning cached file (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakas committed Mar 2, 2023
1 parent 77c53e2 commit 91ae5b8
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,48 @@ function vitePreprocessor(userConfigPath?: string): CypressPreprocessor {
},
}

const watcher = await build({
configFile: userConfigPath,
...defaultConfig,
})

if (shouldWatch && isWatcher(watcher)) {
watcher.on('event', (event) => {
debug('Watcher %s for file %s', event.code, filePath)

if (event.code === 'END') {
file.emit('rerun')
}

if (event.code === 'ERROR') {
console.error(event)
}
})

file.on('close', () => {
delete cache[filePath]
watcher.close()

debug('File %s closed.', filePath)
})
}

cache[filePath] = outputPath
debug('Bundle for file %s cached at %s', filePath, outputPath)

return outputPath
const watcher = (await build({
configFile: userConfigPath,
...defaultConfig,
})) as BuildResult

return new Promise((resolve, reject) => {
if (shouldWatch && isWatcher(watcher)) {
watcher.on('event', (event) => {
debug('Watcher %s for file %s', event.code, filePath)

if (event.code === 'END') {
resolve(outputPath)
file.emit('rerun')
}

if (event.code === 'ERROR') {
console.error(event)
reject(new Error(event.error.message))
}
})

file.on('close', () => {
delete cache[filePath]
watcher.close()

debug('File %s closed.', filePath)
})
} else {
resolve(outputPath)
}
})
}
}

function getWatcherConfig(shouldWatch: boolean): WatcherOptions | null {
return shouldWatch ? {} : null
}

type BuildResult = RollupWatcher | RollupOutput | RollupOutput[]
type BuildResult = RollupOutput | RollupWatcher | RollupOutput[]

function isWatcher(watcher: BuildResult): watcher is RollupWatcher {
return (watcher as RollupWatcher).on !== undefined
Expand Down

0 comments on commit 91ae5b8

Please sign in to comment.