Skip to content

Commit

Permalink
chore: update @types/node to v18 (vitejs#11195)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored and futurGH committed Feb 26, 2023
1 parent 2aba2c9 commit 701a8f5
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it sh

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.

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`.
Use `pnpm run build-types-check` to check that the bundled types do not rely on types in `devDependencies`.

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.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/less": "^3.0.3",
"@types/micromatch": "^4.0.2",
"@types/minimist": "^1.2.2",
"@types/node": "^17.0.42",
"@types/node": "^18.11.10",
"@types/picomatch": "^2.3.0",
"@types/prompts": "^2.4.1",
"@types/resolve": "^1.20.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
"build-types-roll": "api-extractor run && rimraf temp",
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
"build-types-check": "tsc --project tsconfig.check.json",
"build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json",
"lint": "eslint --cache --ext .ts src/**",
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
"prepublishOnly": "npm run build"
Expand Down
100 changes: 100 additions & 0 deletions packages/vite/scripts/checkBuiltTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Checks whether the built files depend on devDependencies types.
* We shouldn't depend on them.
*/
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { readFileSync } from 'node:fs'
import colors from 'picocolors'
import type { ParseResult } from '@babel/parser'
import type { File, SourceLocation } from '@babel/types'
import { parse } from '@babel/parser'
import { walkDir } from './util'

const dir = dirname(fileURLToPath(import.meta.url))
const distDir = resolve(dir, '../dist')

const pkgJson = JSON.parse(
readFileSync(resolve(dir, '../package.json'), 'utf-8'),
)
const deps = new Set(Object.keys(pkgJson.dependencies))

type SpecifierError = {
loc: SourceLocation | null | undefined
value: string
file: string
}

const errors: SpecifierError[] = []
walkDir(distDir, (file) => {
if (!file.endsWith('.d.ts')) return

const specifiers = collectImportSpecifiers(file)
const notAllowedSpecifiers = specifiers.filter(
({ value }) =>
!(
value.startsWith('./') ||
value.startsWith('../') ||
value.startsWith('node:') ||
deps.has(value)
),
)

errors.push(...notAllowedSpecifiers)
})

if (errors.length <= 0) {
console.log(colors.green(colors.bold(`passed built types check`)))
} else {
console.log(colors.red(colors.bold(`failed built types check`)))
console.log()
errors.forEach((error) => {
const pos = error.loc
? `${colors.yellow(error.loc.start.line)}:${colors.yellow(
error.loc.start.column,
)}`
: ''
console.log(
`${colors.cyan(error.file)}:${pos} - importing ${colors.bold(
JSON.stringify(error.value),
)} is not allowed in built files`,
)
})
console.log()
}

function collectImportSpecifiers(file: string) {
const content = readFileSync(file, 'utf-8')

let ast: ParseResult<File>
try {
ast = parse(content, {
sourceType: 'module',
plugins: ['typescript', 'classProperties'],
})
} catch (e) {
console.log(colors.red(`failed to parse ${file}`))
throw e
}

const result: SpecifierError[] = []

for (const statement of ast.program.body) {
if (
statement.type === 'ImportDeclaration' ||
statement.type === 'ExportNamedDeclaration' ||
statement.type === 'ExportAllDeclaration'
) {
const source = statement.source
if (source?.value) {
result.push({
loc: source.loc,
value: source.value,
file,
})
}
}
}

return result
}
27 changes: 1 addition & 26 deletions packages/vite/tsconfig.check.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
{
"compilerOptions": {
"noEmit": true,
"moduleResolution": "classic",
"noResolve": true,
"typeRoots": [],
// Only add entries to `paths` when you are adding/updating dependencies (not devDependencies)
// See CONTRIBUTING.md "Ensure type support" for more details
"paths": {
// direct
"rollup": ["./node_modules/rollup/dist/rollup.d.ts"],
// direct
"esbuild": ["./node_modules/esbuild/lib/main.d.ts"],
// direct
"postcss": ["./node_modules/postcss/lib/postcss.d.ts"],
// indirect: postcss depends on it
"source-map-js": ["./node_modules/source-map-js/source-map.d.ts"]
},
"strict": true,
"exactOptionalPropertyTypes": true
},
"include": [
"../../node_modules/@types/node/**/*",
// dependencies
"./node_modules/rollup/**/*",
"./node_modules/esbuild/**/*",
"./node_modules/postcss/**/*",
"./node_modules/source-map-js/**/*",
// dist
"dist/**/*",
"types/customEvent.d.ts",
"types/hmrPayload.d.ts",
"types/importGlob.d.ts",
"types/importMeta.d.ts",
"types/hot.d.ts"
"types/**/*"
]
}
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 701a8f5

Please sign in to comment.