Skip to content

Commit 060f0e7

Browse files
committed
chore: update
1 parent 424d744 commit 060f0e7

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export async function processDtsFiles(
378378
);
379379
}
380380

381-
const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.ts')), {
381+
const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.{ts,cts,mts}')), {
382382
absolute: true,
383383
});
384384

@@ -399,9 +399,6 @@ export async function processDtsFiles(
399399
rootDir,
400400
);
401401
}
402-
403-
const newFile = file.replace('.d.ts', dtsExtension);
404-
await fsP.rename(file, newFile);
405402
} catch (error) {
406403
logger.error(`Failed to rename declaration file ${file}: ${error}`);
407404
}

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
}),

tests/integration/dts/index.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ describe('dts when bundle: false', () => {
9090

9191
expect(files.cjs).toMatchInlineSnapshot(`
9292
[
93-
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/index.d.cts",
94-
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/sum.d.cts",
95-
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/numbers.d.cts",
96-
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/strings.d.cts",
93+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.cts",
94+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.ts",
95+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.cts",
96+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.ts",
97+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.cts",
98+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.ts",
99+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.cts",
100+
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.ts",
97101
]
98102
`);
99103
});

0 commit comments

Comments
 (0)