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 1 commit
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
Prev Previous commit
Next Next commit
refactor: simplify
  • Loading branch information
JounQin committed Mar 18, 2025
commit 2528b37bedea46330a70ee016ccd91a08f51862c
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"
}
]
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)
)
}
30 changes: 4 additions & 26 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 } 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 @@ -53,11 +53,6 @@ const oxcResolve = (
}
}

type IsBunModule = typeof import('is-bun-module')
let isBunModule: IsBunModule | undefined

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

export const resolve = (
source: string,
file: string,
Expand All @@ -67,24 +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 ??= module.createRequire(_filename)(
'is-bun-module',
) as IsBunModule).isBunModule(source, (bunVersion = 'latest')) ||
isBunModule.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 @@ -112,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')
Loading