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(plugin-legacy): fix regression introduced in #4536 #4861

Merged
merged 1 commit into from
Sep 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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