/** * Docs-host playground runtime allowlist: `@luke-ui/react` subpath exports, * the React specifiers the compiler needs, and the third-party and `#docs` * helpers that documented examples import in the playground. * * `@luke-ui/playground-core/specifiers` turns an `exports` map into * specifiers. Docs owns the package name, its `package.json` path, and the list * order below. */ import { readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { PLAYGROUND_BASE_SPECIFIERS, packageExportSpecifiers, } from '@luke-ui/playground-core/specifiers'; import * as z from 'zod'; /** * Third-party packages that docs examples import directly. Add a package here * whenever an example needs to import it in the playground. The Monaco type * list in `generate-playground-types.ts` is a different set on purpose. */ const DOCS_PLAYGROUND_THIRD_PARTY_SPECIFIERS = [ '@hookform/resolvers/zod', '@tanstack/react-form', 'react-error-boundary', 'react-hook-form', 'zod', ] as const; /** Docs-only helpers that documented examples can import in the playground. */ const DOCS_PLAYGROUND_DOCS_SPECIFIERS = ['#docs'] as const; const packageJsonSchema = z.object({ exports: z.record(z.string(), z.string()), }); const reactPackageJsonPath = resolve( dirname(fileURLToPath(import.meta.url)), '../../../../packages/@luke-ui/react/package.json', ); /** Specifiers the docs playground preview can `require` at runtime. */ export function docsPlaygroundRuntimeSpecifierList(): Array { const reactExports = packageJsonSchema.parse( JSON.parse(readFileSync(reactPackageJsonPath, 'utf8')), ).exports; return [ ...packageExportSpecifiers('@luke-ui/react', reactExports), ...PLAYGROUND_BASE_SPECIFIERS, ...DOCS_PLAYGROUND_THIRD_PARTY_SPECIFIERS, ...DOCS_PLAYGROUND_DOCS_SPECIFIERS, ]; } let cachedSpecifiers: ReadonlySet | undefined; export function docsPlaygroundSpecifiers(): ReadonlySet { cachedSpecifiers ??= new Set(docsPlaygroundRuntimeSpecifierList()); return cachedSpecifiers; }