Skip to content

Commit

Permalink
feat: add utility to strip source maps (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Nov 22, 2022
1 parent c9df8e4 commit 179d8f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
stringify,
stringifyStep,
parseSourceMap,
stripSourceMap,
StringifyOptions,
SourceMap,
} from './stringify.js';
Expand Down
11 changes: 10 additions & 1 deletion src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,25 @@ export async function stringifyStep(
return out.toString();
}

function isSourceMapLine(line: string): boolean {
return line.trim().startsWith(SOURCE_MAP_PREFIX);
}

/**
* Extracts a source map from a text.
*/
export function parseSourceMap(text: string): SourceMap | undefined {
const lines = text.split('\n');
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i] as string;
if (line.trim().startsWith(SOURCE_MAP_PREFIX)) {
if (isSourceMapLine(line)) {
return decode(line.trim().substring(SOURCE_MAP_PREFIX.length));
}
}
return;
}

export function stripSourceMap(text: string): string {
const lines = text.split('\n');
return lines.filter((line) => !isSourceMapLine(line)).join('\n');
}
19 changes: 18 additions & 1 deletion test/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import { parseSourceMap, stringify } from '../src/stringify.js';
import { parseSourceMap, stringify, stripSourceMap } from '../src/stringify.js';
import { assert } from 'chai';
import { StringifyExtension } from '../src/StringifyExtension.js';
import { Step, StepType, UserFlow } from '../src/Schema.js';
Expand Down Expand Up @@ -285,4 +285,21 @@ describe('stringify', () => {
`);
assert.deepStrictEqual(sourceMap, [1, 1, 3]);
});

it('should strip a source map', async () => {
const sourceMap = stripSourceMap(`
test
test
test
//# recorderSourceMap=BBD
`);
assert.deepStrictEqual(
sourceMap,
`
test
test
test
`
);
});
});

0 comments on commit 179d8f4

Please sign in to comment.