| 73 | } |
| 74 | |
| 75 | async function resolveGlsl( |
| 76 | context: ResolutionContext, |
| 77 | id: string, |
| 78 | code: string, |
| 79 | ): Promise<SourceNode> { |
| 80 | const lines = code.split(/\r?\n/); |
| 81 | const source = new SourceNode(1, 0, '', ''); |
| 82 | |
| 83 | if (context.fileStack.includes(id)) { |
| 84 | throw new Error( |
| 85 | `Circular dependency detected: ${context.fileStack.join(' -> ')}`, |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | context.fileStack.push(id); |
| 90 | |
| 91 | const sourceMapId = path.posix.relative(context.rootDir, id); |
| 92 | for (let i = 0; i < lines.length; i++) { |
| 93 | const line = lines[i]; |
| 94 | const match = line.match(INCLUDE_REGEX); |
| 95 | if (match) { |
| 96 | const childId = await context.resolve(match[1], id); |
| 97 | if (!childId) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | const childSourceMapId = path.posix.relative(context.rootDir, childId); |
| 102 | if (context.includeMap.has(childSourceMapId)) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | context.includeMap.set(childSourceMapId, [sourceMapId, i + 1]); |
| 107 | context.watchFile(childId); |
| 108 | const childCode = await fs.promises.readFile(childId, 'utf-8'); |
| 109 | source.add(await resolveGlsl(context, childId, childCode)); |
| 110 | } else { |
| 111 | let j = 0; |
| 112 | for (; j < line.length; j++) { |
| 113 | source.add(new SourceNode(i + 1, j, sourceMapId, line[j])); |
| 114 | } |
| 115 | source.add(new SourceNode(i + 1, j, sourceMapId, '\n')); |
| 116 | } |
| 117 | } |
| 118 | context.fileStack.pop(); |
| 119 | |
| 120 | return source; |
| 121 | } |