-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgetDynamicStyle.ts
More file actions
31 lines (27 loc) · 923 Bytes
/
Copy pathgetDynamicStyle.ts
File metadata and controls
31 lines (27 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export function getDynamicStyle(
styleModules: Record<string, () => Promise<unknown>>,
rawModules: Record<string, () => Promise<string>>
) {
const loadStyleFiles = async () => {
const entries = Object.keys(styleModules)
.sort()
.map(async (path) => {
const [stylesMod, rawMod] = await Promise.all([styleModules[path](), rawModules[path]()]);
return {
styles: (stylesMod as { default: CSSModuleClasses }).default,
raw: rawMod,
fileName: path.replace(/.*\//, ''), // Extract file name from path
};
});
const results = await Promise.all(entries);
return results;
};
return async function loadDynamicStyles() {
const allStyles: { styles: CSSModuleClasses; raw: string }[] = [];
if (allStyles.length === 0) {
const loaded = await loadStyleFiles();
allStyles.push(...loaded);
}
return allStyles;
};
}