UNPKG

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