|
| 1 | +import { readdirSync, readFileSync } from 'node:fs' |
| 2 | +import { extname, join } from 'node:path' |
| 3 | +import { describe, it } from 'vitest' |
| 4 | + |
| 5 | +const sourceRoot = join(process.cwd(), 'src') |
| 6 | +const sourceExtensions = new Set(['.ts', '.tsx']) |
| 7 | +const forbiddenPatterns = [ |
| 8 | + /import\.meta\?\./, |
| 9 | + /typeof\s+import\.meta(?!\.)/, |
| 10 | + /=\s*import\.meta(?!\.)/, |
| 11 | +] |
| 12 | + |
| 13 | +const getSourceFiles = (dir: string): Array<string> => { |
| 14 | + const entries = readdirSync(dir, { withFileTypes: true }) |
| 15 | + return entries.flatMap((entry) => { |
| 16 | + const path = join(dir, entry.name) |
| 17 | + if (entry.isDirectory()) { |
| 18 | + return getSourceFiles(path) |
| 19 | + } |
| 20 | + if (sourceExtensions.has(extname(path))) { |
| 21 | + return [path] |
| 22 | + } |
| 23 | + return [] |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +describe('import.meta compatibility', () => { |
| 28 | + it('avoids direct import.meta access that breaks Rspack parsing', () => { |
| 29 | + const violations = getSourceFiles(sourceRoot).flatMap((path) => { |
| 30 | + const content = readFileSync(path, 'utf8') |
| 31 | + const lines = content.split('\n') |
| 32 | + return lines.flatMap((line, index) => { |
| 33 | + const hasViolation = forbiddenPatterns.some((pattern) => |
| 34 | + pattern.test(line), |
| 35 | + ) |
| 36 | + return hasViolation ? [`${path}:${index + 1}`] : [] |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + if (violations.length > 0) { |
| 41 | + throw new Error( |
| 42 | + [ |
| 43 | + 'Found direct `import.meta` usage in devtools source.', |
| 44 | + 'Rspack only supports property access on `import.meta`.', |
| 45 | + ...violations, |
| 46 | + ].join('\n'), |
| 47 | + ) |
| 48 | + } |
| 49 | + }) |
| 50 | +}) |
0 commit comments