31

I am having problem minimizing the css file output by the extract-text-webpack-plugin

/* webpack.config.js */
...
loader: [{test: /\.css$/, loader: ExtractTextPlugin.extract('css?minimize')}]
...
plugins: [new ExtractTextPlugin("styles.css")]
...

/* test.js */
require('./file1.css')
/* file1.css */
@import './file2.css';
body {color: green;}
body {font-size: 1rem;}

/* file2.css */
body {border: 1px solid;}
body {background: purple;}

/* the output styles.css */
body{color:green;font-size:1rem}body{border:1px solid;background:purple}

In the resulting styles.css, there are 2 body tags. It seems that minifications are performed within a file (within file1.css and within file2.css) but not when the two files are combined and extracted into the final styles.css.

How can minification be performed on the final style.css? So the output is

body{color:green;font-size:1rem;border:1px solid;background:purple}

3 Answers 3

48
+500

You could use optimize-css-assets-webpack-plugin, which was created to solve this exact issue.

plugins: [
    new ExtractTextPlugin("styles.css"),
    new OptimizeCssAssetsPlugin()
]
Sign up to request clarification or add additional context in comments.

4 Comments

Will test against using cssnano standalone after webpack. If equivalent, I guess it will be the best answer and I will award you the bounty. Thanks for the info. Out of curiosity, the project was created 4 days ago (for reference, January 8th, 2016), how did you find it?
I googled the issue, and strangely enough this project was on the first page of Google. I must have used the right set of keywords :)
I can confirm that this as the same net result as running cssnano on the generated bundle.css file ... but directly within webpack. This is excellent news! Bounty awarded.
Should it work with sourceMap option? Because for me optimize-css-assets-webpack-plugin and cssnano break source maps - when I use the first one all rules point to compiled .css file instead .scss source file, when I use cssnano rules point to source file, but always to the end of the file.
2

For css minification you may use the webpack's CSS-loader with the "minimize" option. It solved the problem in my case:

webpack.config.js

...
module: {
   rules: [
      {
         test: /\.css$/,
         use: [{
            loader: "css-loader",
            options: {
               minimize: true
            }
         }
      },
   ]
},
...

2 Comments

This solved it for me! And perhaps an easier solution instead of installing another plugin.
As of May 2019, css-loader does not have such option. github.com/webpack-contrib/css-loader#options
0

Paul's answer has stopped working with breaking change in 1.0.0 Minimize and some other options has been removed from options.

The recommended solution is to use optimize-cssnano-plugin. This plugin works better with source maps than optimize-css-assets-webpack-plugin.

Example:

plugins: [
    new ExtractTextPlugin("styles.css"),
    new OptimizeCssnanoPlugin({
        sourceMap: nextSourceMap,
        cssnanoOptions: {
        preset: ['default', {
            discardComments: {
            removeAll: true,
            },
        }],
        },
    }),
]

Comments

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.