Skip to content

Commit a544010

Browse files
committedMar 10, 2025
feat: resolve dts dependency
closes #75
1 parent 25f1512 commit a544010

File tree

5 files changed

+177
-2
lines changed

5 files changed

+177
-2
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"consola": "^3.4.0",
7373
"debug": "^4.4.0",
7474
"diff": "^7.0.0",
75+
"oxc-resolver": "^5.0.0",
7576
"pkg-types": "^2.1.0",
7677
"rolldown": "^1.0.0-beta.3",
7778
"rollup": "^4.35.0",

‎pnpm-lock.yaml

+110
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/features/dts.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import path from 'node:path'
2+
import process from 'node:process'
3+
import { ResolverFactory } from 'oxc-resolver'
24
import { rollup } from 'rollup'
35
import DtsPlugin from 'rollup-plugin-dts'
4-
import { fsRemove } from '../utils/fs'
6+
import { fsExists, fsRemove } from '../utils/fs'
57
import { typeAsserts } from '../utils/general'
68
import type { NormalizedFormat, ResolvedOptions } from '../options'
79
import type { OutputExtension } from './output'
@@ -13,6 +15,8 @@ export function getTempDtsDir(format: NormalizedFormat) {
1315
return `${TEMP_DTS_DIR}-${format}`
1416
}
1517

18+
let resolver: ResolverFactory | undefined
19+
1620
export async function bundleDts(
1721
options: ResolvedOptions,
1822
jsExtension: OutputExtension,
@@ -28,14 +32,41 @@ export async function bundleDts(
2832
path.resolve(dtsOutDir, `${key}.d.${ext}`),
2933
]),
3034
)
35+
resolver ||= new ResolverFactory({
36+
mainFields: ['types'],
37+
conditionNames: ['types', 'typings', 'import', 'require'],
38+
extensions: ['.d.ts', '.ts'],
39+
modules: ['node_modules', 'node_modules/@types'],
40+
})
3141
const build = await rollup({
3242
input: dtsEntry,
3343
onLog(level, log, defaultHandler) {
3444
if (log.code !== 'EMPTY_BUNDLE' && log.code !== 'UNRESOLVED_IMPORT') {
3545
defaultHandler(level, log)
3646
}
3747
},
38-
plugins: [DtsPlugin()],
48+
plugins: [
49+
{
50+
name: 'resolve-dts',
51+
async resolveId(id, importer) {
52+
if (id[0] === '.' || path.isAbsolute(id)) return
53+
if (/\0/.test(id)) return
54+
55+
const directory = importer ? path.dirname(importer) : process.cwd()
56+
const { path: resolved } = await resolver!.async(directory, id)
57+
if (!resolved) return
58+
59+
// try to resolve same-name d.ts
60+
if (/[cm]?jsx?$/.test(resolved)) {
61+
const dts = resolved.replace(/\.([cm]?)jsx?$/, '.d.$1ts')
62+
return (await fsExists(dts)) ? dts : undefined
63+
}
64+
65+
return resolved
66+
},
67+
},
68+
DtsPlugin(),
69+
],
3970
})
4071

4172
let outDir = options.outDir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## index.d.mts
2+
3+
```mts
4+
interface GlobOptions {
5+
absolute?: boolean;
6+
cwd?: string;
7+
patterns?: string | string[];
8+
ignore?: string | string[];
9+
dot?: boolean;
10+
deep?: number;
11+
followSymbolicLinks?: boolean;
12+
caseSensitiveMatch?: boolean;
13+
expandDirectories?: boolean;
14+
onlyDirectories?: boolean;
15+
onlyFiles?: boolean;
16+
debug?: boolean;
17+
}
18+
19+
export type { GlobOptions };
20+
21+
```
22+
## index.mjs
23+
24+
```mjs
25+
26+
```

‎tests/index.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,10 @@ test('fromVite', async (context) => {
170170
},
171171
])
172172
})
173+
174+
test('inline dependency for dts', async (context) => {
175+
const files = {
176+
'index.ts': `export type { GlobOptions } from 'tinyglobby'`,
177+
}
178+
await testBuild(context, files, { dts: true })
179+
})

0 commit comments

Comments
 (0)
Please sign in to comment.