Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 2e080c2

Browse files
authored
feat(nuxi): support --dotenv for dev, build and preview commands (#7660)
1 parent 4c50488 commit 2e080c2

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# `nuxi build`
22

33
```{bash}
4-
npx nuxi build [rootDir]
4+
npx nuxi build [rootDir] [--prerender] [--dotenv]
55
```
66

77
The `build` command creates a `.output` directory with all your application, server and dependencies ready for production.
88

99
Option | Default | Description
1010
-------------------------|-----------------|------------------
1111
`rootDir` | `.` | The root directory of the application to bundle.
12-
`prerender` | `false` | Pre-render every route of your application. (**note:** This is an experimental flag. The behavior might be changed.)
12+
`--prerender` | `false` | Pre-render every route of your application. (**note:** This is an experimental flag. The behavior might be changed.)
13+
`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory.
1314

1415
This command sets `process.env.NODE_ENV` to `production`.

docs/content/3.api/5.commands/dev.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# `nuxi dev`
22

33
```{bash}
4-
npx nuxi dev [rootDir] [--clipboard] [--open, -o] [--no-clear] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]
4+
npx nuxi dev [rootDir] [--dotenv] [--clipboard] [--open, -o] [--no-clear] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]
55
```
66

77
The `dev` command starts a development server with hot module replacement at [http://localhost:3000](https://localhost:3000)
88

99
Option | Default | Description
1010
-------------------------|-----------------|------------------
1111
`rootDir` | `.` | The root directory of the application to serve.
12+
`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory.
1213
`--clipboard` | `false` | Copy URL to clipboard.
1314
`--open, -o` | `false` | Open URL in browser.
1415
`--no-clear` | `false` | Does not clear the console after startup.

docs/content/3.api/5.commands/preview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# `nuxi preview`
22

33
```{bash}
4-
npx nuxi preview [rootDir]
4+
npx nuxi preview [rootDir] [--dotenv]
55
```
66

77
The `preview` command starts a server to preview your Nuxt application after running the `build` command.
88

99
Option | Default | Description
1010
-------------------------|-----------------|------------------
1111
`rootDir` | `.` | The root directory of the application to preview.
12+
`--dotenv` | `.` | Point to another `.env` file to load, **relative** to the root directory.
1213

1314
This command sets `process.env.NODE_ENV` to `production`. To override, define `NODE_ENV` in a `.env` file or as command-line argument.
1415

packages/nuxi/src/commands/build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { defineNuxtCommand } from './index'
1010
export default defineNuxtCommand({
1111
meta: {
1212
name: 'build',
13-
usage: 'npx nuxi build [--prerender] [rootDir]',
13+
usage: 'npx nuxi build [--prerender] [--dotenv] [rootDir]',
1414
description: 'Build nuxt for production deployment'
1515
},
1616
async invoke (args) {
@@ -23,6 +23,10 @@ export default defineNuxtCommand({
2323

2424
const nuxt = await loadNuxt({
2525
rootDir,
26+
dotenv: {
27+
cwd: rootDir,
28+
fileName: args.dotenv
29+
},
2630
overrides: {
2731
_generate: args.prerender
2832
}

packages/nuxi/src/commands/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { defineNuxtCommand } from './index'
1818
export default defineNuxtCommand({
1919
meta: {
2020
name: 'dev',
21-
usage: 'npx nuxi dev [rootDir] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]',
21+
usage: 'npx nuxi dev [rootDir] [--dotenv] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]',
2222
description: 'Run nuxt development server'
2323
},
2424
async invoke (args) {
@@ -40,7 +40,7 @@ export default defineNuxtCommand({
4040
const rootDir = resolve(args._[0] || '.')
4141
showVersions(rootDir)
4242

43-
await setupDotenv({ cwd: rootDir })
43+
await setupDotenv({ cwd: rootDir, fileName: args.dotenv })
4444

4545
const listener = await listen(serverHandler, {
4646
showURL: false,

packages/nuxi/src/commands/preview.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { defineNuxtCommand } from './index'
1010
export default defineNuxtCommand({
1111
meta: {
1212
name: 'preview',
13-
usage: 'npx nuxi preview|start [rootDir]',
13+
usage: 'npx nuxi preview|start [--dotenv] [rootDir]',
1414
description: 'Launches nitro server for local testing after `nuxi build`.'
1515
},
1616
async invoke (args) {
@@ -35,9 +35,10 @@ export default defineNuxtCommand({
3535
process.exit(1)
3636
}
3737

38-
if (existsSync(resolve(rootDir, '.env'))) {
38+
const envExists = args.dotenv ? existsSync(resolve(rootDir, args.dotenv)) : existsSync(rootDir)
39+
if (envExists) {
3940
consola.info('Loading `.env`. This will not be loaded when running the server in production.')
40-
await setupDotenv({ cwd: rootDir })
41+
await setupDotenv({ cwd: rootDir, fileName: args.dotenv })
4142
}
4243

4344
consola.info('Starting preview command:', nitroJSON.commands.preview)

0 commit comments

Comments
 (0)