Skip to content

Commit

Permalink
fix(plugin-legacy): fix regression introduced in #4536 (#4861)
Browse files Browse the repository at this point in the history
  • Loading branch information
andylizi authored Sep 6, 2021
1 parent 74af8c4 commit fdc3212
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/playground/legacy/__tests__/ssr/legacy-ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isBuild } from '../../../testUtils'
import { port } from './serve'

const url = `http://localhost:${port}`

if (isBuild) {
test('should work', async () => {
await page.goto(url)
expect(await page.textContent('#app')).toMatch('Hello')
})

test('import.meta.env.LEGACY', async () => {
// SSR build is always modern
expect(await page.textContent('#env')).toMatch('false')
})
} else {
// this test doesn't support serve mode
// must contain at least one test
test('should work', () => void 0)
}
52 changes: 52 additions & 0 deletions packages/playground/legacy/__tests__/ssr/serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @ts-check
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace
// the default e2e test serve behavior
const path = require('path')

const port = (exports.port = 9527)

/**
* @param {string} root
* @param {boolean} _isProd
*/
exports.serve = async function serve(root, _isProd) {
const { build } = require('vite')
await build({
root,
logLevel: 'silent',
build: {
target: 'esnext',
ssr: 'entry-server.js',
outDir: 'dist/server'
}
})

const express = require('express')
const app = express()

app.use('/', async (_req, res) => {
const { render } = require(path.resolve(
root,
'./dist/server/entry-server.js'
))
const html = await render()
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
})

return new Promise((resolve, reject) => {
try {
const server = app.listen(port, () => {
resolve({
// for test teardown
async close() {
await new Promise((resolve) => {
server.close(resolve)
})
}
})
})
} catch (e) {
reject(e)
}
})
}
7 changes: 7 additions & 0 deletions packages/playground/legacy/entry-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This counts as 'server-side' rendering, yes?
export async function render() {
return /* html */ `
<div id="app">Hello</div>
<div id="env">${import.meta.env.LEGACY}</div>
`
}
6 changes: 4 additions & 2 deletions packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,14 @@ function viteLegacyPlugin(options = {}) {
const legacyEnvPlugin = {
name: 'legacy-env',

config(_, env) {
config(config, env) {
if (env) {
return {
define: {
'import.meta.env.LEGACY':
env.command === 'serve' ? false : legacyEnvVarMarker
env.command === 'serve' || config.build.ssr
? false
: legacyEnvVarMarker
}
}
} else {
Expand Down

0 comments on commit fdc3212

Please sign in to comment.