Skip to content

Commit d0e9dac

Browse files
committed
chore: update
1 parent 6aef338 commit d0e9dac

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

packages/plugin-dts/src/dts.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
129129
userExternals,
130130
banner,
131131
footer,
132-
redirect,
132+
redirect = {
133+
path: true,
134+
extension: false,
135+
},
133136
} = data;
134137
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
135138

@@ -253,10 +256,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
253256
tsConfigResult,
254257
declarationDir,
255258
dtsExtension,
259+
redirect,
256260
rootDir,
257261
banner,
258262
footer,
259-
redirect,
260263
},
261264
onComplete,
262265
bundle,

packages/plugin-dts/src/tsc.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export type EmitDtsOptions = {
1212
declarationDir: string;
1313
dtsExtension: string;
1414
rootDir: string;
15+
redirect: DtsRedirect;
1516
banner?: string;
1617
footer?: string;
17-
redirect?: DtsRedirect;
1818
};
1919

2020
async function handleDiagnosticsAndProcessFiles(
@@ -24,10 +24,10 @@ async function handleDiagnosticsAndProcessFiles(
2424
bundle: boolean,
2525
declarationDir: string,
2626
dtsExtension: string,
27+
redirect: DtsRedirect,
2728
rootDir: string,
2829
banner?: string,
2930
footer?: string,
30-
redirect?: DtsRedirect,
3131
name?: string,
3232
): Promise<void> {
3333
const diagnosticMessages: string[] = [];
@@ -45,11 +45,11 @@ async function handleDiagnosticsAndProcessFiles(
4545
bundle,
4646
declarationDir,
4747
dtsExtension,
48+
redirect,
4849
configPath,
4950
rootDir,
5051
banner,
5152
footer,
52-
redirect,
5353
);
5454

5555
if (diagnosticMessages.length) {
@@ -147,11 +147,11 @@ export async function emitDts(
147147
bundle,
148148
declarationDir,
149149
dtsExtension,
150+
redirect,
150151
configPath,
151152
rootDir,
152153
banner,
153154
footer,
154-
redirect,
155155
);
156156
}
157157

@@ -162,11 +162,11 @@ export async function emitDts(
162162
bundle,
163163
declarationDir,
164164
dtsExtension,
165+
redirect,
165166
configPath,
166167
rootDir,
167168
banner,
168169
footer,
169-
redirect,
170170
);
171171
}
172172
};
@@ -201,10 +201,10 @@ export async function emitDts(
201201
bundle,
202202
declarationDir,
203203
dtsExtension,
204+
redirect,
204205
rootDir,
205206
banner,
206207
footer,
207-
redirect,
208208
name,
209209
);
210210
} else if (!build && compilerOptions.composite) {
@@ -235,10 +235,10 @@ export async function emitDts(
235235
bundle,
236236
declarationDir,
237237
dtsExtension,
238+
redirect,
238239
rootDir,
239240
banner,
240241
footer,
241-
redirect,
242242
name,
243243
);
244244
} else {
@@ -269,11 +269,11 @@ export async function emitDts(
269269
bundle,
270270
declarationDir,
271271
dtsExtension,
272+
redirect,
272273
configPath,
273274
rootDir,
274275
banner,
275276
footer,
276-
redirect,
277277
);
278278

279279
if (errorNumber > 0) {

packages/plugin-dts/src/utils.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { type RsbuildConfig, logger } from '@rsbuild/core';
1515
import MagicString from 'magic-string';
1616
import color from 'picocolors';
1717
import { convertPathToPattern, glob } from 'tinyglobby';
18-
import { createMatchPath, loadConfig } from 'tsconfig-paths';
18+
import { type MatchPath, createMatchPath, loadConfig } from 'tsconfig-paths';
1919
import ts from 'typescript';
2020
import 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(
171167
export 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\.ts$/, '.js')
247223
.replace(/\.d\.cts$/, '.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

Comments
 (0)