Skip to content

Commit

Permalink
refactor: remove unused source map logic (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 1, 2024
1 parent 625e6c3 commit 933eb7a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 34 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"./esm": "./dist/esm/index.mjs",
"./cli": "./dist/cli.mjs",
"./source-map": "./dist/source-map.cjs",
"./suppress-warnings": "./dist/suppress-warnings.cjs",
"./preflight": "./dist/preflight.cjs",
"./repl": "./dist/repl.mjs"
Expand Down
3 changes: 1 addition & 2 deletions src/esm/api/register.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import module from 'node:module';
import { installSourceMapSupport } from '../../source-map.js';

export const register = () => {
installSourceMapSupport();
process.setSourceMapsEnabled(true);

module.register(
'./index.mjs',
Expand Down
4 changes: 2 additions & 2 deletions src/esm/hook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const initialize: InitializeHook = async (data) => {
* but it shares a closure with the new load hook
*/
export const globalPreload: GlobalPreloadHook = () => `
const require = getBuiltin('module').createRequire("${import.meta.url}");
require('../source-map.cjs').installSourceMapSupport();
const require = getBuiltin('module').createRequire('${import.meta.url}');
process.setSourceMapsEnabled(true);
`;

export { load } from './load.js';
Expand Down
8 changes: 4 additions & 4 deletions src/esm/hook/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { LoadHook } from 'node:module';
import type { TransformOptions } from 'esbuild';
import { transform } from '../../utils/transform/index.js';
import { transformDynamicImport } from '../../utils/transform/transform-dynamic-import.js';
import { installSourceMapSupport } from '../../source-map.js';
import { inlineSourceMap } from '../../source-map.js';
import { isFeatureSupported, importAttributes } from '../../utils/node-features.js';
import { parent } from '../../utils/ipc/client.js';
import {
Expand All @@ -12,7 +12,7 @@ import {
isJsonPattern,
} from './utils.js';

const applySourceMap = installSourceMapSupport();
process.setSourceMapsEnabled(true);

const contextAttributesProperty = (
isFeatureSupported(importAttributes)
Expand Down Expand Up @@ -69,14 +69,14 @@ export const load: LoadHook = async (

return {
format: 'module',
source: applySourceMap(transformed),
source: inlineSourceMap(transformed),
};
}

if (loaded.format === 'module') {
const dynamicImportTransformed = transformDynamicImport(filePath, code);
if (dynamicImportTransformed) {
loaded.source = applySourceMap(dynamicImportTransformed);
loaded.source = inlineSourceMap(dynamicImportTransformed);
}
}

Expand Down
25 changes: 0 additions & 25 deletions src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,3 @@ export const inlineSourceMap = (
+ inlineSourceMapPrefix
+ Buffer.from(JSON.stringify(map), 'utf8').toString('base64')
);

const noSourceMap = ({ code }: Transformed) => code;

export const installSourceMapSupport = () => {
/**
* Check if native source maps are supported by seeing if the API is available
* https://nodejs.org/dist/latest-v18.x/docs/api/process.html#processsetsourcemapsenabledval
*
* Previously, we also checked Error.prepareStackTrace to opt-out of source maps
* as per this recommendation:
* https://nodejs.org/dist/latest-v18.x/docs/api/cli.html#:~:text=Overriding%20Error.prepareStackTrace%20prevents%20%2D%2Denable%2Dsource%2Dmaps%20from%20modifying%20the%20stack%20trace.
*
* But it's been removed because:
* 1. It's set by default from Node v21.6.0 and v20.12.0
* 2. It may have been possible for a custom prepareStackTrace to parse the source maps
*/
const hasNativeSourceMapSupport = 'setSourceMapsEnabled' in process;

if (hasNativeSourceMapSupport) {
process.setSourceMapsEnabled(true);
return inlineSourceMap;
}

return noSourceMap;
};

0 comments on commit 933eb7a

Please sign in to comment.