Skip to content

Instantly share code, notes, and snippets.

@michael-ciniawsky
Last active June 10, 2018 09:17
Show Gist options
  • Save michael-ciniawsky/df6f6ee1ed76c348c1849178107f08a9 to your computer and use it in GitHub Desktop.
Save michael-ciniawsky/df6f6ee1ed76c348c1849178107f08a9 to your computer and use it in GitHub Desktop.
PostCSS Config Integration

Node API

Context

Name Description
cwd process.cwd()
env process.env.NODE_ENV

Usage

import postcssrc from 'postcss-load-config'

postcssrc(ctx, path)
  .then((config) => console.log(config.plugins, config.options, config.file))
  .catch((err) => console.log(err))

CLI

Context

Name Description
cwd process.cwd()
env process.env.NODE_ENV
file extname, dirname, basename (vinyl-like)
options Custom Context

Usage

module.exports = (ctx) => ({
  parser: ctx.file.extname === '.sss' ? 'sugarss' : false
  plugins: {
    'postcss-import': { root: ctx.file.dirname }
    'postcss-assets': ctx.options.assets
    'cssnano': ctx.env === 'production' ? ctx.options.minify : false
  }
})
postcss input.css -o output.css --ctx [options]

TODO

  • Add ctx.file
  • Add ctx.options

webpack

Context

Name Description
cwd process.cwd()
env process.env.NODE_ENV
file extname, dirname, basename (vinyl-like)
options Custom Context

Usage

module.exports = (ctx) => ({
  parser: ctx.file.extname === '.sss' ? 'sugarss' : false
  plugins: {
    'postcss-import': { root: ctx.file.dirname }
    'postcss-assets': ctx.options.assets
    'cssnano': ctx.env === 'production' ? ctx.options.minify : false
  }
})
{
  test: /\.(css|sss}/
  use: [
    'style-loader',
    'css-loader',
    'postcss-loader'
  ]
}

TODO

Gulp

Usage

module.exports = (ctx) => ({
  parser: ctx.file.extname === '.sss' ? 'sugarss' : false
  plugins: {
    'postcss-import': { root: ctx.file.dirname }
    'postcss-assets': ctx.options.assets
    'cssnano': ctx.env === 'production' ? ctx.options.minify : false
  }
})
gulp.task('css', () => {
  const cxt = {}
  
  gulp.src('*.{css|sss}')
    .pipe(postcss(ctx))
    .pipe(gulp.dest('dest'))
})

Grunt

Context

Usage

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment