You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8 KiB

2 years ago
  1. 'use strict'
  2. let CssSyntaxError = require('./css-syntax-error')
  3. let Declaration = require('./declaration')
  4. let LazyResult = require('./lazy-result')
  5. let Container = require('./container')
  6. let Processor = require('./processor')
  7. let stringify = require('./stringify')
  8. let fromJSON = require('./fromJSON')
  9. let Document = require('./document')
  10. let Warning = require('./warning')
  11. let Comment = require('./comment')
  12. let AtRule = require('./at-rule')
  13. let Result = require('./result.js')
  14. let Input = require('./input')
  15. let parse = require('./parse')
  16. let list = require('./list')
  17. let Rule = require('./rule')
  18. let Root = require('./root')
  19. let Node = require('./node')
  20. function postcss(...plugins) {
  21. if (plugins.length === 1 && Array.isArray(plugins[0])) {
  22. plugins = plugins[0]
  23. }
  24. return new Processor(plugins)
  25. }
  26. postcss.plugin = function plugin(name, initializer) {
  27. let warningPrinted = false
  28. function creator(...args) {
  29. // eslint-disable-next-line no-console
  30. if (console && console.warn && !warningPrinted) {
  31. warningPrinted = true
  32. // eslint-disable-next-line no-console
  33. console.warn(
  34. name +
  35. ': postcss.plugin was deprecated. Migration guide:\n' +
  36. 'https://evilmartians.com/chronicles/postcss-8-plugin-migration'
  37. )
  38. if (process.env.LANG && process.env.LANG.startsWith('cn')) {
  39. /* c8 ignore next 7 */
  40. // eslint-disable-next-line no-console
  41. console.warn(
  42. name +
  43. ': 里面 postcss.plugin 被弃用. 迁移指南:\n' +
  44. 'https://www.w3ctech.com/topic/2226'
  45. )
  46. }
  47. }
  48. let transformer = initializer(...args)
  49. transformer.postcssPlugin = name
  50. transformer.postcssVersion = new Processor().version
  51. return transformer
  52. }
  53. let cache
  54. Object.defineProperty(creator, 'postcss', {
  55. get() {
  56. if (!cache) cache = creator()
  57. return cache
  58. }
  59. })
  60. creator.process = function (css, processOpts, pluginOpts) {
  61. return postcss([creator(pluginOpts)]).process(css, processOpts)
  62. }
  63. return creator
  64. }
  65. postcss.stringify = stringify
  66. postcss.parse = parse
  67. postcss.fromJSON = fromJSON
  68. postcss.list = list
  69. postcss.comment = defaults => new Comment(defaults)
  70. postcss.atRule = defaults => new AtRule(defaults)
  71. postcss.decl = defaults => new Declaration(defaults)
  72. postcss.rule = defaults => new Rule(defaults)
  73. postcss.root = defaults => new Root(defaults)
  74. postcss.document = defaults => new Document(defaults)
  75. postcss.CssSyntaxError = CssSyntaxError
  76. postcss.Declaration = Declaration
  77. postcss.Container = Container
  78. postcss.Processor = Processor
  79. postcss.Document = Document
  80. postcss.Comment = Comment
  81. postcss.Warning = Warning
  82. postcss.AtRule = AtRule
  83. postcss.Result = Result
  84. postcss.Input = Input
  85. postcss.Rule = Rule
  86. postcss.Root = Root
  87. postcss.Node = Node
  88. LazyResult.registerPostcss(postcss)
  89. module.exports = postcss
  90. postcss.default = postcss