Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make is-bun-module as optional peer dependency #391

Merged
merged 5 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/silent-cows-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"eslint-import-resolver-typescript": minor
---

feat: make `is-bun-module` as optional peer dependency

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.

So for `bun` users, there are three options:

1. install `is-bun-module` dependency manually and use `bun: true` option
2. run `eslint` with `bun --bun eslint` w/o `bun: true` option
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
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": "./lib/index.js",
"limit": "1.6kB"
"limit": "1.5kB"
}
]
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,23 @@
"peerDependencies": {
"eslint": "*",
"eslint-plugin-import": "*",
"eslint-plugin-import-x": "*"
"eslint-plugin-import-x": "*",
"is-bun-module": "*"
},
"peerDependenciesMeta": {
"eslint-plugin-import": {
"optional": true
},
"eslint-plugin-import-x": {
"optional": true
},
"is-bun-module": {
"optional": true
}
},
"dependencies": {
"debug": "^4.4.0",
"get-tsconfig": "^4.10.0",
"is-bun-module": "^1.3.0",
"rspack-resolver": "^1.1.2",
"stable-hash": "^0.0.5",
"tinyglobby": "^0.2.12"
Expand All @@ -100,6 +103,7 @@
"eslint": "^9.22.0",
"eslint-import-resolver-typescript": "link:.",
"eslint-plugin-import-x": "^4.8.0",
"is-bun-module": "^1.3.0",
"lint-staged": "^15.5.0",
"npm-run-all2": "^7.0.2",
"prettier": "^3.5.3",
Expand Down
17 changes: 17 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from 'node:fs'
import { createRequire } from 'node:module'
import path from 'node:path'

import type { IsBunModule } from './types.js'

/**
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
*/
Expand Down Expand Up @@ -71,3 +74,17 @@ export const toGlobPath = (pathname: string) => pathname.replaceAll('\\', '/')

export const toNativePath = (pathname: string) =>
'/' === path.sep ? pathname : pathname.replaceAll('/', '\\')

let isBunModule: IsBunModule | undefined

const _filename = typeof __filename === 'string' ? __filename : import.meta.url

const DEFAULT_BUN_VERSION = 'latest'

export const isBunBuiltin = (source: string) => {
isBunModule ??= createRequire(_filename)('is-bun-module')
return (
isBunModule!.isBunModule(source, DEFAULT_BUN_VERSION) ||
isBunModule!.isSupportedNodeModule(source, DEFAULT_BUN_VERSION)
)
}
23 changes: 4 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
createFilesMatcher,
parseTsconfig,
} from 'get-tsconfig'
import { type Version, isBunModule, isSupportedNodeModule } from 'is-bun-module'
import { ResolverFactory } from 'rspack-resolver'
import { stableHash } from 'stable-hash'

import { IMPORT_RESOLVER_NAME, JS_EXT_PATTERN } from './constants.js'
import {
isBunBuiltin,
mangleScopedPackage,
removeQuerystring,
sortProjectsByAffinity,
Expand Down Expand Up @@ -62,22 +62,9 @@ export const resolve = (
): ResolvedResult => {
options ||= {}

let bunVersion = process.versions.bun as Version | undefined

// don't worry about bun core modules
if (bunVersion || options.bun) {
if (
bunVersion
? module.isBuiltin(source)
: isBunModule(source, (bunVersion = 'latest')) ||
isSupportedNodeModule(source, bunVersion)
) {
log('matched bun core:', source)
return { found: true, path: null }
}
} else if (module.isBuiltin(source)) {
// don't worry about node core modules
log('matched node core:', source)
// don't worry about node/bun core modules
if (module.isBuiltin(source) || (options.bun && isBunBuiltin(source))) {
log('matched core:', source)
return { found: true, path: null }
}

Expand Down Expand Up @@ -105,8 +92,6 @@ export const resolve = (
resolver = cached
}

options ||= {}

// eslint-disable-next-line sonarjs/label-position, sonarjs/no-labels
createResolver: if (!resolver) {
// must be a array with 2+ items here already ensured by `normalizeOptions`
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export interface TypeScriptResolverOptions extends NapiResolveOptions {
bun?: boolean
noWarnOnMultipleProjects?: boolean
}

export type IsBunModule = typeof import('is-bun-module')
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6715,11 +6715,14 @@ __metadata:
eslint: "*"
eslint-plugin-import: "*"
eslint-plugin-import-x: "*"
is-bun-module: "*"
peerDependenciesMeta:
eslint-plugin-import:
optional: true
eslint-plugin-import-x:
optional: true
is-bun-module:
optional: true
languageName: unknown
linkType: soft

Expand Down