3

Im new to react.js and currently working on a react.js project which runs 100% ok with development environment without any errors. After i build the project in production mode it will not minify my javascript files. But my scss files are get minified.

const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "production",
  entry: {
    main: "./src/index.js",
    vendor: "./src/vendor.js",
  },
  output: {
    filename: "[name].[contentHash].bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.html$/,
        use: ["html-loader"],
      },
      {
        test: /\.(svg|png|jpg|gif)$/,
        use: {
          loader: "file-loader",
          options: {
            name: "[name].[hash].[ext]",
            outputPath: "imgs",
          },
        },
      },
      {
        test: /\.scss$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
      },
    ],
  },
  optimization: {
    minimizer: [
      new OptimizeCssAssetsPlugin(),
      new HtmlWebpackPlugin({
        template: __dirname + "/app/index.html",
        minify: {
          removeAttributeQuotes: true,
          collapseWhitespace: true,
          removeComments: true,
        },
      }),
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({ filename: "[name].[contentHash].css" }),
    new CleanWebpackPlugin(),
  ],
};

Please someone help me to solve this problem and sorry for my bad English.

4
  • Can u please put your development webpack config file as well? Commented Nov 1, 2020 at 5:56
  • 1
    There isn't any big deff compare to my dev and prod config files. only difference is i dont have opitmization key in my dev file. thats it.
    – Chadwick
    Commented Nov 1, 2020 at 5:59
  • running a react application is a little bit fishy with the all above configurations. You have mentioned that your project is running without any error. Are you sure that you don't have any other webpack files that pointing to while you run in development mode? Commented Nov 1, 2020 at 6:06
  • You can add default minimizers by adding in your array of minimizers '...'. "Basically, '...' is a shortcut to access the default configuration value webpack would otherwise set for us." — Optimization | webpack
    – mems
    Commented Apr 13, 2023 at 16:54

3 Answers 3

6

The default javascript minimizer plugin for webpack is TerserPlugin. When you setting minimizer attribute under the optimizaton, it will override the default values provided by webpack itself. To overcome this problem add TerserPlugin to your webpack.config.js file and pass it to the minimizer attribute.

const TerserPlugin = require("terser-webpack-plugin");

optimization: {
  minimizer: [
    new TerserPlugin(),
    new OptimizeCssAssetsPlugin(),
    new HtmlWebpackPlugin({
      minify: {
        removeAttributeQuotes: true,
        collapseWhitespace: true,
        removeComments: true,
      },
    }),
  ],
},
2
  • As per webpack documentation terser will only minify your js files and not CSS file. He is using MiniCssExtractPlugin for separating CSS and js files in build. How will it help to minify CSS files? Commented Nov 1, 2020 at 6:42
  • Yes @DhruviMakvana, you are right. terser is not responsible for CSS and I posted my answer to resolve only his js minify issue. not related to CSS minification. In the question also he is stating about a js minification problem. not about a CSS minification problem. Commented Nov 1, 2020 at 7:04
1

It seems like you only have minimization for your scss files, if you want to minify your javascript files aswell, you'll need a plugin such as Uglify, you just import it on the top and add it to the optimization.minimizer like it says in plugin page example:

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
 
module.exports = {
  optimization: {
    minimizer: [new UglifyJsPlugin()],
  },
};
0
0

MiniCssExtractPlugin will extract your CSS into a separate file and It creates a CSS file per JS file which contains CSS. It won't minify your CSS files.

Import css-minimizer-webpack-plugin for minification. You can configure minification based on your environment.

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const { getIfUtils } = require('webpack-config-utils')
const {ifProduction, ifNotProduction} = getIfUtils(process.env.NODE_ENV)

module.exports = {
  module: {
    loaders: [
      {
        test: /.s?css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
      },
    ],
  },
  optimization: {
    minimize: ifProduction(true, false),
    minimizer: [
      new CssMinimizerPlugin(),
    ],
  },
};

1
  • I see that your minimizer list only contains the cssMinimizerPlugin and does not extend the default terser minimizer. Will this also minify javascript files in prod which seems to be the OP's question? If so, how? Commented Feb 3, 2021 at 20:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.