Skip to content

Commit 6e9f930

Browse files
authored
fix: use writeFile of ts api to rename declaration files (#933)
1 parent 424d744 commit 6e9f930

File tree

13 files changed

+195
-16
lines changed

13 files changed

+195
-16
lines changed

packages/plugin-dts/src/tsc.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,44 @@ export async function emitDts(
165165
}
166166
};
167167

168-
const system = { ...ts.sys };
168+
const renameDtsFile = (fileName: string): string => {
169+
if (bundle) {
170+
return fileName;
171+
}
172+
return fileName.replace(/\.d\.ts$/, dtsExtension);
173+
};
174+
175+
const system: ts.System = {
176+
...ts.sys,
177+
writeFile: (fileName, contents, writeByteOrderMark) => {
178+
ts.sys.writeFile(renameDtsFile(fileName), contents, writeByteOrderMark);
179+
},
180+
};
169181

170182
// build mode
171183
if (!isWatch) {
172184
// normal build - npx tsc
173185
if (!build && !compilerOptions.composite) {
174-
const host: ts.CompilerHost = ts.createCompilerHost(compilerOptions);
186+
const originHost: ts.CompilerHost =
187+
ts.createCompilerHost(compilerOptions);
188+
const host: ts.CompilerHost = {
189+
...originHost,
190+
writeFile: (
191+
fileName,
192+
contents,
193+
writeByteOrderMark,
194+
onError,
195+
sourceFiles,
196+
) => {
197+
originHost.writeFile(
198+
renameDtsFile(fileName),
199+
contents,
200+
writeByteOrderMark,
201+
onError,
202+
sourceFiles,
203+
);
204+
},
205+
};
175206

176207
const program: ts.Program = ts.createProgram({
177208
rootNames: fileNames,
@@ -202,8 +233,26 @@ export async function emitDts(
202233
);
203234
} else if (!build && compilerOptions.composite) {
204235
// incremental build with composite true - npx tsc
205-
const host: ts.CompilerHost =
236+
const originHost: ts.CompilerHost =
206237
ts.createIncrementalCompilerHost(compilerOptions);
238+
const host: ts.CompilerHost = {
239+
...originHost,
240+
writeFile: (
241+
fileName,
242+
contents,
243+
writeByteOrderMark,
244+
onError,
245+
sourceFiles,
246+
) => {
247+
originHost.writeFile(
248+
renameDtsFile(fileName),
249+
contents,
250+
writeByteOrderMark,
251+
onError,
252+
sourceFiles,
253+
);
254+
},
255+
};
207256

208257
const program = ts.createIncrementalProgram({
209258
rootNames: fileNames,

packages/plugin-dts/src/utils.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,7 @@ export async function processDtsFiles(
378378
);
379379
}
380380

381-
const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.ts')), {
382-
absolute: true,
383-
});
381+
const dtsFiles = await globDtsFiles(dir);
384382

385383
await Promise.all(
386384
dtsFiles.map(async (file) => {
@@ -399,9 +397,6 @@ export async function processDtsFiles(
399397
rootDir,
400398
);
401399
}
402-
403-
const newFile = file.replace('.d.ts', dtsExtension);
404-
await fsP.rename(file, newFile);
405400
} catch (error) {
406401
logger.error(`Failed to rename declaration file ${file}: ${error}`);
407402
}
@@ -471,15 +466,19 @@ export async function calcLongestCommonPath(
471466
return lca;
472467
}
473468

474-
export async function cleanDtsFiles(dir: string): Promise<void> {
469+
export const globDtsFiles = async (dir: string): Promise<string[]> => {
475470
const patterns = ['/**/*.d.ts', '/**/*.d.cts', '/**/*.d.mts'];
476-
const files = await Promise.all(
471+
const dtsFiles = await Promise.all(
477472
patterns.map((pattern) =>
478473
glob(convertPath(join(dir, pattern)), { absolute: true }),
479474
),
480475
);
481476

482-
const allFiles = files.flat();
477+
return dtsFiles.flat();
478+
};
479+
480+
export async function cleanDtsFiles(dir: string): Promise<void> {
481+
const allFiles = await globDtsFiles(dir);
483482

484483
await Promise.all(allFiles.map((file) => fsP.rm(file, { force: true })));
485484
}

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-build-auto-extension-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleCjsConfig({
7+
bundle: false,
8+
dts: {
9+
autoExtension: true,
10+
distPath: './dist/types',
11+
bundle: false,
12+
build: true,
13+
},
14+
}),
15+
],
16+
source: {
17+
entry: {
18+
index: ['./src/**'],
19+
},
20+
},
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './sum';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const num1 = 1;
2+
export const num2 = 2;
3+
export const num3 = 3;
4+
5+
export const str1 = 'str1';
6+
export const str2 = 'str2';
7+
export const str3 = 'str3';
8+
9+
export const numSum = num1 + num2 + num3;
10+
export const strSum = str1 + str2 + str3;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"rootDir": "src",
6+
"declaration": true,
7+
"declarationDir": "./dist/types"
8+
},
9+
"include": ["src"],
10+
"references": [
11+
{
12+
"path": "../__references__"
13+
}
14+
]
15+
}

tests/integration/dts/bundle-false/auto-extension/rslib.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ export default defineConfig({
55
lib: [
66
generateBundleEsmConfig({
77
bundle: false,
8+
dts: {
9+
autoExtension: true,
10+
distPath: './dist/types',
11+
bundle: false,
12+
},
813
}),
914
generateBundleCjsConfig({
1015
bundle: false,
1116
dts: {
1217
autoExtension: true,
18+
distPath: './dist/types',
1319
bundle: false,
1420
},
1521
}),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-composite-auto-extension-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}

0 commit comments

Comments
 (0)