Webpack
It is a A static module bundler for modern JavaScript applications
A module bundler takes all of our different files (JavaScript, LESS, CSS, JSX, ESNext, and so on) and turns them into a single file
The two main benefits of bundling are modularity and network performance. It can also put source code through loaders that can transform and compile it
Some of the features are:
Code splitting: Splits up code into different chunks that can be loaded when required. Sometimes these are called as rollups or layers
Minification: Removes whitespace, line breaks, rename lengthy variable names to smaller ones, and unnecessary code to reduce the file size
Feature Flagging: Sends code to one or more-but not all-environments when testing out features
Hot Module Replacement (HMR): Watches for changes in source code. Changes only the updated modules immediately without reloading the browser tab
Modularity: Using module pattern, we can split code into multiple files and import them based on the requirement in each file
Composition: With modules, we can build small, simple, reusable React components that we can compose efficiently into applications. Smaller components are easier to comprehend, test, and reuse. They're also easier to replace down the line when enhancing applications
Speed: Packaging all the application's modules and dependencies into a single client bundle will reduce the load time of an application
Consistency: Since webpack will compile JSX and JavaScript, we can start using tomorrow's JavaScript syntax today. Babel supports a wide range of ESNext syntax, which means we don't have to worry about whether the browser supports our code. It allows developers to consistently use cutting-edge JavaScript syntax
Setup
To get started install webpack and webpack-cli as either global or local dependency
npm install webpack webpack-cli --save-devOnce installed add the below script tags inside package.json file:
webpack
# FOR YARN ADD THIS
node ./node_modules/webpack-cli/bin/cli.jsThe above command will invoke webpack with its default settings and produce results
NOTE
Webpack v5 is being used here
Configuration
We can use webpack out of the box without any user defined configurations
By default:
- It looks for
index.jsfile in thesrcdirectory and starts building from there - Generates the bundled output file called
main.jsinside thedistdirectory - Include all the project dependencies inside
index.jsfile and webpack by default can handle most of these dependencies
If the user wants to override the defaults, customize webpack, or handle dependency, they can create a configuration file
Webpack's configuration file is a JavaScript file that exports a webpack configuration
Create a file called
webpack.config.jsin the root directory of the repository (or anywhere and pass the path to webpack)In the
package.jsonfile add a script that invokes webpack with this configuration file.webpack --config webpack.config.js
Configuration file exports a configuration object:
module.exports = {};entry: Entry point for the webpack. Default:./src/index.jsoutput: Output file and directory. Default:./dist/main.jsmode: Mode in which webpack needs to operate indevelopment,productionornone. Default:productiondevtool: To create source-maps or removeevalfrom output fileloaders: Webpack only understands JavaScript and JSON files by default. Loaders allow webpack to process other types of filesplugins: Plugins can perform wider range of tasks like bundle optimization, asset management and injection of environment variables
Loaders
Loaders help webpack to pre-process files. This allows you to bundle any static resource way beyond JavaScript
Loaders work at the individual file level during or before the bundle is generated
Transpiling
babel-loader: This package allows transpiling JavaScript files using Babel. Usebabel.config.jsfor babel configurationsInstallation:
bashnpm install -D babel-loader @babel/core @babel/preset-envUsage:
javascriptmodule: { rules: [ { test: /\.js$/i, exclude: /node_modules/, use: { loader: "babel-loader", }, }, ], }Configuration:
javascriptmodule.exports = { presets: [ "@babel/preset-env", ["@babel/preset-react", { runtime: "automatic" }], ], };NOTE
Babel uses browserslist to traget browsers
ts-loader: Loads TypeScript
Templating
html-loader: Exports HTML as string. HTML is minimized when the compiler demandsInstallation:
bashnpm install --save-dev html-loaderUsage:
javascriptmodule: { rules: [ { test: /\.html$/i, loader: 'html-loader', }, ], }
Styling
css-loader: Loads CSS files from importsbashnpm install --save-dev css-loaderstyle-loader: Inject CSS into the DOMbashnpm install --save-dev style-loadersass-loader: Loads a Sass/SCSS file and compiles it to CSSbashnpm install --save-dev sass-loader sasspostcss-loader: Loader to process CSS with PostCSSInstallation
bashnpm install --save-dev postcss-loader postcss postcss-preset-envRequires
postcss.config.jsfor configuration:javascriptmodule.exports = { plugins: [require("postcss-preset-env")], };NOTE
PostCSS uses browserslist to traget browsers
Basic styling configuration:
module: {
rules: [
{
test: /\.scss$/i,
use: [
"style-loader",
"css-loader",
"postcss-loader",
"sass-loader",
],
},
],
}Plugins
Plugins work at bundle or chunk level and usually work at the end of the bundle generation process
Plugins can also modify how the bundles themselves are created. Plugins have more powerful control than loaders
