diff --git a/packages/playground/legacy/__tests__/ssr/legacy-ssr.spec.ts b/packages/playground/legacy/__tests__/ssr/legacy-ssr.spec.ts new file mode 100644 index 00000000000000..dad9b94d83509e --- /dev/null +++ b/packages/playground/legacy/__tests__/ssr/legacy-ssr.spec.ts @@ -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) +} diff --git a/packages/playground/legacy/__tests__/ssr/serve.js b/packages/playground/legacy/__tests__/ssr/serve.js new file mode 100644 index 00000000000000..df43f180afb188 --- /dev/null +++ b/packages/playground/legacy/__tests__/ssr/serve.js @@ -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) + } + }) +} diff --git a/packages/playground/legacy/entry-server.js b/packages/playground/legacy/entry-server.js new file mode 100644 index 00000000000000..be475a54287bfc --- /dev/null +++ b/packages/playground/legacy/entry-server.js @@ -0,0 +1,7 @@ +// This counts as 'server-side' rendering, yes? +export async function render() { + return /* html */ ` +