/
remapping.ts
174 lines (154 loc) · 4.38 KB
/
remapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import remapping from '../../src/remapping';
import type { EncodedSourceMap } from '../../src/types';
describe('remapping', () => {
const rawMap: EncodedSourceMap = {
file: 'transpiled.min.js',
// 0th column of 1st line of output file translates into the 1st source
// file, line 2, column 1, using 1st name.
mappings: 'AACCA',
names: ['add'],
sources: ['transpiled.js'],
sourcesContent: ['1+1'],
version: 3,
};
const transpiledMap: EncodedSourceMap = {
// 1st column of 2nd line of output file translates into the 1st source
// file, line 3, column 2
mappings: ';CAEE',
names: [],
sources: ['helloworld.js'],
sourcesContent: ['\n\n 1 + 1;'],
version: 3,
};
const translatedMap: EncodedSourceMap = {
file: 'transpiled.min.js',
// 0th column of 1st line of output file translates into the 1st source
// file, line 3, column 2, using first name
mappings: 'AAEEA',
names: ['add'],
// TODO: support sourceRoot
// sourceRoot: '',
sources: ['helloworld.js'],
sourcesContent: ['\n\n 1 + 1;'],
version: 3,
};
test('does not alter a lone sourcemap', () => {
const map = remapping(rawMap, () => null);
expect(map).toEqual(rawMap);
});
test('traces SourceMapSegments through child sourcemaps', () => {
const map = remapping(rawMap, (name: string) => {
if (name === 'transpiled.js') {
return transpiledMap;
}
});
expect(map).toEqual(translatedMap);
});
test('traces transformations through sourcemap', () => {
const maps = [rawMap, transpiledMap];
const map = remapping(maps, () => null);
expect(map).toEqual(translatedMap);
});
test('resolves sourcemaps realtive to sourceRoot', () => {
const sourceRoot = 'foo/';
const map = remapping(
{
...rawMap,
sourceRoot,
},
(name: string) => {
if (name.endsWith('transpiled.js')) {
return transpiledMap;
}
}
);
expect(map).toEqual({
...translatedMap,
// TODO: support sourceRoot
// sourceRoot,
sources: ['foo/helloworld.js'],
});
});
test('resolves sourcemaps realtive to absolute sourceRoot', () => {
const sourceRoot = 'https://foo.com/';
const map = remapping(
{
...rawMap,
sourceRoot,
},
(name: string) => {
if (name.endsWith('transpiled.js')) {
return transpiledMap;
}
}
);
expect(map).toEqual({
...translatedMap,
// TODO: support sourceRoot
// sourceRoot,
sources: [`${sourceRoot}helloworld.js`],
});
});
test('includes null sourceContent if sourcemap has no sourcesContent', () => {
const map = remapping(rawMap, (name: string) => {
if (name === 'transpiled.js') {
return {
...transpiledMap,
sourcesContent: undefined,
};
}
});
expect(map).toHaveProperty('sourcesContent', [null]);
});
test('excludes null sourceContent if sourcemap is not self-containing', () => {
const map = remapping(rawMap, (name: string) => {
if (name === 'transpiled.js') {
return {
...transpiledMap,
sourcesContent: [null],
};
}
});
expect(map).toHaveProperty('sourcesContent', [null]);
});
describe('boolean options', () => {
test('excludes sourcesContent if `excludeContent` is set', () => {
const map = remapping(
rawMap,
(name: string) => {
if (name === 'transpiled.js') {
return transpiledMap;
}
},
true
);
expect(map).not.toHaveProperty('sourcesContent');
});
});
describe('options bag', () => {
test('excludes sourcesContent if `excludeContent` is set', () => {
const map = remapping(
rawMap,
(name: string) => {
if (name === 'transpiled.js') {
return transpiledMap;
}
},
{ excludeContent: true }
);
expect(map).not.toHaveProperty('sourcesContent');
});
test('returns decoded sourcemap if `decodedMappings` is set', () => {
const map = remapping(
rawMap,
(name: string) => {
if (name === 'transpiled.js') {
return transpiledMap;
}
},
{ decodedMappings: true }
);
expect(map).toHaveProperty('mappings', [[[0, 0, 2, 2, 0]]]);
});
});
});