Skip to content

Commit caad915

Browse files
committed
test(build): add regression test for build tool type leaks in .d.ts files
Adds scripts/verify-dts-imports.ts that scans all published .d.ts files for forbidden imports from build tools (vite, tsup, vitest) and `/// <reference types="node" />`. Wired into test:pr and test:ci to run after the build completes.
1 parent 58feb9e commit caad915

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
"scripts": {
1111
"clean": "pnpm --filter \"./packages/**\" run clean",
1212
"test": "pnpm run test:ci",
13-
"test:pr": "nx affected --targets=test:sherif,test:knip,test:docs,test:eslint,test:lib,test:types,test:build,build",
14-
"test:ci": "nx run-many --targets=test:sherif,test:knip,test:docs,test:eslint,test:lib,test:types,test:build,build",
13+
"test:pr": "nx affected --targets=test:sherif,test:knip,test:docs,test:eslint,test:lib,test:types,test:build,build && pnpm run test:build:dts",
14+
"test:ci": "nx run-many --targets=test:sherif,test:knip,test:docs,test:eslint,test:lib,test:types,test:build,build && pnpm run test:build:dts",
1515
"test:eslint": "nx affected --target=test:eslint",
1616
"test:sherif": "sherif -i typescript -p \"./integrations/*\" -p \"./examples/*\"",
1717
"test:size": "size-limit",
1818
"test:lib": "nx affected --target=test:lib --exclude=examples/**",
1919
"test:lib:dev": "pnpm run test:lib && nx watch --all -- pnpm run test:lib",
2020
"test:build": "nx affected --target=test:build --exclude=examples/**",
21+
"test:build:dts": "node scripts/verify-dts-imports.ts",
2122
"test:types": "nx affected --target=test:types --exclude=examples/**",
2223
"test:knip": "knip --treat-config-hints-as-errors",
2324
"test:docs": "node scripts/verify-links.ts",

scripts/verify-dts-imports.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { readFileSync } from 'node:fs'
2+
import { glob } from 'tinyglobby'
3+
4+
const FORBIDDEN_PATTERNS = [
5+
/\bfrom\s+['"]vite['"]/,
6+
/\bfrom\s+['"]tsup['"]/,
7+
/\bfrom\s+['"]vitest/,
8+
/\/\/\/\s*<reference\s+types=["']node["']\s*\/>/,
9+
]
10+
11+
const errors: Array<{ file: string; line: number; text: string }> = []
12+
13+
const files = await glob(['packages/*/build/**/*.d.ts', 'packages/*/build/**/*.d.cts'])
14+
15+
for (const file of files) {
16+
const content = readFileSync(file, 'utf-8')
17+
const lines = content.split('\n')
18+
for (let i = 0; i < lines.length; i++) {
19+
const line = lines[i]!
20+
for (const pattern of FORBIDDEN_PATTERNS) {
21+
if (pattern.test(line)) {
22+
errors.push({ file, line: i + 1, text: line.trim() })
23+
}
24+
}
25+
}
26+
}
27+
28+
if (errors.length > 0) {
29+
console.error(
30+
'ERROR: Build tool types leaked into published .d.ts files:\n',
31+
)
32+
for (const error of errors) {
33+
console.error(` ${error.file}:${error.line}`)
34+
console.error(` ${error.text}\n`)
35+
}
36+
console.error(
37+
'This usually means a tsconfig.prod.json is missing "include": ["src"].',
38+
)
39+
console.error(
40+
'See https://github.com/TanStack/query/issues/10294 for details.',
41+
)
42+
process.exit(1)
43+
} else {
44+
console.log(
45+
`Verified ${files.length} .d.ts files — no build tool type leaks found.`,
46+
)
47+
}

0 commit comments

Comments
 (0)