Skip to content

Commit 8192a4f

Browse files
author
Simon Milfred
committed
feat: improved efficiency further
1 parent 75d6b5a commit 8192a4f

File tree

5 files changed

+50
-33
lines changed

5 files changed

+50
-33
lines changed

utils/extractThemeConfigurationsFromAppConfig.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
export default function extractThemeConfigurationsFromAppConfig(appConfig = {}) {
22
const { themeConfiguration } = appConfig;
3+
if (!themeConfiguration?.themes || !Array.isArray(themeConfiguration.themes)) {
4+
return {};
5+
}
36

47
const configGlobs = {};
5-
6-
if (themeConfiguration?.themes && Array.isArray(themeConfiguration.themes)) {
7-
for (let configPath of themeConfiguration.themes) {
8-
let name;
9-
if (typeof configPath === 'object') {
10-
name = configPath.name || configPath.path;
11-
configPath = configPath.path;
12-
}
13-
14-
// Use standard dynamic import
8+
for (const configPath of themeConfiguration.themes) {
9+
if (typeof configPath === 'object') {
10+
const name = configPath.name || configPath.path;
11+
const {path} = configPath;
1512
configGlobs[name] = async () => {
13+
try {
14+
const module = await import(/* @vite-ignore */ path);
15+
return module?.default || module;
16+
} catch (error) {
17+
console.warn(`Failed to import theme configuration from "${path}": ${error?.message || String(error)}`);
18+
return undefined;
19+
}
20+
};
21+
} else {
22+
configGlobs[configPath] = async () => {
1623
try {
1724
const module = await import(/* @vite-ignore */ configPath);
1825
return module?.default || module;

utils/getThemeConfiguration.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default function getThemeConfiguration(theme, subset) {
55
if (typeof theme === 'string') {
66
config = $themeConfigurations[theme];
77
if (!config) {
8-
console.warn(`Theme "${theme}" not found. Available themes:`, $themeConfigurations.$getAvailableThemes?.() || []);
8+
const availableThemes = $themeConfigurations.$getAvailableThemes?.();
9+
console.warn(`Theme "${theme}" not found. Available themes:`, availableThemes || []);
910
return undefined;
1011
}
1112
} else if (typeof theme === 'object') {

utils/getThemeConfigurationAsync.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default async function getThemeConfigurationAsync(theme, subset) {
55
if (typeof theme === 'string') {
66
config = $themeConfigurations[theme] || await $themeConfigurations.$loadTheme?.(theme);
77
if (!config) {
8-
console.warn(`Theme "${theme}" not found. Available themes:`, $themeConfigurations.$getAvailableThemes?.() || []);
8+
const availableThemes = $themeConfigurations.$getAvailableThemes?.();
9+
console.warn(`Theme "${theme}" not found. Available themes:`, availableThemes || []);
910
return undefined;
1011
}
1112
} else if (typeof theme === 'object') {

utils/getThemeConfigurationSubset.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ export default function getThemeConfigurationSubset(obj, subset) {
66
}
77

88
if (Array.isArray(subset)) {
9-
const result = {};
9+
let result;
1010
for (const key of subset) {
11-
if (obj[key] !== undefined) result[key] = obj[key];
11+
if (obj[key] !== undefined) {
12+
if (!result) result = {};
13+
result[key] = obj[key];
14+
}
1215
}
13-
return Object.keys(result).length ? result : undefined;
16+
return result;
1417
}
1518

1619
if (subset instanceof RegExp) {
17-
const result = {};
20+
let result;
1821
for (const key in obj) {
19-
if (subset.test(key)) result[key] = obj[key];
22+
if (subset.test(key)) {
23+
if (!result) result = {};
24+
result[key] = obj[key];
25+
}
2026
}
21-
return Object.keys(result).length ? result : undefined;
27+
return result;
2228
}
2329

2430
if (typeof subset === 'object') {
25-
const result = {};
31+
let result;
2632
for (const key in subset) {
2733
if (!subset[key]) continue;
2834

@@ -31,26 +37,26 @@ export default function getThemeConfigurationSubset(obj, subset) {
3137
const regex = new RegExp(regexMatch[1], regexMatch[2]);
3238
for (const objKey in obj) {
3339
if (regex.test(objKey)) {
34-
result[objKey] = typeof subset[key] === 'boolean'
40+
const value = typeof subset[key] === 'boolean'
3541
? obj[objKey]
3642
: getThemeConfigurationSubset(obj[objKey], subset[key]);
43+
if (value !== undefined) {
44+
if (!result) result = {};
45+
result[objKey] = value;
46+
}
3747
}
3848
}
3949
} else if (obj[key] !== undefined) {
40-
result[key] = typeof subset[key] === 'boolean'
50+
const value = typeof subset[key] === 'boolean'
4151
? obj[key]
4252
: getThemeConfigurationSubset(obj[key], subset[key]);
53+
if (value !== undefined) {
54+
if (!result) result = {};
55+
result[key] = value;
56+
}
4357
}
4458
}
45-
46-
// Filter undefined values
47-
for (const key in result) {
48-
if (result[key] === undefined || (typeof result[key] === 'object' && Object.keys(result[key] || {}).length === 0)) {
49-
delete result[key];
50-
}
51-
}
52-
53-
return Object.keys(result).length ? result : undefined;
59+
return result;
5460
}
5561

5662
return undefined;

utils/getThemeConfigurationsAsync.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export default async function getThemeConfigurationsAsync() {
2-
const appConfig = useAppConfig();
32
const configGlobs = import.meta.glob('~/assets/js/theme-configuration.*.(js|cjs|mjs)');
3+
const extracted = extractThemeConfigurationsFromAppConfig(useAppConfig());
44

5-
Object.assign(configGlobs, extractThemeConfigurationsFromAppConfig(appConfig));
5+
if (Object.keys(extracted).length > 0) {
6+
Object.assign(configGlobs, extracted);
7+
}
68

79
const themeLoaders = {};
8-
for (const key of Object.keys(configGlobs)) {
10+
for (const key in configGlobs) {
911
const match = key.match(/theme-configuration\.([a-zA-Z0-9_-]+)\./);
1012
themeLoaders[match ? match[1] : key] = configGlobs[key];
1113
}

0 commit comments

Comments
 (0)