Skip to content

Commit fdc3212

Browse files
authored
fix(plugin-legacy): fix regression introduced in #4536 (#4861)
1 parent 74af8c4 commit fdc3212

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isBuild } from '../../../testUtils'
2+
import { port } from './serve'
3+
4+
const url = `http://localhost:${port}`
5+
6+
if (isBuild) {
7+
test('should work', async () => {
8+
await page.goto(url)
9+
expect(await page.textContent('#app')).toMatch('Hello')
10+
})
11+
12+
test('import.meta.env.LEGACY', async () => {
13+
// SSR build is always modern
14+
expect(await page.textContent('#env')).toMatch('false')
15+
})
16+
} else {
17+
// this test doesn't support serve mode
18+
// must contain at least one test
19+
test('should work', () => void 0)
20+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @ts-check
2+
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace
3+
// the default e2e test serve behavior
4+
const path = require('path')
5+
6+
const port = (exports.port = 9527)
7+
8+
/**
9+
* @param {string} root
10+
* @param {boolean} _isProd
11+
*/
12+
exports.serve = async function serve(root, _isProd) {
13+
const { build } = require('vite')
14+
await build({
15+
root,
16+
logLevel: 'silent',
17+
build: {
18+
target: 'esnext',
19+
ssr: 'entry-server.js',
20+
outDir: 'dist/server'
21+
}
22+
})
23+
24+
const express = require('express')
25+
const app = express()
26+
27+
app.use('/', async (_req, res) => {
28+
const { render } = require(path.resolve(
29+
root,
30+
'./dist/server/entry-server.js'
31+
))
32+
const html = await render()
33+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
34+
})
35+
36+
return new Promise((resolve, reject) => {
37+
try {
38+
const server = app.listen(port, () => {
39+
resolve({
40+
// for test teardown
41+
async close() {
42+
await new Promise((resolve) => {
43+
server.close(resolve)
44+
})
45+
}
46+
})
47+
})
48+
} catch (e) {
49+
reject(e)
50+
}
51+
})
52+
}
Lines changed: 7 additions & 0 deletions
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="env">${import.meta.env.LEGACY}</div>
6+
`
7+
}

packages/plugin-legacy/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,14 @@ function viteLegacyPlugin(options = {}) {
451451
const legacyEnvPlugin = {
452452
name: 'legacy-env',
453453

454-
config(_, env) {
454+
config(config, env) {
455455
if (env) {
456456
return {
457457
define: {
458458
'import.meta.env.LEGACY':
459-
env.command === 'serve' ? false : legacyEnvVarMarker
459+
env.command === 'serve' || config.build.ssr
460+
? false
461+
: legacyEnvVarMarker
460462
}
461463
}
462464
} else {

0 commit comments

Comments
 (0)