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!: make NODE_ENV more predictable #10996

Merged
merged 1 commit into from
Nov 22, 2022
Merged
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
7 changes: 6 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,12 @@ export async function build(
async function doBuild(
inlineConfig: InlineConfig = {}
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
const config = await resolveConfig(inlineConfig, 'build', 'production')
const config = await resolveConfig(
inlineConfig,
'build',
'production',
'production'
)
const options = config.build
const ssr = !!options.ssr
const libOptions = options.lib
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ cli
configFile: options.config,
logLevel: options.logLevel
},
'build',
'development'
'build'
)
await optimizeDeps(config, options.force, true)
} catch (e) {
Expand Down
14 changes: 5 additions & 9 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,21 +372,17 @@ export type ResolveFn = (
export async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
defaultMode = 'development'
defaultMode = 'development',
defaultNodeEnv = 'development'
): Promise<ResolvedConfig> {
let config = inlineConfig
let configFileDependencies: string[] = []
let mode = inlineConfig.mode || defaultMode

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

const configEnv = {
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ export type PreviewServerHook = (
export async function preview(
inlineConfig: InlineConfig = {}
): Promise<PreviewServer> {
const config = await resolveConfig(inlineConfig, 'serve', 'production')
const config = await resolveConfig(
inlineConfig,
'serve',
'production',
'production'
)

const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export interface ResolvedServerUrls {
export async function createServer(
inlineConfig: InlineConfig = {}
): Promise<ViteDevServer> {
const config = await resolveConfig(inlineConfig, 'serve', 'development')
const config = await resolveConfig(inlineConfig, 'serve')
const { root, server: serverConfig } = config
const httpsOptions = await resolveHttpsConfig(config.server.https)
const { middlewareMode } = serverConfig
Expand Down
2 changes: 1 addition & 1 deletion playground/json/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('node:fs')
const path = require('node:path')
const express = require('express')

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

async function createServer(
root = process.cwd(),
Expand Down
13 changes: 2 additions & 11 deletions playground/legacy/__tests__/client-and-ssr/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ export const port = ports['legacy/client-and-ssr']
export async function serve(): Promise<{ close(): Promise<void> }> {
const { build } = await import('vite')

// In a CLI app it is possible that you may run `build` several times one after another
// For example, you may want to override an option specifically for the SSR build
// And you may have a CLI app built for that purpose to make a more concise API
// An unexpected behaviour is for the plugin-legacy to override the process.env.NODE_ENV value
// And any build after the first client build that called plugin-legacy will misbehave and
// build with process.env.NODE_ENV=production, rather than your CLI's env: NODE_ENV=myWhateverEnv my-cli-app build
// The issue is with plugin-legacy's index.ts file not explicitly passing mode: process.env.NODE_ENV to vite's build function
// This causes vite to call resolveConfig with defaultMode = 'production' and mutate process.env.NODE_ENV to 'production'

await build({
mode: process.env.NODE_ENV,
mode: 'test',
root: rootDir,
logLevel: 'silent',
build: {
Expand All @@ -28,7 +19,7 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
})

await build({
mode: process.env.NODE_ENV,
mode: 'test',
root: rootDir,
logLevel: 'silent',
build: {
Expand Down
2 changes: 1 addition & 1 deletion playground/optimize-missing-deps/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('node:fs')
const path = require('node:path')
const express = require('express')

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

async function createServer(root = process.cwd(), hmrPort) {
const resolve = (p) => path.resolve(__dirname, p)
Expand Down
4 changes: 3 additions & 1 deletion playground/ssr-deps/__tests__/ssr-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ test('msg from external using external entry', async () => {

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

test('msg from linked no external', async () => {
Expand Down
4 changes: 3 additions & 1 deletion playground/ssr-deps/linked-no-external/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const hello = function () {
// make sure linked package is not externalized so Vite features like
// import.meta.env works (or handling TS files)
return import.meta.env.DEV && 'Hello World!'
return `Hello World from ${
import.meta.env.DEV ? 'development' : 'production'
}!`
}
2 changes: 1 addition & 1 deletion playground/ssr-deps/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import express from 'express'

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

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

export async function createServer(root = process.cwd(), hmrPort) {
const resolve = (p) => path.resolve(__dirname, p)
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-deps/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function render(url, rootDir) {
html += `\n<p class="external-using-external-entry">message from external-using-external-entry: ${externalUsingExternalEntryMessage}</p>`

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

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

Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-html/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'
import express from 'express'

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

const DYNAMIC_SCRIPTS = `
<script type="module">
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-pug/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import express from 'express'

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

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

const DYNAMIC_SCRIPTS = `
<script type="module">
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import express from 'express'

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

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

process.env.MY_CUSTOM_SECRET = 'API_KEY_qwertyuiop'

Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-vue/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'
import express from 'express'

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isTest = process.env.VITEST

export async function createServer(
root = process.cwd(),
Expand Down
4 changes: 4 additions & 0 deletions playground/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')
let browserServer: BrowserServer | undefined

export async function setup(): Promise<void> {
process.env.NODE_ENV = process.env.VITE_TEST_BUILD
? 'production'
: 'development'

browserServer = await chromium.launchServer({
headless: !process.env.VITE_DEBUG_SERVE,
args: process.env.CI
Expand Down