Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/dts-resolver. Resolves TypeScript declaration files for dependencies.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153import path from 'node:path'import process from 'node:process'import * as ts from 'typescript'import { describe, expect, test } from 'vitest'import { createResolver } from '../src'
const resolver = createResolver({ resolveNodeModules: true })
const fixtures = path.resolve(import.meta.dirname, 'fixtures')
function buildExts(name: string) { return [ name, `${name}.js`, `${name}.mjs`, `${name}.cjs`, `${name}.ts`, `${name}.mts`, `${name}.cts`, `${name}.jsx`, `${name}.tsx`, `${name}.d`, ]}
describe('resolver', () => { const CASES: Array<string> = [ 'node', 'magic-string-ast', 'magic-string', 'fast-glob', '@babel/parser', 'yargs', 'debug', 'vue', '@vue/reactivity', 'pathe', 'oxc-resolver', 'eslint',
'./fixtures/js-with-dts', ...buildExts('./fixtures/js-with-dts/index'), ...buildExts('./fixtures/same-name/main'), './fixtures/jsx', './fixtures/jsx.js', './fixtures/jsx.jsx', './fixtures/jsx.ts', './fixtures/jsx.tsx',
'./fixtures/tsx', './fixtures/jsx.ts', './fixtures/jsx.tsx', './fixtures/jsx.js', './fixtures/jsx.jsx',
'../src', '../src/index', '../src/index.ts', '../src/index.js',
'../package.json', '../tsconfig.json', '../eslint.config.js',
'node:fs', 'whatever', '../package', '../README.md', '../pnpm-lock.yaml', '../.gitignore', ]
test.each(CASES)('%s', (id) => { const resolved = resolver(id, import.meta.filename) const tsResolved = tsResolve(id, import.meta.filename) expectPath(resolved).toMatchSnapshot() expect(resolved).toBe(tsResolved) })
const FAILS: Array<string> = [ '@babel/generator', // definitely typed '@babel/types', // typesVersions field ]
test.fails.each(FAILS)('%s fails', (id) => { const resolved = resolver(id, import.meta.filename) const tsResolved = tsResolve(id, import.meta.filename) expectPath(resolved).toMatchSnapshot() expect(resolved).toBe(tsResolved) })
test('@babel/types', () => { expectPath(resolver('@babel/types')).toMatchInlineSnapshot( `"@babel/types/lib/index-legacy.d.ts"`, ) })
test('paths', () => { const tsconfig = path.resolve(import.meta.dirname, 'tsconfig-test.json') const resolver = createResolver({ tsconfig }) expect(resolver('lib/alias.ts', import.meta.filename)).toBe( tsResolve('lib/alias.ts', import.meta.filename, tsconfig), ) })
test('no baseUrl', () => { const root = path.resolve(fixtures, 'no-baseurl') const tsconfig = path.resolve(root, 'tsconfig.json') const filename = path.resolve(root, 'index.ts') const resolver = createResolver({ tsconfig, resolveNodeModules: true, })
expect(resolver('vite', filename)).toBe( tsResolve('vite', filename, tsconfig), ) })})
function expectPath(value: string | null) { if (typeof value === 'string') value = path .relative(process.cwd(), value) .replaceAll('\\', '/') .replaceAll(/.+node_modules\//g, '') return expect(value)}
function tsResolve(id: string, importer: string, tsconfig?: string) { let compilerOptions: ts.CompilerOptions = {} if (tsconfig) { const readResult = ts.readConfigFile(tsconfig, ts.sys.readFile) const config = ts.parseJsonConfigFileContent( readResult.config, ts.sys, path.dirname(tsconfig), ) compilerOptions = config.options } const resolved = ts.resolveModuleName( id, importer, { ...compilerOptions, moduleResolution: ts.ModuleResolutionKind.Bundler, resolveJsonModule: true, }, ts.sys, ) return resolved.resolvedModule?.resolvedFileName ?? null}