|
| 1 | +import { readFile } from 'node:fs/promises' |
| 2 | +import { dirname, relative, resolve } from 'node:path' |
| 3 | +import { findDynamicImports, findExports, findStaticImports } from 'mlly' |
| 4 | + |
| 5 | +interface ForbiddenRule { |
| 6 | + name: string |
| 7 | + match: (specifier: string) => boolean |
| 8 | +} |
| 9 | + |
| 10 | +const FORBIDDEN: ForbiddenRule[] = [ |
| 11 | + { name: 'ws', match: id => id === 'ws' || id.startsWith('ws/') }, |
| 12 | + { name: 'h3', match: id => id === 'h3' || id.startsWith('h3/') }, |
| 13 | + { name: 'node:* builtin', match: id => id.startsWith('node:') }, |
| 14 | + { name: 'devframe/rpc/transports/*', match: id => id.startsWith('devframe/rpc/transports/') }, |
| 15 | + { name: 'devframe/node*', match: id => id === 'devframe/node' || id.startsWith('devframe/node/') }, |
| 16 | +] |
| 17 | + |
| 18 | +interface ScannedSpecifiers { |
| 19 | + static: string[] |
| 20 | + dynamic: string[] |
| 21 | +} |
| 22 | + |
| 23 | +interface Violation { |
| 24 | + file: string |
| 25 | + specifier: string |
| 26 | + rule: string |
| 27 | +} |
| 28 | + |
| 29 | +async function scanSpecifiers(file: string): Promise<ScannedSpecifiers> { |
| 30 | + const code = await readFile(file, 'utf8') |
| 31 | + const staticIds = new Set<string>() |
| 32 | + for (const i of findStaticImports(code)) |
| 33 | + staticIds.add(i.specifier) |
| 34 | + for (const e of findExports(code)) { |
| 35 | + if (e.specifier) |
| 36 | + staticIds.add(e.specifier) |
| 37 | + } |
| 38 | + const dynamicIds = new Set<string>() |
| 39 | + for (const d of findDynamicImports(code)) { |
| 40 | + // Only consider plain string expressions; ignore variable/template imports. |
| 41 | + const match = d.expression.match(/^\s*['"]([^'"]+)['"]\s*$/) |
| 42 | + if (match?.[1]) |
| 43 | + dynamicIds.add(match[1]) |
| 44 | + } |
| 45 | + return { static: [...staticIds], dynamic: [...dynamicIds] } |
| 46 | +} |
| 47 | + |
| 48 | +export interface CheckClientDistOptions { |
| 49 | + /** Absolute paths to the client entry chunks to walk from. */ |
| 50 | + entries: string[] |
| 51 | + /** Used to build relative paths in error messages. */ |
| 52 | + cwd: string |
| 53 | +} |
| 54 | + |
| 55 | +export async function checkClientDist(options: CheckClientDistOptions): Promise<void> { |
| 56 | + const { entries, cwd } = options |
| 57 | + const visited = new Set<string>() |
| 58 | + const violations: Violation[] = [] |
| 59 | + |
| 60 | + async function visit(file: string): Promise<void> { |
| 61 | + if (visited.has(file)) |
| 62 | + return |
| 63 | + visited.add(file) |
| 64 | + |
| 65 | + let scanned: ScannedSpecifiers |
| 66 | + try { |
| 67 | + scanned = await scanSpecifiers(file) |
| 68 | + } |
| 69 | + catch (err) { |
| 70 | + throw new Error(`[check-client-dist] Failed to read ${relative(cwd, file)}: ${(err as Error).message}`) |
| 71 | + } |
| 72 | + |
| 73 | + // Static imports load eagerly when the file is evaluated — they're the leak |
| 74 | + // vector this guard exists to catch. Flag any forbidden specifier. |
| 75 | + for (const id of scanned.static) { |
| 76 | + const hit = FORBIDDEN.find(r => r.match(id)) |
| 77 | + if (hit) |
| 78 | + violations.push({ file, specifier: id, rule: hit.name }) |
| 79 | + } |
| 80 | + |
| 81 | + // Follow both static and dynamic relative imports to discover every chunk |
| 82 | + // the browser can end up loading. Dynamic specifiers themselves aren't |
| 83 | + // checked against FORBIDDEN — the chunk they target is, on visit. |
| 84 | + for (const id of [...scanned.static, ...scanned.dynamic]) { |
| 85 | + if (id.startsWith('./') || id.startsWith('../')) { |
| 86 | + const next = resolve(dirname(file), id) |
| 87 | + await visit(next) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + for (const entry of entries) |
| 93 | + await visit(entry) |
| 94 | + |
| 95 | + if (violations.length > 0) { |
| 96 | + const lines: string[] = ['[check-client-dist] Forbidden server-only imports found in client dist:', ''] |
| 97 | + for (const v of violations) { |
| 98 | + lines.push(` ${relative(cwd, v.file)}`) |
| 99 | + lines.push(` imports ${JSON.stringify(v.specifier)} (matches forbidden rule: ${v.rule})`) |
| 100 | + } |
| 101 | + lines.push('') |
| 102 | + lines.push(`Scanned ${visited.size} chunks reachable from ${entries.length} client entries.`) |
| 103 | + lines.push('Client chunks must not statically import server-only modules — see packages/core/tsdown.config.ts.') |
| 104 | + throw new Error(lines.join('\n')) |
| 105 | + } |
| 106 | + |
| 107 | + console.log(`[check-client-dist] OK — scanned ${visited.size} chunks reachable from ${entries.length} client entries`) |
| 108 | +} |
0 commit comments