Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack build vs react-scripts build

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.

like image 670
magpie Avatar asked Sep 05 '18 04:09

magpie


People also ask

Do react-scripts use Webpack?

react-scripts is using webpack version "4.44. 2" instead of "^5.61.

Which is better create react app or Webpack?

Configuring webpack provides complete control over the development environment, while initializing with Create React App prevents any custom configuration by default.

What does react-scripts build do?

react-scripts are simply scripts to run the build tools required to transform React JSX syntax into plain JavaScript programmatically. When you run one of the scripts, the /bin/react-scripts. js will be executed to start the process. This script will look into the arguments that you passed into the call.

What is the difference between react and Webpack?

Webpack is a module bundler whereas react-script is an npm package with dependencies a react-based project may need to quickly start a project like babel, and webpack in the list of react-script dependencies.


1 Answers

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.

like image 63
hazardous Avatar answered Sep 29 '22 08:09

hazardous