MCPcopy Create free account
hub / github.com/Dstack-TEE/dstack / processModule

Function processModule

vmm/ui/build.mjs:37–88  ·  view source on GitHub ↗
(moduleId)

Source from the content-addressed store, hash-verified

35 const modules = new Map();
36
37 async function processModule(moduleId) {
38 if (modules.has(moduleId)) {
39 return;
40 }
41 const absPath = path.resolve(ROOT, moduleId);
42 const ext = path.extname(absPath);
43 if (ext === '.html') {
44 const content = await readFileCached(absPath);
45 modules.set(moduleId, {
46 type: 'raw',
47 code: `module.exports = ${JSON.stringify(content)};`,
48 dependencyMap: {},
49 });
50 return;
51 }
52 if (ext === '.json') {
53 const content = await readFileCached(absPath);
54 modules.set(moduleId, {
55 type: 'raw',
56 code: `module.exports = ${content};`,
57 dependencyMap: {},
58 });
59 return;
60 }
61 if (ext !== '.js' && ext !== '.cjs' && ext !== '.mjs') {
62 throw new Error(`Unsupported module extension: ${absPath}`);
63 }
64 const source = await readFileCached(absPath);
65 const dependencyMap = {};
66 const requireRegex = /require\(['"](.+?)['"]\)/g;
67 let match;
68 while ((match = requireRegex.exec(source)) !== null) {
69 const lineStart = source.lastIndexOf('\n', match.index) + 1;
70 const trimmed = source.slice(lineStart, match.index).trim();
71 if (trimmed.startsWith('//') || trimmed.startsWith('*')) {
72 continue;
73 }
74 const request = match[1];
75 const resolved = resolveModule(moduleId, request);
76 dependencyMap[request] = resolved;
77 }
78 const dependencies = Array.from(new Set(Object.values(dependencyMap)));
79 modules.set(moduleId, {
80 type: 'js',
81 code: source,
82 dependencyMap,
83 dependencies,
84 });
85 for (const dep of dependencies) {
86 await processModule(dep);
87 }
88 }
89
90 await processModule(entryId);
91 return modules;

Callers 1

collectModulesFunction · 0.85

Calls 5

readFileCachedFunction · 0.85
resolveModuleFunction · 0.85
resolveMethod · 0.80
setMethod · 0.80
fromMethod · 0.45

Tested by

no test coverage detected