-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
parsers.js
122 lines (117 loc) · 2.79 KB
/
parsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Results on Node 20.3.1, Github Actions:
Stylis: 15 ms (1.8 times faster)
CSSOM: 24 ms (1.2 times faster)
PostCSS: 28 ms
CSSTree: 37 ms (1.3 times slower)
Mensch: 37 ms (1.3 times slower)
Rework: 49 ms (1.8 times slower)
Stylecow: 73 ms (2.6 times slower)
PostCSS Full: 95 ms (3.4 times slower)
ParserLib: 153 ms (5.5 times slower)
Gonzales: 177 ms (6.4 times slower)
*/
let { existsSync, readFileSync } = require('node:fs')
let postcssSP = require('postcss-selector-parser')
let postcssVP = require('postcss-value-parser')
let parserlib = require('parserlib')
let stylecow = require('stylecow-core')
let gonzales = require('gonzales')
let { join } = require('node:path')
let csstree = require('css-tree')
let postcss = require('postcss')
let rework = require('rework')
let mensch = require('mensch')
let stylis = require('stylis')
let CSSOM = require('cssom')
let example = join(__dirname, 'cache', 'bootstrap.css')
let css = readFileSync(example).toString()
module.exports = {
maxTime: 15,
name: 'Parsers',
tests: [
{
fn: () => {
rework(css).toString()
},
name: 'Rework'
},
{
defer: true,
fn: done => {
postcss.parse(css, { from: example }).toResult()
done.resolve()
},
name: 'PostCSS'
},
{
defer: true,
fn: done => {
let root = postcss.parse(css, { from: example })
root.walk(node => {
if (node.type === 'rule') {
node.selectorAST = postcssSP().process(node.selector)
} else if (node.type === 'decl') {
node.valueAST = postcssVP(node.value)
}
})
root.toResult()
done.resolve()
},
name: 'PostCSS Full'
},
{
fn: () => {
CSSOM.parse(css).toString()
},
name: 'CSSOM'
},
{
fn: () => {
mensch.stringify(mensch.parse(css))
},
name: 'Mensch'
},
{
fn: () => {
gonzales.csspToSrc(gonzales.srcToCSSP(css))
},
name: 'Gonzales'
},
{
fn: () => {
csstree.generate(csstree.parse(css))
},
name: 'CSSTree'
},
{
fn: () => {
new parserlib.css.Parser().parse(css)
},
name: 'ParserLib'
},
{
fn: () => {
stylecow.parse(css).toString()
},
name: 'Stylecow'
},
{
fn: () => {
stylis.serialize(stylis.compile(css), stylis.stringify)
},
name: 'Stylis'
}
]
}
let devPath = join(__dirname, '../postcss/lib/postcss.js')
if (existsSync(devPath)) {
let devPostcss = require(devPath)
module.exports.tests.splice(1, 0, {
defer: true,
fn: done => {
devPostcss.parse(css, { from: example }).toResult()
done.resolve()
},
name: 'Next PostCSS'
})
}