Skip to content

Commit c5a23be

Browse files
committed
add option decodeMappings to remapping
1 parent 91c3160 commit c5a23be

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/remapping.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import buildSourceMapTree from './build-source-map-tree';
1818
import SourceMap from './source-map';
19-
import { SourceMapInput, SourceMapLoader } from './types';
19+
import { DecodedSourceMap, SourceMapInput, SourceMapLoader } from './types';
2020

2121
/**
2222
* Traces through all the mappings in the root sourcemap, through the sources
@@ -27,14 +27,19 @@ import { SourceMapInput, SourceMapLoader } from './types';
2727
* it returns a falsey value, that source file is treated as an original,
2828
* unmodified source file.
2929
*
30-
* Pass `excludeContent` content to exclude any self-containing source file
31-
* content from the output sourcemap.
30+
* Pass `excludeContent` to exclude any self-containing source file content
31+
* from the output sourcemap.
32+
*
33+
* Pass `decodeMappings` to get a sourcemap with decoded mappings.
3234
*/
3335
export default function remapping(
3436
input: SourceMapInput | SourceMapInput[],
3537
loader: SourceMapLoader,
36-
excludeContent?: boolean
37-
): SourceMap {
38+
excludeContent?: boolean,
39+
decodeMappings?: boolean
40+
): SourceMap | DecodedSourceMap {
3841
const graph = buildSourceMapTree(input, loader);
39-
return new SourceMap(graph.traceMappings(), !!excludeContent);
42+
return decodeMappings
43+
? graph.traceMappings()
44+
: new SourceMap(graph.traceMappings(), !!excludeContent);
4045
}

test/unit/remapping.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import remapping from '../../src/remapping';
18-
import { RawSourceMap } from '../../src/types';
18+
import { DecodedSourceMap, RawSourceMap } from '../../src/types';
1919

2020
describe('remapping', () => {
2121
const rawMap: RawSourceMap = {
@@ -49,6 +49,16 @@ describe('remapping', () => {
4949
sourcesContent: ['\n\n 1 + 1;'],
5050
version: 3,
5151
};
52+
const decodedMap: DecodedSourceMap = {
53+
file: 'transpiled.min.js',
54+
mappings: [[[0,0,2,2,0]]],
55+
names: ['add'],
56+
// TODO: support sourceRoot
57+
// sourceRoot: '',
58+
sources: ['helloworld.js'],
59+
sourcesContent: ['\n\n 1 + 1;'],
60+
version: 3,
61+
};
5262

5363
test('does not alter a lone sourcemap', () => {
5464
const map = remapping(rawMap, () => null);
@@ -155,4 +165,19 @@ describe('remapping', () => {
155165

156166
expect(map).not.toHaveProperty('sourcesContent');
157167
});
168+
169+
test('returns decoded mappings if `decodeMappings` is set', () => {
170+
const map = remapping(
171+
rawMap,
172+
(name: string) => {
173+
if (name === 'transpiled.js') {
174+
return transpiledMap;
175+
}
176+
},
177+
false,
178+
true
179+
);
180+
181+
expect(map).toEqual(decodedMap);
182+
});
158183
});

0 commit comments

Comments
 (0)