-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-module-loader.js
More file actions
69 lines (57 loc) · 2.31 KB
/
vite-module-loader.js
File metadata and controls
69 lines (57 loc) · 2.31 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import fs from 'fs/promises';
import path from 'path';
async function collectModuleAssetsPaths(modulesPath) {
return await getExportedModulesArrayAttributes(modulesPath, 'paths');
}
async function collectModulePlugins(modulesPath) {
return await getExportedModulesArrayAttributes(modulesPath, 'plugins');
}
async function getExportedModulesArrayAttributes(modulesPath, attribute) {
const result = [];
modulesPath = path.join(__dirname, modulesPath);
const moduleStatusesPath = path.join(__dirname, 'modules_statuses.json');
let moduleStatuses = {};
try {
const moduleStatusesContent = await fs.readFile(moduleStatusesPath, 'utf-8');
moduleStatuses = JSON.parse(moduleStatusesContent);
} catch (error) {
// If the statuses file is missing, assume no modules are enabled and return empty result silently
if (error && error.code !== 'ENOENT') {
// Log non-ENOENT errors as warnings
console.warn(`Warning while reading module statuses: ${error}`);
}
return result;
}
try {
// Read module directories
const moduleDirectories = await fs.readdir(modulesPath);
for (const moduleDir of moduleDirectories) {
if (moduleDir === '.DS_Store') {
// Skip .DS_Store directory
continue;
}
// Check if the module is enabled (status is true)
if (moduleStatuses[moduleDir] === true) {
const viteConfigPath = path.join(modulesPath, moduleDir, 'vite.config.js');
let stat;
try {
stat = await fs.stat(viteConfigPath);
} catch {
continue;
}
if (stat.isFile()) {
// Import the module-specific Vite configuration
const moduleConfig = await import(viteConfigPath);
if (moduleConfig[attribute] && Array.isArray(moduleConfig[attribute])) {
result.push(...moduleConfig[attribute]);
}
}
}
}
} catch {
// If modules directory doesn't exist, return empty result silently
return result;
}
return result;
}
export { collectModuleAssetsPaths, collectModulePlugins };