1

i'm using webpack and would like to minimize css file. At this time it is 2.25MB. I found this optimize-css-assets-webpack-plugin plugin but it din't help and my css file is same size. Here is my settings

'use strict';    
const webpack = require('webpack');
const merge = require('webpack-merge');
const webpackCommon = require('./webpack-common.config');
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = merge(webpackCommon, {
   devtool: 'none',
   plugins: [
      new webpack.optimize.OccurrenceOrderPlugin(),
      new webpack.optimize.AggressiveMergingPlugin(),
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin({
         /\.js$/,
         beautify: false,
         comments: false,
         compress: {
            unused: true,
            dead_code: true,
            warnings: false,
            screw_ie8: true
         }
      }),
      new webpack.NoErrorsPlugin(),
      new webpack.DefinePlugin({
         'process.env.NODE_ENV': JSON.stringify('production'),
         '__DEV__': false
      }),
      new webpack.optimize.CommonsChunkPlugin({
         minChunks: 2,
         maxChunks: 15,
         minChunkSize: 10000,
         name: 'vendor'
      }),
      new OptimizeCssAssetsPlugin({
         assetNameRegExp: /\.scss$/g,
         cssProcessor: require('cssnano'),
         cssProcessorOptions: { discardComments: { removeAll: true } },
         canPrint: true
      })
   ]
});

this is prod config and it requires common config file. Here it is

'use strict';

const path = require('path')
const webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
   entry: {
      main: './src/main.js',
   },
   output: {
      filename: '[name].js',
      path: path.resolve(__dirname, '../../PashaGraph/WebClient/public')
   },
   watch: true,
   watchOptions: {
      aggregateTimeout: 100
   },
   plugins: [
      new ExtractTextPlugin("[name].css")
   ],
   module: {
      loaders: [
         {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel'
         },
         {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap')
         },
         {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', 'css-loader?minimize!sourceMap')
         },
         {
            test: /\.woff2?$|\.ttf$|\.eot$|\.svg$|\.png|\.jpe?g|\.gif$/,
            exclude: /node_modules/,
            loader: 'url-loader'
         }
      ]
   },
   resolve: {
      extensions: ['', '.js', '.jsx']
   }
};

as u see i use sass in my app. Maybe this is a reason why it doesn't reduce size.

Solved! actually i have fonts and they were converted and embedded into css file. Thats why it was such big event with compression (2.25mb). By the way i found this solution to restrict maximal size for embedding

{
            test: /\.woff2?$|\.ttf$|\.eot$|\.svg$|\.png|\.jpe?g|\.gif$/,
            exclude: /node_modules/,
            loader: "url-loader?limit=60&name=fonts/[name]_[hash].[ext]"
         }

3 Answers 3

1

Just run the webpack with -p option. It will build for production with very high compression.

webpack -p

Also, you may need to set the Node_Environment to production else the final build will give some warning message in the browser's console.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for solution. I have also found my own solution and update question with it
1

You can also minify css classnames - that can give up to 40% in bundle size reduction. https://github.com/vreshch/optimize-css-classnames-plugin

2 Comments

nice solution! Ths
how this plugin works with classes from js libs if related css placed inside index.html?
0

Don't use source maps in production code. Remove !sourceMap.

1 Comment

thanks, i did it but my bundle.css file still is same size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.