Skip to content

Commit a945a24

Browse files
authored
Reduce bundle size: 169kB -> 76kB (#217)
1 parent 0dd122b commit a945a24

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as csstree from 'css-tree'
1+
import parse from 'css-tree/parser'
2+
import walk from 'css-tree/walker'
3+
import { property as getProperty } from '../node_modules/css-tree/lib/utils/names.js'
24
import { compareSpecificity } from './selectors/specificity.js'
35
import { analyzeRules } from './rules/rules.js'
46
import { colorFunctions, colorNames } from './values/colors.js'
@@ -66,7 +68,7 @@ const analyze = (css) => {
6668
let totalComments = 0
6769
let commentsSize = 0
6870

69-
const ast = csstree.parse(css, {
71+
const ast = parse(css, {
7072
parseAtrulePrelude: false,
7173
parseCustomProperty: true, // To find font-families, colors, etc.
7274
positions: true, // So we can use stringifyNode()
@@ -96,7 +98,7 @@ const analyze = (css) => {
9698
const units = new ContextCollection()
9799
const embeds = new CountableCollection()
98100

99-
csstree.walk(ast, {
101+
walk(ast, {
100102
enter: function (node) {
101103
switch (node.type) {
102104
case 'Atrule': {
@@ -141,10 +143,9 @@ const analyze = (css) => {
141143
})
142144

143145
const { value, property } = node
144-
const fullProperty = {
145-
authored: property,
146-
...csstree.property(property)
147-
}
146+
const fullProperty = Object.assign({
147+
authored: property
148+
}, getProperty(property))
148149

149150
properties.push(fullProperty)
150151
values.push(value)
@@ -192,7 +193,7 @@ const analyze = (css) => {
192193
}
193194
}
194195

195-
csstree.walk(node.value, {
196+
walk(value, {
196197
enter: function (valueNode) {
197198
switch (valueNode.type) {
198199
case 'Hash': {

src/rules/rules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as csstree from 'css-tree'
1+
import walk from 'css-tree/walker'
22
import { AggregateCollection } from '../aggregate-collection.js'
33

44
const analyzeRules = ({ rules }) => {
@@ -13,7 +13,7 @@ const analyzeRules = ({ rules }) => {
1313
let selectors = 0
1414
let declarations = 0
1515

16-
csstree.walk(rules[i], {
16+
walk(rules[i], {
1717
enter: function (childNode) {
1818
if (childNode.type === 'Selector') {
1919
selectors++

src/selectors/specificity.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as csstree from 'css-tree'
1+
import parse from 'css-tree/parser'
2+
import walk from 'css-tree/walker'
23

34
/**
45
* Compare specificity A to Specificity B
@@ -25,7 +26,7 @@ function compareSpecificity(a, b) {
2526
*/
2627
function selectorListSpecificities(selectorListAst) {
2728
const childSelectors = []
28-
csstree.walk(selectorListAst, {
29+
walk(selectorListAst, {
2930
visit: 'Selector',
3031
enter(node) {
3132
childSelectors.push(analyzeSpecificity(node))
@@ -51,7 +52,7 @@ const analyzeSpecificity = (ast) => {
5152
let complexity = 0
5253
let isA11y = false
5354

54-
csstree.walk(ast, {
55+
walk(ast, {
5556
enter: function (selector) {
5657
switch (selector.type) {
5758
case 'IdSelector': {
@@ -94,9 +95,8 @@ const analyzeSpecificity = (ast) => {
9495

9596
// CSSTree doesn't parse the arguments of :is, :has and :matches,
9697
// so we need to create an AST out of them ourselves
97-
if (['is', 'has', 'matches'].includes(selector.name)) {
98-
const rawSelectorList = csstree.find(selector, ({ type }) => type === 'Raw')
99-
const childAst = csstree.parse(rawSelectorList.value, { context: 'selectorList' })
98+
if (['is', 'has', 'matches', '-webkit-any', '-moz-any'].includes(selector.name)) {
99+
const childAst = parse(selector.children.first.value, { context: 'selectorList' })
100100
const selectorList = selectorListSpecificities(childAst)
101101
const [topA, topB, topC] = selectorList[0].specificity
102102
A += topA
@@ -157,8 +157,7 @@ const analyzeSpecificity = (ast) => {
157157
// The specificity of a :where() pseudo-class is replaced by zero,
158158
// but it does count towards complexity.
159159
if (selector.name === 'where') {
160-
const rawSelectorList = csstree.find(selector, ({ type }) => type === 'Raw')
161-
const childAst = csstree.parse(rawSelectorList.value, { context: 'selectorList' })
160+
const childAst = parse(selector.children.first.value, { context: 'selectorList' })
162161
const childSelectors = selectorListSpecificities(childAst)
163162

164163
for (let i = 0; i < childSelectors.length; i++) {

src/values/font-families.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as csstree from 'css-tree'
1+
import walk from 'css-tree/walker'
2+
import generate from 'css-tree/generator'
23
import { CountableCollection } from '../countable-collection.js'
34

45
const systemKeywords = {
@@ -69,7 +70,7 @@ const analyzeFontFamilies = ({ fontValues, fontFamilyValues }) => {
6970

7071
const parts = []
7172

72-
csstree.walk(value, {
73+
walk(value, {
7374
reverse: true,
7475
enter: function (fontNode) {
7576
if (fontNode.type === 'String') {
@@ -87,7 +88,7 @@ const analyzeFontFamilies = ({ fontValues, fontFamilyValues }) => {
8788
}
8889
})
8990

90-
all.push(parts.map(csstree.generate).join(''))
91+
all.push(parts.map(generate).join(''))
9192
}
9293

9394
return all.count()

src/values/font-sizes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as csstree from 'css-tree'
1+
import walk from 'css-tree/walker'
22
import { CountableCollection } from '../countable-collection.js'
33

44
const sizeKeywords = {
@@ -47,7 +47,7 @@ const analyzeFontSizes = ({ stringifyNode, fontSizeValues, fontValues }) => {
4747
let operator = false
4848
let size
4949

50-
csstree.walk(fontNode, {
50+
walk(fontNode, {
5151
enter: function (fontNode) {
5252
switch (fontNode.type) {
5353
case 'Number': {

0 commit comments

Comments
 (0)