Skip to content

Commit

Permalink
fix(source map): handle source map in middle of file (#392)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
nwalters512 and privatenumber authored Nov 17, 2023
1 parent 7676143 commit 497f69a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ const sourceMapPrefix = '\n//# sourceMappingURL=';

export const stripSourceMap = (code: string) => {
const sourceMapIndex = code.indexOf(sourceMapPrefix);
if (sourceMapIndex !== -1) {
return code.slice(0, sourceMapIndex);
if (sourceMapIndex === -1) {
return code;
}
return code;

const nextNewLine = code.indexOf('\n', sourceMapIndex + sourceMapPrefix.length);
const afterSourceMap = nextNewLine === -1 ? '' : code.slice(nextNewLine);
return code.slice(0, sourceMapIndex) + afterSourceMap;
};

const inlineSourceMapPrefix = `${sourceMapPrefix}data:application/json;base64,`;
Expand Down
1 change: 1 addition & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nodeVersions } from './utils/node-versions';
await runTestSuite(import('./specs/cli'));
await runTestSuite(import('./specs/watch'));
await runTestSuite(import('./specs/repl'));
await runTestSuite(import('./specs/source-map'));

for (const nodeVersion of nodeVersions) {
const node = await createNode(nodeVersion);
Expand Down
9 changes: 7 additions & 2 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import { pathToFileURL } from 'url';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import outdent from 'outdent';
import type { NodeApis } from '../utils/tsx';

const cjsContextCheck = 'typeof module !== \'undefined\'';
Expand Down Expand Up @@ -329,10 +330,12 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
const fixture = await createFixture({
...files,
'package.json': JSON.stringify({ type: packageType }),
'import-from-js.js': `
'import-from-js.js': outdent`
import assert from 'assert';
import { expectErrors } from './expect-errors';
//# sourceMappingURL=shouldnt affect the file
// node: prefix
import 'node:fs';
Expand Down Expand Up @@ -453,10 +456,12 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
...files,
'package.json': JSON.stringify({ type: packageType }),

'import-from-ts.ts': `
'import-from-ts.ts': outdent`
import assert from 'assert';
import { expectErrors } from './expect-errors';
//# sourceMappingURL=shouldnt affect the file
// node: prefix
import 'node:fs';
Expand Down
23 changes: 23 additions & 0 deletions tests/specs/source-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { testSuite, expect } from 'manten';
import { stripSourceMap } from '../../src/source-map';

export default testSuite(({ describe }) => {
describe('Source Map', ({ describe }) => {
describe('stripSourceMap', ({ test }) => {
test('end of file, no trailing newline', () => {
const file = 'const foo = 1;\n//# sourceMappingURL=foo.js.map';
expect(stripSourceMap(file)).toBe('const foo = 1;');
});

test('end of file, with trailing newlines', () => {
const file = 'const foo = 1;\n//# sourceMappingURL=foo.js.map\n';
expect(stripSourceMap(file)).toBe('const foo = 1;\n');
});

test('middle of file', () => {
const file = 'const foo = 1;\n//# sourceMappingURL=foo.js.map\nconst bar = 2;';
expect(stripSourceMap(file)).toBe('const foo = 1;\nconst bar = 2;');
});
});
});
});

0 comments on commit 497f69a

Please sign in to comment.