fix(nuxi): hard-reload nuxt when .env changes (#20501)

This commit is contained in:
Daniel Roe 2023-05-11 22:00:41 +01:00 committed by GitHub
parent 5e74fe4e0b
commit 83c7172ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,6 +78,20 @@ export default defineNuxtCommand({
baseURL: withTrailingSlash(currentNuxt?.options.app.baseURL) || '/'
})
}
async function hardRestart (reason?: string) {
if (process.send) {
await listener.close().catch(() => {})
await currentNuxt.close().catch(() => {})
await watcher.close().catch(() => {})
await distWatcher.close().catch(() => {})
if (reason) {
consola.info(`${reason ? reason + '. ' : ''}Restarting nuxt...`)
}
process.send({ type: 'nuxt:restart' })
} else {
await load(true, reason)
}
}
const load = async (isRestart: boolean, reason?: string) => {
try {
loadingMessage = `${reason ? reason + '. ' : ''}${isRestart ? 'Restarting' : 'Starting'} nuxt...`
@ -124,15 +138,8 @@ export default defineNuxtCommand({
const unsub = currentNuxt.hooks.hook('restart', async (options) => {
unsub() // we use this instead of `hookOnce` for Nuxt Bridge support
if (options?.hard && process.send) {
await listener.close().catch(() => {})
await currentNuxt.close().catch(() => {})
await watcher.close().catch(() => {})
await distWatcher.close().catch(() => {})
process.send({ type: 'nuxt:restart' })
} else {
if (options?.hard) { return hardRestart() }
await load(true)
}
})
await currentNuxt.hooks.callHook('listen', listener.server, listener)
@ -164,7 +171,8 @@ export default defineNuxtCommand({
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 0 })
watcher.on('all', (_event, _file) => {
const file = relative(rootDir, _file)
if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.env|\.nuxtrc)$/)) {
if (file === (args.dotenv || '.env')) { return hardRestart('.env updated') }
if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.nuxtrc)$/)) {
dLoad(true, `${file} updated`)
}
})