Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source maps to node loader #2458

Merged
merged 4 commits into from Apr 4, 2024
Merged

Add source maps to node loader #2458

merged 4 commits into from Apr 4, 2024

Conversation

remcohaszing
Copy link
Member

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

This allows people to debug MDX files when using the Node.js loader. I confirmed this works using the VSCode debugger. I’m not sure how to test this properly.

This allows people to debug MDX files when using the Node.js loader.
@remcohaszing remcohaszing added 🦋 type/enhancement This is great to have 🗄 area/interface This affects the public interface 🧒 semver/minor This is backwards-compatible change 🤞 phase/open Post is being triaged manually labels Mar 12, 2024
Copy link

vercel bot commented Mar 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mdx ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2024 2:19pm

@wooorm
Copy link
Member

wooorm commented Mar 12, 2024

How would people use this? What has a VS Code debugger to do with running node and its loaders from a command line?

Can we detect if some source maps flag is passed to Node? Then it can work the same as Rollup: auto-add a SourceMapGenerator to options.

You can change the test script in the package.json file to include such a flag.

Or, can this be default behavior?

@remcohaszing
Copy link
Member Author

How would people use this? What has a VS Code debugger to do with running node and its loaders from a command line?

People use this by debugging a script that imports MDX. For example by specifying the --enable-source-maps Node.js option or by using the JavaScript Debug Terminal from VSCode.

I was using @mdx-js/node-loader to test if breakpoints from VSCode work in MDX files. They almost automagically do.

Can we detect if some source maps flag is passed to Node? Then it can work the same as Rollup: auto-add a SourceMapGenerator to options.

I like this idea. Given the following script:

import inspector from 'node:inspector'

console.dir(process.sourceMapsEnabled)
console.dir(process.execArgv)
console.dir(inspector.url())

Running with node --enable-source-maps script.js yields:

true
[ '--enable-source-maps' ]
undefined

Running from the VSCode JavaScript Debug Terminal yields:

false
[]
'ws://127.0.0.1:36027/d594f5d2-a3dd-4f1e-8484-7ed291f5b7b3'

Note that process.sourceMapsEnabled is still experimental.

You can change the test script in the package.json file to include such a flag.

👍

Or, can this be default behavior?

You mean always pass SourceMapGenerator? Maybe. I don’t know if this has any performance impact. On the other hand, if people really care about performance, they should just build their project before deploying to production instead of using a Node.js loader. It’s always useful to have stack traces mapped to their original location.

@wooorm
Copy link
Member

wooorm commented Mar 13, 2024

Running from the VSCode JavaScript Debug Terminal yields:

Wait, does that mean that auto-detecting this would not work with your debugger? 🤔
Or, does it work if you run that debugger with a certain flag?
If we’re going with “automatically works”, then the debugger should be supported too? Or is that a mistake on the VS Code debugger’s part?

Or do you mean to enable this feature when either inspector.url() is truthy or process.sourceMapsEnabled is on?

You can change the test script in the package.json file to include such a flag.

Perhaps we should spawn different Node processes in the tests btw. That way we can properly test whether (in the future, different) flags are passed?

I don’t know if this has any performance impact.

Yeah, that’s the only potential downside, right?
I’d assume generating the source map is fast. It’s just some positional info in a comment at the bottom.
The lookup part might be slow, but stack traces are slow anyway, and folks would pass that flag if they want that.

@remcohaszing
Copy link
Member Author

remcohaszing commented Mar 14, 2024

Running from the VSCode JavaScript Debug Terminal yields:

Wait, does that mean that auto-detecting this would not work with your debugger? 🤔 Or, does it work if you run that debugger with a certain flag? If we’re going with “automatically works”, then the debugger should be supported too? Or is that a mistake on the VS Code debugger’s part?

Or do you mean to enable this feature when either inspector.url() is truthy or process.sourceMapsEnabled is on?

It basically means there are two distinct features that benefit from source maps.

  1. --enable-source-maps, detectable via process.sourceMapsEnables, for stack traces.
  2. Node.js debug inspector, detectable via inspector.url(), for debugging.

You can change the test script in the package.json file to include such a flag.

Perhaps we should spawn different Node processes in the tests btw. That way we can properly test whether (in the future, different) flags are passed?

Yes, even better.

I don’t know if this has any performance impact.

Yeah, that’s the only potential downside, right? I’d assume generating the source map is fast. It’s just some positional info in a comment at the bottom. The lookup part might be slow, but stack traces are slow anyway, and folks would pass that flag if they want that.

Yes, that’s the only potential downside. I’m fine with adding SourceMapGenerator by default in @mdx-js/node-loader. Should we allow overriding it by explicitly passing undefined?


Two more things I noticed:

  1. The esbuild integration doesn’t support source maps. Supporting source maps for esbuild appears to work the same: just add the sourceMappingURL comment, based on their Svelte plugin example.
  2. Source maps are slightly off, by 1 line, or a statement, or a few bytes. I’m not entirely sure.

@wooorm
Copy link
Member

wooorm commented Apr 3, 2024

Are you planning to continue working on this, or were you waiting for a response from me?

@remcohaszing
Copy link
Member Author

Just this question: Should we always pass SourceMapGenerator in @mdx-js/node?

@wooorm
Copy link
Member

wooorm commented Apr 4, 2024

Yeah 👍

There’s not really a reason not to.
@wooorm
Copy link
Member

wooorm commented Apr 4, 2024

Are you looking into the Windows failure?

Also, the different spawned threads? (“Perhaps we should spawn different Node processes in the tests btw.”)

@remcohaszing
Copy link
Member Author

Are you looking into the Windows failure?

Ugh, Windows path separators must be one of the most expensive mistakes in the history of software.

Also, the different spawned threads? (“Perhaps we should spawn different Node processes in the tests btw.”)

This was useful to test detect whether source maps are useful as discussed earlier. Since we now just always enable source map generation, we no longer have to test this detection.

@wooorm wooorm changed the title Support source maps in the node loader Add source maps to node loader Apr 4, 2024
@wooorm wooorm merged commit ceea80d into main Apr 4, 2024
10 checks passed
@wooorm wooorm deleted the node-source-map branch April 4, 2024 15:30
@wooorm wooorm added the 💪 phase/solved Post is done label Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 🤞 phase/open Post is being triaged manually 💪 phase/solved Post is done 🧒 semver/minor This is backwards-compatible change 🦋 type/enhancement This is great to have
Development

Successfully merging this pull request may close these issues.

None yet

2 participants