Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from May 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/nuxi/src/commands/dev.ts
Expand Up @@ -77,6 +77,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...`
Expand Down Expand Up @@ -123,15 +137,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)
Expand Down Expand Up @@ -163,7 +170,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 === '.env') { return hardRestart('.env updated') }
if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.nuxtrc)$/)) {
dLoad(true, `${file} updated`)
}
})
Expand Down