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

Commit

Permalink
fix(nuxi): always override NODE_ENV (#5417)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 12, 2022
1 parent 0f4fa56 commit a9c061c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { listen } from 'listhen'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { clearDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
Expand All @@ -14,7 +15,7 @@ export default defineNuxtCommand({
description: 'Build nuxt and analyze production bundle (experimental)'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
overrideEnv('production')

const rootDir = resolve(args._[0] || '.')
const statsDir = join(rootDir, '.nuxt/stats')
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxi/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import consola from 'consola'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { clearDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
Expand All @@ -12,7 +13,8 @@ export default defineNuxtCommand({
description: 'Build nuxt for production deployment'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
overrideEnv('production')

const rootDir = resolve(args._[0] || '.')

const { loadNuxt, buildNuxt } = await loadKit(rootDir)
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { showBanner } from '../utils/banner'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { importModule } from '../utils/cjs'
import { overrideEnv } from '../utils/env'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
Expand All @@ -17,7 +18,7 @@ export default defineNuxtCommand({
description: 'Run nuxt development server'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
overrideEnv('development')

const { listen } = await import('listhen')
let currentHandler
Expand Down
8 changes: 8 additions & 0 deletions packages/nuxi/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const overrideEnv = (targetEnv: string) => {
const currentEnv = process.env.NODE_ENV
if (currentEnv && currentEnv !== targetEnv) {
console.warn(`Changing \`NODE_ENV\` from \`${currentEnv}\` to \`${targetEnv}\`.`)
}

process.env.NODE_ENV = targetEnv
}

2 comments on commit a9c061c

@ennioVisco
Copy link
Contributor

@ennioVisco ennioVisco commented on a9c061c Jun 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I recommend a more informative message like:

console.warn(`Changing \`NODE_ENV\` from \`${currentEnv}\` to \`${targetEnv}\`, to avoid unexpected errors from Vite.`)?

@pi0
Copy link
Member

@pi0 pi0 commented on a9c061c Jun 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently because of Vite but I remembered tailwind had similar issues. I think it is safer if we permanently do this (and in the future allow a way to force set with an alternative env)

Please sign in to comment.