Skip to content

Commit c8c3ee9

Browse files
authoredApr 3, 2024
fix: export @scope root and limit locals (#61)
1 parent 2ef17a3 commit c8c3ee9

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed
 

‎src/index.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ const plugin = (options = {}) => {
177177
);
178178
}
179179

180-
function traverseNode(node, needExport = true) {
180+
function traverseNode(node) {
181181
switch (node.type) {
182182
case "pseudo":
183183
if (node.value === ":local") {
184184
if (node.nodes.length !== 1) {
185185
throw new Error('Unexpected comma (",") in :local block');
186186
}
187187

188-
const selector = localizeNode(node.first, needExport);
188+
const selector = localizeNode(node.first);
189189
// move the spaces that were around the pseudo selector to the first
190190
// non-container node
191191
selector.first.spaces = node.spaces;
@@ -208,12 +208,12 @@ const plugin = (options = {}) => {
208208
/* falls through */
209209
case "root":
210210
case "selector": {
211-
node.each((item) => traverseNode(item, needExport));
211+
node.each((item) => traverseNode(item));
212212
break;
213213
}
214214
case "id":
215215
case "class":
216-
if (needExport && exportGlobals) {
216+
if (exportGlobals) {
217217
exports[node.value] = [node.value];
218218
}
219219
break;
@@ -317,22 +317,24 @@ const plugin = (options = {}) => {
317317
});
318318

319319
root.walkAtRules(/scope$/i, (atRule) => {
320-
atRule.params = atRule.params
321-
.split("to")
322-
.map((item) => {
323-
const selector = item.trim().slice(1, -1).trim();
320+
if (atRule.params) {
321+
atRule.params = atRule.params
322+
.split("to")
323+
.map((item) => {
324+
const selector = item.trim().slice(1, -1).trim();
324325

325-
const localMatch = /^\s*:local\s*\((.+?)\)\s*$/.exec(selector);
326+
const localMatch = /^\s*:local\s*\((.+?)\)\s*$/.exec(selector);
326327

327-
if (!localMatch) {
328-
return `(${selector})`;
329-
}
328+
if (!localMatch) {
329+
return `(${selector})`;
330+
}
330331

331-
let parsedSelector = selectorParser().astSync(selector);
332+
let parsedSelector = selectorParser().astSync(selector);
332333

333-
return `(${traverseNode(parsedSelector, false).toString()})`;
334-
})
335-
.join(" to ");
334+
return `(${traverseNode(parsedSelector).toString()})`;
335+
})
336+
.join(" to ");
337+
}
336338
});
337339

338340
// If we found any :locals, insert an :export rule

‎test/test-cases/at-rule-scope/expected.css

+9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@
2727
}
2828
}
2929

30+
@scope {
31+
:scope {
32+
color: red;
33+
}
34+
}
35+
3036
:export {
3137
d: _input__d;
3238
c: _input__c;
3339
e: _input__e;
3440
f: _input__f;
41+
a: _input__a;
42+
b: _input__b;
43+
g: _input__g;
3544
}

‎test/test-cases/at-rule-scope/source.css

+6
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@
2626
backdrop-filter: blur(2px);
2727
}
2828
}
29+
30+
@scope {
31+
:scope {
32+
color: red;
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.