Skip to content

Commit c8121e5

Browse files
authored
feat: make is-bun-module as optional peer dependency (#391)
1 parent 458fdaf commit c8121e5

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

.changeset/silent-cows-drive.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"eslint-import-resolver-typescript": minor
3+
---
4+
5+
feat: make `is-bun-module` as optional peer dependency
6+
7+
Technically this is a BREAKING CHANGE, but considering we just raise out v4 recently and this only affects `bun` users, `bun --bun eslint` even works without this dependency, so I'd consider this as a minor change.
8+
9+
So for `bun` users, there are three options:
10+
11+
1. install `is-bun-module` dependency manually and use `bun: true` option
12+
2. run `eslint` with `bun --bun eslint` w/o `bun: true` option
13+
3. enable `run#bun` in [`bunfig.toml`](https://bun.sh/docs/runtime/bunfig#run-bun-auto-alias-node-to-bun) w/o `bun: true` option

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"path": "./lib/index.js",
4-
"limit": "1.6kB"
4+
"limit": "1.5kB"
55
}
66
]

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,23 @@
6565
"peerDependencies": {
6666
"eslint": "*",
6767
"eslint-plugin-import": "*",
68-
"eslint-plugin-import-x": "*"
68+
"eslint-plugin-import-x": "*",
69+
"is-bun-module": "*"
6970
},
7071
"peerDependenciesMeta": {
7172
"eslint-plugin-import": {
7273
"optional": true
7374
},
7475
"eslint-plugin-import-x": {
7576
"optional": true
77+
},
78+
"is-bun-module": {
79+
"optional": true
7680
}
7781
},
7882
"dependencies": {
7983
"debug": "^4.4.0",
8084
"get-tsconfig": "^4.10.0",
81-
"is-bun-module": "^1.3.0",
8285
"rspack-resolver": "^1.1.2",
8386
"stable-hash": "^0.0.5",
8487
"tinyglobby": "^0.2.12"
@@ -100,6 +103,7 @@
100103
"eslint": "^9.22.0",
101104
"eslint-import-resolver-typescript": "link:.",
102105
"eslint-plugin-import-x": "^4.8.0",
106+
"is-bun-module": "^1.3.0",
103107
"lint-staged": "^15.5.0",
104108
"npm-run-all2": "^7.0.2",
105109
"prettier": "^3.5.3",

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-19
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, isBunModule, isSupportedNodeModule } 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,
@@ -62,22 +62,9 @@ export const resolve = (
6262
): ResolvedResult => {
6363
options ||= {}
6464

65-
let bunVersion = process.versions.bun as Version | undefined
66-
67-
// don't worry about bun core modules
68-
if (bunVersion || options.bun) {
69-
if (
70-
bunVersion
71-
? module.isBuiltin(source)
72-
: isBunModule(source, (bunVersion = 'latest')) ||
73-
isSupportedNodeModule(source, bunVersion)
74-
) {
75-
log('matched bun core:', source)
76-
return { found: true, path: null }
77-
}
78-
} else if (module.isBuiltin(source)) {
79-
// don't worry about node core modules
80-
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)
8168
return { found: true, path: null }
8269
}
8370

@@ -105,8 +92,6 @@ export const resolve = (
10592
resolver = cached
10693
}
10794

108-
options ||= {}
109-
11095
// eslint-disable-next-line sonarjs/label-position, sonarjs/no-labels
11196
createResolver: if (!resolver) {
11297
// 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')

yarn.lock

+3
Original file line numberDiff line numberDiff line change
@@ -6715,11 +6715,14 @@ __metadata:
67156715
eslint: "*"
67166716
eslint-plugin-import: "*"
67176717
eslint-plugin-import-x: "*"
6718+
is-bun-module: "*"
67186719
peerDependenciesMeta:
67196720
eslint-plugin-import:
67206721
optional: true
67216722
eslint-plugin-import-x:
67226723
optional: true
6724+
is-bun-module:
6725+
optional: true
67236726
languageName: unknown
67246727
linkType: soft
67256728

0 commit comments

Comments
 (0)