Skip to content

feat: supports multiple composes #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const plugin = (options = {}) => {
Once(root, { rule }) {
const exports = Object.create(null);

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

if (!needExport) {
return scopedName;
}

exports[key] = exports[key] || [];

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

function localizeNode(node, needExport = true) {
function localizeNode(node) {
switch (node.type) {
case "selector":
node.nodes = node.map((item) => localizeNode(item, needExport));
node.nodes = node.map((item) => localizeNode(item));
return node;
case "class":
return selectorParser.className({
value: exportScopedName(
node.value,
node.raws && node.raws.value ? node.raws.value : null,
node,
needExport
node
),
});
case "id": {
return selectorParser.id({
value: exportScopedName(
node.value,
node.raws && node.raws.value ? node.raws.value : null,
node,
needExport
node
),
});
}
Expand All @@ -166,7 +160,7 @@ const plugin = (options = {}) => {
attribute: node.attribute,
operator: node.operator,
quoteMark: "'",
value: exportScopedName(node.value, null, null, needExport),
value: exportScopedName(node.value, null, null),
});
}
}
Expand Down Expand Up @@ -236,35 +230,39 @@ const plugin = (options = {}) => {

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

rule.walkDecls(/composes|compose-with/i, (decl) => {
rule.walkDecls(/^(composes|compose-with)$/i, (decl) => {
const localNames = getSingleLocalNamesForComposes(
parsedSelector,
decl.parent
);
const classes = decl.value.split(/\s+/);
const multiple = decl.value.split(",");

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

if (global) {
localNames.forEach((exportedName) => {
exports[exportedName].push(global[1]);
});
} else if (hasOwnProperty.call(importedNames, className)) {
localNames.forEach((exportedName) => {
exports[exportedName].push(className);
});
} else if (hasOwnProperty.call(exports, className)) {
localNames.forEach((exportedName) => {
exports[className].forEach((item) => {
exports[exportedName].push(item);
classes.forEach((className) => {
const global = /^global\(([^)]+)\)$/.exec(className);

if (global) {
localNames.forEach((exportedName) => {
exports[exportedName].push(global[1]);
});
});
} else {
throw decl.error(
`referenced class name "${className}" in ${decl.prop} not found`
);
}
} else if (hasOwnProperty.call(importedNames, className)) {
localNames.forEach((exportedName) => {
exports[exportedName].push(className);
});
} else if (hasOwnProperty.call(exports, className)) {
localNames.forEach((exportedName) => {
exports[className].forEach((item) => {
exports[exportedName].push(item);
});
});
} else {
throw decl.error(
`referenced class name "${className}" in ${decl.prop} not found`
);
}
});
});

decl.remove();
Expand Down
9 changes: 9 additions & 0 deletions test/test-cases/composes-only-allowed/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
._input__class {
a-composes: global(c);
composes-b: global(d);
a-composes-b: global(e);
a-compose-with-b: global(b);
}
:export {
class: _input__class a b;
}
8 changes: 8 additions & 0 deletions test/test-cases/composes-only-allowed/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:local(.class) {
composes: global(a);
compose-with: global(b);
a-composes: global(c);
composes-b: global(d);
a-composes-b: global(e);
a-compose-with-b: global(b);
}
12 changes: 12 additions & 0 deletions test/test-cases/multiple-composes/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:import("path") {
i__i_a_0: a;
i__i_b_0: b;
i__i_c_0: c;
i__i_d_0: d;
}
._input__class {
color: red;
}
:export {
class: _input__class i__i_a_0 i__i_b_0 i__i_c_0 d e f i__i_d_0;
}
10 changes: 10 additions & 0 deletions test/test-cases/multiple-composes/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:import("path") {
i__i_a_0: a;
i__i_b_0: b;
i__i_c_0: c;
i__i_d_0: d;
}
:local(.class) {
composes: i__i_a_0 i__i_b_0, i__i_c_0, global(d) global(e), global(f), i__i_d_0;
color: red;
}