From 7b6c59e18f3964a1e0d6cf62df339a0723de7138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 19 Nov 2024 13:39:40 +0900 Subject: [PATCH] test: run ssr-conditions build test (#18695) --- playground/ssr-conditions/__tests__/serve.ts | 31 ++++++++- .../__tests__/ssr-conditions.spec.ts | 33 ++++----- playground/ssr-conditions/package.json | 6 +- playground/ssr-conditions/server.js | 68 ++++++++++++------- pnpm-lock.yaml | 3 + 5 files changed, 97 insertions(+), 44 deletions(-) diff --git a/playground/ssr-conditions/__tests__/serve.ts b/playground/ssr-conditions/__tests__/serve.ts index 5e0546db2..ff8a5fd67 100644 --- a/playground/ssr-conditions/__tests__/serve.ts +++ b/playground/ssr-conditions/__tests__/serve.ts @@ -3,15 +3,42 @@ import path from 'node:path' import kill from 'kill-port' -import { hmrPorts, ports, rootDir } from '~utils' +import { hmrPorts, isBuild, ports, rootDir } from '~utils' export const port = ports['ssr-conditions'] export async function serve(): Promise<{ close(): Promise }> { + if (isBuild) { + // build first + const { build } = await import('vite') + // client build + await build({ + root: rootDir, + logLevel: 'silent', // exceptions are logged by Vitest + build: { + minify: false, + outDir: 'dist/client', + }, + }) + // server build + await build({ + root: rootDir, + logLevel: 'silent', + build: { + ssr: 'src/app.js', + outDir: 'dist/server', + }, + }) + } + await kill(port) const { createServer } = await import(path.resolve(rootDir, 'server.js')) - const { app, vite } = await createServer(rootDir, hmrPorts['ssr-conditions']) + const { app, vite } = await createServer( + rootDir, + isBuild, + hmrPorts['ssr-conditions'], + ) return new Promise((resolve, reject) => { try { diff --git a/playground/ssr-conditions/__tests__/ssr-conditions.spec.ts b/playground/ssr-conditions/__tests__/ssr-conditions.spec.ts index e7e861a73..078c6357e 100644 --- a/playground/ssr-conditions/__tests__/ssr-conditions.spec.ts +++ b/playground/ssr-conditions/__tests__/ssr-conditions.spec.ts @@ -11,24 +11,25 @@ test('ssr.resolve.conditions affect non-externalized imports during ssr', async ) }) -test('ssr.resolve.externalConditions affect externalized imports during ssr', async () => { - await page.goto(url) - expect(await page.textContent('.external-react-server')).toMatch('edge.js') -}) - +// externalConditions is only used for dev test.runIf(isServe)( - 'ssr.resolve settings do not affect non-ssr imports', + 'ssr.resolve.externalConditions affect externalized imports during ssr', async () => { await page.goto(url) - await withRetry(async () => { - expect( - await page.textContent('.browser-no-external-react-server'), - ).toMatch('default.js') - }) - await withRetry(async () => { - expect(await page.textContent('.browser-external-react-server')).toMatch( - 'default.js', - ) - }) + expect(await page.textContent('.external-react-server')).toMatch('edge.js') }, ) + +test('ssr.resolve settings do not affect non-ssr imports', async () => { + await page.goto(url) + await withRetry(async () => { + expect(await page.textContent('.browser-no-external-react-server')).toMatch( + 'default.js', + ) + }) + await withRetry(async () => { + expect(await page.textContent('.browser-external-react-server')).toMatch( + 'default.js', + ) + }) +}) diff --git a/playground/ssr-conditions/package.json b/playground/ssr-conditions/package.json index 97435afda..de08645b4 100644 --- a/playground/ssr-conditions/package.json +++ b/playground/ssr-conditions/package.json @@ -5,6 +5,9 @@ "type": "module", "scripts": { "dev": "node server", + "build": "npm run build:client && npm run build:server", + "build:client": "vite build --outDir dist/client", + "build:server": "vite build --ssr src/app.js --outDir dist/server", "serve": "NODE_ENV=production node server", "debug": "node --inspect-brk server" }, @@ -13,6 +16,7 @@ "@vitejs/test-ssr-conditions-no-external": "file:./no-external" }, "devDependencies": { - "express": "^5.0.1" + "express": "^5.0.1", + "sirv": "^3.0.0" } } diff --git a/playground/ssr-conditions/server.js b/playground/ssr-conditions/server.js index bccdbedba..00b0336d6 100644 --- a/playground/ssr-conditions/server.js +++ b/playground/ssr-conditions/server.js @@ -2,49 +2,67 @@ import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import express from 'express' +import sirv from 'sirv' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const isTest = process.env.VITEST -export async function createServer(root = process.cwd(), hmrPort) { +export async function createServer( + root = process.cwd(), + isProd = process.env.NODE_ENV === 'production', + hmrPort, +) { const resolve = (p) => path.resolve(__dirname, p) + const indexProd = isProd + ? fs.readFileSync(resolve('dist/client/index.html'), 'utf-8') + : '' + const app = express() /** * @type {import('vite').ViteDevServer} */ - const vite = await ( - await import('vite') - ).createServer({ - root, - logLevel: isTest ? 'error' : 'info', - server: { - middlewareMode: true, - watch: { - // During tests we edit the files too fast and sometimes chokidar - // misses change events, so enforce polling for consistency - usePolling: true, - interval: 100, + let vite + if (!isProd) { + vite = await ( + await import('vite') + ).createServer({ + root, + logLevel: isTest ? 'error' : 'info', + server: { + middlewareMode: true, + watch: { + // During tests we edit the files too fast and sometimes chokidar + // misses change events, so enforce polling for consistency + usePolling: true, + interval: 100, + }, + hmr: { + port: hmrPort, + }, }, - hmr: { - port: hmrPort, - }, - }, - appType: 'custom', - }) - - app.use(vite.middlewares) + appType: 'custom', + }) + app.use(vite.middlewares) + } else { + app.use(sirv(resolve('dist/client'), { extensions: [] })) + } app.use('*all', async (req, res) => { try { const url = req.originalUrl - let template - template = fs.readFileSync(resolve('index.html'), 'utf-8') - template = await vite.transformIndexHtml(url, template) - const render = (await vite.ssrLoadModule('/src/app.js')).render + let template, render + if (!isProd) { + template = fs.readFileSync(resolve('index.html'), 'utf-8') + template = await vite.transformIndexHtml(url, template) + render = (await vite.ssrLoadModule('/src/app.js')).render + } else { + template = indexProd + render = (await import('./dist/server/app.js')).render + } const appHtml = await render(url, __dirname) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 987b8b1e4..a5e1b8ce6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1347,6 +1347,9 @@ importers: express: specifier: ^5.0.1 version: 5.0.1 + sirv: + specifier: ^3.0.0 + version: 3.0.0(patch_hash=plxlsciwiebyhal5sm4vtpekka) playground/ssr-conditions/external: {}