Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using postcss-loader after css-loader

I am using CSS modules with webpack css-loader, then I bundle them with mini-css-extract-plugin. Here is how my config looks like:

{
    test: /\.css$/,
    use: [
      MiniCssExtractPlugin.loader,
      {
        loader: "css-loader",
        options: {
          modules: true,
          localIdentName: "[name]-[local]_[hash:base64:5]",
          imports: true,
          importLoaders: 1
        }
      },
      "postcss-loader"
    ]
  }

For some reason (see comments section) I would really like to apply postcss-loader not to every individual .css file, but to the whole bundle. How can I achieve that?

like image 750
Grundiss Avatar asked May 23 '18 13:05

Grundiss


People also ask

Do I need PostCSS-loader?

It's not required but I do highly recommend the autoprefixer plugin. The loaders allow you to import the specified file types.

What is the use of PostCSS-loader?

PostCSS-loader is a WP-Loader so you can process CSS with PostCSS inside Webpack. i.e. It loads PostCSS into Webpack.

Does Webpack use PostCSS?

For Webpack v4, you have to install postcss-loader v4. Then add the plugin to your webpack config. For example: In the following configuration the plugin postcss-preset-env is used, which is not installed by default.

How do I set up PostCSS?

Setup PostCSS Through NPM scripts in the package. Like for example: -o public/main. css . We can configure our command to run in PostCSS CLI with different options to get our desired result. Now to run the command above, we type npm run <command name> in our terminal.


1 Answers

Thank you all for trying to resolve my problem. After all I've found the solution. I do not use postcss-loader any more, but instead I use OptimizeCssAssetsPlugin like this:

new OptimizeCssAssetsPlugin({
  cssProcessor: postcss([CSSMQPacker]),
}),
like image 77
Grundiss Avatar answered Sep 28 '22 23:09

Grundiss