Skip to content

Commit 2a89c38

Browse files
committedMar 10, 2025··
feat: expose ResolveDtsPlugin
1 parent 13baadd commit 2a89c38

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed
 

‎src/features/dts.ts

+33-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import process from 'node:process'
33
import { ResolverFactory } from 'oxc-resolver'
4-
import { rollup } from 'rollup'
4+
import { rollup, type Plugin } from 'rollup'
55
import DtsPlugin from 'rollup-plugin-dts'
66
import { fsExists, fsRemove } from '../utils/fs'
77
import { typeAsserts } from '../utils/general'
@@ -17,8 +17,6 @@ export function getTempDtsDir(format: NormalizedFormat) {
1717
return `${TEMP_DTS_DIR}-${format}`
1818
}
1919

20-
let resolver: ResolverFactory | undefined
21-
2220
export async function bundleDts(
2321
options: ResolvedOptions,
2422
jsExtension: OutputExtension,
@@ -35,12 +33,6 @@ export async function bundleDts(
3533
path.resolve(dtsOutDir, `${key}.d.${ext}`),
3634
]),
3735
)
38-
resolver ||= new ResolverFactory({
39-
mainFields: ['types'],
40-
conditionNames: ['types', 'typings', 'import', 'require'],
41-
extensions: ['.d.ts', '.ts'],
42-
modules: ['node_modules', 'node_modules/@types'],
43-
})
4436
const build = await rollup({
4537
input: dtsEntry,
4638
external: options.external,
@@ -51,25 +43,7 @@ export async function bundleDts(
5143
},
5244
plugins: [
5345
ExternalPlugin(options, pkg) as any,
54-
{
55-
name: 'resolve-dts',
56-
async resolveId(id, importer) {
57-
if (id[0] === '.' || path.isAbsolute(id)) return
58-
if (/\0/.test(id)) return
59-
60-
const directory = importer ? path.dirname(importer) : process.cwd()
61-
const { path: resolved } = await resolver!.async(directory, id)
62-
if (!resolved) return
63-
64-
// try to resolve same-name d.ts
65-
if (/[cm]?jsx?$/.test(resolved)) {
66-
const dts = resolved.replace(/\.([cm]?)jsx?$/, '.d.$1ts')
67-
return (await fsExists(dts)) ? dts : undefined
68-
}
69-
70-
return resolved
71-
},
72-
},
46+
ResolveDtsPlugin(),
7347
DtsPlugin(),
7448
],
7549
})
@@ -87,3 +61,34 @@ export async function bundleDts(
8761
})
8862
await fsRemove(dtsOutDir)
8963
}
64+
65+
let resolver: ResolverFactory | undefined
66+
export function ResolveDtsPlugin(): Plugin {
67+
return {
68+
name: 'resolve-dts',
69+
buildStart() {
70+
resolver ||= new ResolverFactory({
71+
mainFields: ['types'],
72+
conditionNames: ['types', 'typings', 'import', 'require'],
73+
extensions: ['.d.ts', '.ts'],
74+
modules: ['node_modules', 'node_modules/@types'],
75+
})
76+
},
77+
async resolveId(id, importer) {
78+
if (id[0] === '.' || path.isAbsolute(id)) return
79+
if (/\0/.test(id)) return
80+
81+
const directory = importer ? path.dirname(importer) : process.cwd()
82+
const { path: resolved } = await resolver!.async(directory, id)
83+
if (!resolved) return
84+
85+
// try to resolve same-name d.ts
86+
if (/[cm]?jsx?$/.test(resolved)) {
87+
const dts = resolved.replace(/\.([cm]?)jsx?$/, '.d.$1ts')
88+
return (await fsExists(dts)) ? dts : undefined
89+
}
90+
91+
return resolved
92+
},
93+
}
94+
}

‎src/plugins.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { ExternalPlugin } from './features/external'
2+
export { ResolveDtsPlugin } from './features/dts'

0 commit comments

Comments
 (0)
Please sign in to comment.