Skip to content

Commit

Permalink
add option decodeMappings to remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Sep 28, 2020
1 parent 91c3160 commit 28a42b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/remapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

/**
* Traces through all the mappings in the root sourcemap, through the sources
Expand All @@ -27,14 +27,19 @@ import { SourceMapInput, SourceMapLoader } from './types';
* it returns a falsey value, that source file is treated as an original,
* unmodified source file.
*
* Pass `excludeContent` content to exclude any self-containing source file
* content from the output sourcemap.
* Pass `excludeContent` to exclude any self-containing source file content
* from the output sourcemap.
*
* Pass `decodeMappings` to get a sourcemap with decoded mappings.
*/
export default function remapping(
input: SourceMapInput | SourceMapInput[],
loader: SourceMapLoader,
excludeContent?: boolean
): SourceMap {
excludeContent?: boolean,
decodeMappings?: boolean
): SourceMap | DecodedSourceMap {
const graph = buildSourceMapTree(input, loader);
return new SourceMap(graph.traceMappings(), !!excludeContent);
return !!decodeMappings
? graph.traceMappings()
: new SourceMap(graph.traceMappings(), !!excludeContent);
}
27 changes: 26 additions & 1 deletion test/unit/remapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import remapping from '../../src/remapping';
import { RawSourceMap } from '../../src/types';
import { DecodedSourceMap, RawSourceMap } from '../../src/types';

describe('remapping', () => {
const rawMap: RawSourceMap = {
Expand Down Expand Up @@ -49,6 +49,16 @@ describe('remapping', () => {
sourcesContent: ['\n\n 1 + 1;'],
version: 3,
};
const decodedMap: DecodedSourceMap = {
file: 'transpiled.min.js',
mappings: [[[0,0,2,2,0]]],
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);
Expand Down Expand Up @@ -155,4 +165,19 @@ describe('remapping', () => {

expect(map).not.toHaveProperty('sourcesContent');
});

test('returns decoded mappings if `decodeMappings` is set', () => {
const map = remapping(
rawMap,
(name: string) => {
if (name === 'transpiled.js') {
return transpiledMap;
}
},
false,
true
);

expect(map).toEqual(decodedMap);
});
});

0 comments on commit 28a42b3

Please sign in to comment.