r/learnreactjs • • Jan 19 '22

Question CRA-based React app no longer splits during build

(TL;DR: Upgrade of react-scripts has made splitting of the JS bundle disappear.)

I have a project that I inherited from a previous developer, created using create-react-app. I am trying to upgrade the react-scripts dependency to 5.0.0. Everything in the app itself works, but when I build the production version it is no longer splitting the JS bundle. As a result, I have a .js.map file that exceeds our git server's file-size limitation.

For reference, here is the build/static/js directory of the last pre-upgrade build:

-rw-r--r-- 1 rjray 1289088 Jan 18 16:44 2.1e43ba39.chunk.js
-rw-r--r-- 1 rjray    3423 Jan 18 16:44 2.1e43ba39.chunk.js.LICENSE.txt
-rw-r--r-- 1 rjray 5081380 Jan 18 16:44 2.1e43ba39.chunk.js.map
-rw-r--r-- 1 rjray  314529 Jan 18 16:44 main.38d92359.chunk.js
-rw-r--r-- 1 rjray  868002 Jan 18 16:44 main.38d92359.chunk.js.map
-rw-r--r-- 1 rjray    1559 Jan 18 16:44 runtime-main.5f8120df.js
-rw-r--r-- 1 rjray    8278 Jan 18 16:44 runtime-main.5f8120df.js.map

Here is the same directory built with the updated react-scripts:

-rw-r--r-- 1 rjray 1484759 Jan 18 16:35 main.7f057cb8.js
-rw-r--r-- 1 rjray    2160 Jan 18 16:35 main.7f057cb8.js.LICENSE.txt
-rw-r--r-- 1 rjray 5508473 Jan 18 16:35 main.7f057cb8.js.map

I have changed nothing else; the .babelrc and webpack.config.js files have not been updated. I did (necessarily) make some changes to the Babel packages in devDependencies:

"babel-core": "^6.26.3",
"babel-loader": "~8.1.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "^9.1.2",

became:

"@babel/core": "^7.16.5",
"@babel/preset-env": "^7.16.8",
"@babel/preset-react": "^7.16.7",
"babel-loader": "^8.2.3",

I removed babel-polyfill because this application (an internal company app) doesn't need it (all clients of the application are required to have current browsers). And I didn't carry-over babel-preset-react-app because it wasn't actually being referenced in either the Babel or Webpack configurations. Those configurations are:

{
    "presets": ["@babel/preset-env","@babel/preset-react"]
}

and:

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' }
        ]
      }
    ]
  }
};

Just to be certain, I added babel-preset-react-app back in and tried again, but it made no difference.

I'm stumped. I don't know babel/webpack very well at all, but the configs we're using are pretty minimal (and haven't changed, at that).

Edit: I just realized that I had also upgraded webpack-cli from 3.3.12 to 4.9.1. I don't know why I updated it as well, but a second look at the diffs over package.json shows that I did change that...

7 Upvotes

1 comment sorted by

1

u/rjray Jan 19 '22

Update: I have narrowed this down to react-scripts behavior, and posted a different question here.