Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

build-and-render.mjs

101 lines2.6 KB

1/**
2 * Helper subprocess for the hydration tests. Runs in plain node (vite/esbuild
3 * cannot run inside the jsdom test worker): builds the fixture app twice
4 * (server + hydratable client bundles), executes the SSR entry, and prints a
5 * JSON report on stdout.
6 *
7 * Usage: node build-and-render.mjs <outDir>
8 */
9import { execFileSync } from 'node:child_process'
10import * as path from 'node:path'
11import { fileURLToPath } from 'node:url'
12import { build } from 'vite'
13import solidPlugin from '@solidjs/vite-plugin'
14​
15const fixtureDir = fileURLToPath(new URL('.', import.meta.url))
16const packageRoot = path.join(fixtureDir, '..', '..', '..', '..')
17const outDir = process.argv[2]
18if (!outDir) {
19 throw new Error('usage: node build-and-render.mjs <outDir>')
21​
22const alias = {
23 '@tanstack/solid-query': path.join(packageRoot, 'src', 'index.ts'),
24 '@tanstack/query-core': path.join(
25 packageRoot,
26 '..',
27 'query-core',
28 'src',
29 'index.ts',
30 ),
31 'solid-js/web': '@solidjs/web',
33​
34// Server bundles: everything inlined so module resolution inside the temp
35// output dir is a non-issue.
36for (const entry of [
37 'entry-server',
38 'entry-server-stream',
39 'entry-server-disabled',
40]) {
41 await build({
42 configFile: false,
43 logLevel: 'error',
44 plugins: [solidPlugin({ ssr: true })],
45 resolve: { alias },
46 ssr: { noExternal: true },
47 build: {
48 ssr: path.join(fixtureDir, `${entry}.tsx`),
49 outDir,
50 emptyOutDir: false,
51 minify: false,
52 target: 'node18',
53 rollupOptions: {
54 output: { entryFileNames: `${entry}.mjs` },
55 },
56 },
57 })
59​
60// Client bundle: hydratable DOM output as a single self-contained ES module.
61await build({
62 configFile: false,
63 logLevel: 'error',
64 plugins: [solidPlugin({ ssr: true })],
65 resolve: { alias },
66 build: {
67 outDir,
68 emptyOutDir: false,
69 minify: false,
70 target: 'esnext',
71 lib: {
72 entry: path.join(fixtureDir, 'entry-client.tsx'),
73 formats: ['es'],
74 fileName: () => 'entry-client.mjs',
75 },
76 },
77})
78​
79const report = execFileSync(
80 process.execPath,
81 [path.join(outDir, 'entry-server.mjs')],
82 { encoding: 'utf-8' },
84const streamReport = execFileSync(
85 process.execPath,
86 [path.join(outDir, 'entry-server-stream.mjs')],
87 { encoding: 'utf-8' },
89const disabledReport = execFileSync(
90 process.execPath,
91 [path.join(outDir, 'entry-server-disabled.mjs')],
92 { encoding: 'utf-8' },
94​
95// Sanity-check all three parse before handing them to the test.
96const combined = JSON.stringify({
97 string: JSON.parse(report),
98 stream: JSON.parse(streamReport),
99 disabled: JSON.parse(disabledReport),
100})
101process.stdout.write(combined)