Skip to content

Commit

Permalink
fix: source maps in Node v21.6.0+ & v20.12.0+ (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
cenfun and privatenumber authored Apr 4, 2024
1 parent 61b1836 commit 1a10da7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.10.0
v20.12.0
30 changes: 13 additions & 17 deletions src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ import type { Transformed } from './utils/transform/apply-transformers.js';
const inlineSourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,';

export const installSourceMapSupport = () => {
const hasNativeSourceMapSupport = (

/**
* 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
*/
'setSourceMapsEnabled' in process

/**
* Overriding Error.prepareStackTrace prevents --enable-source-maps from modifying
* the stack trace
* 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.
*
* https://github.com/nodejs/node/blob/91193825551f9301b6ab52d96211b38889149892/lib/internal/errors.js#L141
*/
&& typeof Error.prepareStackTrace !== 'function'
);
/**
* 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);
Expand Down
5 changes: 3 additions & 2 deletions tests/utils/node-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export const nodeVersions = [
&& process.platform !== 'win32'
)
? [
'21.5.0',
latestMajor('20.10.0'),
latestMajor('21.7.2'),
'21.0.0',
latestMajor('20.12.0'),
'20.0.0',
latestMajor('18.20.0'),
'18.0.0',
Expand Down

0 comments on commit 1a10da7

Please sign in to comment.