@@ -12,6 +12,12 @@ common.expectWarning(
12
12
'stripTypeScriptTypes is an experimental feature and might change at any time' ,
13
13
) ;
14
14
15
+ const sourceToBeTransformed = `
16
+ namespace MathUtil {
17
+ export const add = (a: number, b: number) => a + b;
18
+ }` ;
19
+ const sourceToBeTransformedMapping = 'UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' ;
20
+
15
21
test ( 'stripTypeScriptTypes' , ( ) => {
16
22
const source = 'const x: number = 1;' ;
17
23
const result = stripTypeScriptTypes ( source ) ;
@@ -48,45 +54,52 @@ test('stripTypeScriptTypes sourceUrl throws when mode is strip', () => {
48
54
} ) ;
49
55
50
56
test ( 'stripTypeScriptTypes source map when mode is transform' , ( ) => {
51
- const source = `
52
- namespace MathUtil {
53
- export const add = (a: number, b: number) => a + b;
54
- }` ;
55
- const result = stripTypeScriptTypes ( source , { mode : 'transform' , sourceMap : true } ) ;
57
+ const result = stripTypeScriptTypes ( sourceToBeTransformed , { mode : 'transform' , sourceMap : true } ) ;
56
58
const script = new vm . Script ( result ) ;
57
59
const sourceMap =
58
60
{
59
61
version : 3 ,
60
- sources : [
61
- '<anon>' ,
62
- ] ,
63
- sourcesContent : [
64
- '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }' ,
65
- ] ,
62
+ sources : [ '' ] ,
66
63
names : [ ] ,
67
- mappings : ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA'
64
+ mappings : sourceToBeTransformedMapping ,
68
65
} ;
69
- assert ( script . sourceMapURL , `sourceMappingURL=data:application/json;base64,${ JSON . stringify ( sourceMap ) } ` ) ;
66
+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
67
+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
70
68
} ) ;
71
69
72
70
test ( 'stripTypeScriptTypes source map when mode is transform and sourceUrl' , ( ) => {
73
- const source = `
74
- namespace MathUtil {
75
- export const add = (a: number, b: number) => a + b;
76
- }` ;
77
- const result = stripTypeScriptTypes ( source , { mode : 'transform' , sourceMap : true , sourceUrl : 'test.ts' } ) ;
71
+ const result = stripTypeScriptTypes ( sourceToBeTransformed , {
72
+ mode : 'transform' ,
73
+ sourceMap : true ,
74
+ sourceUrl : 'test.ts'
75
+ } ) ;
76
+ const script = new vm . Script ( result ) ;
77
+ const sourceMap =
78
+ {
79
+ version : 3 ,
80
+ sources : [ 'test.ts' ] ,
81
+ names : [ ] ,
82
+ mappings : sourceToBeTransformedMapping ,
83
+ } ;
84
+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
85
+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
86
+ } ) ;
87
+
88
+ test ( 'stripTypeScriptTypes source map when mode is transform and sourceUrl with non-latin-1 chars' , ( ) => {
89
+ const sourceUrl = 'dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`.cts' ;
90
+ const result = stripTypeScriptTypes ( sourceToBeTransformed , {
91
+ mode : 'transform' ,
92
+ sourceMap : true ,
93
+ sourceUrl,
94
+ } ) ;
78
95
const script = new vm . Script ( result ) ;
79
96
const sourceMap =
80
97
{
81
98
version : 3 ,
82
- sources : [
83
- 'test.ts' ,
84
- ] ,
85
- sourcesContent : [
86
- '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }' ,
87
- ] ,
99
+ sources : [ sourceUrl ] ,
88
100
names : [ ] ,
89
- mappings : ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA'
101
+ mappings : sourceToBeTransformedMapping ,
90
102
} ;
91
- assert ( script . sourceMapURL , `sourceMappingURL=data:application/json;base64,${ JSON . stringify ( sourceMap ) } ` ) ;
103
+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
104
+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
92
105
} ) ;
0 commit comments