From 56de4b0eaf58ffcc6435772a4968874705e07c82 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 25 Apr 2023 16:05:53 +0100 Subject: [PATCH 1/4] fix(nuxi): hard-reload nuxt when `.env` changes --- packages/nuxi/src/commands/dev.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index ee527aff480e..b12787912f7b 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -77,6 +77,17 @@ export default defineNuxtCommand({ baseURL: withTrailingSlash(currentNuxt?.options.app.baseURL) || '/' }) } + async function hardRestart () { + if (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) + } + } const load = async (isRestart: boolean, reason?: string) => { try { loadingMessage = `${reason ? reason + '. ' : ''}${isRestart ? 'Restarting' : 'Starting'} nuxt...` @@ -123,12 +134,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' }) + if (options?.hard) { + return hardRestart() } else { await load(true) } @@ -163,7 +170,10 @@ 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() + } + if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.nuxtrc)$/)) { dLoad(true, `${file} updated`) } }) From b5728f557f20d8ef4189ac85ce8897259b209852 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 25 Apr 2023 16:10:29 +0100 Subject: [PATCH 2/4] fix: print reason when hard-restarting --- packages/nuxi/src/commands/dev.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index b12787912f7b..3fa55c89e8b8 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -77,15 +77,18 @@ export default defineNuxtCommand({ baseURL: withTrailingSlash(currentNuxt?.options.app.baseURL) || '/' }) } - async function hardRestart () { + 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) + await load(true, reason) } } const load = async (isRestart: boolean, reason?: string) => { @@ -134,11 +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) { - return hardRestart() - } else { - await load(true) - } + if (options?.hard) { return hardRestart() } + await load(true) }) await currentNuxt.hooks.callHook('listen', listener.server, listener) @@ -170,9 +170,7 @@ export default defineNuxtCommand({ const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 0 }) watcher.on('all', (_event, _file) => { const file = relative(rootDir, _file) - if (file === '.env') { - return hardRestart() - } + if (file === '.env') { return hardRestart('.env updated') } if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.nuxtrc)$/)) { dLoad(true, `${file} updated`) } From 2c56c2e3d521813ea43d8d51d9e2078d157f7402 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 11 May 2023 21:33:00 +0100 Subject: [PATCH 3/4] fix: respect passed `--dotenv` flag --- packages/nuxi/src/commands/dev.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 5f912cbf829f..cac98bcab4ad 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -63,9 +63,9 @@ export default defineNuxtCommand({ hostname: args.host || args.h || process.env.NUXT_HOST || config.devServer.host, https: (args.https !== false && (args.https || config.devServer.https)) ? { - cert: args['ssl-cert'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.cert) || undefined, - key: args['ssl-key'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.key) || undefined - } + cert: args['ssl-cert'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.cert) || undefined, + key: args['ssl-key'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.key) || undefined + } : false }) @@ -171,7 +171,7 @@ export default defineNuxtCommand({ const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 0 }) watcher.on('all', (_event, _file) => { const file = relative(rootDir, _file) - if (file === '.env') { return hardRestart('.env updated') } + if (file === (args.dotenv || '.env')) { return hardRestart('.env updated') } if (file.match(/^(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore|\.nuxtrc)$/)) { dLoad(true, `${file} updated`) } From a4de3ed8944cc917624e985c3dda275f228bf243 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 11 May 2023 20:34:35 +0000 Subject: [PATCH 4/4] [autofix.ci] apply automated fixes --- packages/nuxi/src/commands/dev.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index cac98bcab4ad..cdf7c77aef47 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -63,9 +63,9 @@ export default defineNuxtCommand({ hostname: args.host || args.h || process.env.NUXT_HOST || config.devServer.host, https: (args.https !== false && (args.https || config.devServer.https)) ? { - cert: args['ssl-cert'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.cert) || undefined, - key: args['ssl-key'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.key) || undefined - } + cert: args['ssl-cert'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.cert) || undefined, + key: args['ssl-key'] || (typeof config.devServer.https !== 'boolean' && config.devServer.https.key) || undefined + } : false })