Webpack 5 Update:
I attempted rrd answer above and ran into an error trying to install uglifyjs-webpack-plugin
as I am using Webpack 5 which appears to not work past Webpack 4 and instead to use the terser-webpack-plugin
which comes with Webpack 5. Below is the modified webpack.config.js
to make this work with Webpack 5.
webpack.config.js
const path = require("path")
const TerserPlugin = require("terser-webpack-plugin");
const glob = require("glob")
module.exports = {
entry: {
"bundle.js": glob.sync("build/static/?(js|css)/main.*.?(js|css)").map(f => path.resolve(__dirname, f)),
},
output: {
filename: "build/static/js/bundle.min.js",
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
}
modified build scripts in package.json
"build": "npm run build:react && npm run build:bundle",
"build:react": "react-scripts build",
"build:bundle": "webpack --config webpack.config.js",
devDependencies in package.json
"devDependencies": {
"css-loader": "^6.7.1",
"glob": "^8.0.3",
"path": "^0.12.7",
"style-loader": "^3.3.1",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2"
}