Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Webpack is a module bundler that takes all your JavaScript files, CSS, images, and other assets, and bundles them into optimized files for the browser. It transforms, bundles, and packages your code for production.
Combines multiple files into one
Convert modern JS to browser-compatible
Minify, compress, and optimize
Starting point where Webpack begins bundling
Where to save the bundled files
Transform non-JS files (CSS, images, TS)
Perform advanced tasks (minification, optimization)
// webpack.config.js
const path = require('path');
module.exports = {
// Entry point
entry: './src/index.js',
// Output configuration
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true // Clean dist folder before each build
},
// Mode: development or production
mode: 'development',
// Dev server
devServer: {
static: './dist',
port: 3000,
hot: true // Hot module replacement
}
};
// Run with:
// npx webpack (build)
// npx webpack serve (dev server)// webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
// JavaScript with Babel
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
// CSS files
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
// Images
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
},
// Fonts
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource'
}
]
}
};
// Install loaders:
// npm install --save-dev babel-loader @babel/core @babel/preset-env
// npm install --save-dev style-loader css-loaderconst HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './src/index.js',
output: {
filename: '[name].[contenthash].js', // Cache busting
path: path.resolve(__dirname, 'dist'),
clean: true
},
plugins: [
// Generate HTML file
new HtmlWebpackPlugin({
template: './src/index.html',
title: 'My App',
minify: true
}),
// Extract CSS to separate file
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css'
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader, // Instead of style-loader
'css-loader'
]
}
]
}
};
// Install:
// npm install --save-dev html-webpack-plugin
// npm install --save-dev mini-css-extract-pluginconst TerserPlugin = require('terser-webpack-plugin');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
clean: true
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
// Code splitting
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: 10
}
}
},
// Runtime chunk
runtimeChunk: 'single'
},
performance: {
hints: 'warning',
maxEntrypointSize: 512000,
maxAssetSize: 512000
}
};Combine hundreds of files into one or few bundles
ES6+ to ES5, TypeScript to JavaScript, SCSS to CSS
Split code into chunks loaded on demand
See changes instantly without full page reload
Handle images, fonts, CSS with ease
Minify, tree shake, compress for production
Combines files for browser
Optimizes for production
Loaders transform files
Plugins optimize output
webpack.config.js
Entry, output, rules, plugins
Hot module replacement
Fast development server