Skip to content

Commit

Permalink
feat: add bun built-in modules support
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 25, 2024
1 parent acaaddd commit 9a78e44
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"prepare": "simple-git-hooks",
"release": "changeset publish",
"test": "run-p 'test:*'",
"test:bun": "eslint --ext ts,tsx tests/bun",
"test:multipleEslintrcs": "eslint --ext ts,tsx tests/multipleEslintrcs",
"test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs",
"test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension",
Expand Down
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ let resolver: Resolver | undefined
const digestHashObject = (value: object | null | undefined) =>
hashObject(value ?? {}).digest('hex')

let bunCoreModules: Set<string> | undefined
const getBunCoreModules = (): Set<string> => {
if (!bunCoreModules) {
bunCoreModules = new Set<string>()

const { found, path } = resolve('bun-types', 'bun-types')
if (found && path) {
const regex = /declare module ["'](bun:\w+)["']/g
const content = fs.readFileSync(path, 'utf8')
let match: RegExpExecArray | null

while ((match = regex.exec(content)) !== null) {
// The first captured group is at index 1
if (match[1]) {
bunCoreModules.add(match[1])
}
}
}
}

return bunCoreModules
}

/**
* @param source the module to resolve; i.e './some-module'
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
Expand Down Expand Up @@ -169,6 +192,16 @@ export function resolve(
}
}

// match against bun core modules
if (getBunCoreModules().has(source)) {
log('matched bun core:', source)

return {
found: true,
path: null,
}
}

initMappers(cachedOptions)

const mappedPath = getMappedPath(source, file, cachedOptions.extensions, true)
Expand Down
1 change: 1 addition & 0 deletions tests/bun/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig.cjs')(__dirname)
5 changes: 5 additions & 0 deletions tests/bun/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable */
// @ts-expect-error
import { Database } from 'bun:sqlite'

export default new Database()
4 changes: 4 additions & 0 deletions tests/bun/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"files": ["index.ts"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"module": "Node16",
"outDir": "./lib"
},
"include": ["./src", "./shim.d.ts"]
"include": ["./src", "./shim.d.ts"],
"exclude": ["./node_modules/bun-types/**/*"]
}

0 comments on commit 9a78e44

Please sign in to comment.