Skip to content

Commit

Permalink
feat: auto-detect jsconfig at root (#46)
Browse files Browse the repository at this point in the history
* test: add cases for `jsconfig`

* feat: auto-detect `jsconfig` at root

* docs: improve description
  • Loading branch information
9romise authored Dec 20, 2024
1 parent 88a6c8e commit 973d147
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ Default options see [normalizeOptions.ts](./src/normalizeOptions.ts)

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

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.
#### Feature

The resolver will automatically detect `jsconfig.json` and `tsconfig.json` in the root (`process.cwd()`).

## Who is using?

Expand Down
14 changes: 12 additions & 2 deletions src/normalizeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ const defaultOptions: NapiResolveOptions = {
roots: [cwd()],
}

function findConfigFile(files: string[]) {
while (files.length) {
const file = files.shift()!
const absPath = path.resolve(cwd(), file)
if (fs.existsSync(absPath)) {
return absPath
}
}
}

export function normalizeOptions(options: NapiResolveOptions | null = {}): NapiResolveOptions {
if (!options?.tsconfig) {
const configFile = path.resolve(cwd(), './tsconfig.json')
if (fs.existsSync(configFile)) {
const configFile = findConfigFile(['tsconfig.json', 'jsconfig.json'])
if (configFile) {
defaultOptions.tsconfig = {
configFile,
references: 'auto',
Expand Down
8 changes: 8 additions & 0 deletions tests/source/fixtures/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"#test/*": ["src/*"]
},
},
}
1 change: 1 addition & 0 deletions tests/source/fixtures/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1
20 changes: 20 additions & 0 deletions tests/source/jsconfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import { createOxcImportResolver } from '../../src/index'

const rs = (...p: string[]) => resolve(import.meta.dirname, ...p)

const resolver = createOxcImportResolver({
tsconfig: {
configFile: rs('./fixtures/jsconfig.json'),
},
})

describe('jsconfig', () => {
it('should work', () => {
expect(resolver.resolve('#test/a', rs('./fixtures/index.js'))).toEqual({
found: true,
path: rs('./fixtures/src/a.js'),
})
})
})
2 changes: 1 addition & 1 deletion tests/source/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('absolute', () => {
expectResolve('index.ts', false)
})

describe('alias', () => {
describe('tsconfig alias', () => {
expectResolve('@/index.ts', true)
expectResolve('@/index', true)
})

0 comments on commit 973d147

Please sign in to comment.