Have you ever zipped all your files and folders into a single file? Why do you do that? A simple answer can be to bundle them together. Also, have you ever encountered that there are some places at web where you need to upload a file (let’s say your passport-sized photograph) and you are given a size limit that the uploaded file should not exceed (let’s say 2MB)? What do you do in case your file is larger than the given size limit? You reduce its size, don’t you?
You’ll see similar processes in this segment. You’ll learn about the processes of bundling and minification. Also, you’ll learn about the role that Webpack plays in a React application.
So far, you learned how bundling reduces the number of HTTP requests sent by the client to the server.
You looked at an example where a webpage references 10 stylesheets and 20 script files. This makes a total of 31 files (= 1 webpage + 10 stylesheets + 20 scripts) to be referenced. A batch size is the number of files that a browser can reference in parallel. Considering this batch size to be 6, you need to make a total of 31/6 which is equal to 6 (ceil of 5.17) HTTP requests.
If you use the bundling technique, you can bundle all the stylesheets into one file and all the scripts into one file. Thus, after bundling, you need to reference 3 files which is equal to the sum of 1 webpage + 1 bundled stylesheet (containing all 10 stylesheets) + 1 bundled script (containing all 20 scripts). Now, with ideal batch size as 6, you need to make only 3/6 which is equal to 1 (ceil of 0.5) HTTP request. Thus, as seen in this example, bundling reduces the number of HTTP requests from 6 to 1.
You learned how minification helps in the process of shortening file’s contents, resulting in faster response time and lower bandwidth cost. You also saw how Webpack bundles your files into a single file in a React application.
In the next video, you’ll learn the importance of debugging your application beforehand. You’ll also learn what a Lint is and that ESLint is being popularly used in React applications.
ESLint, as explained in the last video, helps you to follow the coding guidelines and principles while making you commit fewer errors and mistakes.
For instance, consider a code snippet given below:
var name = "UpGrad"; var name = "UpGrad Education"; console.log(name);
When this is written in a React application that has ESLint configured in its environment, then one can see the message as given below:
'name' is already defined no-redeclare
no-redeclare is one of the ESLint rules. Such rules are applied to a lot of different code style use cases. In order to see the list of all available rules provided by ESLint, click here.
In case you don’t want to specify your own set of rules every time, there are plenty of recommendations out there. One such recommendation is provided by Airbnb Style Guide. Airbnb provides a bunch of ESLint presets that cover ES6, JSX, etc., which makes it a great choice for React projects. It also helps by providing performance tips. Airbnb open sourced its own ESLint configuration so that it can be used by anyone. Check out this link to learn more about Airbnb JavaScript Style Guide.
Additional Resources: