mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(nuxi): hard-reload nuxt when .env
changes (#20501)
This commit is contained in:
parent
5e74fe4e0b
commit
83c7172ace
@ -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 {
|
||||
await load(true)
|
||||
}
|
||||
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`)
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user