From 973d147436adaa220a99471f67297e87ee3123f3 Mon Sep 17 00:00:00 2001 From: Vida Xie Date: Fri, 20 Dec 2024 15:06:28 +0800 Subject: [PATCH] feat: auto-detect `jsconfig` at root (#46) * test: add cases for `jsconfig` * feat: auto-detect `jsconfig` at root * docs: improve description --- README.md | 4 +++- src/normalizeOptions.ts | 14 ++++++++++++-- tests/source/fixtures/jsconfig.json | 8 ++++++++ tests/source/fixtures/src/a.js | 1 + tests/source/jsconfig.test.ts | 20 ++++++++++++++++++++ tests/source/resolve.test.ts | 2 +- 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/source/fixtures/jsconfig.json create mode 100644 tests/source/fixtures/src/a.js create mode 100644 tests/source/jsconfig.test.ts diff --git a/README.md b/README.md index 95ce8e5..1257ce1 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/src/normalizeOptions.ts b/src/normalizeOptions.ts index 987edc6..57d1ef0 100644 --- a/src/normalizeOptions.ts +++ b/src/normalizeOptions.ts @@ -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', diff --git a/tests/source/fixtures/jsconfig.json b/tests/source/fixtures/jsconfig.json new file mode 100644 index 0000000..0cb99a5 --- /dev/null +++ b/tests/source/fixtures/jsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "#test/*": ["src/*"] + }, + }, +} diff --git a/tests/source/fixtures/src/a.js b/tests/source/fixtures/src/a.js new file mode 100644 index 0000000..4171549 --- /dev/null +++ b/tests/source/fixtures/src/a.js @@ -0,0 +1 @@ +export const a = 1 diff --git a/tests/source/jsconfig.test.ts b/tests/source/jsconfig.test.ts new file mode 100644 index 0000000..6c116dc --- /dev/null +++ b/tests/source/jsconfig.test.ts @@ -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'), + }) + }) +}) diff --git a/tests/source/resolve.test.ts b/tests/source/resolve.test.ts index 89fb53a..56f20e7 100644 --- a/tests/source/resolve.test.ts +++ b/tests/source/resolve.test.ts @@ -55,7 +55,7 @@ describe('absolute', () => { expectResolve('index.ts', false) }) -describe('alias', () => { +describe('tsconfig alias', () => { expectResolve('@/index.ts', true) expectResolve('@/index', true) })