42 行
1.7 KiB
TypeScript
42 行
1.7 KiB
TypeScript
import { execFileSync } from 'node:child_process'
|
|
import { resolveConfig, transformWithEsbuild } from 'vite'
|
|
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import viteConfig from './vite.config'
|
|
|
|
vi.hoisted (() => {
|
|
vi.stubGlobal ('Uint8Array', Object.getPrototypeOf (new TextEncoder ().encode ('')).constructor)
|
|
})
|
|
afterAll (() => vi.unstubAllGlobals ())
|
|
afterEach (() => vi.unstubAllEnvs ())
|
|
|
|
describe ('JSX runtime configuration', () => {
|
|
it.each (['development', 'test', 'production']) (
|
|
'builds JSX compatible with production React under NODE_ENV=%s',
|
|
async environment => {
|
|
vi.stubEnv ('NODE_ENV', environment)
|
|
const config = await resolveConfig ({
|
|
...viteConfig ({ command: 'build', mode: 'production' }),
|
|
configFile: false, build: { ssr: true } }, 'build')
|
|
if (config.esbuild === false) throw new Error ('JSX transformation disabled')
|
|
const { code } = await transformWithEsbuild (
|
|
'export default <div>SSR</div>', 'runtime.tsx', {
|
|
jsx: config.esbuild.jsx,
|
|
jsxDev: config.esbuild.jsxDev,
|
|
jsxImportSource: config.esbuild.jsxImportSource })
|
|
expect (code).toContain ('react/jsx-runtime')
|
|
expect (code).not.toContain ('jsxDEV')
|
|
execFileSync (process.execPath, ['--input-type=module', '-e', code], {
|
|
cwd: import.meta.dirname,
|
|
env: { ...process.env, NODE_ENV: 'production' } })
|
|
})
|
|
|
|
it ('preserves development JSX for the development server', async () => {
|
|
vi.stubEnv ('NODE_ENV', 'development')
|
|
const config = await resolveConfig ({
|
|
...viteConfig ({ command: 'serve', mode: 'development' }),
|
|
configFile: false }, 'serve')
|
|
expect (config.esbuild && config.esbuild.jsxDev).toBe (true)
|
|
})
|
|
})
|