Skip to content

Commit

Permalink
Enable sass parser in ui/build to parse indirect color assignments
Browse files Browse the repository at this point in the history
The sass build for colors currently only works for direct assignments,
but doesn't work correctly when being assigned to another variables and
causes the assignee to be #000 instead.
  • Loading branch information
FawzyAshraf committed Dec 16, 2024
1 parent 9e95478 commit 77a15c8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ui/.build/src/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,30 @@ async function parseScss(src: string) {
async function parseThemeColorDefs() {
const themeFiles = await globArray('./common/css/theme/_*.scss', { absolute: false });
const themes: string[] = ['dark'];
const capturedColors = new Map<string, string>();
for (const themeFile of themeFiles ?? []) {
const theme = /_([^/]+)\.scss/.exec(themeFile)?.[1];
if (!theme) {
env.log(`${errorMark} - invalid theme filename '${c.cyan(themeFile)}'`, { ctx: 'sass' });
continue;
}
const text = fs.readFileSync(themeFile, 'utf8');
const colorMap = new Map<string, clr.Instance>();
for (const match of text.matchAll(/\s\$c-([-a-z0-9]+):\s*([^;]+);/g)) {
colorMap.set(match[1], clr(match[2]));
capturedColors.set(match[1], match[2]);
}
const colorMap = new Map<string, clr.Instance>();
for (const [color, colorVal] of capturedColors) {
let val = colorVal;
const visitedVariables = new Set();
while (val.startsWith('$')) {
const inferredValue = capturedColors.get(val.substring(3)) ?? '#000';
if (visitedVariables.has(inferredValue)) {
break;
}
visitedVariables.add(inferredValue);
val = inferredValue;
}
colorMap.set(color, clr(val));
}
if (theme !== 'default') themes.push(theme);
themeColorMap.set(theme, colorMap);
Expand Down

0 comments on commit 77a15c8

Please sign in to comment.