-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Fix node_modules sourcemap config (which will fix VSCode debugging of CRA apps) #7022
Fix node_modules sourcemap config (which will fix VSCode debugging of CRA apps) #7022
Conversation
I just noticed that @gordonmleigh pointed me back in March to exactly the solution implemented in this PR. But I didn't notice his comment. Arrrgh. Anyway, kudos to Gordon for figuring out the solution. I could have saved a lot of time by paying closer attention! Gordon also had a related suggestion which is to add
|
Until this gets merged, should we be on an older version of react-scripts? |
@justingrant Thanks for the PR. Sorry we haven't looked yet, it's been very busy few months. Do you have a sense of how much slower this makes the build? |
@gaearon - no worries, thanks for taking a look! In totally unscientific testing with my largest project on my dev machine (2015 MBP), I'm seeing about 10% slower In a minimal project like the repro repo in this PR, the difference was also too small to accurately measure-- a few hundred msecs or less. Hot reloads on my biggest project were also too fast to reliably tell the difference between with and without the PR. Given that performance-sensitive users already have a command-line option to disable sourcemaps completely, it seems like folks who are concerned about build times already have a way to speed things up much more than 10%. But newbies who want to fix bugs or to understand react or other libraries by debugging into them with VSCode (or any IDE whose debugger matches sourcemaps to files like VSCode does) have no easy options today regardless of perf impact. Given CRAs prime directive as the on-ramp to the React ecosystem, I'd argue for "it just works" being the default. Folks with big apps or who are obsessive about initial build time can (like they already can today) opt into faster builds by turning off sourcemaps. What do you think? |
I rebased to catch up with the last few months of commits. No changes on my end. Hope that this can make it into 3.2! |
Looks like 3.2 shipped and this is in the 3.3 list. It would be awesome to see this in 3.2.1 to get it sooner though! Myself and colleagues have been using 2.1.4 which seems to be the last version with fully working VSCode debugging (as noted here: #6074 (comment)) |
Thanks a lot, this looks great! |
This PR fixes #6044 and fixes #6296, which are sourcemap-related problems that make the VSCode debugger unable to debug code in
node_modules
of CRA apps. "Unable to debug" means:node_modules
code trigger on different code lines than where the breakpoint was setnode_modules
code will highlight the wrong line, and sometimes even the wrong module!node_modules
fileThe cause of this problem was a Babel config change in #5096 from Sept 2018:
create-react-app/packages/react-scripts/config/webpack.config.js
Lines 432 to 436 in 57ef103
The goal of that change was to force debuggers to show transpiled code instead of original source. Unfortunately, that's not what it did. Webpack still generates a sourcemap for
node_modules
(to see it, open https://localhost:3000/static/js/0.chunk.js.map in any CRA app in dev mode), but this sourcemap doesn't match the code that Babel generated for each module. Notably there are differences in whitespace (extra blank lines) and comments between the sourcemap code insourcesContent
and the original source. When VSCode tries to set a breakpoint or step into node_modules code, the sourcemap points to the wrong line in the webpack node_modules chunk.BTW, this explains why the problem doesn't show up in the Chrome debugger, because the Chrome debugger only uses the inline
sourcesContent
code in the sourcemap-- it ignores the actual source files on disk. VSCode, however, will try to load the original source files from disk using thesources
array in the sourcemap. This provides a better debugging experience-- for example, if you quit your debug session the same source files are still loaded, and you can set breakpoints before your app is running to debug startup behavior.Anyway, this PR changes Babel config to ensure that Babel is correctly generating sourcemaps for node_modules code. Heads up: it will likely make build times longer, because the original change in #5096 made build times shorter.
To validate that the PR is working, use the following steps:
git clone https://github.com/justingrant/cra-sourcemap.git
- this repo is unaltered CRA boilerplate code. The only changes I made was to add VSCode config files to make it easier for VSCode newbies to get a debug session going to repro the problem and validate the fix.cra-sourcemap
folder you cloned abovesrc/index.js
in the IDE editorReactDOM.Render
. If you don't know how to set a breakpoint in VSCode, one way to do it is to set the cursor on that line of code and press F9.yarn start
index.js
. Now it's actually ready to call into React code.Expected (after PR is applied):
IDE highlights the first line of
createElementWithValidation()
, which is whatReactDOM.Render
resolves to inside React's original source code.Actual (current behavior):
A line of code is highlighted in the middle of a method-- it's clearly the wrong line.
BTW, to validate this PR I wrote a webpack plugin that compared the code in
sourcesContent
of 0.chunk.js.map to the actual code sitting on disk. I ran it on my main project and got870 of 1029 source files don't match the source map static/js/0.chunk.js.map
. Then I ran it against the PR configs and zero mismatches were reported.Here's an excerpt from the plugin's output for current CRA configs, omitting the first 867 (!!!) mismatched files: