|
| 1 | +import { candidate, css, fetchStyles, json, retryAssertion, test, ts, txt } from '../utils' |
| 2 | + |
| 3 | +const WORKSPACE = { |
| 4 | + 'package.json': json` |
| 5 | + { |
| 6 | + "type": "module", |
| 7 | + "dependencies": { |
| 8 | + "@react-router/dev": "^7", |
| 9 | + "@react-router/node": "^7", |
| 10 | + "@react-router/serve": "^7", |
| 11 | + "@tailwindcss/vite": "workspace:^", |
| 12 | + "@types/node": "^20", |
| 13 | + "@types/react-dom": "^19", |
| 14 | + "@types/react": "^19", |
| 15 | + "isbot": "^5", |
| 16 | + "react-dom": "^19", |
| 17 | + "react-router": "^7", |
| 18 | + "react": "^19", |
| 19 | + "tailwindcss": "workspace:^", |
| 20 | + "vite": "^5" |
| 21 | + } |
| 22 | + } |
| 23 | + `, |
| 24 | + 'react-router.config.ts': ts` |
| 25 | + import type { Config } from '@react-router/dev/config' |
| 26 | + export default { ssr: true } satisfies Config |
| 27 | + `, |
| 28 | + 'vite.config.ts': ts` |
| 29 | + import { defineConfig } from 'vite' |
| 30 | + import { reactRouter } from '@react-router/dev/vite' |
| 31 | + import tailwindcss from '@tailwindcss/vite' |
| 32 | +
|
| 33 | + export default defineConfig({ |
| 34 | + plugins: [tailwindcss(), reactRouter()], |
| 35 | + }) |
| 36 | + `, |
| 37 | + 'app/routes/home.tsx': ts` |
| 38 | + export default function Home() { |
| 39 | + return <h1 className="font-bold">Welcome to React Router</h1> |
| 40 | + } |
| 41 | + `, |
| 42 | + 'app/app.css': css`@import 'tailwindcss';`, |
| 43 | + 'app/routes.ts': ts` |
| 44 | + import { type RouteConfig, index } from '@react-router/dev/routes' |
| 45 | + export default [index('routes/home.tsx')] satisfies RouteConfig |
| 46 | + `, |
| 47 | + 'app/root.tsx': ts` |
| 48 | + import { Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router' |
| 49 | + import './app.css' |
| 50 | + export function Layout({ children }: { children: React.ReactNode }) { |
| 51 | + return ( |
| 52 | + <html lang="en"> |
| 53 | + <head> |
| 54 | + <meta charSet="utf-8" /> |
| 55 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 56 | + <Meta /> |
| 57 | + <Links /> |
| 58 | + </head> |
| 59 | + <body> |
| 60 | + {children} |
| 61 | + <ScrollRestoration /> |
| 62 | + <Scripts /> |
| 63 | + </body> |
| 64 | + </html> |
| 65 | + ) |
| 66 | + } |
| 67 | +
|
| 68 | + export default function App() { |
| 69 | + return <Outlet /> |
| 70 | + } |
| 71 | + `, |
| 72 | +} |
| 73 | + |
| 74 | +test('dev mode', { fs: WORKSPACE }, async ({ fs, spawn, expect }) => { |
| 75 | + let process = await spawn('pnpm react-router dev') |
| 76 | + |
| 77 | + let url = '' |
| 78 | + await process.onStdout((m) => { |
| 79 | + let match = /Local:\s*(http.*)\//.exec(m) |
| 80 | + if (match) url = match[1] |
| 81 | + return Boolean(url) |
| 82 | + }) |
| 83 | + |
| 84 | + await retryAssertion(async () => { |
| 85 | + let css = await fetchStyles(url) |
| 86 | + expect(css).toContain(candidate`font-bold`) |
| 87 | + }) |
| 88 | + |
| 89 | + await retryAssertion(async () => { |
| 90 | + await fs.write( |
| 91 | + 'app/routes/home.tsx', |
| 92 | + ts` |
| 93 | + export default function Home() { |
| 94 | + return <h1 className="font-bold underline">Welcome to React Router</h1> |
| 95 | + } |
| 96 | + `, |
| 97 | + ) |
| 98 | + |
| 99 | + let css = await fetchStyles(url) |
| 100 | + expect(css).toContain(candidate`underline`) |
| 101 | + expect(css).toContain(candidate`font-bold`) |
| 102 | + }) |
| 103 | +}) |
| 104 | + |
| 105 | +test('build mode', { fs: WORKSPACE }, async ({ spawn, exec, expect }) => { |
| 106 | + await exec('pnpm react-router build') |
| 107 | + let process = await spawn('pnpm react-router-serve ./build/server/index.js') |
| 108 | + |
| 109 | + let url = '' |
| 110 | + await process.onStdout((m) => { |
| 111 | + let match = /\[react-router-serve\]\s*(http.*)\ \/?/.exec(m) |
| 112 | + if (match) url = match[1] |
| 113 | + return url != '' |
| 114 | + }) |
| 115 | + |
| 116 | + await retryAssertion(async () => { |
| 117 | + let css = await fetchStyles(url) |
| 118 | + expect(css).toContain(candidate`font-bold`) |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +test( |
| 123 | + 'build mode using ?url stylesheet imports should only build one stylesheet (requires `file-system` scanner)', |
| 124 | + { |
| 125 | + fs: { |
| 126 | + ...WORKSPACE, |
| 127 | + 'app/root.tsx': ts` |
| 128 | + import { Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router' |
| 129 | + import styles from './app.css?url' |
| 130 | + export const links: Route.LinksFunction = () => [{ rel: 'stylesheet', href: styles }] |
| 131 | + export function Layout({ children }: { children: React.ReactNode }) { |
| 132 | + return ( |
| 133 | + <html lang="en"> |
| 134 | + <head> |
| 135 | + <meta charSet="utf-8" /> |
| 136 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 137 | + <Meta /> |
| 138 | + <Links /> |
| 139 | + </head> |
| 140 | + <body class="dark"> |
| 141 | + {children} |
| 142 | + <ScrollRestoration /> |
| 143 | + <Scripts /> |
| 144 | + </body> |
| 145 | + </html> |
| 146 | + ) |
| 147 | + } |
| 148 | +
|
| 149 | + export default function App() { |
| 150 | + return <Outlet /> |
| 151 | + } |
| 152 | + `, |
| 153 | + 'vite.config.ts': ts` |
| 154 | + import { defineConfig } from 'vite' |
| 155 | + import { reactRouter } from '@react-router/dev/vite' |
| 156 | + import tailwindcss from '@tailwindcss/vite' |
| 157 | +
|
| 158 | + export default defineConfig({ |
| 159 | + plugins: [tailwindcss(), reactRouter()], |
| 160 | + }) |
| 161 | + `, |
| 162 | + '.gitignore': txt` |
| 163 | + node_modules/ |
| 164 | + build/ |
| 165 | + `, |
| 166 | + }, |
| 167 | + }, |
| 168 | + async ({ fs, exec, expect }) => { |
| 169 | + await exec('pnpm react-router build') |
| 170 | + |
| 171 | + let files = await fs.glob('build/client/assets/**/*.css') |
| 172 | + |
| 173 | + expect(files).toHaveLength(1) |
| 174 | + let [filename] = files[0] |
| 175 | + |
| 176 | + await fs.expectFileToContain(filename, [candidate`font-bold`]) |
| 177 | + }, |
| 178 | +) |
0 commit comments