mardi 20 octobre 2020

How to migrate postcss.plugin to postCSS 8 format

I'm trying to migrate old postCSS plugin to a new version to get unit tests to succeed. My index.js looks like this:

const postcss = require('postcss')

const params = require('./lib/params')
const formatAtRules = require('./lib/formatAtRules')
const formatOrder = require('./lib/formatOrder')
const formatRules = require('./lib/formatRules')
const formatComments = require('./lib/formatComments')
const formatSassVariables = require('./lib/formatSassVariables')

const stylefmt = postcss.plugin('stylefmt', function (options) {
  var paramer = params(options)
  return function (root, result) {
    return paramer(root, result).then(function (params) {
      if(params) {
        formatComments(root, params)
        formatAtRules(root, params)
        formatRules(root, params)
        formatSassVariables(root, params)
        // order should be the last to prevent empty line collapse in order rules
        formatOrder(root, params)
      }
    }).catch(function (err) {
      console.error(err.stack)
    })
  }
})

module.exports = stylefmt

This throws errors like this in console when running tape test/*.js |faucet:

Error: postcss.plugin was deprecated. Migration guide: https://evilmartians.com/chronicles/postcss-8-plugin-migration 
    ---
      operator: error
      at: <anonymous> (/Users/rolle/Projects/stylefmt/test/cli.js:11:9)
      stack: |-
        Error: postcss.plugin was deprecated. Migration guide:
        https://evilmartians.com/chronicles/postcss-8-plugin-migration
        
            at ChildProcess.<anonymous> (/Users/rolle/Projects/stylefmt/test/cli.js:151:16)
            at ChildProcess.emit (events.js:198:13)
            at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
    ...

I'm kinda new to postCSS plugins and unit testing in JavaScript side. I have tried using migration guide with no avail. So, how to convert this piece to postCSS 8 format so the tests would pass? Here's my repo: https://github.com/ronilaukkarinen/stylefmt

Aucun commentaire:

Enregistrer un commentaire