Skip to content

Commit

Permalink
revert: source-map stripping (#398)
Browse files Browse the repository at this point in the history
This reverts commit 0e83db7.
  • Loading branch information
privatenumber committed Nov 19, 2023
1 parent 5bdf022 commit a95a482
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 63 deletions.
7 changes: 1 addition & 6 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { installSourceMapSupport } from '../source-map';
import { transformSync } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
Expand Down Expand Up @@ -69,11 +69,6 @@ const transformer = (

let code = fs.readFileSync(filePath, 'utf8');

// Strip source maps if originally disabled
if (shouldStripSourceMap) {
code = stripSourceMap(code);
}

if (filePath.endsWith('.cjs')) {
// Contains native ESM check
const transformed = transformDynamicImport(filePath, code);
Expand Down
9 changes: 2 additions & 7 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { TransformOptions } from 'esbuild';
import { transform } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { installSourceMapSupport } from '../source-map';
import { importAttributes } from '../utils/node-features';
import {
tsconfigPathsMatcher,
Expand Down Expand Up @@ -281,12 +281,7 @@ export const load: LoadHook = async function (
}

const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
let code = loaded.source.toString();

// Strip source maps if originally disabled
if (shouldStripSourceMap) {
code = stripSourceMap(code);
}
const code = loaded.source.toString();

if (
// Support named imports in JSON modules
Expand Down
21 changes: 1 addition & 20 deletions src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,7 @@ type PortMessage = {
map: RawSourceMap;
};

// If Node.js has source map disabled, we should strip source maps to speed up processing
export const shouldStripSourceMap = (
('sourceMapsEnabled' in process)
&& process.sourceMapsEnabled === false
);

const sourceMapPrefix = '\n//# sourceMappingURL=';

export const stripSourceMap = (code: string) => {
const sourceMapIndex = code.indexOf(sourceMapPrefix);
if (sourceMapIndex === -1) {
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,`;
const inlineSourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,';

export const installSourceMapSupport = (
/**
Expand Down
1 change: 0 additions & 1 deletion tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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'));
await runTestSuite(import('./specs/transform'));

for (const nodeVersion of nodeVersions) {
Expand Down
10 changes: 4 additions & 6 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,9 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
'import-from-js.js': outdent`
import assert from 'assert';
import { expectErrors } from './expect-errors';
//# sourceMappingURL=shouldnt affect the file
// const shouldntAffectFile = \`
// //# sourceMappingURL=\`;
const shouldntAffectFile = \`
//# sourceMappingURL=\`;
//# sourceMappingURL=shouldnt affect the file
// node: prefix
Expand Down Expand Up @@ -466,8 +464,8 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
import assert from 'assert';
import { expectErrors } from './expect-errors';
// const shouldntAffectFile = \`
// //# sourceMappingURL=\`;
const shouldntAffectFile = \`
//# sourceMappingURL=\`;
//# sourceMappingURL=shouldnt affect the file
// node: prefix
Expand Down
23 changes: 0 additions & 23 deletions tests/specs/source-map.ts

This file was deleted.

0 comments on commit a95a482

Please sign in to comment.