Skip to content

Commit

Permalink
feat: support node: prefix for built-in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sorccu committed Jan 20, 2025
1 parent 19656b8 commit 9bbfcad
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {} from 'node:path'
import {} from 'node:url'
import {} from 'crypto'
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ describe('dependency-parser - parser()', () => {
])
})

it('should handle node: prefix for built-ins', () => {
const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'builtin-with-node-prefix', ...filepath)
const parser = new Parser({
supportedNpmModules: defaultNpmModules,
})
parser.parse(toAbsolutePath('entrypoint.ts'))
})

/*
* There is an unhandled edge-case when require() is reassigned.
* Even though the check might execute fine, we throw an error for a missing dependency.
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/services/check-parser/package-files/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,11 @@ export function isLocalPath (importPath: string) {

return false
}

export function isBuiltinPath (importPath: string) {
if (importPath.startsWith('node:')) {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SourceFile } from './source-file'
import { PackageJsonFile } from './package-json-file'
import { TSConfigFile } from './tsconfig-json-file'
import { JSConfigFile } from './jsconfig-json-file'
import { isLocalPath, PathResult } from './paths'
import { isBuiltinPath, isLocalPath, PathResult } from './paths'
import { FileLoader, LoadFile } from './loader'
import { JsonSourceFile } from './json-source-file'
import { LookupContext } from './lookup'
Expand Down Expand Up @@ -248,6 +248,13 @@ export class PackageFilesResolver {

resolve:
for (const importPath of dependencies) {
if (isBuiltinPath(importPath)) {
resolved.external.push({
importPath,
})
continue resolve
}

if (isLocalPath(importPath)) {
const relativeDepPath = path.resolve(dirname, importPath)
const sourceFile = this.cache.sourceFile(relativeDepPath, context)
Expand Down
31 changes: 28 additions & 3 deletions packages/cli/src/services/check-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ type SupportedFileExtension = '.js' | '.mjs' | '.ts'
const PACKAGE_EXTENSION = `${path.sep}package.json`

const supportedBuiltinModules = [
'assert', 'buffer', 'crypto', 'dns', 'fs', 'path', 'querystring', 'readline ', 'stream', 'string_decoder',
'timers', 'tls', 'url', 'util', 'zlib',
'node:assert',
'node:buffer',
'node:crypto',
'node:dns',
'node:fs',
'node:path',
'node:querystring',
'node:readline',
'node:stream',
'node:string_decoder',
'node:timers',
'node:tls',
'node:url',
'node:util',
'node:zlib',
]

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

supportsModule (importPath: string) {
if (this.supportedModules.has(importPath)) {
return true
}

if (this.supportedModules.has('node:' + importPath)) {
return true
}

return false
}

parse (entrypoint: string) {
const { content } = validateEntrypoint(entrypoint)

Expand Down Expand Up @@ -130,7 +155,7 @@ export class Parser {

if (this.checkUnsupportedModules) {
const unsupportedDependencies = resolvedDependencies.external.flatMap(dep => {
if (!this.supportedModules.has(dep.importPath)) {
if (!this.supportsModule(dep.importPath)) {
return [dep.importPath]
} else {
return []
Expand Down

0 comments on commit 9bbfcad

Please sign in to comment.