Skip to content

Commit 3dab67d

Browse files
committed
refactor: simplify
1 parent cbf9f9c commit 3dab67d

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

src/helpers.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fs from 'node:fs'
2+
import { createRequire } from 'node:module'
23
import path from 'node:path'
34

5+
import type { IsBunModule } from './types.js'
6+
47
/**
58
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
69
*/
@@ -71,3 +74,17 @@ export const toGlobPath = (pathname: string) => pathname.replaceAll('\\', '/')
7174

7275
export const toNativePath = (pathname: string) =>
7376
'/' === path.sep ? pathname : pathname.replaceAll('/', '\\')
77+
78+
let isBunModule: IsBunModule | undefined
79+
80+
const _filename = typeof __filename === 'string' ? __filename : import.meta.url
81+
82+
const DEFAULT_BUN_VERSION = 'latest'
83+
84+
export const isBunBuiltin = (source: string) => {
85+
isBunModule ??= createRequire(_filename)('is-bun-module')
86+
return (
87+
isBunModule!.isBunModule(source, DEFAULT_BUN_VERSION) ||
88+
isBunModule!.isSupportedNodeModule(source, DEFAULT_BUN_VERSION)
89+
)
90+
}

src/index.ts

+4-26
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
createFilesMatcher,
99
parseTsconfig,
1010
} from 'get-tsconfig'
11-
import { type Version } from 'is-bun-module'
1211
import { ResolverFactory } from 'rspack-resolver'
1312
import { stableHash } from 'stable-hash'
1413

1514
import { IMPORT_RESOLVER_NAME, JS_EXT_PATTERN } from './constants.js'
1615
import {
16+
isBunBuiltin,
1717
mangleScopedPackage,
1818
removeQuerystring,
1919
sortProjectsByAffinity,
@@ -53,11 +53,6 @@ const oxcResolve = (
5353
}
5454
}
5555

56-
type IsBunModule = typeof import('is-bun-module')
57-
let isBunModule: IsBunModule | undefined
58-
59-
const _filename = typeof __filename === 'string' ? __filename : import.meta.url
60-
6156
export const resolve = (
6257
source: string,
6358
file: string,
@@ -67,24 +62,9 @@ export const resolve = (
6762
): ResolvedResult => {
6863
options ||= {}
6964

70-
let bunVersion = process.versions.bun as Version | undefined
71-
72-
// don't worry about bun core modules
73-
if (bunVersion || options.bun) {
74-
if (
75-
bunVersion
76-
? module.isBuiltin(source)
77-
: (isBunModule ??= module.createRequire(_filename)(
78-
'is-bun-module',
79-
) as IsBunModule).isBunModule(source, (bunVersion = 'latest')) ||
80-
isBunModule.isSupportedNodeModule(source, bunVersion)
81-
) {
82-
log('matched bun core:', source)
83-
return { found: true, path: null }
84-
}
85-
} else if (module.isBuiltin(source)) {
86-
// don't worry about node core modules
87-
log('matched node core:', source)
65+
// don't worry about node/bun core modules
66+
if (module.isBuiltin(source) || (options.bun && isBunBuiltin(source))) {
67+
log('matched core:', source)
8868
return { found: true, path: null }
8969
}
9070

@@ -112,8 +92,6 @@ export const resolve = (
11292
resolver = cached
11393
}
11494

115-
options ||= {}
116-
11795
// eslint-disable-next-line sonarjs/label-position, sonarjs/no-labels
11896
createResolver: if (!resolver) {
11997
// must be a array with 2+ items here already ensured by `normalizeOptions`

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export interface TypeScriptResolverOptions extends NapiResolveOptions {
1212
bun?: boolean
1313
noWarnOnMultipleProjects?: boolean
1414
}
15+
16+
export type IsBunModule = typeof import('is-bun-module')

0 commit comments

Comments
 (0)