|
| 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 | +} |
0 commit comments