|
| 1 | +// The deployment contract (README, "Deployment contract"), as a stand-in |
| 2 | +// provider relies on it: it moves both outputs, builds the server first |
| 3 | +// from a `post` buildApp hook, and serves the `ssr` environment's `index` |
| 4 | +// entry from a separate Node process, as a host would. The dev half (point |
| 5 | +// 6) runs in examples/start-ssr's `external` and `detect` modes. |
| 6 | +import { spawnSync } from 'node:child_process'; |
| 7 | +import { existsSync, readFileSync } from 'node:fs'; |
| 8 | +import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises'; |
| 9 | +import { tmpdir } from 'node:os'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 12 | +import { createBuilder, type Plugin } from 'vite'; |
| 13 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 14 | +import solidPlugin from './index.js'; |
| 15 | + |
| 16 | +let root: string; |
| 17 | + |
| 18 | +beforeAll(async () => { |
| 19 | + root = await realpath(await mkdtemp(path.join(tmpdir(), 'solid-deployment-contract-'))); |
| 20 | + await symlink( |
| 21 | + fileURLToPath(new URL('../node_modules', import.meta.url)), |
| 22 | + path.join(root, 'node_modules'), |
| 23 | + 'dir', |
| 24 | + ); |
| 25 | + await mkdir(path.join(root, 'src')); |
| 26 | + await writeFile( |
| 27 | + path.join(root, 'src/App.tsx'), |
| 28 | + [ |
| 29 | + "import { getRequestEvent } from '@solidjs/web';", |
| 30 | + 'export default function App() {', |
| 31 | + ' const event = getRequestEvent() as { nativeEvent?: { marker?: string } } | undefined;', |
| 32 | + " return <h1>native event: {event?.nativeEvent?.marker ?? 'none'}</h1>;", |
| 33 | + '}', |
| 34 | + ].join('\n'), |
| 35 | + ); |
| 36 | +}); |
| 37 | + |
| 38 | +afterAll(async () => { |
| 39 | + await rm(root, { recursive: true, force: true }); |
| 40 | +}); |
| 41 | + |
| 42 | +function provider(output: string): Plugin { |
| 43 | + return { |
| 44 | + name: 'stand-in-provider', |
| 45 | + configEnvironment(name) { |
| 46 | + if (name === 'client') return { build: { outDir: `${output}/public` } }; |
| 47 | + if (name === 'ssr') return { build: { outDir: `${output}/server` } }; |
| 48 | + }, |
| 49 | + buildApp: { |
| 50 | + order: 'post', |
| 51 | + async handler(builder) { |
| 52 | + // Server first, as orchestrators that build their workers first do. |
| 53 | + for (const name of ['ssr', 'client']) { |
| 54 | + const environment = builder.environments[name]; |
| 55 | + if (environment && !environment.isBuilt) await builder.build(environment); |
| 56 | + } |
| 57 | + }, |
| 58 | + }, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +async function buildApp(output: string, options: Parameters<typeof solidPlugin>[0]) { |
| 63 | + const builder = await createBuilder({ |
| 64 | + root, |
| 65 | + configFile: false, |
| 66 | + // Own optimizer cache: node_modules is a symlink to the repo's. |
| 67 | + cacheDir: path.join(root, '.vite'), |
| 68 | + logLevel: 'silent', |
| 69 | + plugins: [solidPlugin(options), provider(output)], |
| 70 | + }); |
| 71 | + await builder.buildApp(); |
| 72 | +} |
| 73 | + |
| 74 | +// The client entry a page loads, which must exist in the client output. |
| 75 | +function clientEntry(html: string, publicDir: string) { |
| 76 | + const src = html.match(/<script[^>]*type="?module"?[^>]*src="([^"]+)"/)?.[1]; |
| 77 | + return { src, exists: !!src && existsSync(path.join(publicDir, src)) }; |
| 78 | +} |
| 79 | + |
| 80 | +describe('deployment contract', () => { |
| 81 | + it('serves an SSR app through the index entry a provider builds', async () => { |
| 82 | + await buildApp('.ssr-output', { start: true, ssr: true }); |
| 83 | + const publicDir = path.join(root, '.ssr-output/public'); |
| 84 | + expect(existsSync(path.join(publicDir, '.vite/manifest.json'))).toBe(true); |
| 85 | + const serverEntry = path.join(root, '.ssr-output/server/server.js'); |
| 86 | + expect(existsSync(serverEntry)).toBe(true); |
| 87 | + |
| 88 | + const script = ` |
| 89 | + const app = await import(${JSON.stringify(pathToFileURL(serverEntry).href)}); |
| 90 | + const request = () => new Request('http://localhost/', { headers: { accept: 'text/html' } }); |
| 91 | + const page = await app.default.fetch(request()); |
| 92 | + const hosted = await app.handleRequest(request(), { |
| 93 | + event: { nativeEvent: { marker: 'from-host' } }, |
| 94 | + }); |
| 95 | + console.log(JSON.stringify({ |
| 96 | + status: page.status, |
| 97 | + type: page.headers.get('content-type'), |
| 98 | + page: await page.text(), |
| 99 | + hosted: await hosted.text(), |
| 100 | + })); |
| 101 | + `; |
| 102 | + const result = spawnSync(process.execPath, ['--input-type=module', '-e', script], { |
| 103 | + cwd: root, |
| 104 | + encoding: 'utf8', |
| 105 | + }); |
| 106 | + expect(result.stderr).toBe(''); |
| 107 | + const served = JSON.parse(result.stdout.trim().split('\n').pop()!); |
| 108 | + expect(served.status).toBe(200); |
| 109 | + expect(served.type).toMatch(/^text\/html/); |
| 110 | + expect(served.page).toMatch(/native event: (<!--[^>]*-->)*none/); |
| 111 | + expect(served.hosted).toMatch(/native event: (<!--[^>]*-->)*from-host/); |
| 112 | + // Built client-first, whatever order the provider asks for. |
| 113 | + expect(clientEntry(served.page, publicDir)).toEqual({ src: expect.any(String), exists: true }); |
| 114 | + }, 120_000); |
| 115 | + |
| 116 | + it('builds a client-mode app to a static site, keeping the service for the host', async () => { |
| 117 | + await buildApp('.client-output', { start: true }); |
| 118 | + const publicDir = path.join(root, '.client-output/public'); |
| 119 | + const html = readFileSync(path.join(publicDir, 'index.html'), 'utf-8'); |
| 120 | + expect(clientEntry(html, publicDir)).toEqual({ src: expect.any(String), exists: true }); |
| 121 | + expect(existsSync(path.join(root, '.client-output/server/server.js'))).toBe(true); |
| 122 | + }, 120_000); |
| 123 | +}); |
0 commit comments