Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,42 @@ export async function emitDts(
if (bundle) {
return fileName;
}

if (fileName.endsWith('.d.ts.map')) {
return fileName.replace(/\.d\.ts\.map$/, `${dtsExtension}.map`);
}

return fileName.replace(/\.d\.ts$/, dtsExtension);
};

const updateDeclarationMapContent = (
fileName: string,
content: string,
): string => {
if (bundle || !compilerOptions.declarationMap) {
return content;
}

if (fileName.endsWith('.d.ts')) {
return content.replace(
/(\/\/# sourceMappingURL=.+)\.d\.ts\.map/g,
`$1${dtsExtension}.map`,
);
}

if (fileName.endsWith('.d.ts.map')) {
return content.replace(/\.d\.ts/, dtsExtension);
}

return content;
};

const system: ts.System = {
...ts.sys,
writeFile: (fileName, contents, writeByteOrderMark) => {
ts.sys.writeFile(renameDtsFile(fileName), contents, writeByteOrderMark);
const newFileName = renameDtsFile(fileName);
const newContents = updateDeclarationMapContent(fileName, contents);
ts.sys.writeFile(newFileName, newContents, writeByteOrderMark);
},
};

Expand All @@ -203,9 +232,11 @@ export async function emitDts(
onError,
sourceFiles,
) => {
const newFileName = renameDtsFile(fileName);
const newContents = updateDeclarationMapContent(fileName, contents);
originHost.writeFile(
renameDtsFile(fileName),
contents,
newFileName,
newContents,
writeByteOrderMark,
onError,
sourceFiles,
Expand Down Expand Up @@ -254,9 +285,11 @@ export async function emitDts(
onError,
sourceFiles,
) => {
const newFileName = renameDtsFile(fileName);
const newContents = updateDeclarationMapContent(fileName, contents);
originHost.writeFile(
renameDtsFile(fileName),
contents,
newFileName,
newContents,
writeByteOrderMark,
onError,
sourceFiles,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/dts/build/declaration-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-build-declaration-map-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
27 changes: 27 additions & 0 deletions tests/integration/dts/build/declaration-map/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
dts: {
autoExtension: true,
build: true,
},
source: {
tsconfigPath: './tsconfig.esm.json',
},
}),
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
build: true,
},
source: {
tsconfigPath: './tsconfig.cjs.json',
},
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/dts/build/declaration-map/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
16 changes: 16 additions & 0 deletions tests/integration/dts/build/declaration-map/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"declaration": true,
"declarationMap": true,
"declarationDir": "./dist/cjs"
},
"include": ["src"],
"references": [
{
"path": "../__references__"
}
]
}
16 changes: 16 additions & 0 deletions tests/integration/dts/build/declaration-map/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"declaration": true,
"declarationMap": true,
"declarationDir": "./dist/esm"
},
"include": ["src"],
"references": [
{
"path": "../__references__"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-bundle-false-declaration-map-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
19 changes: 19 additions & 0 deletions tests/integration/dts/bundle-false/declaration-map/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
dts: {
autoExtension: true,
},
}),
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
},
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"declaration": true,
"declarationMap": true
},
"include": ["src"]
}
6 changes: 6 additions & 0 deletions tests/integration/dts/composite/declaration-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-composite-declaration-map-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
19 changes: 19 additions & 0 deletions tests/integration/dts/composite/declaration-map/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
dts: {
autoExtension: true,
},
}),
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
},
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
10 changes: 10 additions & 0 deletions tests/integration/dts/composite/declaration-map/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "./src",
"composite": true,
"declarationMap": true
},
"include": ["src"]
}
Loading
Loading