Loading...
Loading...
Webpack is module bundler tool that transforms your code (and any dependencies in form of JavaScript, CSS, images, etc.) into optimized files ready for browser deployment. Webpack allows simplifying and automating build process, provides support for modern JavaScript standards (ES6/ESNext) and helps split code into modules.
Code Splitting → Splits code into parts to optimize load speed.
Tree Shaking → Removes unused code, reducing bundle size.
Hot Module Replacement (HMR) → Allows updating code in real-time without page reload.
Asset management → Efficiently processes images, fonts and other static files.
Webpack starts with main file (e.g., index.js). This is starting point of your application.
Analyzes project dependencies, tracking import or require in code.
By default Webpack understands only JavaScript and JSON. Loaders allow working with other file types (CSS, images, TypeScript, etc.):
Extend Webpack functionality:
HtmlWebpackPlugin → Generates HTML file with connected bundles.MiniCssExtractPlugin → Extracts CSS into separate files.TerserPlugin → Minifies JavaScript.After processing Webpack creates optimized bundle (e.g., bundle.js) in output folder (/dist).
Development:
Keeps code in readable form, includes sourcemaps, doesn't minify.
Useful for local development and debugging.
Production: Minification and optimization (tree shaking, code splitting), removes comments, often disables source maps. Result is "light" and optimized for production environment.
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js', // Step 1: Entry point
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'), // Step 5: Output files
},
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader' }, // Loader for JS
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }, // Loaders for CSS
],
},
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })], // Plugins
mode: 'development', // or 'production'
};
Webpack remains one of most popular despite more "heavy" setup process, thanks to flexibility and extensive ecosystem.