Loading...
Loading...
Best practices and guidelines for Webpack module bundler configuration, optimization, and development workflows
npx skill4agent add mindrally/skills webpack-bundlerproject/
├── src/
│ ├── index.js # Main entry point
│ ├── components/ # UI components
│ ├── utils/ # Utility functions
│ ├── styles/ # CSS/SCSS files
│ └── assets/ # Images, fonts, etc.
├── dist/ # Build output (gitignored)
├── webpack.config.js # Main configuration
├── webpack.dev.js # Development config
├── webpack.prod.js # Production config
└── package.jsonmodule.exports = {
entry: {
main: './src/index.js',
vendor: './src/vendor.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
clean: true
}
};module.exports = {
mode: 'production', // or 'development'
// production mode enables tree-shaking, minification, and optimizations
};// Use dynamic imports for on-demand loading
const module = await import('./heavy-module.js');
// With React
const LazyComponent = React.lazy(() => import('./LazyComponent'));optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
}mode: 'production'sideEffects{
"sideEffects": false
}{
"sideEffects": ["*.css", "*.scss"]
}{
"presets": [
["@babel/preset-env", { "modules": false }]
]
}module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: 'ts-loader'
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
}
]
}const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css'
}),
// Use for bundle analysis
new BundleAnalyzerPlugin()
]webpack-bundle-analyzermodule.exports = {
cache: {
type: 'filesystem'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'@': path.resolve(__dirname, 'src')
}
}
};optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true
}),
new CssMinimizerPlugin()
],
moduleIds: 'deterministic',
runtimeChunk: 'single'
}devServer: {
static: './dist',
hot: true,
port: 3000,
historyApiFallback: true,
proxy: {
'/api': 'http://localhost:8080'
}
}const webpack = require('webpack');
plugins: [
new webpack.DefinePlugin({
'process.env.API_URL': JSON.stringify(process.env.API_URL)
})
]devtool: 'source-map'webpack-bundle-analyzerstats--mode production