Webpack is a module bundler that builds a dependency graph starting from entry points and bundles modules into output files. Core concepts: Entry (starting point), Output (result), Loaders (transform files), Plugins (extend functionality), and Mode (optimization level).
Core Concepts:
How It Works:
Common Loaders:
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
// Entry point(s)
entry: {
main: './src/index.js',
admin: './src/admin.js',
},
// Output configuration
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
clean: true, // Clean dist folder
},
// Mode: development or production
mode: 'production',
// Module rules (loaders)
module: {
rules: [
// JavaScript/JSX with Babel
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
// CSS
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
// Images
{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource',
},
],
},
// Plugins
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
}),
],
};