Throughout this tutorial, we have been manually making changes to our source code, running the build process, and inspecting the build files. It would be more efficient if we could automate this process so that we can immediately see the changes we have made to our source code.
Webpack Dev Server is a development server that comes with webpack. It provides a number of features that make development easier, including:
Overall, webpack-dev-server
is a very useful tool for development. It can save us a lot of time and frustration by making it easier to make changes to our code and see the results of those changes.
Splitting webpack configuration for production and development environments offers several advantages:
So let’s include this in our development workflow to take advantage of all the sweet features supported by webpack.
To improve our webpack configuration, we will divide our existing webpack.config.js
file into three separate files.
webpack.common.js
: This file will contain the base configuration shared by both development and production environments. It will include common settings such as entry points, output paths, and module rules that are applicable to all environments.webpack.dev.js
: This file will focus on development-specific configuration. It will include settings like the development mode, development server options, source maps for better debugging, and any additional development-specific optimizations or plugins.webpack.prod.js
: This file will focus on production-specific configuration. It will include optimizations such as minification, tree shaking, and code splitting to generate optimized bundles. Additionally, it will include production-specific plugins like CSS extraction, file compression, and any other performance-related optimizations.We will rename the existing webpack.config.js
file to webpack.common.js
and relocate it to a dedicated directory named webpack.
To align with the updated configuration structure where the config files are placed inside a webpack
folder, we need to modify the output.path
configuration in the webpack.common.js
file. By specifying the path as path.resolve(__dirname, '../dist')
, we ensure that webpack builds the output in the dist
folder located one directory above the webpack
folder.
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
optimization: {
minimizer: [new CssMinimizerPlugin(), new TerserPlugin()],
},
output: {
filename: 'main.js',
path: path.resolve(__dirname, '../dist')
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
}),
new MiniCssExtractPlugin({
filename: 'index.css',
}),
new PurgeCSSPlugin({
paths: ['./src/index.html'],
}),
],
}
Inside the webpack directory, we will create two new files, namely webpack.dev.js
and webpack.prod.js
.
The webpack.dev.js
file will inherit all the configuration settings from the webpack.common.js
file and will set the mode to development.
Similarly, the webpack.prod.js
file will also inherit all the configuration settings from the webpack.common.js
file but will set the mode to production.