Webpack build vs react-scripts build

magpie picture magpie · Sep 5, 2018 · Viewed 23.7k times · Source

Say the webpack config is as follows

{
    entry: path.join(__dirname, 'src', 'index.js'),
    output: {
    path: path.join(__dirname, 'build'),
    filename: 'bundle.js'
},

Now the build from webpack is

enter image description here

and the build from react-scripts build ( static contains css, js and media in seperate folders )

enter image description here

Question: Is there any specific advantage of webpack over react-scripts build? ( including but not limited to performance )

NOTE: package.json is edited to achieve this.

Answer

hazardous picture hazardous · Sep 5, 2018

Webpack is a general purpose bundler, with applications beyond React. Before create-react-app, the web was full of examples of setting up a brand new React project which uses webpack as the bundler. It is extremely flexible and can handle things including and beyond what a React application would need. It works for Angular, Vue, NodeJS and even Web Assembly.

But it used to take a while to setup. You will need to understand its working and configure it so that you can transpile your React+ES6 code into plan-vanilla JS. You would need to decide the output structure you like and configure webpack for it. And then also add hot-module-reloading and code-splitting support yourself. During this, you will also need to add other plugins required by Webpack to support all the above :).

This naturally caused some fatigue with folks who were starting with React.

So facebook created cra which internally uses webpack, pre-configured to include all the nice tools to take care of these basics and help you focus on the React part of your code. It hides webpack from you as much as possible, otherwise the build process may break if the configuration is changed by the user.

With that aside, the structural conventions which cra uses should not be having any performance impact over a bare-bones webpack setup. It's just a convention.

Your question should then be, when would I use create-react-app and when would I use Webpack?

As a beginner you might want to stick to cra as you focus on your react app. Eventually there would come a time where what you want to do is not supported by the webpack configuration cra is managing under the hood. A very common example is if you want to write a component library to reuse in other apps. This cannot be done by cra (it's about the whole app :)). You can then switch over to webpack and start learning it.