Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Unclear how to pass options in postcss.config.js (missing usage in docs) #334

Closed
folmert opened this issue Dec 16, 2016 · 10 comments
Closed

Comments

@folmert
Copy link

folmert commented Dec 16, 2016

Instructions posted at http://cssnext.io/usage/ are not very helpful in my case. Sorry, but these docs are very vague, they don't even state in which file should the example be written. These can be obvious things for "insiders", but not for someone new to postcss.

I'm using Webpack 2 and including postcss-cssnext along with other postcss plugins in my postcss.config.js:

module.exports = {
    plugins: [
        require('precss'),
        require('postcss-cssnext')
    ]
};

and this works, but how do I pass options here?

This syntax doesn't work:

module.exports = {
    plugins: [
        require('precss'),
        require('postcss-cssnext')({features: {autoprefixer: {}}})
    ]
};

this neither:

module.exports = {
    plugins: {
        'postcss-precss':  {},
        'postcss-cssnext': {
            features: {
                autoprefixer: {}
            }
        }
    }
};

When I run webpack I get errors like:
TypeError: Cannot read property 'default' of undefined
or
TypeError: [object Object] must be a function, did you require() it ?

@MoOx
Copy link
Owner

MoOx commented Dec 16, 2016

Is postcss.config.js something official?
If so I am open to an improvement in the docs.

That said, cssnext is just a normal postcss plugin. I think there is a bug about that. See webpack-contrib/postcss-loader#138

@piotr-cz
Copy link

piotr-cz commented Jan 4, 2017

@MoOx
Yes, using javascript as a config file is official, for example this is how I use package.json to define build scripts such as

"scripts": {
  "build-css": "postcss --config config/postcss.library.config.js"
}

@folmert
This is what works for me, although I'm not sure why I should include postcss-cssnext when I have to include plugins anyway under the use key

let config = {
  use: [
    'postcss-cssnext',
    'postcss-import',
    'postcss-custom-properties',
    'postcss-nesting',
    'postcss-calc',
  ],
  'autoprefixer': {
    browsers: '> 5%'
  },
  'postcssCssnext': {
    features: {
      autoprefixer: true
    }
  },
  // Other options such as input and output
};

module.exports = config;

@MoOx
Copy link
Owner

MoOx commented Jan 4, 2017

@piotr-cz Feel free to make a PR to show the usage of postcss.config.js in the docs!

@folmert
Copy link
Author

folmert commented Jan 4, 2017

@piotr-cz
I've made it work by installing postcss-load-plugins. Thanks to this plugin I can simply write now in my postcsss.config.js:

module.exports = function () {
    return {
        plugins: [
            require('precss')({}),
            require('postcss-cssnext')({ browsers: ['> 0.05%', 'IE 7'], cascade: false })
        ]
    }
};

@piotr-cz
Copy link

piotr-cz commented Jan 4, 2017

@folmert I was considering postcss-load-config, but after some time got working with postcss-cli

@piotr-cz
Copy link

piotr-cz commented Jan 4, 2017

@MoOx
I'm not sure how to phrase it, I'd add something like this to the docs for on cssnext.io docs Setup > Usage page.


Usage with CLI

It's possible to pass cssnext options in config file as described in postcss-cli docs:

{
    "input": "screen.css",
    "output": "bundle.css",
    "postcssCssnext": {
        "features": {
            "autoprefixer": true
        }
    }
}

@rgalieva
Copy link

Webpack2 config for post-css with ExtractTextPlugin:

{
    test: /\.pcss$/,
    use: ExtractTextPlugin.extract({
        fallbackLoader: 'style-loader',
        loader: [ 
            {loader: 'css-loader'},
            {loader: 'postcss-loader',
                options: {
                    sourceMap: 'inline',
                    plugins: function () {
                        return [
                            require('postcss-import'),
                            require('postcss-cssnext')({
                                customProperties: {
                                    variables: {
                                        c_bg: "#222"
                                    },
                                }
                            })
                        ]
                    }
                }
            }
        ]
    })
}

@rgalieva
Copy link

rgalieva commented Mar 17, 2017

If you prefer store settings in separate js, your config may look like:

// webpack.config.js

const cssSettings = require('../../src/config/vars.js') // before module.exports

{
    loader: 'postcss-loader',
    options: {
        sourceMap: 'inline',
        plugins: function () {
            return [
                require('postcss-import'),
                require('postcss-cssnext')({
                    features: cssSettings
                })
            ]
        }
    }
}
// vars.js

module.exports = {
    customProperties: {
        variables: {
            c_bg: "#222"
        },
    },
    customMedia: {
        extensions: {
            v_small:   "(min-width: 30rem)",
            v_medium:  "(min-width: 48rem)"
        }
    }
}

@pldg
Copy link

pldg commented Feb 8, 2018

Without using additional loaders you can pass options in postcss.config.js like this:

module.exports = {
  plugins: {
    'postcss-import': {},

    'postcss-cssnext': {
      features: {
        customProperties: { preserve: true }
      }
    },
    
    'cssnano': { autoprefixer: false }
  }
}

customProperties refer to "postcss-custom-properties" plugin, you can use the same options of the original plugin https://github.com/postcss/postcss-custom-properties#options

The list of all features / plugins names can be found here: https://github.com/MoOx/postcss-cssnext/blob/master/src/features.js

Maybe the documentation http://cssnext.io/usage/ can be improved?

@MoOx
Copy link
Owner

MoOx commented May 29, 2018

postcss-cssnext has been deprecated in favor of postcss-preset-env. Read more at https://moox.io/blog/deprecating-cssnext/

@MoOx MoOx closed this as completed May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants