Skip to content

Commit 5c8c66e

Browse files
authored
feat(dts): support emitting .d.cts.map and .d.mts.map files (#1176)
1 parent d8ed70f commit 5c8c66e

File tree

16 files changed

+341
-5
lines changed

16 files changed

+341
-5
lines changed

packages/plugin-dts/src/tsc.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,42 @@ export async function emitDts(
178178
if (bundle) {
179179
return fileName;
180180
}
181+
182+
if (fileName.endsWith('.d.ts.map')) {
183+
return fileName.replace(/\.d\.ts\.map$/, `${dtsExtension}.map`);
184+
}
185+
181186
return fileName.replace(/\.d\.ts$/, dtsExtension);
182187
};
183188

189+
const updateDeclarationMapContent = (
190+
fileName: string,
191+
content: string,
192+
): string => {
193+
if (bundle || !compilerOptions.declarationMap) {
194+
return content;
195+
}
196+
197+
if (fileName.endsWith('.d.ts')) {
198+
return content.replace(
199+
/(\/\/# sourceMappingURL=.+)\.d\.ts\.map/g,
200+
`$1${dtsExtension}.map`,
201+
);
202+
}
203+
204+
if (fileName.endsWith('.d.ts.map')) {
205+
return content.replace(/("file":"[^"]*)\.d\.ts"/g, `$1${dtsExtension}"`);
206+
}
207+
208+
return content;
209+
};
210+
184211
const system: ts.System = {
185212
...ts.sys,
186213
writeFile: (fileName, contents, writeByteOrderMark) => {
187-
ts.sys.writeFile(renameDtsFile(fileName), contents, writeByteOrderMark);
214+
const newFileName = renameDtsFile(fileName);
215+
const newContents = updateDeclarationMapContent(fileName, contents);
216+
ts.sys.writeFile(newFileName, newContents, writeByteOrderMark);
188217
},
189218
};
190219

@@ -203,9 +232,11 @@ export async function emitDts(
203232
onError,
204233
sourceFiles,
205234
) => {
235+
const newFileName = renameDtsFile(fileName);
236+
const newContents = updateDeclarationMapContent(fileName, contents);
206237
originHost.writeFile(
207-
renameDtsFile(fileName),
208-
contents,
238+
newFileName,
239+
newContents,
209240
writeByteOrderMark,
210241
onError,
211242
sourceFiles,
@@ -254,9 +285,11 @@ export async function emitDts(
254285
onError,
255286
sourceFiles,
256287
) => {
288+
const newFileName = renameDtsFile(fileName);
289+
const newContents = updateDeclarationMapContent(fileName, contents);
257290
originHost.writeFile(
258-
renameDtsFile(fileName),
259-
contents,
291+
newFileName,
292+
newContents,
260293
writeByteOrderMark,
261294
onError,
262295
sourceFiles,

pnpm-lock.yaml

Lines changed: 6 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-declaration-map-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
dts: {
9+
autoExtension: true,
10+
build: true,
11+
},
12+
source: {
13+
tsconfigPath: './tsconfig.esm.json',
14+
},
15+
}),
16+
generateBundleCjsConfig({
17+
bundle: false,
18+
dts: {
19+
autoExtension: true,
20+
build: true,
21+
},
22+
source: {
23+
tsconfigPath: './tsconfig.cjs.json',
24+
},
25+
}),
26+
],
27+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"rootDir": "src",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"declarationDir": "./dist/cjs"
9+
},
10+
"include": ["src"],
11+
"references": [
12+
{
13+
"path": "../__references__"
14+
}
15+
]
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"rootDir": "src",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"declarationDir": "./dist/esm"
9+
},
10+
"include": ["src"],
11+
"references": [
12+
{
13+
"path": "../__references__"
14+
}
15+
]
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-bundle-false-declaration-map-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
dts: {
9+
autoExtension: true,
10+
},
11+
}),
12+
generateBundleCjsConfig({
13+
bundle: false,
14+
dts: {
15+
autoExtension: true,
16+
},
17+
}),
18+
],
19+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;

0 commit comments

Comments
 (0)