import react from '@vitejs/plugin-react-swc' import path from 'path' import { PluginOption, defineConfig } from 'vite' const PR_NUMBER = process.env.VERCEL_GIT_PULL_REQUEST_ID function getEnv() { if (!process.env.VERCEL_ENV) { return 'development' } if (PR_NUMBER !== undefined && PR_NUMBER !== '') { return 'preview' } if (process.env.VERCEL_ENV === 'production') { return 'production' } return 'canary' } const env = getEnv() // eslint-disable-next-line no-console console.log('build env:', env) function urlOrLocalFallback(mode: string, url: string | undefined, localFallbackPort: number) { if (url) { return JSON.stringify(url) } if (mode === 'development') { // in dev, vite lets us inline javascript expressions - so we return a template string that // will be evaluated on the client return '`http://${location.hostname}:' + localFallbackPort + '`' } else { // in production, we have to fall back to a hardcoded value return JSON.stringify(`http://localhost:${localFallbackPort}`) } } const TLDRAW_BEMO_URL_STRING = env === 'production' ? '"https://demo.tldraw.xyz"' : env === 'canary' ? '"https://canary-demo.tldraw.xyz"' : `"https://pr-${PR_NUMBER}-demo.tldraw.xyz"` export default defineConfig(({ mode }) => ({ plugins: [react({ tsDecorators: true }), exampleReadmePlugin()], root: path.join(__dirname, 'src'), publicDir: path.join(__dirname, 'public'), build: { outDir: path.join(__dirname, 'dist'), assetsInlineLimit: 0, target: 'es2022', }, esbuild: { target: 'es2022', }, server: { port: 5420, }, clearScreen: false, optimizeDeps: { exclude: ['@tldraw/assets'], esbuildOptions: { target: 'es2022', }, }, define: { 'process.env.TLDRAW_ENV': JSON.stringify(process.env.VERCEL_ENV ?? 'development'), 'process.env.TLDRAW_BEMO_URL': urlOrLocalFallback(mode, TLDRAW_BEMO_URL_STRING, 8989), }, })) function exampleReadmePlugin(): PluginOption { return { name: 'example-readme', async transform(src, id) { const match = id.match(/examples\/src\/examples\/(.*)\/README.md$/) if (!match) return const remark = (await import('remark')).remark const remarkFrontmatter = (await import('remark-frontmatter')).default const remarkHtml = (await import('remark-html')).default const matter = (await import('vfile-matter')).matter const file = await remark() .use(remarkFrontmatter) .use(remarkHtml) .use(() => (_, file) => matter(file)) .process(src) const frontmatter = parseFrontMatter(file.data.matter, id) const separator = '\n