Skip to content

feat: auto-detect jsconfig at root #46

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

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
})
Loading