Webpack User Manual
Core Concepts
Entry Points
An entry point indicates which module webpack should use to begin building its internal dependency graph. By default, the value is ./src/index.js, but you can specify different (or multiple) entry points.
Single Entry:
// webpack.config.js
export default {
entry: './path/to/my/entry/file.js',
};
Multiple Entries:
export default {
entry: {
app: './src/app.js',
admin: './src/admin.js'
}
};
Output Configuration
The output property tells webpack where to emit the bundles and how to name them. It defaults to ./dist/main.js for the main output file.
import path from "node:path";
export default {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js',
},
};
Loaders
Loaders allow webpack to process non-JavaScript files and convert them into valid modules.
Basic Loader Configuration:
export default {
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
{ test: /\.(png|svg|jpg|jpeg|gif)$/i, type: 'asset/resource' }
],
},
};
Plugins
Plugins perform a wider range of tasks like bundle optimization, asset management, and environment variable injection.
Common Plugins:
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import webpack from "webpack";
export default {
plugins: [
new HtmlWebpackPlugin({ template: './src/index.html' }),
new MiniCssExtractPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
],
};
Mode
The mode parameter enables built-in optimizations:
- development: Enables useful error messages and fast rebuilds
- production: Enables minification, scope hoisting, and other optimizations
- none: Disables all default optimizations
export default {
mode: 'production',
};
Advanced Features
Code Splitting
Webpack provides multiple ways to split your code:
Entry Points: Manually split code by creating multiple entry points.
Prevent Duplication: Use SplitChunksPlugin to remove duplicate dependencies.
Dynamic Imports: Split code on demand using dynamic imports:
// Before
import _ from 'lodash';
// After
const _ = await import('lodash');
Caching
Webpack provides several strategies for caching:
- Output Filenames: Use
[contenthash]to create unique filenames - SplitChunks: Separate vendor and application code
- Runtime Chunk: Extract webpack runtime into separate chunk
Development Tools
Source Maps: Enable source maps for easier debugging:
export default {
devtool: 'inline-source-map',
};
Development Server: Use webpack-dev-server for live reloading:
npm install --save-dev webpack-dev-server
// package.json
{
"scripts": {
"start": "webpack serve --open"
}
}
Configuration Files
Basic Configuration
// webpack.config.js
import path from "node:path";
export default {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [],
mode: 'development'
};
Environment-Specific Configurations
Create separate configurations for different environments:
webpack.common.js: Shared configurationwebpack.dev.js: Development-specific settingswebpack.prod.js: Production-specific settings
Use webpack-merge to combine configurations:
import { merge } from 'webpack-merge';
import common from './webpack.common.js';
export default merge(common, {
mode: 'development',
devtool: 'inline-source-map',
});
Version Differences
Webpack v5 vs Webpack v4
| Feature | Webpack v5 | Webpack v4 |
|---|---|---|
| Module System | ES Modules + CommonJS | CommonJS only |
| Caching | Persistent caching | Memory caching |
| Asset Handling | Built-in asset modules | Requires file-loader/url-loader |
| Performance | Faster builds with persistent cache | Standard performance |
| Module Federation | Built-in support | Not available |
| Node.js Support | Node.js 10.13.0+ | Node.js 6.11.5+ |
Migration Considerations
- Update loader configurations to use asset modules instead of file-loader/url-loader
- Review plugin compatibility with Webpack v5
- Update configuration syntax if migrating to ES Modules
- Test thoroughly as some plugins may have breaking changes
