From 6e253dea10d7890fde0d166e6f83d83ea82c9490 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Mon, 19 Sep 2022 17:19:42 -0400 Subject: [PATCH 1/4] feat(nuxi): add `--dotenv` to `dev` & `preview` --- packages/nuxi/src/commands/dev.ts | 5 +++-- packages/nuxi/src/commands/preview.ts | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index d4b6a83604d..efeca812b25 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -18,7 +18,7 @@ import { defineNuxtCommand } from './index' export default defineNuxtCommand({ meta: { name: 'dev', - usage: 'npx nuxi dev [rootDir] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]', + usage: 'npx nuxi dev [rootDir] [--dotenv] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]', description: 'Run nuxt development server' }, async invoke (args) { @@ -40,7 +40,8 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') showVersions(rootDir) - await setupDotenv({ cwd: rootDir }) + const envWrkDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir + await setupDotenv({ cwd: envWrkDir }) const listener = await listen(serverHandler, { showURL: false, diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index 0fb07832a01..6dc895a4ba6 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -10,7 +10,7 @@ import { defineNuxtCommand } from './index' export default defineNuxtCommand({ meta: { name: 'preview', - usage: 'npx nuxi preview|start [rootDir]', + usage: 'npx nuxi preview|start [--dotenv] [rootDir]', description: 'Launches nitro server for local testing after `nuxi build`.' }, async invoke (args) { @@ -35,9 +35,10 @@ export default defineNuxtCommand({ process.exit(1) } - if (existsSync(resolve(rootDir, '.env'))) { + const envWrkDir = args.dotenv ? resolve(rootDir, args.dotenv) : resolve(rootDir) + if (existsSync(resolve(envWrkDir, '.env'))) { consola.info('Loading `.env`. This will not be loaded when running the server in production.') - await setupDotenv({ cwd: rootDir }) + await setupDotenv({ cwd: envWrkDir }) } consola.info('Starting preview command:', nitroJSON.commands.preview) From 04913ff578a0845e489aa9e2e28456060d476fd9 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Tue, 20 Sep 2022 09:05:47 -0400 Subject: [PATCH 2/4] chore: update docs & build command --- docs/content/3.api/5.commands/build.md | 5 +++-- docs/content/3.api/5.commands/dev.md | 3 ++- docs/content/3.api/5.commands/preview.md | 3 ++- packages/nuxi/src/commands/build.ts | 4 +++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/content/3.api/5.commands/build.md b/docs/content/3.api/5.commands/build.md index fbedf82429a..696f3e8a728 100644 --- a/docs/content/3.api/5.commands/build.md +++ b/docs/content/3.api/5.commands/build.md @@ -1,7 +1,7 @@ # `nuxi build` ```{bash} -npx nuxi build [rootDir] +npx nuxi build [rootDir] [--prerender] [--dotenv] ``` The `build` command creates a `.output` directory with all your application, server and dependencies ready for production. @@ -9,6 +9,7 @@ The `build` command creates a `.output` directory with all your application, ser Option | Default | Description -------------------------|-----------------|------------------ `rootDir` | `.` | The root directory of the application to bundle. -`prerender` | `false` | Pre-render every route of your application. (**note:** This is an experimental flag. The behavior might be changed.) +`--prerender` | `false` | Pre-render every route of your application. (**note:** This is an experimental flag. The behavior might be changed.) +`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory. This command sets `process.env.NODE_ENV` to `production`. diff --git a/docs/content/3.api/5.commands/dev.md b/docs/content/3.api/5.commands/dev.md index 05d1fb6b8fe..ebd8f6d72f9 100644 --- a/docs/content/3.api/5.commands/dev.md +++ b/docs/content/3.api/5.commands/dev.md @@ -1,7 +1,7 @@ # `nuxi dev` ```{bash} -npx nuxi dev [rootDir] [--clipboard] [--open, -o] [--no-clear] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key] +npx nuxi dev [rootDir] [--dotenv] [--clipboard] [--open, -o] [--no-clear] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key] ``` The `dev` command starts a development server with hot module replacement at [http://localhost:3000](https://localhost:3000) @@ -9,6 +9,7 @@ The `dev` command starts a development server with hot module replacement at [ht Option | Default | Description -------------------------|-----------------|------------------ `rootDir` | `.` | The root directory of the application to serve. +`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory. `--clipboard` | `false` | Copy URL to clipboard. `--open, -o` | `false` | Open URL in browser. `--no-clear` | `false` | Does not clear the console after startup. diff --git a/docs/content/3.api/5.commands/preview.md b/docs/content/3.api/5.commands/preview.md index c7d2b524d1b..4b86222e999 100644 --- a/docs/content/3.api/5.commands/preview.md +++ b/docs/content/3.api/5.commands/preview.md @@ -1,7 +1,7 @@ # `nuxi preview` ```{bash} -npx nuxi preview [rootDir] +npx nuxi preview [rootDir] [--dotenv] ``` The `preview` command starts a server to preview your Nuxt application after running the `build` command. @@ -9,6 +9,7 @@ The `preview` command starts a server to preview your Nuxt application after run Option | Default | Description -------------------------|-----------------|------------------ `rootDir` | `.` | The root directory of the application to preview. +`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory. This command sets `process.env.NODE_ENV` to `production`. To override, define `NODE_ENV` in a `.env` file or as command-line argument. diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index b2868355a00..1d05a05d681 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -10,7 +10,7 @@ import { defineNuxtCommand } from './index' export default defineNuxtCommand({ meta: { name: 'build', - usage: 'npx nuxi build [--prerender] [rootDir]', + usage: 'npx nuxi build [--prerender] [--dotenv] [rootDir]', description: 'Build nuxt for production deployment' }, async invoke (args) { @@ -19,10 +19,12 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') showVersions(rootDir) + const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir const { loadNuxt, buildNuxt, useNitro } = await loadKit(rootDir) const nuxt = await loadNuxt({ rootDir, + dotenv: dotenvDir, overrides: { _generate: args.prerender } From f3628439f069a0c16bb0f06c6e3f8c77b0e5db61 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Tue, 20 Sep 2022 09:20:54 -0400 Subject: [PATCH 3/4] chore: rename variable --- packages/nuxi/src/commands/dev.ts | 4 ++-- packages/nuxi/src/commands/preview.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index efeca812b25..9029abd9225 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -40,8 +40,8 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') showVersions(rootDir) - const envWrkDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir - await setupDotenv({ cwd: envWrkDir }) + const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir + await setupDotenv({ cwd: dotenvDir }) const listener = await listen(serverHandler, { showURL: false, diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index 6dc895a4ba6..a298c5efb8b 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -35,10 +35,10 @@ export default defineNuxtCommand({ process.exit(1) } - const envWrkDir = args.dotenv ? resolve(rootDir, args.dotenv) : resolve(rootDir) - if (existsSync(resolve(envWrkDir, '.env'))) { + const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : resolve(rootDir) + if (existsSync(resolve(dotenvDir, '.env'))) { consola.info('Loading `.env`. This will not be loaded when running the server in production.') - await setupDotenv({ cwd: envWrkDir }) + await setupDotenv({ cwd: dotenvDir }) } consola.info('Starting preview command:', nitroJSON.commands.preview) From 170185b6920a1166f0e4e03772ccfb6d814dce52 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Tue, 20 Sep 2022 23:22:39 -0400 Subject: [PATCH 4/4] fix: add fileName option --- packages/nuxi/src/commands/build.ts | 6 ++++-- packages/nuxi/src/commands/dev.ts | 3 +-- packages/nuxi/src/commands/preview.ts | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index 1d05a05d681..da19795a5fc 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -19,12 +19,14 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') showVersions(rootDir) - const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir const { loadNuxt, buildNuxt, useNitro } = await loadKit(rootDir) const nuxt = await loadNuxt({ rootDir, - dotenv: dotenvDir, + dotenv: { + cwd: rootDir, + fileName: args.dotenv + }, overrides: { _generate: args.prerender } diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 8995c0897ba..54ed2a3f21e 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -40,8 +40,7 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') showVersions(rootDir) - const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : rootDir - await setupDotenv({ cwd: dotenvDir }) + await setupDotenv({ cwd: rootDir, fileName: args.dotenv }) const listener = await listen(serverHandler, { showURL: false, diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index a298c5efb8b..028367c4654 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -35,10 +35,10 @@ export default defineNuxtCommand({ process.exit(1) } - const dotenvDir = args.dotenv ? resolve(rootDir, args.dotenv) : resolve(rootDir) - if (existsSync(resolve(dotenvDir, '.env'))) { + const envExists = args.dotenv ? existsSync(resolve(rootDir, args.dotenv)) : existsSync(rootDir) + if (envExists) { consola.info('Loading `.env`. This will not be loaded when running the server in production.') - await setupDotenv({ cwd: dotenvDir }) + await setupDotenv({ cwd: rootDir, fileName: args.dotenv }) } consola.info('Starting preview command:', nitroJSON.commands.preview)