-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
build: add sourcemaps #6823
build: add sourcemaps #6823
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a good idea to me.
Good enough for me. Thanks! |
It's my understanding that we don't actually need to have the source code in our npm package, because the sourcemap already encodes that data, right? I'd like to avoid including our |
I believe that you are correct; as long as the sourcemaps have the From the Source Map spec
|
Thanks for the thoughtful reply in #7016, @justingrant. Sorry, I should have unlocked this PR when I made my comment. In that issue, one thing you said was:
That's not accurate, unless you're doing something that I haven't seen before. When you start up your app, you're not actually using any of our source code. You're using one of our builds. So how is one of those breakpoints going to work?
I believe that including the original source in our npm package causes confusion, as has already been demonstrated here. You're not actually running that code, so setting breakpoints won't work, and changing the code won't change how your app runs. Also, we reserve the right to do things in our source code that could probably break one of your tools, like including feature flags that we interpret at build time. One such flag that we currently use is the |
[copying my comment from #7016 here to keep discussion in one place, per @pshrmn request] Thanks for unlocking! First, some context: with sourcemaps original source is usually present in two places: 1) in the
Anyway, that's a few reasons why including original source is preferred. None of these are super-high-priority things, but taken together they save developer time and reduce developer confusion when the original source is on disk. What do you think? Turning it around, what are the reasons that original source should not be included? FWIW, one suggestion I'd have would be to rename the |
Hi @mjackson - thanks for the quick reply! Here's a few notes:
This is the magic of sourcemaps! With a sourcemap, as long as your build config is correct, then you can set breakpoints (using VSCode or any file-based debugger, including AFAIK Chrome's workspace mode) in the original source code files and the debugger will halt there when the corresponding code is about to run in the transpiled JS. I'm not an expert in debugger implementation, but I believe it works something like this:
Note that there are debugger settings which can control this behavior. For example, you can set
Yep, but changing code inside node_modules is problematic even without sourcemaps. For example, react-router includes esm, cjs, and umd folders. How will a newbie developer know which one to change or even which one to set a breakpoint inside of? The answer is (at least in my case, as a relative newbie to the JS ecosystem) is that they don't know-- they'll experiment with all 3 (or 4 if there's a Therefore, I think a better approach is to dissuade developers-- especially newbies who won't deeply understand transpilation, build systems, and bundle types-- from modifying node_modules code. Both for the reasons above, but also because I actually think that sourcemaps + original source can help prevent devs from modifying dependency code. AFAIK, the main reason that people modify node_modules dependency code is to insert My take is that we should be encouraging newbies (aka folks who won't know how to safely patch packages installed by npm or yarn) to use debuggers instead of modifying code that's not theirs, and that including sourcemaps + original source could help with this encouragement.
Yep, agreed. That said, now that this PR added the missing files and fixed your sourcemap config, I haven't seen any complaining from my sourcemap-validating tools. But if you change your build process to do a valid thing, like adding feature flags, that causes sourcemap-validating tools to complain, then IMHO the tools need to be fixed.. and if so they I'd volunteer to PR them! ;-)
I guess it depends what you mean by "use". If you mean "read, understand, copy, or debug" then I think these tasks are easier when original source is included in npm, as noted in my earlier post. If you mean "modify in-place for reasons other than debugging" then I agree that including original source may make things more confusing. My assumption is simply that the former set of read-only tasks are much more common, especially for the set of relative newbies who may not yet understand that original source != source that actually runs. In summary, I'm not implying that there are no downsides to including original source, only that the upsides IMHO seem to outweigh those downsides. What do you think? BTW, sorry for the long response! |
Unless your idea of fun is mentally un-transpiling ES5 code into ES6 and JSX, you'll like this PR. It makes debugging easier by adding sourcemaps for the react-router, react-router-dom, and react-router-config packages. The following build-time config changes are included:
sourceMaps: true
to babel config for each packageoutput
config, except it's calledsourcemap: true
here because, uh, diversity is good? ;-)modules
folder to thefiles
whitelist in each package's package.json, so that the original source will be installed by npm or yarn so that the sourcemaps have original source to point to.node_modules
. Unfortunately, the default paths to these folders are correct at build time but won't be correct when packages are installed on dev machines. This is a common problem, especially with monorepos. To fix, I added a one-linesourcemapPathTransform
to rollup's output config (for UMD only). This transform replaces all variations ofnode_modules
paths with a normalized path starting with../../node_modules/
which is where these files are most likely to be installed by npm or yarn on a dev machine.Note that this PR doesn't touch the react-router-native package because I am not familiar enough with React Native to know how to add sourcemaps there.
Using this PR,
npm pack
, and one of my apps that uses react-router, I verified that sourcemaps are now working with the VSCode and chrome devtools debuggers. "Working" means: (in case you want to verify it too)./node_modules/react-router*/modules
foldersBTW, this PR is analogous to bvaughn/react-window#275 which I recently filed to add sourcemaps to Brian Vaughn's react-window library. That library also uses rollup and babel, so the changes needed to add sourcemaps there were similar to this PR. Slowly but surely I'm gonna try to fix sourcemaps of all my most important dependencies! ;-)