feat(nuxi): load `.env` when previewing builds (#4016)

This commit is contained in:
Daniel Roe 2022-04-01 11:28:39 +01:00 committed by GitHub
parent 51f3bbfd23
commit 02d2d1ae35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 0 deletions

View File

@ -67,6 +67,10 @@ Option | Default | Description
This command sets `process.env.NODE_ENV` to `production`. To override, define `NODE_ENV` in a `.env` file or as command-line argument.
::alert{type=info}
For convenience, in preview mode, your `.env` file will be loaded into `process.env`. (However, in production you will need to ensure your environment variables are set yourself.)
::
## Info
```{bash}

View File

@ -24,6 +24,7 @@
"@types/mri": "^1.1.1",
"@types/rimraf": "^3",
"@types/semver": "^7",
"c12": "^0.2.4",
"chokidar": "^3.5.3",
"clear": "^0.1.0",
"clipboardy": "^3.0.0",

View File

@ -1,6 +1,7 @@
import { existsSync, promises as fsp } from 'fs'
import { dirname, relative } from 'path'
import { execa } from 'execa'
import { loadDotenv } from 'c12'
import { resolve } from 'pathe'
import consola from 'consola'
@ -34,6 +35,11 @@ export default defineNuxtCommand({
process.exit(1)
}
if (existsSync(resolve(rootDir, '.env'))) {
consola.info('Loading `.env`. This will not be loaded when running the server in production.')
process.env = await loadDotenv({ cwd: rootDir, fileName: '.env', env: process.env })
}
consola.info('Starting preview command:', nitroJSON.commands.preview)
const [command, ...commandArgs] = nitroJSON.commands.preview.split(' ')
consola.log('')

View File

@ -15369,6 +15369,7 @@ __metadata:
"@types/mri": ^1.1.1
"@types/rimraf": ^3
"@types/semver": ^7
c12: ^0.2.4
chokidar: ^3.5.3
clear: ^0.1.0
clipboardy: ^3.0.0