Skip to content

Passing options to plugins on the command line #144

@ao2

Description

@ao2

Hi,

IMHO it is not very clear how to pass options to plugins enabled with the --use option.

I experienced this with autoprefixer: at the beginning I was having a hard time figuring out how to pass the "browsers" option to it.

Another postcss-cli implementation (https://github.com/pirxpilot/postcss-cli) uses the syntax --${plugin}.option value which in my case would be something like --autoprefixer.browsers "> 1%", but that didn't work with the "official" postcss-cli.

I later figured out a way to solve my particular problem using environment variables:

BROWSERSLIST="invalid" \
./bin/postcss --no-map --use autoprefixer < ../../css/style.css

However it would be handy if the other mechanism was supported too, which could even be applied to other plugins.

As an example, the following command line currently has the same effect as not passing any browsers option at all:

./bin/postcss --no-map --use autoprefixer --autoprefixer.browsers "invalid" < ../../css/style.css

A patch like the following improves the situation:

--- index.js.orig	2017-07-04 17:09:22.328881652 +0200
+++ index.js	2017-07-04 17:22:13.402544610 +0200
@@ -153,7 +153,7 @@ let config = {
   plugins: argv.use
     ? argv.use.map((plugin) => {
       try {
-        return require(plugin)()
+        return require(plugin)(argv[plugin])
       } catch (e) {
         error(`Plugin Error: Cannot find module '${plugin}'`)
       }

After that the option is passed along: to verify this the extreme case of an invalid query is reported to the user:

⠋ Processing stdinYour current PostCSS version is 6.0.5, but autoprefixer uses 5.2.17. Perhaps this is the source of the error below.
✖ Processing stdin
{ [BrowserslistError: Unknown browser query `invalid`]
  name: 'BrowserslistError',
  message: 'Unknown browser query `invalid`',
  browserslist: true }

I am not a Javascript developer, so I don't know if the patch above is the best solution.

Thanks,
Antonio

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions