Skip to content

Commit 8148af7

Browse files
authoredNov 22, 2022
fix!: make NODE_ENV more predictable (#10996)
1 parent 3fa96f6 commit 8148af7

File tree

17 files changed

+39
-35
lines changed

17 files changed

+39
-35
lines changed
 

‎packages/vite/src/node/build.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,12 @@ export async function build(
452452
async function doBuild(
453453
inlineConfig: InlineConfig = {}
454454
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
455-
const config = await resolveConfig(inlineConfig, 'build', 'production')
455+
const config = await resolveConfig(
456+
inlineConfig,
457+
'build',
458+
'production',
459+
'production'
460+
)
456461
const options = config.build
457462
const ssr = !!options.ssr
458463
const libOptions = options.lib

‎packages/vite/src/node/cli.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ cli
242242
configFile: options.config,
243243
logLevel: options.logLevel
244244
},
245-
'build',
246-
'development'
245+
'build'
247246
)
248247
await optimizeDeps(config, options.force, true)
249248
} catch (e) {

‎packages/vite/src/node/config.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,17 @@ export type ResolveFn = (
372372
export async function resolveConfig(
373373
inlineConfig: InlineConfig,
374374
command: 'build' | 'serve',
375-
defaultMode = 'development'
375+
defaultMode = 'development',
376+
defaultNodeEnv = 'development'
376377
): Promise<ResolvedConfig> {
377378
let config = inlineConfig
378379
let configFileDependencies: string[] = []
379380
let mode = inlineConfig.mode || defaultMode
380381

381382
// some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
382-
// production-specific behavior, so set it here even though we haven't
383-
// resolve the final mode yet
384-
if (mode === 'production') {
385-
process.env.NODE_ENV = 'production'
386-
}
387-
// production env would not work in serve, fallback to development
388-
if (command === 'serve' && process.env.NODE_ENV === 'production') {
389-
process.env.NODE_ENV = 'development'
383+
// production-specific behavior, so set it early on
384+
if (!process.env.NODE_ENV) {
385+
process.env.NODE_ENV = defaultNodeEnv
390386
}
391387

392388
const configEnv = {

‎packages/vite/src/node/preview.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export type PreviewServerHook = (
7676
export async function preview(
7777
inlineConfig: InlineConfig = {}
7878
): Promise<PreviewServer> {
79-
const config = await resolveConfig(inlineConfig, 'serve', 'production')
79+
const config = await resolveConfig(
80+
inlineConfig,
81+
'serve',
82+
'production',
83+
'production'
84+
)
8085

8186
const app = connect() as Connect.Server
8287
const httpServer = await resolveHttpServer(

‎packages/vite/src/node/server/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export interface ResolvedServerUrls {
310310
export async function createServer(
311311
inlineConfig: InlineConfig = {}
312312
): Promise<ViteDevServer> {
313-
const config = await resolveConfig(inlineConfig, 'serve', 'development')
313+
const config = await resolveConfig(inlineConfig, 'serve')
314314
const { root, server: serverConfig } = config
315315
const httpsOptions = await resolveHttpsConfig(config.server.https)
316316
const { middlewareMode } = serverConfig

‎playground/json/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('node:fs')
33
const path = require('node:path')
44
const express = require('express')
55

6-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
6+
const isTest = process.env.VITEST
77

88
async function createServer(
99
root = process.cwd(),

‎playground/legacy/__tests__/client-and-ssr/serve.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,8 @@ export const port = ports['legacy/client-and-ssr']
88
export async function serve(): Promise<{ close(): Promise<void> }> {
99
const { build } = await import('vite')
1010

11-
// In a CLI app it is possible that you may run `build` several times one after another
12-
// For example, you may want to override an option specifically for the SSR build
13-
// And you may have a CLI app built for that purpose to make a more concise API
14-
// An unexpected behaviour is for the plugin-legacy to override the process.env.NODE_ENV value
15-
// And any build after the first client build that called plugin-legacy will misbehave and
16-
// build with process.env.NODE_ENV=production, rather than your CLI's env: NODE_ENV=myWhateverEnv my-cli-app build
17-
// The issue is with plugin-legacy's index.ts file not explicitly passing mode: process.env.NODE_ENV to vite's build function
18-
// This causes vite to call resolveConfig with defaultMode = 'production' and mutate process.env.NODE_ENV to 'production'
19-
2011
await build({
21-
mode: process.env.NODE_ENV,
12+
mode: 'test',
2213
root: rootDir,
2314
logLevel: 'silent',
2415
build: {
@@ -28,7 +19,7 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
2819
})
2920

3021
await build({
31-
mode: process.env.NODE_ENV,
22+
mode: 'test',
3223
root: rootDir,
3324
logLevel: 'silent',
3425
build: {

‎playground/optimize-missing-deps/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('node:fs')
33
const path = require('node:path')
44
const express = require('express')
55

6-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
6+
const isTest = process.env.VITEST
77

88
async function createServer(root = process.cwd(), hmrPort) {
99
const resolve = (p) => path.resolve(__dirname, p)

‎playground/ssr-deps/__tests__/ssr-deps.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ test('msg from external using external entry', async () => {
102102

103103
test('msg from linked no external', async () => {
104104
await page.goto(url)
105-
expect(await page.textContent('.linked-no-external')).toMatch('Hello World!')
105+
expect(await page.textContent('.linked-no-external')).toMatch(
106+
`Hello World from ${process.env.NODE_ENV}!`
107+
)
106108
})
107109

108110
test('msg from linked no external', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const hello = function () {
22
// make sure linked package is not externalized so Vite features like
33
// import.meta.env works (or handling TS files)
4-
return import.meta.env.DEV && 'Hello World!'
4+
return `Hello World from ${
5+
import.meta.env.DEV ? 'development' : 'production'
6+
}!`
57
}

‎playground/ssr-deps/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import express from 'express'
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url))
88

9-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
9+
const isTest = process.env.VITEST
1010

1111
export async function createServer(root = process.cwd(), hmrPort) {
1212
const resolve = (p) => path.resolve(__dirname, p)

‎playground/ssr-deps/src/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function render(url, rootDir) {
7979
html += `\n<p class="external-using-external-entry">message from external-using-external-entry: ${externalUsingExternalEntryMessage}</p>`
8080

8181
const linkedNoExternalMessage = linkedNoExternal()
82-
html += `\n<p class="linked-no-external">message from linked-no-external: ${linkedNoExternalMessage}</p>`
82+
html += `\n<p class="linked-no-external">linked-no-external msg: ${linkedNoExternalMessage}</p>`
8383

8484
html += `\n<p class="dep-virtual">message from dep-virtual: ${virtualMessage}</p>`
8585

‎playground/ssr-html/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'
44
import express from 'express'
55

66
const __dirname = path.dirname(fileURLToPath(import.meta.url))
7-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
7+
const isTest = process.env.VITEST
88

99
const DYNAMIC_SCRIPTS = `
1010
<script type="module">

‎playground/ssr-pug/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import express from 'express'
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url))
88

9-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
9+
const isTest = process.env.VITEST
1010

1111
const DYNAMIC_SCRIPTS = `
1212
<script type="module">

‎playground/ssr-react/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import express from 'express'
55

66
const __dirname = path.dirname(fileURLToPath(import.meta.url))
77

8-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
8+
const isTest = process.env.VITEST
99

1010
process.env.MY_CUSTOM_SECRET = 'API_KEY_qwertyuiop'
1111

‎playground/ssr-vue/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import express from 'express'
66

7-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
7+
const isTest = process.env.VITEST
88

99
export async function createServer(
1010
root = process.cwd(),

‎playground/vitestGlobalSetup.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')
99
let browserServer: BrowserServer | undefined
1010

1111
export async function setup(): Promise<void> {
12+
process.env.NODE_ENV = process.env.VITE_TEST_BUILD
13+
? 'production'
14+
: 'development'
15+
1216
browserServer = await chromium.launchServer({
1317
headless: !process.env.VITE_DEBUG_SERVE,
1418
args: process.env.CI

0 commit comments

Comments
 (0)
Please sign in to comment.