Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I turn off create-react-app chunking mechanism?

I am setting up my React app project using create-react-app.

I was wondering if there is a way to turn-off the chunking mechanism that is built-in into the react scripts. The thing is that I need to fix the name of the bundle created on the build.

like image 893
Darko Avatar asked Apr 29 '19 19:04

Darko


People also ask

Do I need to create react app every time?

It depends on how you installed create-react-app. If you installed it using the npm install -g , then it shouldn't be necessary to install each time. However, that isn't the recommended way to use it anymore, and you may have used the method that doesn't leave it installed globally.

Should I eject from create react app?

Create React App is the easiest and most available way to get into React. And ejecting your app is seen as the gateway to being a “Real React Developer.” I'm not normally for gatekeeping, but ejecting is worth avoiding.

How do you stop running create react app?

To stop this from running, in the command prompt, type CTRL-C. You will get the prompt to Terminate batch job: Type Y and the app will stop running: You can now run npm start again if you want to relaunch the app.

What to remove from create react app?

If you've previously installed create-react-app globally via npm install -g create-react-app , we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.


2 Answers

It can be done by extending your CRA with react-app-rewired package which allows you to modify webpack config.

Changes needed to remove hash in build file names.

  1. Install react-app-rewired

npm install react-app-rewired --save-dev

  1. create config-overrides.js file in your root folder (where package.json is)

  2. place the following code to the config-overrides.js file. It keeps all CRA settings, only remove the hash part from filenames.

    module.exports = function override(config, env) {
        config.output = {
            ...config.output, // copy all settings
            filename: "static/js/[name].js",
            chunkFilename: "static/js/[name].chunk.js",
        };
        return config;
    };
  1. use the new config. In the package.json file in scripts section replace "build": "react-scripts build", with "build": "react-app-rewired build",

Unless you are going to change more configuration, it is enough to only use react-app-rewired in build. Otherwise replace react-scripts with react-app-rewired in others scripts except eject

like image 145
fandasson Avatar answered Oct 04 '22 05:10

fandasson


This is extended and improved version of Darko's answer. I created it mostly to save time for others who is not fully satisfied with solution mentioned in this comment and didn't have a patience to dig to this comment that solved the issue in much nicer way.

Main idea of this "hacky" approach is to re-write standard react-scripts's webpack configuration on the fly and inject it back to original scripts.

For that you would need to install rewire package from npmjs.org, like so:

npm install rewire --save-dev

Then you create separate build script that will will "wrap" original react build script and make sure that it will relieve corrected webpack configuration. Conventional way is to save this file inside ./scripts folder. So let's call it ./scripts/build.js. It's content:

const rewire = require('rewire');
const path = require('path');

// Pointing to file which we want to re-wire — this is original build script
const defaults = rewire('react-scripts/scripts/build.js');

// Getting configuration from original build script
let config = defaults.__get__('config');

// If we want to move build result into a different folder, we can do that!
// Please note: that should be an absolute path!
config.output.path = path.join(path.dirname(__dirname), 'custom/target/folder');

// If we want to rename resulting bundle file to not have hashes, we can do that!
config.output.filename = 'custom-bundle-name.js';

// And the last thing: disabling splitting
config.optimization.splitChunks = {
    cacheGroups: {
        default: false,
    },
};
config.optimization.runtimeChunk = false;

Then, we should use this build script instead of standard one in our packages.json, something like so:

...
  "scripts": {
    "start": "react-scripts start",
    "build": "node ./scripts/build.js",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
...
like image 25
shytikov Avatar answered Oct 04 '22 06:10

shytikov