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

Commit

Permalink
feat(nuxi): support --dotenv for dev, build and preview comma…
Browse files Browse the repository at this point in the history
…nds (#7660)
  • Loading branch information
cpreston321 committed Sep 26, 2022
1 parent 4c50488 commit 2e080c2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions docs/content/3.api/5.commands/build.md
@@ -1,14 +1,15 @@
# `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.

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`.
3 changes: 2 additions & 1 deletion docs/content/3.api/5.commands/dev.md
@@ -1,14 +1,15 @@
# `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)

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.
Expand Down
3 changes: 2 additions & 1 deletion docs/content/3.api/5.commands/preview.md
@@ -1,14 +1,15 @@
# `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.

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.

Expand Down
6 changes: 5 additions & 1 deletion packages/nuxi/src/commands/build.ts
Expand Up @@ -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) {
Expand All @@ -23,6 +23,10 @@ export default defineNuxtCommand({

const nuxt = await loadNuxt({
rootDir,
dotenv: {
cwd: rootDir,
fileName: args.dotenv
},
overrides: {
_generate: args.prerender
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxi/src/commands/dev.ts
Expand Up @@ -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) {
Expand All @@ -40,7 +40,7 @@ export default defineNuxtCommand({
const rootDir = resolve(args._[0] || '.')
showVersions(rootDir)

await setupDotenv({ cwd: rootDir })
await setupDotenv({ cwd: rootDir, fileName: args.dotenv })

const listener = await listen(serverHandler, {
showURL: false,
Expand Down
7 changes: 4 additions & 3 deletions packages/nuxi/src/commands/preview.ts
Expand Up @@ -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) {
Expand All @@ -35,9 +35,10 @@ export default defineNuxtCommand({
process.exit(1)
}

if (existsSync(resolve(rootDir, '.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: rootDir })
await setupDotenv({ cwd: rootDir, fileName: args.dotenv })
}

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

0 comments on commit 2e080c2

Please sign in to comment.