Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lukebennett88/luke-ui. ui.lukebennett.dev
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197import { relative, sep } from 'node:path';import { fileURLToPath } from 'node:url';import tailwindcss from '@tailwindcss/vite';import { tanstackStart } from '@tanstack/react-start/plugin/vite';import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';import react from '@vitejs/plugin-react';import mdx from 'fumadocs-mdx/vite';import type { Plugin } from 'vite-plus';import { defineConfig, lazyPlugins } from 'vite-plus';import { findMdxFiles } from './src/lib/docs-mdx-files.js';import { highlightSourcePlugin } from './src/lib/highlight-source-plugin.js';import { getMarkdownPagePath } from './src/lib/markdown-page-path.js';
// staticFunctionMiddleware hardcodes `/__tsr/staticServerFnCache/...` for the// client fetch URL with no base-path support. When deployed under a sub-path// (e.g. GitHub Pages /luke-ui/), the client requests /__tsr/... and 404s.// This patches only the client fetch so the on-disk layout stays unchanged.function staticFunctionBasePathPlugin(): Plugin { return { name: 'static-function-base-path', transform(code, id) { if (!id.includes('start-static-server-functions')) return null; if (!id.endsWith('staticFunctionMiddleware.js')) return null; if (!code.includes('fetch(url,')) return null; return code.replace( 'fetch(url,', "fetch(import.meta.env.BASE_URL.replace(/\\/$/, '') + url,", ); }, };}
const TRAILING_SLASH_PATTERN = /\/$/;
// The dev server's origin, used when `SITE_URL` is unset.const LOCAL_SITE_URL = 'http://localhost:3000';
const contentDocsDir = fileURLToPath(new URL('./content/docs/', import.meta.url));
function getMarkdownPrerenderPages(): Array<{ path: string }> { return findMdxFiles(contentDocsDir).map((filePath) => { const relativePath = relative(contentDocsDir, filePath).split(sep).join('/'); return { path: getMarkdownPagePath(relativePath) }; });}
function readBaseUrl(): string { const value = process.env.VITE_BASE_URL || '/'; if (!value.startsWith('/') || !value.endsWith('/')) { throw new Error(`VITE_BASE_URL must start and end with "/", got "${value}"`); } return value;}
export default defineConfig(async () => { const markdownPrerenderPages = getMarkdownPrerenderPages(); const baseUrl = readBaseUrl(); const siteUrl = (process.env.SITE_URL || LOCAL_SITE_URL).replace(TRAILING_SLASH_PATTERN, '');
return { // Allow overriding the base URL for deployments to sub-paths (e.g. GitHub Pages). // Set VITE_BASE_URL to the base path with a trailing slash, e.g. /luke-ui/ base: baseUrl, // The public origin for absolute URLs. It is baked in at build time so the // prerendered files and the SSR function agree. define: { 'import.meta.env.SITE_URL': JSON.stringify(siteUrl), }, environments: { ssr: { build: { rolldownOptions: { external: ['env', 'wasi_snapshot_preview1'], }, }, }, }, optimizeDeps: { // @luke-ui/react is a workspace package excluded from pre-bundling so its // source hot-reloads directly. Its runtime npm dependencies are listed // explicitly here so Vite discovers them at cold start instead of lazily // when a doc page first renders a component that needs them — a lazy // discovery mid-navigation forces a dependency re-optimize + full reload, // which corrupts the in-flight React render with an "Invalid hook call". exclude: ['@luke-ui/react'], include: [ '@luke-ui/playground-core > lz-string', '@luke-ui/playground-core > sucrase', '@monaco-editor/react', '@react-aria/utils', '@tanstack/react-router', '@vanilla-extract/recipes', '@vanilla-extract/recipes/createRuntimeFn', // Fumadocs' chrome (search trigger, popover, sidebar) imports icons // from lucide-react's barrel, which unbundled is ~1750 separate module // requests. On the playground, which also loads Monaco, that exhausts // the browser's connection pool and the page never hydrates. 'fumadocs-ui > lucide-react', 'monaco-editor', 'react-aria-components/Breadcrumbs', 'react-aria-components/Button', 'react-aria-components/Collection', 'react-aria-components/ComboBox', 'react-aria-components/composeRenderProps', 'react-aria-components/FieldError', 'react-aria-components/Form', 'react-aria-components/Group', 'react-aria-components/Header', 'react-aria-components/I18nProvider', 'react-aria-components/Input', 'react-aria-components/Label', 'react-aria-components/Link', 'react-aria-components/ListBox', 'react-aria-components/slots', 'react-aria-components/Text', 'react-aria-components/TextField', 'react-aria-components/useAsyncList', ], }, // `vp pack` compiles each pre-hydration script to an inline-able IIFE // artifact; it runs as part of `docs#generate`, not `vp build`. pack: [ { clean: false, dts: false, // Emitted as src/generated/editor-skeleton-script.iife.js — the `.iife` // suffix is fixed by tsdown for this format. entry: ['src/components/playground/editor-skeleton-script.ts'], format: 'iife' as const, // The artifact is inlined into every playground HTML response, so // strip the source's documentation comments. minify: true, outDir: 'src/generated', platform: 'browser' as const, }, { clean: false, dts: false, // Emitted as src/generated/theme-prefs-script.iife.js — the `.iife` // suffix is fixed by tsdown for this format. entry: ['src/lib/theme-prefs-script.ts'], format: 'iife' as const, // The artifact is inlined into every page response, so strip the // source's documentation comments. minify: true, outDir: 'src/generated', platform: 'browser' as const, }, ], plugins: lazyPlugins(async () => [ staticFunctionBasePathPlugin(), highlightSourcePlugin(), mdx(await import('./source.config')), tailwindcss(), vanillaExtractPlugin(), tanstackStart({ pages: [ { path: '/api/search' }, { path: '/llms.txt' }, { path: '/llms-full.txt' }, { path: '/sitemap.xml' }, { path: '/robots.txt' }, { path: '/index.md' }, // The preview page is loaded via an iframe src, which the link // crawler does not follow, so it must be prerendered explicitly. { path: '/playground/preview' }, ...markdownPrerenderPages, ], prerender: { // Serialize requests to the internal Vite preview server and retry a // transient failure without omitting the iframe preview page. concurrency: 1, crawlLinks: true, enabled: true, retryCount: 2, }, }), react(), ]), resolve: { alias: { '#recipe-engine': fileURLToPath( new URL( '../../packages/@luke-ui/react/src/core/styles/recipe-engine.ts', import.meta.url, ), ), }, tsconfigPaths: true, }, server: { // Keep in sync with `LOCAL_SITE_URL`. port: 3000, }, };});