Skip to content

Commit 4ec9f53

Browse files
authoredDec 5, 2022
chore: update @types/node to v18 (#11195)
1 parent 8b83089 commit 4ec9f53

File tree

6 files changed

+116
-41
lines changed

6 files changed

+116
-41
lines changed
 

‎CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it sh
222222

223223
To get around this, we inline some of these dependencies' types in `packages/vite/src/types`. This way, we can still expose the typing but bundle the dependency's source code.
224224

225-
Use `pnpm run check-dist-types` to check that the bundled types do not rely on types in `devDependencies`. If you are adding `dependencies`, make sure to configure `tsconfig.check.json`.
225+
Use `pnpm run build-types-check` to check that the bundled types do not rely on types in `devDependencies`.
226226

227227
For types shared between client and node, they should be added into `packages/vite/types`. These types are not bundled and are published as is (though they are still considered internal). Dependency types within this directory (e.g. `packages/vite/types/chokidar.d.ts`) are deprecated and should be added to `packages/vite/src/types` instead.
228228

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@types/less": "^3.0.3",
5151
"@types/micromatch": "^4.0.2",
5252
"@types/minimist": "^1.2.2",
53-
"@types/node": "^17.0.42",
53+
"@types/node": "^18.11.10",
5454
"@types/picomatch": "^2.3.0",
5555
"@types/prompts": "^2.4.1",
5656
"@types/resolve": "^1.20.2",

‎packages/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
5252
"build-types-roll": "api-extractor run && rimraf temp",
5353
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
54-
"build-types-check": "tsc --project tsconfig.check.json",
54+
"build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json",
5555
"lint": "eslint --cache --ext .ts src/**",
5656
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
5757
"prepublishOnly": "npm run build"
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Checks whether the built files depend on devDependencies types.
3+
* We shouldn't depend on them.
4+
*/
5+
import { dirname, resolve } from 'node:path'
6+
import { fileURLToPath } from 'node:url'
7+
import { readFileSync } from 'node:fs'
8+
import colors from 'picocolors'
9+
import type { ParseResult } from '@babel/parser'
10+
import type { File, SourceLocation } from '@babel/types'
11+
import { parse } from '@babel/parser'
12+
import { walkDir } from './util'
13+
14+
const dir = dirname(fileURLToPath(import.meta.url))
15+
const distDir = resolve(dir, '../dist')
16+
17+
const pkgJson = JSON.parse(
18+
readFileSync(resolve(dir, '../package.json'), 'utf-8'),
19+
)
20+
const deps = new Set(Object.keys(pkgJson.dependencies))
21+
22+
type SpecifierError = {
23+
loc: SourceLocation | null | undefined
24+
value: string
25+
file: string
26+
}
27+
28+
const errors: SpecifierError[] = []
29+
walkDir(distDir, (file) => {
30+
if (!file.endsWith('.d.ts')) return
31+
32+
const specifiers = collectImportSpecifiers(file)
33+
const notAllowedSpecifiers = specifiers.filter(
34+
({ value }) =>
35+
!(
36+
value.startsWith('./') ||
37+
value.startsWith('../') ||
38+
value.startsWith('node:') ||
39+
deps.has(value)
40+
),
41+
)
42+
43+
errors.push(...notAllowedSpecifiers)
44+
})
45+
46+
if (errors.length <= 0) {
47+
console.log(colors.green(colors.bold(`passed built types check`)))
48+
} else {
49+
console.log(colors.red(colors.bold(`failed built types check`)))
50+
console.log()
51+
errors.forEach((error) => {
52+
const pos = error.loc
53+
? `${colors.yellow(error.loc.start.line)}:${colors.yellow(
54+
error.loc.start.column,
55+
)}`
56+
: ''
57+
console.log(
58+
`${colors.cyan(error.file)}:${pos} - importing ${colors.bold(
59+
JSON.stringify(error.value),
60+
)} is not allowed in built files`,
61+
)
62+
})
63+
console.log()
64+
}
65+
66+
function collectImportSpecifiers(file: string) {
67+
const content = readFileSync(file, 'utf-8')
68+
69+
let ast: ParseResult<File>
70+
try {
71+
ast = parse(content, {
72+
sourceType: 'module',
73+
plugins: ['typescript', 'classProperties'],
74+
})
75+
} catch (e) {
76+
console.log(colors.red(`failed to parse ${file}`))
77+
throw e
78+
}
79+
80+
const result: SpecifierError[] = []
81+
82+
for (const statement of ast.program.body) {
83+
if (
84+
statement.type === 'ImportDeclaration' ||
85+
statement.type === 'ExportNamedDeclaration' ||
86+
statement.type === 'ExportAllDeclaration'
87+
) {
88+
const source = statement.source
89+
if (source?.value) {
90+
result.push({
91+
loc: source.loc,
92+
value: source.value,
93+
file,
94+
})
95+
}
96+
}
97+
}
98+
99+
return result
100+
}

‎packages/vite/tsconfig.check.json

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
{
22
"compilerOptions": {
33
"noEmit": true,
4-
"moduleResolution": "classic",
5-
"noResolve": true,
6-
"typeRoots": [],
7-
// Only add entries to `paths` when you are adding/updating dependencies (not devDependencies)
8-
// See CONTRIBUTING.md "Ensure type support" for more details
9-
"paths": {
10-
// direct
11-
"rollup": ["./node_modules/rollup/dist/rollup.d.ts"],
12-
// direct
13-
"esbuild": ["./node_modules/esbuild/lib/main.d.ts"],
14-
// direct
15-
"postcss": ["./node_modules/postcss/lib/postcss.d.ts"],
16-
// indirect: postcss depends on it
17-
"source-map-js": ["./node_modules/source-map-js/source-map.d.ts"]
18-
},
194
"strict": true,
205
"exactOptionalPropertyTypes": true
216
},
227
"include": [
23-
"../../node_modules/@types/node/**/*",
24-
// dependencies
25-
"./node_modules/rollup/**/*",
26-
"./node_modules/esbuild/**/*",
27-
"./node_modules/postcss/**/*",
28-
"./node_modules/source-map-js/**/*",
298
// dist
309
"dist/**/*",
31-
"types/customEvent.d.ts",
32-
"types/hmrPayload.d.ts",
33-
"types/importGlob.d.ts",
34-
"types/importMeta.d.ts",
35-
"types/hot.d.ts"
10+
"types/**/*"
3611
]
3712
}

‎pnpm-lock.yaml

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.