Skip to content

Commit 5f9a487

Browse files
feat: support multiple composes (#191)
1 parent d8a9676 commit 5f9a487

3 files changed

Lines changed: 100 additions & 38 deletions

File tree

src/index.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,54 +84,63 @@ module.exports = (options = {}) => {
8484
});
8585

8686
root.walkDecls(/^composes$/, (declaration) => {
87-
const matches = declaration.value.match(matchImports);
87+
const multiple = declaration.value.split(",");
88+
const values = [];
8889

89-
if (!matches) {
90-
return;
91-
}
92-
93-
let tmpSymbols;
94-
let [
95-
,
96-
/*match*/ symbols,
97-
doubleQuotePath,
98-
singleQuotePath,
99-
global,
100-
] = matches;
101-
102-
if (global) {
103-
// Composing globals simply means changing these classes to wrap them in global(name)
104-
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
105-
} else {
106-
const importPath = doubleQuotePath || singleQuotePath;
90+
multiple.forEach((value) => {
91+
const matches = value.trim().match(matchImports);
10792

108-
let parent = declaration.parent;
109-
let parentIndexes = "";
93+
if (!matches) {
94+
values.push(value);
11095

111-
while (parent.type !== "root") {
112-
parentIndexes =
113-
parent.parent.index(parent) + "_" + parentIndexes;
114-
parent = parent.parent;
96+
return;
11597
}
11698

117-
const { selector } = declaration.parent;
118-
const parentRule = `_${parentIndexes}${selector}`;
119-
120-
addImportToGraph(importPath, parentRule, graph, visited);
99+
let tmpSymbols;
100+
let [
101+
,
102+
/*match*/ symbols,
103+
doubleQuotePath,
104+
singleQuotePath,
105+
global,
106+
] = matches;
107+
108+
if (global) {
109+
// Composing globals simply means changing these classes to wrap them in global(name)
110+
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
111+
} else {
112+
const importPath = doubleQuotePath || singleQuotePath;
121113

122-
importDecls[importPath] = declaration;
123-
imports[importPath] = imports[importPath] || {};
114+
let parent = declaration.parent;
115+
let parentIndexes = "";
124116

125-
tmpSymbols = symbols.split(/\s+/).map((s) => {
126-
if (!imports[importPath][s]) {
127-
imports[importPath][s] = createImportedName(s, importPath);
117+
while (parent.type !== "root") {
118+
parentIndexes =
119+
parent.parent.index(parent) + "_" + parentIndexes;
120+
parent = parent.parent;
128121
}
129122

130-
return imports[importPath][s];
131-
});
132-
}
123+
const { selector } = declaration.parent;
124+
const parentRule = `_${parentIndexes}${selector}`;
125+
126+
addImportToGraph(importPath, parentRule, graph, visited);
127+
128+
importDecls[importPath] = declaration;
129+
imports[importPath] = imports[importPath] || {};
130+
131+
tmpSymbols = symbols.split(/\s+/).map((s) => {
132+
if (!imports[importPath][s]) {
133+
imports[importPath][s] = createImportedName(s, importPath);
134+
}
135+
136+
return imports[importPath][s];
137+
});
138+
}
139+
140+
values.push(tmpSymbols.join(" "));
141+
});
133142

134-
declaration.value = tmpSymbols.join(" ");
143+
declaration.value = values.join(", ");
135144
});
136145

137146
const importsOrder = topologicalSort(graph, failOnWrongOrder);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
:import("path/library.css") {
2+
i__imported_importName_0: importName;
3+
i__imported_firstImport_1: firstImport;
4+
i__imported_secondImport_2: secondImport;
5+
i__imported_importName2_6: importName2;
6+
i__imported_importName3_7: importName3;
7+
i__imported_importName4_8: importName4;
8+
}
9+
10+
:import("./aa.css") {
11+
i__imported_a_3: a;
12+
}
13+
14+
:import("./bb.css") {
15+
i__imported_b_4: b;
16+
}
17+
18+
:import("./cc.css") {
19+
i__imported_c_5: c;
20+
}
21+
22+
:local(.exportName) {
23+
composes: i__imported_importName_0, global(beforeName), global(importName) global(secondImport), i__imported_firstImport_1 i__imported_secondImport_2;
24+
other: rule;
25+
}
26+
27+
:local(.duplicate) {
28+
composes: i__imported_a_3, i__imported_b_4, i__imported_c_5, i__imported_a_3, i__imported_c_5;
29+
}
30+
31+
:local(.spaces) {
32+
composes: i__imported_importName_0 i__imported_importName2_6, i__imported_importName3_7 i__imported_importName4_8;
33+
}
34+
35+
:local(.unknown) {
36+
composes: foo bar, baz;
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
:local(.exportName) {
2+
composes: importName from "path/library.css", beforeName from global, importName secondImport from global, firstImport secondImport from "path/library.css";
3+
other: rule;
4+
}
5+
6+
:local(.duplicate) {
7+
composes: a from "./aa.css", b from "./bb.css", c from "./cc.css", a from "./aa.css", c from "./cc.css";
8+
}
9+
10+
:local(.spaces) {
11+
composes: importName importName2 from "path/library.css", importName3 importName4 from "path/library.css";
12+
}
13+
14+
:local(.unknown) {
15+
composes: foo bar, baz;
16+
}

0 commit comments

Comments
 (0)