forked from MarvelSQ/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update @types/node to v18 (vitejs#11195)
- Loading branch information
1 parent
2aba2c9
commit 701a8f5
Showing
6 changed files
with
116 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.