Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev,preview): correctly apply --dotenv arg #381

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export default defineCommand({
args: {
...sharedArgs,
...legacyRootDirArgs,
dotenv: {
type: 'string',
description: 'Path to .env file',
default: '.env',
},
clear: {
type: 'boolean',
description: 'Clear console on restart',
},
},
async run(ctx) {
const logger = consola.withTag('nuxi')
Expand All @@ -42,8 +51,8 @@ export default defineCommand({
cwd,
overrides: ctx.data?.overrides,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: !!ctx.args.clear,
dotenv: !!ctx.args.dotenv,
clear: ctx.args.clear,
dotenv: ctx.args.dotenv,
port: process.env._PORT ?? undefined,
devContext,
})
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const command = defineCommand({
dotenv: {
type: 'string',
description: 'Path to .env file',
default: '.env',
},
clear: {
type: 'boolean',
Expand Down Expand Up @@ -79,7 +80,7 @@ const command = defineCommand({
overrides: ctx.data?.overrides,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: ctx.args.clear,
dotenv: !!ctx.args.dotenv,
dotenv: ctx.args.dotenv,
loadingTemplate: nuxtOptions.devServer.loadingTemplate,
devContext: {},
},
Expand Down
23 changes: 15 additions & 8 deletions src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,21 @@ export default defineCommand({
),
)

const envExists = ctx.args.dotenv
? existsSync(resolve(cwd, ctx.args.dotenv))
: existsSync(cwd)
if (envExists) {
consola.info(
'Loading `.env`. This will not be loaded when running the server in production.',
)
await setupDotenv({ cwd, fileName: ctx.args.dotenv })
if (ctx.args.dotenv !== undefined) {
const fileName = ctx.args.dotenv || '.env'

const envExists = existsSync(resolve(cwd, fileName))

if (envExists) {
consola.info(
`Loading \`${fileName}\`. This will not be loaded when running the server in production.`,
)

await setupDotenv({ cwd, fileName })
} else {
consola.error(`Cannot find \`${fileName}\`.`)
process.exit(1)
}
}

consola.info(`Starting preview command: \`${nitroJSON.commands.preview}\``)
Expand Down
8 changes: 6 additions & 2 deletions src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface NuxtDevContext {
export interface NuxtDevServerOptions {
cwd: string
logLevel: 'silent' | 'info' | 'verbose'
dotenv: boolean
dotenv: string
clear: boolean
overrides: NuxtConfig
port?: string | number
Expand Down Expand Up @@ -173,6 +173,10 @@ class NuxtDevServer extends EventEmitter {
const kit = await loadKit(this.options.cwd)
this._currentNuxt = await kit.loadNuxt({
cwd: this.options.cwd,
dotenv: {
cwd: this.options.cwd,
fileName: this.options.dotenv,
},
dev: true,
ready: false,
overrides: {
Expand Down Expand Up @@ -311,7 +315,7 @@ class NuxtDevServer extends EventEmitter {
)
configWatcher.on('all', (_event, _file) => {
const file = relative(this.options.cwd, _file)
if (file === (this.options.dotenv || '.env')) {
if (file === this.options.dotenv) {
this.emit('restart')
}
if (RESTART_RE.test(file)) {
Expand Down
Loading