Skip to content

Commit a8279af

Browse files
authored
fix(plugin-legacy): prevent global process.env.NODE_ENV mutation (#9741)
1 parent ba62be4 commit a8279af

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

packages/plugin-legacy/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
205205
modernPolyfills
206206
)
207207
await buildPolyfillChunk(
208+
config.mode,
208209
modernPolyfills,
209210
bundle,
210211
facadeToModernPolyfillMap,
@@ -237,6 +238,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
237238
)
238239

239240
await buildPolyfillChunk(
241+
config.mode,
240242
legacyPolyfills,
241243
bundle,
242244
facadeToLegacyPolyfillMap,
@@ -615,6 +617,7 @@ function createBabelPresetEnvOptions(
615617
}
616618

617619
async function buildPolyfillChunk(
620+
mode: string,
618621
imports: Set<string>,
619622
bundle: OutputBundle,
620623
facadeToChunkMap: Map<string, string>,
@@ -626,6 +629,7 @@ async function buildPolyfillChunk(
626629
let { minify, assetsDir } = buildOptions
627630
minify = minify ? 'terser' : false
628631
const res = await build({
632+
mode,
629633
// so that everything is resolved from here
630634
root: path.dirname(fileURLToPath(import.meta.url)),
631635
configFile: false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { port } from './serve'
3+
import { isBuild, page } from '~utils'
4+
5+
const url = `http://localhost:${port}`
6+
7+
describe.runIf(isBuild)('client-legacy-ssr-sequential-builds', () => {
8+
test('should work', async () => {
9+
await page.goto(url)
10+
expect(await page.textContent('#app')).toMatch('Hello')
11+
})
12+
13+
test('import.meta.env.MODE', async () => {
14+
// SSR build is always modern
15+
expect(await page.textContent('#mode')).toMatch('test')
16+
})
17+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
2+
// the default e2e test serve behavior
3+
import path from 'node:path'
4+
import { ports, rootDir } from '~utils'
5+
6+
export const port = ports['legacy/client-and-ssr']
7+
8+
export async function serve(): Promise<{ close(): Promise<void> }> {
9+
const { build } = await import('vite')
10+
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+
20+
await build({
21+
mode: process.env.NODE_ENV,
22+
root: rootDir,
23+
logLevel: 'silent',
24+
build: {
25+
target: 'esnext',
26+
outDir: 'dist/client'
27+
}
28+
})
29+
30+
await build({
31+
mode: process.env.NODE_ENV,
32+
root: rootDir,
33+
logLevel: 'silent',
34+
build: {
35+
target: 'esnext',
36+
ssr: 'entry-server-sequential.js',
37+
outDir: 'dist/server'
38+
}
39+
})
40+
41+
const { default: express } = await import('express')
42+
const app = express()
43+
44+
app.use('/', async (_req, res) => {
45+
const { render } = await import(
46+
path.resolve(rootDir, './dist/server/entry-server-sequential.mjs')
47+
)
48+
const html = await render()
49+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
50+
})
51+
52+
return new Promise((resolve, reject) => {
53+
try {
54+
const server = app.listen(port, () => {
55+
resolve({
56+
// for test teardown
57+
async close() {
58+
await new Promise((resolve) => {
59+
server.close(resolve)
60+
})
61+
}
62+
})
63+
})
64+
} catch (e) {
65+
reject(e)
66+
}
67+
})
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This counts as 'server-side' rendering, yes?
2+
export async function render() {
3+
return /* html */ `
4+
<div id="app">Hello</div>
5+
<div id="mode">${import.meta.env.MODE}</div>
6+
`
7+
}

playground/test-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const ports = {
2121
'legacy/ssr': 9520,
2222
lib: 9521,
2323
'optimize-missing-deps': 9522,
24+
'legacy/client-and-ssr': 9523,
2425
'ssr-deps': 9600,
2526
'ssr-html': 9601,
2627
'ssr-pug': 9602,

0 commit comments

Comments
 (0)