@@ -15,7 +15,7 @@ import { type RsbuildConfig, logger } from '@rsbuild/core';
1515import MagicString from 'magic-string' ;
1616import color from 'picocolors' ;
1717import { convertPathToPattern , glob } from 'tinyglobby' ;
18- import { createMatchPath , loadConfig } from 'tsconfig-paths' ;
18+ import { type MatchPath , createMatchPath , loadConfig } from 'tsconfig-paths' ;
1919import ts from 'typescript' ;
2020import type { DtsEntry , DtsRedirect } from './index' ;
2121
@@ -148,10 +148,6 @@ export async function addBannerAndFooter(
148148 banner ?: string ,
149149 footer ?: string ,
150150) : Promise < void > {
151- if ( ! banner && ! footer ) {
152- return ;
153- }
154-
155151 const content = await fsP . readFile ( dtsFile , 'utf-8' ) ;
156152 const code = new MagicString ( content ) ;
157153
@@ -171,15 +167,11 @@ export async function addBannerAndFooter(
171167export async function redirectDtsImports (
172168 dtsFile : string ,
173169 dtsExtension : string ,
174- tsconfigPath : string ,
170+ redirect : DtsRedirect ,
171+ matchPath : MatchPath ,
175172 outDir : string ,
176173 rootDir : string ,
177- redirect ?: DtsRedirect ,
178174) : Promise < void > {
179- if ( ! redirect || ( ! redirect . path && ! redirect . extension ) ) {
180- return ;
181- }
182-
183175 const content = await fsP . readFile ( dtsFile , 'utf-8' ) ;
184176 const code = new MagicString ( content ) ;
185177 const sgNode = ( await parseAsync ( 'typescript' , content ) ) . root ( ) ;
@@ -226,22 +218,6 @@ export async function redirectDtsImports(
226218 e : matchNode . range ( ) . end . index ,
227219 } ;
228220 } ) ;
229-
230- const result = loadConfig ( tsconfigPath ) ;
231-
232- if ( result . resultType === 'failed' ) {
233- logger . error ( result . message ) ;
234- return ;
235- }
236-
237- const { absoluteBaseUrl, paths, mainFields, addMatchAll } = result ;
238- const matchPath = createMatchPath (
239- absoluteBaseUrl ,
240- paths ,
241- mainFields ,
242- addMatchAll ,
243- ) ;
244-
245221 const extensions = dtsExtension
246222 . replace ( / \. d \. t s $ / , '.js' )
247223 . replace ( / \. d \. c t s $ / , '.cjs' )
@@ -333,31 +309,56 @@ export async function processDtsFiles(
333309 bundle : boolean ,
334310 dir : string ,
335311 dtsExtension : string ,
312+ redirect : DtsRedirect ,
336313 tsconfigPath : string ,
337314 rootDir : string ,
338315 banner ?: string ,
339316 footer ?: string ,
340- redirect ?: DtsRedirect ,
341317) : Promise < void > {
342318 if ( bundle ) {
343319 return ;
344320 }
345321
322+ let matchPath : MatchPath | undefined ;
323+
324+ if ( redirect . path || redirect . extension ) {
325+ const result = loadConfig ( tsconfigPath ) ;
326+
327+ if ( result . resultType === 'failed' ) {
328+ logger . error ( result . message ) ;
329+ return ;
330+ }
331+
332+ const { absoluteBaseUrl, paths, mainFields, addMatchAll } = result ;
333+ matchPath = createMatchPath (
334+ absoluteBaseUrl ,
335+ paths ,
336+ mainFields ,
337+ addMatchAll ,
338+ ) ;
339+ }
340+
346341 const dtsFiles = await glob ( convertPath ( join ( dir , '/**/*.d.ts' ) ) , {
347342 absolute : true ,
348343 } ) ;
349344
350345 for ( const file of dtsFiles ) {
351346 try {
352- await addBannerAndFooter ( file , banner , footer ) ;
353- await redirectDtsImports (
354- file ,
355- dtsExtension ,
356- tsconfigPath ,
357- dir ,
358- rootDir ,
359- redirect ,
360- ) ;
347+ if ( banner || footer ) {
348+ await addBannerAndFooter ( file , banner , footer ) ;
349+ }
350+
351+ if ( ( redirect . path || redirect . extension ) && matchPath ) {
352+ await redirectDtsImports (
353+ file ,
354+ dtsExtension ,
355+ redirect ,
356+ matchPath ,
357+ dir ,
358+ rootDir ,
359+ ) ;
360+ }
361+
361362 const newFile = file . replace ( '.d.ts' , dtsExtension ) ;
362363 fs . renameSync ( file , newFile ) ;
363364 } catch ( error ) {
0 commit comments