Skip to content

Commit 0141278

Browse files
feat: supports multiple composes (#62)
1 parent ef877d0 commit 0141278

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

src/index.js

+32-34
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const plugin = (options = {}) => {
107107
Once(root, { rule }) {
108108
const exports = Object.create(null);
109109

110-
function exportScopedName(name, rawName, node, needExport = true) {
110+
function exportScopedName(name, rawName, node) {
111111
const scopedName = generateScopedName(
112112
rawName ? rawName : name,
113113
root.source.input.from,
@@ -123,10 +123,6 @@ const plugin = (options = {}) => {
123123
);
124124
const { key, value } = exportEntry;
125125

126-
if (!needExport) {
127-
return scopedName;
128-
}
129-
130126
exports[key] = exports[key] || [];
131127

132128
if (exports[key].indexOf(value) < 0) {
@@ -136,27 +132,25 @@ const plugin = (options = {}) => {
136132
return scopedName;
137133
}
138134

139-
function localizeNode(node, needExport = true) {
135+
function localizeNode(node) {
140136
switch (node.type) {
141137
case "selector":
142-
node.nodes = node.map((item) => localizeNode(item, needExport));
138+
node.nodes = node.map((item) => localizeNode(item));
143139
return node;
144140
case "class":
145141
return selectorParser.className({
146142
value: exportScopedName(
147143
node.value,
148144
node.raws && node.raws.value ? node.raws.value : null,
149-
node,
150-
needExport
145+
node
151146
),
152147
});
153148
case "id": {
154149
return selectorParser.id({
155150
value: exportScopedName(
156151
node.value,
157152
node.raws && node.raws.value ? node.raws.value : null,
158-
node,
159-
needExport
153+
node
160154
),
161155
});
162156
}
@@ -166,7 +160,7 @@ const plugin = (options = {}) => {
166160
attribute: node.attribute,
167161
operator: node.operator,
168162
quoteMark: "'",
169-
value: exportScopedName(node.value, null, null, needExport),
163+
value: exportScopedName(node.value, null, null),
170164
});
171165
}
172166
}
@@ -236,35 +230,39 @@ const plugin = (options = {}) => {
236230

237231
rule.selector = traverseNode(parsedSelector.clone()).toString();
238232

239-
rule.walkDecls(/composes|compose-with/i, (decl) => {
233+
rule.walkDecls(/^(composes|compose-with)$/i, (decl) => {
240234
const localNames = getSingleLocalNamesForComposes(
241235
parsedSelector,
242236
decl.parent
243237
);
244-
const classes = decl.value.split(/\s+/);
238+
const multiple = decl.value.split(",");
245239

246-
classes.forEach((className) => {
247-
const global = /^global\(([^)]+)\)$/.exec(className);
240+
multiple.forEach((value) => {
241+
const classes = value.trim().split(/\s+/);
248242

249-
if (global) {
250-
localNames.forEach((exportedName) => {
251-
exports[exportedName].push(global[1]);
252-
});
253-
} else if (hasOwnProperty.call(importedNames, className)) {
254-
localNames.forEach((exportedName) => {
255-
exports[exportedName].push(className);
256-
});
257-
} else if (hasOwnProperty.call(exports, className)) {
258-
localNames.forEach((exportedName) => {
259-
exports[className].forEach((item) => {
260-
exports[exportedName].push(item);
243+
classes.forEach((className) => {
244+
const global = /^global\(([^)]+)\)$/.exec(className);
245+
246+
if (global) {
247+
localNames.forEach((exportedName) => {
248+
exports[exportedName].push(global[1]);
261249
});
262-
});
263-
} else {
264-
throw decl.error(
265-
`referenced class name "${className}" in ${decl.prop} not found`
266-
);
267-
}
250+
} else if (hasOwnProperty.call(importedNames, className)) {
251+
localNames.forEach((exportedName) => {
252+
exports[exportedName].push(className);
253+
});
254+
} else if (hasOwnProperty.call(exports, className)) {
255+
localNames.forEach((exportedName) => {
256+
exports[className].forEach((item) => {
257+
exports[exportedName].push(item);
258+
});
259+
});
260+
} else {
261+
throw decl.error(
262+
`referenced class name "${className}" in ${decl.prop} not found`
263+
);
264+
}
265+
});
268266
});
269267

270268
decl.remove();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
._input__class {
2+
a-composes: global(c);
3+
composes-b: global(d);
4+
a-composes-b: global(e);
5+
a-compose-with-b: global(b);
6+
}
7+
:export {
8+
class: _input__class a b;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:local(.class) {
2+
composes: global(a);
3+
compose-with: global(b);
4+
a-composes: global(c);
5+
composes-b: global(d);
6+
a-composes-b: global(e);
7+
a-compose-with-b: global(b);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:import("path") {
2+
i__i_a_0: a;
3+
i__i_b_0: b;
4+
i__i_c_0: c;
5+
i__i_d_0: d;
6+
}
7+
._input__class {
8+
color: red;
9+
}
10+
:export {
11+
class: _input__class i__i_a_0 i__i_b_0 i__i_c_0 d e f i__i_d_0;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
:import("path") {
2+
i__i_a_0: a;
3+
i__i_b_0: b;
4+
i__i_c_0: c;
5+
i__i_d_0: d;
6+
}
7+
:local(.class) {
8+
composes: i__i_a_0 i__i_b_0, i__i_c_0, global(d) global(e), global(f), i__i_d_0;
9+
color: red;
10+
}

0 commit comments

Comments
 (0)