Skip to content

Commit 973d147

Browse files
authored
feat: auto-detect jsconfig at root (#46)
* test: add cases for `jsconfig` * feat: auto-detect `jsconfig` at root * docs: improve description
1 parent 88a6c8e commit 973d147

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ Default options see [normalizeOptions.ts](./src/normalizeOptions.ts)
9999

100100
More info see [oxc-resolver](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#options)
101101

102-
If you use `TypeScript`, you can set `tsconfig.configFile` to specify the path of `tsconfig.json`. If there is a `tsconfig.json` in the root of your workspace, it will be set automatically by default.
102+
#### Feature
103+
104+
The resolver will automatically detect `jsconfig.json` and `tsconfig.json` in the root (`process.cwd()`).
103105

104106
## Who is using?
105107

src/normalizeOptions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,20 @@ const defaultOptions: NapiResolveOptions = {
6868
roots: [cwd()],
6969
}
7070

71+
function findConfigFile(files: string[]) {
72+
while (files.length) {
73+
const file = files.shift()!
74+
const absPath = path.resolve(cwd(), file)
75+
if (fs.existsSync(absPath)) {
76+
return absPath
77+
}
78+
}
79+
}
80+
7181
export function normalizeOptions(options: NapiResolveOptions | null = {}): NapiResolveOptions {
7282
if (!options?.tsconfig) {
73-
const configFile = path.resolve(cwd(), './tsconfig.json')
74-
if (fs.existsSync(configFile)) {
83+
const configFile = findConfigFile(['tsconfig.json', 'jsconfig.json'])
84+
if (configFile) {
7585
defaultOptions.tsconfig = {
7686
configFile,
7787
references: 'auto',

tests/source/fixtures/jsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"#test/*": ["src/*"]
6+
},
7+
},
8+
}

tests/source/fixtures/src/a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1

tests/source/jsconfig.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { resolve } from 'node:path'
2+
import { describe, expect, it } from 'vitest'
3+
import { createOxcImportResolver } from '../../src/index'
4+
5+
const rs = (...p: string[]) => resolve(import.meta.dirname, ...p)
6+
7+
const resolver = createOxcImportResolver({
8+
tsconfig: {
9+
configFile: rs('./fixtures/jsconfig.json'),
10+
},
11+
})
12+
13+
describe('jsconfig', () => {
14+
it('should work', () => {
15+
expect(resolver.resolve('#test/a', rs('./fixtures/index.js'))).toEqual({
16+
found: true,
17+
path: rs('./fixtures/src/a.js'),
18+
})
19+
})
20+
})

tests/source/resolve.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('absolute', () => {
5555
expectResolve('index.ts', false)
5656
})
5757

58-
describe('alias', () => {
58+
describe('tsconfig alias', () => {
5959
expectResolve('@/index.ts', true)
6060
expectResolve('@/index', true)
6161
})

0 commit comments

Comments
 (0)