Last updated
What Does the Webpack Config Generator Create?
The Webpack Config Generator creates customized webpack.config.js files for React, Vue, TypeScript, and vanilla JavaScript projects. It configures entry points, output, loaders, plugins, code splitting, and separate dev/production builds — eliminating the need to research webpack's complex configuration API.
React + TypeScript webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isDev = process.env.NODE_ENV !== 'production';
module.exports = {
entry: './src/index.tsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: isDev ? '[name].js' : '[name].[contenthash].js',
chunkFilename: isDev ? '[name].chunk.js' : '[name].[contenthash].chunk.js',
publicPath: '/',
clean: true,
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.jsx'],
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: [
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
],
},
{
test: /\.(png|jpg|gif|svg|webp)$/,
type: 'asset/resource',
generator: {
filename: 'images/[name].[hash][ext]',
},
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name].[hash][ext]',
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
favicon: './public/favicon.ico',
}),
!isDev && new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash].css',
}),
].filter(Boolean),
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
devServer: {
port: 3000,
hot: true,
historyApiFallback: true,
open: true,
},
devtool: isDev ? 'eval-source-map' : 'source-map',
mode: isDev ? 'development' : 'production',
};
Vue.js webpack.config.js
const { VueLoaderPlugin } = require('vue-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/main.js',
output: {
filename: '[name].[contenthash].js',
clean: true,
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['vue-style-loader', 'css-loader'],
},
],
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({ template: './public/index.html' }),
],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
};
Separate Dev and Production Configs
// webpack.common.js — shared config
module.exports = {
entry: './src/index.js',
resolve: { extensions: ['.js', '.jsx'] },
module: {
rules: [
{ test: /\.jsx?$/, use: 'babel-loader', exclude: /node_modules/ },
],
},
plugins: [new HtmlWebpackPlugin({ template: './public/index.html' })],
};
// webpack.dev.js
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'eval-source-map',
devServer: { port: 3000, hot: true, historyApiFallback: true },
});
// webpack.prod.js
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = merge(common, {
mode: 'production',
devtool: 'source-map',
output: {
filename: '[name].[contenthash].js',
clean: true,
},
plugins: [new MiniCssExtractPlugin({ filename: '[name].[contenthash].css' })],
optimization: {
minimizer: ['...', new CssMinimizerPlugin()],
},
});
Generated package.json Scripts
{
"scripts": {
"start": "webpack serve --config webpack.dev.js",
"build": "webpack --config webpack.prod.js",
"build:analyze": "webpack --config webpack.prod.js --env analyze",
"type-check": "tsc --noEmit"
},
"devDependencies": {
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-merge": "^5.10.0",
"html-webpack-plugin": "^5.5.4",
"mini-css-extract-plugin": "^2.7.6",
"css-loader": "^6.8.1",
"style-loader": "^3.3.3",
"ts-loader": "^9.5.1",
"babel-loader": "^9.1.3",
"webpack-bundle-analyzer": "^4.10.1"
}
}
Module Federation (Micro-Frontends)
// Host app webpack.config.js
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'host',
remotes: {
checkout: 'checkout@http://localhost:3001/remoteEntry.js',
catalog: 'catalog@http://localhost:3002/remoteEntry.js',
},
shared: { react: { singleton: true }, 'react-dom': { singleton: true } },
}),
],
};
// Remote app (checkout) webpack.config.js
new ModuleFederationPlugin({
name: 'checkout',
filename: 'remoteEntry.js',
exposes: {
'./CheckoutPage': './src/CheckoutPage',
'./CartWidget': './src/CartWidget',
},
shared: { react: { singleton: true } },
})
Common Use Cases
- Starting a new React or Vue project with optimized webpack config
- Migrating from webpack 4 to webpack 5
- Adding TypeScript support to an existing webpack project
- Setting up code splitting and lazy loading
- Configuring micro-frontend architecture with Module Federation