test: run ssr-conditions build test (#18695)

This commit is contained in:
翠 / green 2024-11-19 13:39:40 +09:00 committed by GitHub
parent 473424ee8d
commit 7b6c59e18f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 97 additions and 44 deletions

View File

@ -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<void> }> {
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 {

View File

@ -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)
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')
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',
)
})
},
)
})

View File

@ -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"
}
}

View File

@ -2,20 +2,31 @@ 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 (
let vite
if (!isProd) {
vite = await (
await import('vite')
).createServer({
root,
@ -34,17 +45,24 @@ export async function createServer(root = process.cwd(), hmrPort) {
},
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
let template, render
if (!isProd) {
template = fs.readFileSync(resolve('index.html'), 'utf-8')
template = await vite.transformIndexHtml(url, template)
const render = (await vite.ssrLoadModule('/src/app.js')).render
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)

View File

@ -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: {}