Skip to content

Commit 222fa9f

Browse files
committed
Merge branch 'master' of github.com:parcel-bundler/lightningcss
2 parents e5c575d + fbae126 commit 222fa9f

File tree

2 files changed

+133
-4
lines changed

2 files changed

+133
-4
lines changed

node/composeVisitors.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ function composeVisitors(visitors) {
1313

1414
/** @type Visitor */
1515
let res = {};
16-
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapUnknownAtRule);
17-
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapUnknownAtRule);
16+
composeSimpleVisitors(res, visitors, 'StyleSheet');
17+
composeSimpleVisitors(res, visitors, 'StyleSheetExit');
18+
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapCustomAndUnknownAtRule);
19+
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapCustomAndUnknownAtRule);
1820
composeObjectVisitors(res, visitors, 'Declaration', declarationVisitor, wrapCustomProperty);
1921
composeObjectVisitors(res, visitors, 'DeclarationExit', declarationVisitor, wrapCustomProperty);
2022
composeSimpleVisitors(res, visitors, 'Url');
@@ -45,8 +47,14 @@ function composeVisitors(visitors) {
4547

4648
module.exports = composeVisitors;
4749

48-
function wrapUnknownAtRule(k, f) {
49-
return k === 'unknown' ? (value => f({ type: 'unknown', value })) : f;
50+
function wrapCustomAndUnknownAtRule(k, f) {
51+
if (k === 'unknown') {
52+
return (value => f({ type: 'unknown', value }));
53+
}
54+
if (k === 'custom') {
55+
return (value => f({ type: 'custom', value }));
56+
}
57+
return f;
5058
}
5159

5260
function wrapCustomProperty(k, f) {
@@ -66,6 +74,13 @@ function ruleVisitor(f, item) {
6674
}
6775
return v?.(item.value);
6876
}
77+
if (item.type === 'custom') {
78+
let v = f.custom;
79+
if (typeof v === 'object') {
80+
v = v[item.value.name];
81+
}
82+
return v?.(item.value);
83+
}
6984
return f[item.type]?.(item);
7085
}
7186
return f?.(item);

node/test/composeVisitors.test.mjs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,87 @@ test('unknown rules', () => {
513513
assert.equal(res.code.toString(), '.menu_link{background:#056ef0}');
514514
});
515515

516+
test('custom at rules', () => {
517+
let res = transform({
518+
filename: 'test.css',
519+
minify: true,
520+
code: Buffer.from(`
521+
@testA;
522+
@testB;
523+
`),
524+
customAtRules: {
525+
testA: {},
526+
testB: {}
527+
},
528+
visitor: composeVisitors([
529+
{
530+
Rule: {
531+
custom: {
532+
testA(rule) {
533+
return {
534+
type: 'style',
535+
value: {
536+
loc: rule.loc,
537+
selectors: [
538+
[{ type: 'class', name: 'testA' }]
539+
],
540+
declarations: {
541+
declarations: [
542+
{
543+
property: 'color',
544+
value: {
545+
type: 'rgb',
546+
r: 0xff,
547+
g: 0x00,
548+
b: 0x00,
549+
alpha: 1,
550+
}
551+
}
552+
]
553+
}
554+
}
555+
};
556+
}
557+
}
558+
}
559+
},
560+
{
561+
Rule: {
562+
custom: {
563+
testB(rule) {
564+
return {
565+
type: 'style',
566+
value: {
567+
loc: rule.loc,
568+
selectors: [
569+
[{ type: 'class', name: 'testB' }]
570+
],
571+
declarations: {
572+
declarations: [
573+
{
574+
property: 'color',
575+
value: {
576+
type: 'rgb',
577+
r: 0x00,
578+
g: 0xff,
579+
b: 0x00,
580+
alpha: 1,
581+
}
582+
}
583+
]
584+
}
585+
}
586+
};
587+
}
588+
}
589+
}
590+
}
591+
])
592+
});
593+
594+
assert.equal(res.code.toString(), '.testA{color:red}.testB{color:#0f0}');
595+
});
596+
516597
test('known rules', () => {
517598
let declared = new Map();
518599
let res = transform({
@@ -686,4 +767,37 @@ test('variables', () => {
686767
assert.equal(res.code.toString(), 'body{padding:20px;width:600px}');
687768
});
688769

770+
test('StyleSheet', () => {
771+
let styleSheetCalledCount = 0;
772+
let styleSheetExitCalledCount = 0;
773+
transform({
774+
filename: 'test.css',
775+
code: Buffer.from(`
776+
body {
777+
color: blue;
778+
}
779+
`),
780+
visitor: composeVisitors([
781+
{
782+
StyleSheet() {
783+
styleSheetCalledCount++
784+
},
785+
StyleSheetExit() {
786+
styleSheetExitCalledCount++
787+
}
788+
},
789+
{
790+
StyleSheet() {
791+
styleSheetCalledCount++
792+
},
793+
StyleSheetExit() {
794+
styleSheetExitCalledCount++
795+
}
796+
}
797+
])
798+
});
799+
assert.equal(styleSheetCalledCount, 2);
800+
assert.equal(styleSheetExitCalledCount, 2);
801+
});
802+
689803
test.run();

0 commit comments

Comments
 (0)