Before dive in:
In these series parts, I am going to take you with me in a stroll around webpack4, trying to understand and master its features and tools through variety of examples.
What is webpack?
Webpack is a static module bundler for modern JavaScript applications.
Why using webpack?
Before webpack, we can run JavaScript on browser using one of the two ways below:
So what solution was taken from the two ways?
Emmm, combining the two was the taken solution. The idea is simply giving the developer the good experience of splitting its functionalities over multiple files(so that they will be easy to maintain later) and bundling all of them into one script (to overcome network bottleneck as explained above).
In fact this concept is implemented by many tools like Gulp …etc.
But wait! There were not any scope collisions led by combining all files into one?
Absolutely yeah, if IIFE’s (Immediately Invoked Function ExpressionS) were not here! Wrapping script files in IIFE makes combing safe from scope collision.
Emmm, until here it’s ok. But what if I change a small piece of code in one script file?
The answer is you have to rebuild all script files to generate the bundle. That impacts performance negatively the way that browsers have to reload all bundle file again even for a little code changes(loss of bandwidth).
Also there are other performance problems related to this solution that I will list below:
So, how to overcome all these problems?
Let’s begin with resolving the first performance problem.
— Worth to be mentioned —
Node.js which is a JavaScript runtime that can be used in computers and servers outside browsers.
CommonJS comes and introduces require method, which makes loading modules in the Node.js script files simple, solves scope collisions problems and makes code clean since only the needed code is imported.
— Bad to announce —
Sadly, CommonJS is not available for browsers!
Fortunately, tools and bundlers like Browserify, RequireJS and SystemJS were created to make possible using modules in browsers.
— The Big good news —
Using JavaScript modules in browsers becomes an official feature of ECMAScript standard.
So why always webpack(until now at least)?
Bundlers like webpack, still recommended today, as first it’s faster and then not all browsers yet support this ESM (ECMAScript modules) new feature. Furthermore, webpack supports not only JavaScript modules but also any other modules format (CSS, fonts and images).
Also, I can add that webpack cares a lot about loading time and performance so as to deliver the best possible version of your project to the users. That’s why it is always adding and improving features like async chunks loading, bundle splitting, lazy chunk loading.
Finally, webpack maps every module(JavaScript, CSS, Node.js,…) and every assets(images, fonts,…) your project needs and generates one or more bundles based on an internal dependency graph that it builds.
Core concepts:
Webpack builds internally a dependency graph for bundling your project.
Webpack v4.0.0 and more does not require a configuration file to bundle your project, nevertheless it is incredibly configurable to better fit any kind of projects complexities.
Webpack is configured through the next properties :
import './filename.css';
. That is to say, without these two loaders you can’t import CSS as modules in your application.test:/\.css$/
use:['style-loader','css-loader']
I can’t wait to see you in the next parts of this webpack4 series.
Thank you for your time.
If you have any feedback, suggestions or questions please let me know!