Skip to content

Commit 9a78e44

Browse files
committed
feat: add bun built-in modules support
1 parent acaaddd commit 9a78e44

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"prepare": "simple-git-hooks",
5252
"release": "changeset publish",
5353
"test": "run-p 'test:*'",
54+
"test:bun": "eslint --ext ts,tsx tests/bun",
5455
"test:multipleEslintrcs": "eslint --ext ts,tsx tests/multipleEslintrcs",
5556
"test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs",
5657
"test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension",

src/index.ts

+33
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ let resolver: Resolver | undefined
117117
const digestHashObject = (value: object | null | undefined) =>
118118
hashObject(value ?? {}).digest('hex')
119119

120+
let bunCoreModules: Set<string> | undefined
121+
const getBunCoreModules = (): Set<string> => {
122+
if (!bunCoreModules) {
123+
bunCoreModules = new Set<string>()
124+
125+
const { found, path } = resolve('bun-types', 'bun-types')
126+
if (found && path) {
127+
const regex = /declare module ["'](bun:\w+)["']/g
128+
const content = fs.readFileSync(path, 'utf8')
129+
let match: RegExpExecArray | null
130+
131+
while ((match = regex.exec(content)) !== null) {
132+
// The first captured group is at index 1
133+
if (match[1]) {
134+
bunCoreModules.add(match[1])
135+
}
136+
}
137+
}
138+
}
139+
140+
return bunCoreModules
141+
}
142+
120143
/**
121144
* @param source the module to resolve; i.e './some-module'
122145
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
@@ -169,6 +192,16 @@ export function resolve(
169192
}
170193
}
171194

195+
// match against bun core modules
196+
if (getBunCoreModules().has(source)) {
197+
log('matched bun core:', source)
198+
199+
return {
200+
found: true,
201+
path: null,
202+
}
203+
}
204+
172205
initMappers(cachedOptions)
173206

174207
const mappedPath = getMappedPath(source, file, cachedOptions.extensions, true)

tests/bun/.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../baseEslintConfig.cjs')(__dirname)

tests/bun/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-disable */
2+
// @ts-expect-error
3+
import { Database } from 'bun:sqlite'
4+
5+
export default new Database()

tests/bun/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compilerOptions": {},
3+
"files": ["index.ts"]
4+
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"module": "Node16",
55
"outDir": "./lib"
66
},
7-
"include": ["./src", "./shim.d.ts"]
7+
"include": ["./src", "./shim.d.ts"],
8+
"exclude": ["./node_modules/bun-types/**/*"]
89
}

0 commit comments

Comments
 (0)