| 6 | import fs from 'node:fs'; |
| 7 | import path from 'node:path'; |
| 8 | import { fileURLToPath } from 'node:url'; |
| 9 | import { widgetSandbox } from '@telegram/widget-sandbox/vite'; |
| 10 | import { REFERENCE_STYLESHEET, referenceSafeAreaCss } from '@telegram/widget-sandbox/shared/reference'; |
| 11 |
| 12 | const appRoot = path.dirname(fileURLToPath(import.meta.url)); |
| 13 | const repoRoot = path.resolve(appRoot, '..', '..'); |
| 14 | const corpusDir = path.join(repoRoot, 'corpus'); |
| 15 | /** The Blitz wasm renderer prototype (pnpm build:blitz-web), served at /blitz/ outside the module graph. Dev tooling. */ |
| 16 | const blitzDir = path.join(repoRoot, 'apps', 'renderer-blitz', 'pkg-web'); |
| 17 | /** The litehtml wasm renderer prototype (pnpm build:litehtml-web), served at /litehtml/ the same way. Dev tooling. */ |
| 18 | const litehtmlDir = path.join(repoRoot, 'apps', 'renderer-litehtml', 'pkg-web'); |
| 19 |
| 20 | /** |
| 21 | * Reference mode: a fragment is rendered inside our (standards-mode) document in |
| 22 | * the product; give it a doctype so the native comparison does not fall into quirks mode, |
| 23 | * and the renderer's own stylesheet (the root and text defaults, the client-styled <button>, |
| 24 | * no selection) and the host's theme variables with the framework's default palettes and the |
| 25 | * reference text tokens (docs/sdk-v1.md §2.3, §2.4), so the comparison shows what the |
| 26 | * profile removes rather than the root style, the button style or the theme. |
| 27 | */ |
| 28 | const withDoctype = (html: string, safeArea = '') => { |
| 29 | const ua = `<style data-tw-ua>${REFERENCE_STYLESHEET}${safeArea}</style>`; |
| 30 | const m = /^\s*<!doctype[^>]*>/i.exec(html); |
| 31 | return m ? m[0] + ua + html.slice(m[0].length) : '<!DOCTYPE html>\n' + ua + html; |
| 32 | }; |
| 33 |
| 34 | /** CSP for the dev-only reference mode. Not part of the product path. */ |
| 35 | const REFERENCE_CSP = |
| 36 | "default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'unsafe-inline'; " + |
| 37 | "img-src data: blob:; font-src data:; media-src data: blob:; connect-src 'none'; frame-src 'none'; " + |
| 38 | "child-src 'none'; worker-src 'none'; form-action 'none'; base-uri 'none'"; |
| 39 |
| 40 | /** The conformance set ships with the framework (packages/conformance); the dev app lists it as the `conformance/` group. */ |
| 41 | const conformanceDir = path.join(repoRoot, 'packages', 'conformance'); |
| 42 | const groupDir = (group: string) => (group === 'conformance' ? conformanceDir : path.join(corpusDir, group)); |
| 43 |
| 44 | function listCorpus(): string[] { |
| 45 | const out: string[] = []; |
| 46 | for (const group of ['compat', 'attacks', 'generated', 'htmlbubbles-ported', 'common', 'showcase', 'defaults', 'gestures', 'resize', 'safe-area', 'interactive', 'bot-demos', 'conformance']) { |
| 47 | const base = groupDir(group); |
| 48 | const walk = (dir: string) => { |
| 49 | if (!fs.existsSync(dir)) return; |
| 50 | for (const name of fs.readdirSync(dir).sort()) { |
| 51 | const p = path.join(dir, name); |
| 52 | if (fs.statSync(p).isDirectory()) { if (group !== 'conformance' || name === 'auto' || name === 'visual' || dir !== base) walk(p); } |
| 53 | else if (name.endsWith('.html')) out.push(group + '/' + path.relative(base, p).split(path.sep).join('/')); |
| 54 | } |
| 55 | }; |
| 56 | walk(base); |
| 57 | } |
| 58 | return out; |
| 59 | } |
| 60 |
| 61 | /** Resolve a corpus-relative path (`<group>/…`), refusing anything outside its group. */ |
| 62 | function corpusFile(rel: string): string | null { |
| 63 | const decoded = decodeURIComponent(rel); |
| 64 | const group = decoded.split('/')[0]; |
| 65 | const base = groupDir(group); |
| 66 | const p = path.normalize(path.join(base, decoded.slice(group.length + 1))); |
| 67 | if (!p.startsWith(base + path.sep) || !p.endsWith('.html') || !fs.existsSync(p)) return null; |
| 68 | return p; |
| 69 | } |
| 70 |
| 71 | function serveWasmDir(dir: string, prefix: string, hint: string) { |
| 72 | return (url: string, res: import('node:http').ServerResponse): boolean => { |
| 73 | if (!url.startsWith(prefix)) return false; |
| 74 | const name = url.slice(prefix.length); |
| 75 | const p = path.join(dir, name); |
| 76 | if (!/^[\w.-]+\.(js|wasm)$/.test(name) || !fs.existsSync(p)) { res.statusCode = 404; res.end(`not built: ${hint}`); return true; } |
| 77 | res.setHeader('Content-Type', name.endsWith('.wasm') ? 'application/wasm' : 'text/javascript'); |
| 78 | res.end(fs.readFileSync(p)); |
| 79 | return true; |
| 80 | }; |
| 81 | } |
| 82 | const serveBlitz = serveWasmDir(blitzDir, '/blitz/', 'corepack pnpm build:blitz-web'); |
| 83 | const serveLitehtml = serveWasmDir(litehtmlDir, '/litehtml/', 'corepack pnpm build:litehtml-web'); |
| 84 |
| 85 | export default defineConfig({ |
| 86 | plugins: [ |
| 87 | widgetSandbox(), |
| 88 | { |
| 89 | name: 'twidget-dev', |
| 90 | configureServer(server) { |
| 91 | server.middlewares.use((req, res, next) => { |
| 92 | const url = (req.url || '').split('?')[0]; |
| 93 | if (url === '/api/corpus') { |
| 94 | res.setHeader('Content-Type', 'application/json'); |
| 95 | res.end(JSON.stringify(listCorpus())); |
| 96 | return; |
| 97 | } |
| 98 | if (serveBlitz(url, res) || serveLitehtml(url, res)) return; |
| 99 | if (url.startsWith('/corpus/')) { |
| 100 | const p = corpusFile(url.slice('/corpus/'.length)); |
| 101 | if (!p) return next(); |
| 102 | res.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 103 | res.end(fs.readFileSync(p)); |
| 104 | return; |
| 105 | } |
| 106 | // Reference mode (dev tooling only): native rendering of repo corpus files. |
| 107 | if (url.startsWith('/reference/')) { |
| 108 | const p = corpusFile(url.slice('/reference/'.length)); |
| 109 | if (!p) return next(); |
| 110 | res.setHeader('Content-Type', 'text/html; charset=utf-8'); |
| 111 | res.setHeader('Content-Security-Policy', REFERENCE_CSP); |
| 112 | // `?safeArea=bottom,cornerRight,cornerLeft` (the dev app's time control): the root variables a renderer sets for |
| 113 | // the client's time, there before the widget's first script runs (its env() stays the browser's own). |
| 114 | const area = (new URLSearchParams((req.url || '').split('?')[1] ?? '').get('safeArea') ?? '').split(',').map(Number); |
| 115 | res.end(withDoctype(fs.readFileSync(p, 'utf8'), area.some((n) => n > 0) ? referenceSafeAreaCss({ bottom: area[0], cornerRight: area[1], cornerLeft: area[2] }) : '')); |
| 116 | return; |
| 117 | } |
| 118 | next(); |
| 119 | }); |
| 120 | }, |
| 121 | }, |
| 122 | ], |
| 123 | server: { port: 5173, strictPort: true, host: '127.0.0.1' }, |
| 124 | }) |
| 125 |