I am new to react programming.I finished my project and i am trying to understand build optimisation techniques. My application contains graphs,antd,redux libraries. After finishing my project i executed npm run build and npm run analyze, bundle combined size is showing 3MB. I searched in internet using webpack we can reduce size of the build then i added webpack.config.js to my project and i executed webpack --mode=production, bundle.js size is showing 5MB. I am not understanding which approach i should use.
webpack.config.js:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const withReport = process.env.npm_config_withReport
module.exports = {
// webpack will take the files from ./src/index
entry: './src/index',
// and output it into /dist as bundle.js
output: {
path: path.join(__dirname, 'build'),
filename: 'bundle.js',
publicPath: '/'
},
devServer: {
contentBase: path.join(__dirname, 'output'),
compress: true,
port: 3000,
historyApiFallback: true,
publicPath: '/'
},
// adding .ts and .tsx to resolve.extensions will help babel look for .ts and .tsx files to transpile
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
// we use babel-loader to load our jsx and tsx files
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
},
},
// css-loader to bundle all the css files into one file and style-loader to add all the styles inside the style tag of the document
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ico)$/,
exclude: /node_modules/,
loader: "file-loader"
},
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"]
}
]
},plugins: [
new HtmlWebpackPlugin({
template: path.resolve( __dirname, 'public/index.html' ),
filename: 'index.html',
favicon: "./public/appicon.png",
manifest: "./public/manifest.json"
}),
withReport ? new BundleAnalyzerPlugin() : '',
],
optimization: {
usedExports: true
}
};
npm run build ===> first approach. ==>. 3 MB "build": "webpack --mode=production", ===> second approach ==> 5.2 MB