Skip to content

Commit 1827d1b

Browse files
committed
fix: declaration map
closes #159
1 parent dee6872 commit 1827d1b

File tree

12 files changed

+37
-24
lines changed

12 files changed

+37
-24
lines changed

src/fake-js.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,20 @@ export function createFakeJsPlugin({
110110
},
111111
renderChunk,
112112

113-
generateBundle: sourcemap
114-
? undefined
115-
: (options, bundle) => {
116-
for (const chunk of Object.values(bundle)) {
117-
if (!RE_DTS_MAP.test(chunk.fileName)) continue
118-
delete bundle[chunk.fileName]
119-
}
120-
},
113+
generateBundle(options, bundle) {
114+
for (const chunk of Object.values(bundle)) {
115+
if (!RE_DTS_MAP.test(chunk.fileName)) continue
116+
if (sourcemap) {
117+
if (chunk.type === 'chunk' || typeof chunk.source !== 'string')
118+
continue
119+
const map = JSON.parse(chunk.source)
120+
map.sourcesContent = undefined
121+
chunk.source = JSON.stringify(map)
122+
} else {
123+
delete bundle[chunk.fileName]
124+
}
125+
}
126+
},
121127
}
122128

123129
function transform(code: string, id: string): TransformResult {
@@ -324,6 +330,12 @@ export function createFakeJsPlugin({
324330
const symbolId = symbolIdNode.value
325331
const original = getSymbol(symbolId)
326332

333+
walkAST(original.decl, {
334+
enter(node) {
335+
delete node.loc
336+
},
337+
})
338+
327339
for (const [i, decl] of node.declarations.entries()) {
328340
const transformedBinding = {
329341
...decl.id,

tests/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ declare global {
384384
exports[`tree-shaking 1`] = `
385385
"// index.d.ts
386386
//#region tests/fixtures/tree-shaking/mod.d.ts
387-
388387
type A = string;
389388
//#endregion
390389
export { type A };

tests/__snapshots__/source-map-oxc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
//#region tests/fixtures/source-map.d.ts
55
declare const a: string;
66
declare const b: string;
7+
type Str = string;
8+
declare function fn(param: Str): string;
79
//#endregion
8-
export { a, b };
10+
export { a, b, fn };
911
//# sourceMappingURL=source-map.d.ts.map
1012
```
1113

1214
## source-map.d.ts.map
1315

1416
```map
15-
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"sourcesContent":[],"mappings":";cAAa;AAAA,cAEA,CAFA,EAAA,MAAA"}
17+
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
1618
```

tests/__snapshots__/source-map-tsc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
//#region tests/fixtures/source-map.d.ts
55
declare const a: string;
66
declare const b: string;
7+
type Str = string;
8+
declare function fn(param: Str): string;
79
//#endregion
8-
export { a, b };
10+
export { a, b, fn };
911
//# sourceMappingURL=source-map.d.ts.map
1012
```
1113

1214
## source-map.d.ts.map
1315

1416
```map
15-
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"sourcesContent":[],"mappings":";cAAa;AAAA,cAEA,CAFc,EAAA,MAAA"}
17+
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
1618
```

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ declare const a = 1;
2020
export { a };
2121
//# sourceMappingURL=index.d.ts.map
2222
// index.d.ts.map
23-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";cAAa,CAAA"}
23+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";cAAa,CAAA"}
2424
// index.js
2525
//#region tests/fixtures/deep-source-map/src/index.ts
2626
const a = 1;
@@ -40,7 +40,7 @@ declare const a = 1;
4040
export { a };
4141
//# sourceMappingURL=index.d.ts.map
4242
// index.d.ts.map
43-
{"version":3,"file":"index.d.ts","names":["a"],"sources":["../src/index.d.ts"],"sourcesContent":["export declare const a = 1;\\n"],"mappings":";cAAqBA,CAAAA"}
43+
{"version":3,"file":"index.d.ts","names":["a"],"sources":["../src/index.d.ts"],"mappings":";cAAqBA,CAAAA"}
4444
// index.js
4545
//#region tests/fixtures/deep-source-map/src/index.ts
4646
const a = 1;
@@ -346,7 +346,6 @@ exports[`tsc > jsdoc 1`] = `
346346
import { Plugin } from "rolldown";
347347
348348
//#region tests/fixtures/jsdoc.d.ts
349-
350349
/**
351350
* with imports
352351
*/
@@ -360,8 +359,8 @@ declare function fn(): number;
360359
*/
361360
interface Options {
362361
/**
363-
* interface member
364-
*/
362+
* interface member
363+
*/
365364
foo: string;
366365
}
367366
/**

tests/fixtures/source-map.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ export const a: string = ''
33
export const b: string = ''
44

55
console.log('Hello World!')
6+
7+
type Str = string
8+
export function fn(param: Str): string {
9+
return param
10+
}

tests/rollup-plugin-dts/circular-to-entry/snapshot.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// index.d.ts
22
//#region tests/rollup-plugin-dts/circular-to-entry/Foo.d.ts
3-
43
declare class Foo {
54
manager: FooManager;
65
constructor(manager: FooManager);

tests/rollup-plugin-dts/overrides-with-rename/snapshot.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// index.d.ts
22
//#region tests/rollup-plugin-dts/overrides-with-rename/b.d.ts
3-
43
declare function autobind$1(): typeof autobind;
54
declare function autobind$1(constructor: Function): void;
65
declare function autobind$1(prototype: typeof autobind, name: string, descriptor: PropertyDescriptor): PropertyDescriptor;
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// index.d.ts
22
/// <reference path="/some/absolute/path" />
33
//#region tests/rollup-plugin-dts/reference-path-remapping-should-not-touch-absolute-path/index.d.ts
4-
54
interface Hello {}
65
//#endregion
76
export { Hello };

tests/rollup-plugin-dts/reference-path-remapping/snapshot.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/// <reference path="./ns-api2.d.ts" />
33
/// <reference path="./ns-api.d.ts" />
44
//#region tests/rollup-plugin-dts/reference-path-remapping/sub/api2.d.ts
5-
65
declare class C2 {
76
public X: x2.I2;
87
}

0 commit comments

Comments
 (0)