You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constpath=require('path');constconfig={entry: './src',// default entry point is index.js in the src directory if file is not specifiedoutput: {path: path.resolve(__dirname,'dist'),filename: 'bundle.js',// default output filename is main.js},mode: 'production',// default mode is 'production'};module.exports=config;
Section
Property
Description
Core
entry
Entry point(s) for the application (string, array, or object).
output
Where and how to emit bundled files (path, filename, publicPath).
// .babelrc or babel.config.json{"presets": ["@babel/preset-env","@babel/preset-react","@babel/preset-typescript"],"plugins": ["@babel/plugin-transform-runtime","@babel/plugin-proposal-class-properties","@babel/plugin-proposal-object-rest-spread","@babel/plugin-syntax-dynamic-import","@babel/plugin-transform-arrow-functions","@babel/plugin-transform-async-to-generator","@babel/plugin-proposal-optional-chaining","@babel/plugin-proposal-nullish-coalescing-operator"]}
//webpack.config.js
use: 'babel-loader';
Note
all major browsers have already added support for class properties.
Nowadays this feature works out-of-the-box, and you don't have to include @babel/plugin-proposal-class-properties separately in your Webpack configuration.
Plugins in Webpack?
Plugins extend or customize Webpackβs build process.
While loaders transform individual files, plugins can affect the entire bundle or lifecycle (optimization, asset management, injection, etc.).
They hook into Webpackβs compilation lifecycle.
Plugin
Package
Purpose
HtmlWebpackPlugin
html-webpack-plugin
Generates index.html with <script> tags for bundles.
CleanWebpackPlugin
clean-webpack-plugin
Cleans /dist folder before each build.
MiniCssExtractPlugin
mini-css-extract-plugin
Extracts CSS into separate .css files (instead of inline).
Auto-reload or Hot Module Replacement (HMR) when code changes.
Faster rebuilds using memory instead of writing to disk.
Built-in proxying for API requests.
π Itβs meant for development only (not production).
npm install --save-dev webpack-dev-server
npm webpack serve --mode development
// webpack.config.jsconstpath=require('path');module.exports={entry: './src/index.js',output: {filename: 'bundle.js',path: path.resolve(__dirname,'dist'),},mode: 'development',devServer: {port: 3000,// run on http://localhost:3000open: true,// auto-open browserhot: true,// enable Hot Module Replacementstatic: {directory: path.join(__dirname,'public'),// serve static files},},};
Option
Description
port
Port number (default: 8080).
host
Host address (default: localhost).
open
Auto-open browser when server starts.
hot
Enable Hot Module Replacement (update modules without full reload).
liveReload
Reload the page when files change (default: true if HMR off).
static
Serve static files from a folder.
historyApiFallback
Redirect 404s β index.html (useful for SPAs with React/Angular/Vue).
proxy
Forward API requests to a backend server.
compress
Enable gzip compression.
https
Enable HTTPS server.
client
Configure browser overlay for errors/warnings.
How to generate multiple html files
constpath=require('path');constMiniCssExtractPlugin=require('mini-css-extract-plugin');const{ CleanWebpackPlugin }=require('clean-webpack-plugin');constHtmlWebpackPlugin=require('html-webpack-plugin');module.exports={entry: {'hello-world': './src/hello-world.js',kiwi: './src/kiwi.js',},output: {filename: '[name].[contenthash].js',// [name] is the key of entry pointpath: path.resolve(__dirname,'./dist'),publicPath: '',},mode: 'production',module: {rules: [{test: /\.(png|jpg)$/,type: 'asset',parser: {dataUrlCondition: {maxSize: 3*1024,},},},{test: /\.txt/,type: 'asset/source',},{test: /\.css$/,use: [MiniCssExtractPlugin.loader,'css-loader'],},{test: /\.scss$/,use: [MiniCssExtractPlugin.loader,'css-loader','sass-loader'],},{test: /\.js$/,exclude: /node_modules/,use: {loader: 'babel-loader',options: {presets: ['@babel/env'],plugins: ['@babel/plugin-proposal-class-properties'],},},},{test: /\.hbs$/,use: ['handlebars-loader'],},],},plugins: [newMiniCssExtractPlugin({filename: '[name].[contenthash].css',// [name] is the key of entry point}),newCleanWebpackPlugin(),newHtmlWebpackPlugin({filename: 'hello-world.html',// output file name of html filechunks: ['hello-world'],// same name as entry point keytitle: 'Hello world',description: 'some description',template: 'src/page-template.hbs',// reference to how to generate html file}),newHtmlWebpackPlugin({filename: 'kiwi.html',// output file name of html filechunks: ['kiwi'],// same name as entry point keytitle: 'Kiwi',description: 'Kiwi',template: 'src/page-template.hbs',// reference to how to generate html file}),],};
// Automatically split vendor libraries & shared code.module.exports={optimization: {splitChunks: {chunks: 'all',// async, initial, or allminSize: 20000,// minimum size to create a chunk (20kb default)maxSize: 0,// no max size by defaultminChunks: 1,// must be shared at least oncecacheGroups: {vendors: {test: /[\\/]node_modules[\\/]/,name: 'vendors',chunks: 'all',},commons: {name: 'commons',minChunks: 2,chunks: 'all',},},},},};