如何迁移 postcss.plugin 到 postCSS 8 格式
How to migrate postcss.plugin to postCSS 8 format
我正在尝试将旧的 postCSS 插件迁移到新版本以使单元测试成功。我的 index.js 看起来像这样:
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
当 运行 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)
...
我对 JavaScript 方面的 postCSS 插件和单元测试有点陌生。我试过使用 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
回答我自己的问题。终于找到有人用不同的插件做了同样的事情,得到了提示here。
最终代码:
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')
module.exports = function (options) {
var paramer = params(options)
return {
postcssPlugin: 'stylefmt',
Once: function Once (root, _ref) {
var result = _ref.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.postcss = true
这使测试通过。
我正在尝试将旧的 postCSS 插件迁移到新版本以使单元测试成功。我的 index.js 看起来像这样:
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
当 运行 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)
...
我对 JavaScript 方面的 postCSS 插件和单元测试有点陌生。我试过使用 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
回答我自己的问题。终于找到有人用不同的插件做了同样的事情,得到了提示here。
最终代码:
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')
module.exports = function (options) {
var paramer = params(options)
return {
postcssPlugin: 'stylefmt',
Once: function Once (root, _ref) {
var result = _ref.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.postcss = true
这使测试通过。