Skip to content

Commit 16b9be3

Browse files
committedMar 11, 2025··
feat: support resolving specific id for bundle dts
1 parent f47a3c1 commit 16b9be3

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed
 

‎src/features/dts.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export async function bundleDts(
3030
format: NormalizedFormat,
3131
pkg?: PackageJson,
3232
): Promise<void> {
33-
typeAsserts<IsolatedDeclOptions>(options.dts)
34-
typeAsserts<BundleDtsOptions>(options.bundleDts)
33+
const { dts, bundleDts } = options
34+
typeAsserts<IsolatedDeclOptions>(dts)
35+
typeAsserts<BundleDtsOptions>(bundleDts)
3536

3637
const ext = jsExtension.replace('j', 't')
3738
const dtsOutDir = path.resolve(options.outDir, getTempDtsDir(format))
@@ -41,6 +42,7 @@ export async function bundleDts(
4142
path.resolve(dtsOutDir, `${key}.d.${ext}`),
4243
]),
4344
)
45+
4446
const build = await rollup({
4547
input: dtsEntry,
4648
external: options.external,
@@ -51,10 +53,13 @@ export async function bundleDts(
5153
},
5254
plugins: [
5355
ExternalPlugin(options, pkg) as any,
54-
options.bundleDts.resolve && ResolveDtsPlugin(),
56+
bundleDts.resolve &&
57+
ResolveDtsPlugin(
58+
bundleDts.resolve !== true ? bundleDts.resolve : undefined,
59+
),
5560
DtsPlugin({
5661
compilerOptions: {
57-
...options.bundleDts.compilerOptions,
62+
...bundleDts.compilerOptions,
5863
declaration: true,
5964
noEmit: false,
6065
emitDeclarationOnly: true,
@@ -70,7 +75,7 @@ export async function bundleDts(
7075
})
7176

7277
let outDir = options.outDir
73-
const extraOutdir = options.dts.extraOutdir
78+
const extraOutdir = dts.extraOutdir
7479
if (extraOutdir) {
7580
outDir = path.resolve(outDir, extraOutdir)
7681
}
@@ -84,7 +89,7 @@ export async function bundleDts(
8489
}
8590

8691
let resolver: ResolverFactory | undefined
87-
export function ResolveDtsPlugin(): Plugin {
92+
export function ResolveDtsPlugin(resolveOnly?: Array<string | RegExp>): Plugin {
8893
return {
8994
name: 'resolve-dts',
9095
buildStart() {
@@ -99,6 +104,17 @@ export function ResolveDtsPlugin(): Plugin {
99104
if (id[0] === '.' || path.isAbsolute(id)) return
100105
if (/\0/.test(id)) return
101106

107+
if (resolveOnly) {
108+
const shouldResolve = resolveOnly.some((value) => {
109+
if (typeof value === 'string') return value === id
110+
return value.test(id)
111+
})
112+
if (!shouldResolve) {
113+
debug('skipped by matching resolveOnly: %s', id)
114+
return
115+
}
116+
}
117+
102118
const directory = importer ? path.dirname(importer) : process.cwd()
103119
debug('Resolving:', id, 'from:', directory)
104120
const { path: resolved } = await resolver!.async(directory, id)

‎src/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import type { ConfigEnv, UserConfigExport as ViteUserConfigExport } from 'vite'
2929
export type Sourcemap = boolean | 'inline' | 'hidden'
3030

3131
export interface BundleDtsOptions {
32-
resolve?: boolean
32+
/** Resolve external types used in dts files from node_modules */
33+
resolve?: boolean | (string | RegExp)[]
3334
compilerOptions?: CompilerOptions
3435
}
3536

‎tests/__snapshots__/resolve-dependency-for-dts.snap.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## index.d.mts
22

33
```mts
4+
export * from 'consola';
5+
46
interface GlobOptions {
57
absolute?: boolean;
68
cwd?: string;

‎tests/index.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ test('fromVite', async (context) => {
176176

177177
test('resolve dependency for dts', async (context) => {
178178
const files = {
179-
'index.ts': `export type { GlobOptions } from 'tinyglobby'`,
179+
'index.ts': `export type { GlobOptions } from 'tinyglobby'
180+
export type * from 'consola'`,
180181
}
181-
await testBuild(context, files, { dts: true, bundleDts: { resolve: true } })
182+
const { snapshot } = await testBuild(context, files, {
183+
dts: true,
184+
bundleDts: { resolve: ['tinyglobby'] },
185+
})
186+
expect(snapshot).contain(`export * from 'consola'`)
182187
})

0 commit comments

Comments
 (0)
Please sign in to comment.