Skip to content

Commit

Permalink
Make the code generic
Browse files Browse the repository at this point in the history
  • Loading branch information
rvetere committed Oct 16, 2024
1 parent 2c3dc1d commit 0b7305a
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/next-yak/loaders/lib/resolveCrossFileSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,8 @@ async function resolveModuleSpecifierRecursively(
} else if (exportValue.type === "record") {
let current: any = exportValue.value;

// Remove any duplicate "layout" entries from the specifier
specifier = specifier.filter(
(item, index) => index === 0 || item !== "layout",
);
// It's possible that we have a specifier like ['layout', 'layout', 'sectorBandHeight', 'val']
specifier = removeDuplicates(specifier);

for (let depth = 1; depth < specifier.length; depth++) {
if (current === undefined) {
Expand All @@ -537,9 +535,12 @@ async function resolveModuleSpecifierRecursively(
);
}

// Handle CSSVariable objects
if (current && typeof current === "object" && "val" in current) {
return { type: "constant", value: current.val };
if (
current &&
typeof current === "object" &&
typeof current[specifier[depth]] === "string"
) {
return { type: "constant", value: current[specifier[depth]] };
}

if (typeof current === "string" || typeof current === "number") {
Expand All @@ -560,10 +561,6 @@ async function resolveModuleSpecifierRecursively(
);
}
if (typeof finalValue === "object" && finalValue !== null) {
// Handle CSSVariable objects
if ("val" in finalValue) {
return { type: "constant", value: finalValue.val };
}
return { type: "record", value: finalValue };
} else {
return { type: "constant", value: finalValue };
Expand Down Expand Up @@ -595,6 +592,10 @@ async function resolveModuleSpecifierRecursively(
}
}

function removeDuplicates(specifiers: string[]): string[] {
return Array.from(new Set(specifiers));
}

type ParsedFile =
| { type: "regular"; exports: Record<string, ParsedExport>; filePath: string }
| { type: "yak"; exports: Record<string, ParsedExport>; filePath: string };
Expand Down

0 comments on commit 0b7305a

Please sign in to comment.