Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using autoprefixer with postcss in webpack 2.x

Tags:

How to use autoprefixer with webpack 2.x.

Previously, it used to be something like this...

...  module: {   loaders: [      {        test: /\.scss$/,         loader: 'style!css!sass!postcss'      }    ] }, postcss: () => {   return [autoprefixer] },  ... 

But, it's not working anymore.

How to rewrite it to [email protected]?

like image 860
Pranesh Ravi Avatar asked Nov 23 '16 10:11

Pranesh Ravi


People also ask

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.

What is PostCSS Webpack?

PostCSS is: a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.

What is PostCSS used for?

PostCSS is a JavaScript tool that transforms your CSS code into an abstract syntax tree (AST) and then provides an API (application programming interface) for analyzing and modifying it using JavaScript plugins.


1 Answers

There is no more need to use LoaderOptionsPlugin. Now you can pass options directly to the loader declaration.

const autoprefixer = require('autoprefixer');  let settings = {     /*...*/     module: {         rules: [{             test: /\.css$/,             use: [                 /*...other loaders...*/                 {                     loader: 'postcss-loader',                     options: {                         plugins: function () {                             return [autoprefixer]                         }                     }                 }                 /*...other loaders...*/             ]         }]}              }     /*...*/ }; 

In case if you need to provide specific compatibility configuration, you can pass it as argument to autoprefixer function:

options: {     plugins: function () {         return [autoprefixer('last 2 versions', 'ie 10')]     } } 
like image 112
kreig Avatar answered Sep 21 '22 07:09

kreig