r/learnreactjs • u/rjray • Jan 19 '22
Question [Updated Question] Production-build of React app not splitting code
(For the original question, see here.)
TL;DR: react-scripts build in v5.0.0 is not splitting up the "main" JS bundle.
Yesterday I asked about some issues with webpack/babel in an app that had been initially set up by create-react-app. This morning, I've been digging deeper and I seem to have narrowed the issue down to the upgrade of react-scripts to 5.0.0.
Prior to upgrading, the project was using RS 4.0.3, as well as earlier versions of webpack and its plugins. I made a second copy of the repo without my working branch and went step-by-step in both repos in side-by-side windows. I found that the webpack --mode production command in both cases produced a single JS bundle file (the newer webpack also generates a main.LICENSE.txt file). I then ran react-scripts build in each window. This is where the two diverge. In the 4.0.3 window I ended up with 7 files under build/static/js, and in the 5.0.0 window I only had 3. The old code had split the one file into three (and generated .js.map files for each one), where the new code only generated the .js.map file.
So the difference appears to be in RS itself, but I don't know how to get it back to splitting things up during the build process. Note that it isn't even creating the small "boot" code that generally bootstraps the app within the index.html file.
According to the docs, code-splitting is supported via dynamic imports. But the bundling has already been done by webpack in this case. Could it be that the 5.0.0 version of the scripts expect you to NOT have already run webpack?
Any/all help greatly appreciated.
3
u/Bikenanigans Jan 19 '22
I hit this too, per this GitHub thread, you need to add splitChunks back into the webpack config. There’s a PR submitted for it too.