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

nest-hosted-assets.ts

22 lines1.2 KB

1// The hosted flavor (CLOUDFLARE_ENV=modelschemas) serves this app at
2// modelschemas.com/examples/<name>/, so client asset URLs carry that prefix
3// (vite `base`) — but Workers static assets map URL paths directly onto the
4// asset directory layout, and vite does not nest its output under `base`.
5// Mirror the URL space by moving the client output into the prefix
6// directory. No-op for standalone builds (base "/"). Identical file in
7// every example; the prefix derives from the example's directory name.
8import { mkdirSync, readdirSync, renameSync } from 'node:fs'
9import { basename, join } from 'node:path'
10​
11if (process.env.CLOUDFLARE_ENV === 'modelschemas') {
12 const appRoot = join(import.meta.dirname, '..')
13 const client = join(appRoot, 'dist', 'client')
14 const nested = join(client, 'examples', basename(appRoot))
15 mkdirSync(nested, { recursive: true })
16 for (const entry of readdirSync(client)) {
17 // .assetsignore configures the asset upload and must stay at the root.
18 if (entry === 'examples' || entry === '.assetsignore') continue
19 renameSync(join(client, entry), join(nested, entry))
20 }
21 console.log(`nested client assets under /examples/${basename(appRoot)}/`)