Skip to content

Commit 33f3c08

Browse files
authored
feat: support node: prefix for built-in modules (#1009)
1 parent 19656b8 commit 33f3c08

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {} from 'node:path'
2+
import {} from 'node:url'
3+
import {} from 'crypto'

packages/cli/src/services/check-parser/__tests__/check-parser.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ describe('dependency-parser - parser()', () => {
233233
])
234234
})
235235

236+
it('should handle node: prefix for built-ins', () => {
237+
const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'builtin-with-node-prefix', ...filepath)
238+
const parser = new Parser({
239+
supportedNpmModules: defaultNpmModules,
240+
})
241+
parser.parse(toAbsolutePath('entrypoint.ts'))
242+
})
243+
236244
/*
237245
* There is an unhandled edge-case when require() is reassigned.
238246
* Even though the check might execute fine, we throw an error for a missing dependency.

packages/cli/src/services/check-parser/package-files/paths.ts

+8
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,11 @@ export function isLocalPath (importPath: string) {
260260

261261
return false
262262
}
263+
264+
export function isBuiltinPath (importPath: string) {
265+
if (importPath.startsWith('node:')) {
266+
return true
267+
}
268+
269+
return false
270+
}

packages/cli/src/services/check-parser/package-files/resolver.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SourceFile } from './source-file'
66
import { PackageJsonFile } from './package-json-file'
77
import { TSConfigFile } from './tsconfig-json-file'
88
import { JSConfigFile } from './jsconfig-json-file'
9-
import { isLocalPath, PathResult } from './paths'
9+
import { isBuiltinPath, isLocalPath, PathResult } from './paths'
1010
import { FileLoader, LoadFile } from './loader'
1111
import { JsonSourceFile } from './json-source-file'
1212
import { LookupContext } from './lookup'
@@ -248,6 +248,13 @@ export class PackageFilesResolver {
248248

249249
resolve:
250250
for (const importPath of dependencies) {
251+
if (isBuiltinPath(importPath)) {
252+
resolved.external.push({
253+
importPath,
254+
})
255+
continue resolve
256+
}
257+
251258
if (isLocalPath(importPath)) {
252259
const relativeDepPath = path.resolve(dirname, importPath)
253260
const sourceFile = this.cache.sourceFile(relativeDepPath, context)

packages/cli/src/services/check-parser/parser.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@ type SupportedFileExtension = '.js' | '.mjs' | '.ts'
2121
const PACKAGE_EXTENSION = `${path.sep}package.json`
2222

2323
const supportedBuiltinModules = [
24-
'assert', 'buffer', 'crypto', 'dns', 'fs', 'path', 'querystring', 'readline ', 'stream', 'string_decoder',
25-
'timers', 'tls', 'url', 'util', 'zlib',
24+
'node:assert',
25+
'node:buffer',
26+
'node:crypto',
27+
'node:dns',
28+
'node:fs',
29+
'node:path',
30+
'node:querystring',
31+
'node:readline',
32+
'node:stream',
33+
'node:string_decoder',
34+
'node:timers',
35+
'node:tls',
36+
'node:url',
37+
'node:util',
38+
'node:zlib',
2639
]
2740

2841
function validateEntrypoint (entrypoint: string): {extension: SupportedFileExtension, content: string} {
@@ -87,6 +100,18 @@ export class Parser {
87100
this.checkUnsupportedModules = options.checkUnsupportedModules ?? true
88101
}
89102

103+
supportsModule (importPath: string) {
104+
if (this.supportedModules.has(importPath)) {
105+
return true
106+
}
107+
108+
if (this.supportedModules.has('node:' + importPath)) {
109+
return true
110+
}
111+
112+
return false
113+
}
114+
90115
parse (entrypoint: string) {
91116
const { content } = validateEntrypoint(entrypoint)
92117

@@ -130,7 +155,7 @@ export class Parser {
130155

131156
if (this.checkUnsupportedModules) {
132157
const unsupportedDependencies = resolvedDependencies.external.flatMap(dep => {
133-
if (!this.supportedModules.has(dep.importPath)) {
158+
if (!this.supportsModule(dep.importPath)) {
134159
return [dep.importPath]
135160
} else {
136161
return []

0 commit comments

Comments
 (0)