|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { dirname, join } from 'node:path' |
| 3 | +import { fileURLToPath, pathToFileURL } from 'node:url' |
| 4 | +import type { RouteConfigEntry } from '@react-router/dev/routes' |
| 5 | + |
| 6 | +/** |
| 7 | + * Build-time resolution of the optional Pro overlay. See `pro-contract.ts` for |
| 8 | + * what an overlay provides. |
| 9 | + * |
| 10 | + * The overlay is a directory at `app/pro/`, absent from this repository and |
| 11 | + * cloned in by a Pro build. Presence alone does not enable it — `PGBOSS_PRO=1` |
| 12 | + * must also be set, so a build is never silently different from what was asked |
| 13 | + * for. Setting the flag without an overlay is a hard error rather than a |
| 14 | + * quiet fallback. |
| 15 | + * |
| 16 | + * Two halves, resolved two ways, because React Router's config loader runs |
| 17 | + * outside the Vite module graph and honours neither `resolve.alias` nor `~`: |
| 18 | + * |
| 19 | + * - **Config-time** — `proRoutes()`, imported by relative path from |
| 20 | + * `app/routes.ts`. Anything reachable from `app/pro/routes.ts` must avoid `~` |
| 21 | + * imports for the same reason. Route definitions are pure data, so that costs |
| 22 | + * the overlay nothing. |
| 23 | + * - **Runtime** — `proAlias()` gives Vite and Vitest the target for `~pro`, |
| 24 | + * which resolves to the no-op `pro-stub.ts` in every ordinary build. |
| 25 | + */ |
| 26 | + |
| 27 | +const here = dirname(fileURLToPath(import.meta.url)) |
| 28 | + |
| 29 | +/** Where a Pro build clones the overlay. The default for every caller but the tests. */ |
| 30 | +export const overlayDir = join(here, '..', 'pro') |
| 31 | +export const stubPath = join(here, 'pro-stub.ts') |
| 32 | + |
| 33 | +/** Read at call time rather than import time, so tests can exercise both states. */ |
| 34 | +export function proEnabled (): boolean { |
| 35 | + return process.env.PGBOSS_PRO === '1' |
| 36 | +} |
| 37 | + |
| 38 | +function requireOverlay (dir: string): void { |
| 39 | + if (!existsSync(dir)) { |
| 40 | + throw new Error( |
| 41 | + `PGBOSS_PRO=1 but no overlay is present at ${dir}. ` + |
| 42 | + 'Clone the Pro overlay into that directory before building, or unset PGBOSS_PRO.' |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Target for the `~pro` alias: the overlay's runtime entry, or the stub. |
| 49 | + * |
| 50 | + * `dir` exists so the tests can point at a scratch directory they own. Nothing |
| 51 | + * here ever writes to or removes `dir`, and no test may pass `overlayDir` — a |
| 52 | + * developer's overlay clone is live, uncommitted work. |
| 53 | + */ |
| 54 | +export function proAlias (dir: string = overlayDir): string { |
| 55 | + if (!proEnabled()) { |
| 56 | + return stubPath |
| 57 | + } |
| 58 | + |
| 59 | + requireOverlay(dir) |
| 60 | + return join(dir, 'index.tsx') |
| 61 | +} |
| 62 | + |
| 63 | +/** Routes the overlay adds, appended to the free route table. See `proAlias` on `dir`. */ |
| 64 | +export async function proRoutes (dir: string = overlayDir): Promise<RouteConfigEntry[]> { |
| 65 | + if (!proEnabled()) { |
| 66 | + return [] |
| 67 | + } |
| 68 | + |
| 69 | + requireOverlay(dir) |
| 70 | + |
| 71 | + // The specifier is computed so TypeScript does not try to resolve a directory |
| 72 | + // that is absent from every build but a Pro one. |
| 73 | + const entry = pathToFileURL(join(dir, 'routes.ts')).href |
| 74 | + const { default: routes } = await import(/* @vite-ignore */ entry) as { default: RouteConfigEntry[] } |
| 75 | + return routes |
| 76 | +} |
0 commit comments