fix: force rebuild on new files being added (#136)

This commit is contained in:
Daniel Roe 2021-02-19 14:01:08 +00:00 committed by GitHub
parent 85da52d390
commit e1f409ea51
2 changed files with 17 additions and 10 deletions

View File

@ -79,16 +79,8 @@ async function _build (nitroContext: NitroContext) {
}
}
async function _watch (nitroContext: NitroContext) {
function startRollupWatcher (nitroContext: NitroContext) {
const watcher = rollupWatch(nitroContext.rollupConfig)
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir,
(middleware, event, file) => {
nitroContext.scannedMiddleware = middleware
watcher.emit(event, file)
}
)
let start
watcher.on('event', (event) => {
@ -114,4 +106,19 @@ async function _watch (nitroContext: NitroContext) {
// consola.error(event.error)
}
})
return watcher
}
async function _watch (nitroContext: NitroContext) {
let watcher = startRollupWatcher(nitroContext)
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir,
(middleware, event) => {
nitroContext.scannedMiddleware = middleware
if (['add', 'addDir'].includes(event)) {
watcher.close()
watcher = startRollupWatcher(nitroContext)
}
}
)
}

View File

@ -23,7 +23,7 @@ function filesToMiddleware (files: string[], baseDir: string, basePath: string,
.map(m => ({ ...m, ...overrides }))
}
export function scanMiddleware (serverDir: string, onChange?: Function): Promise<ServerMiddleware[]> {
export function scanMiddleware (serverDir: string, onChange?: (results: ServerMiddleware[], event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', file: string) => void): Promise<ServerMiddleware[]> {
const pattern = '**/*.{js,ts}'
const globalDir = resolve(serverDir, 'middleware')
const apiDir = resolve(serverDir, 'api')