|
| 1 | +import type { IncludeStatement, IncludeToken } from '../types' |
| 2 | +import { existsSync, readFileSync } from 'node:fs' |
| 3 | +import { isAbsolute, resolve } from 'node:path' |
| 4 | +import { defineDirective } from '../directive' |
| 5 | +import { createProgramNode, simpleMatchToken } from '../utils' |
| 6 | + |
| 7 | +export const includeDirective = defineDirective<IncludeToken, IncludeStatement>((context) => { |
| 8 | + // 用于跟踪已包含的文件,防止循环引用(每次转换时重置) |
| 9 | + const includedFilesStack: Set<string>[] = [] |
| 10 | + |
| 11 | + return { |
| 12 | + lex(comment) { |
| 13 | + return simpleMatchToken(comment, /#(include)\s+["<](.+)[">]/) |
| 14 | + }, |
| 15 | + parse(token) { |
| 16 | + if (token.type === 'include') { |
| 17 | + this.current++ |
| 18 | + return { |
| 19 | + type: 'IncludeStatement', |
| 20 | + value: token.value, |
| 21 | + } |
| 22 | + } |
| 23 | + }, |
| 24 | + transform(node) { |
| 25 | + if (node.type === 'IncludeStatement') { |
| 26 | + let filePath = node.value |
| 27 | + |
| 28 | + // 解析文件路径 |
| 29 | + // 如果不是绝对路径,则相对于 cwd |
| 30 | + if (!isAbsolute(filePath)) { |
| 31 | + filePath = resolve(context.options.cwd, filePath) |
| 32 | + } |
| 33 | + |
| 34 | + // 检查文件是否存在 |
| 35 | + if (!existsSync(filePath)) { |
| 36 | + context.logger.warn(`Include file not found: ${filePath}`) |
| 37 | + return createProgramNode() |
| 38 | + } |
| 39 | + |
| 40 | + // 获取当前的包含文件集合 |
| 41 | + const currentIncludedFiles = includedFilesStack[includedFilesStack.length - 1] || new Set<string>() |
| 42 | + |
| 43 | + // 防止循环引用 |
| 44 | + if (currentIncludedFiles.has(filePath)) { |
| 45 | + context.logger.warn(`Circular include detected: ${filePath}`) |
| 46 | + return createProgramNode() |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + // 创建新的包含文件集合并添加当前文件 |
| 51 | + const newIncludedFiles = new Set(currentIncludedFiles) |
| 52 | + newIncludedFiles.add(filePath) |
| 53 | + includedFilesStack.push(newIncludedFiles) |
| 54 | + |
| 55 | + // 读取文件内容 |
| 56 | + const fileContent = readFileSync(filePath, 'utf-8') |
| 57 | + |
| 58 | + // 递归处理被包含的文件 |
| 59 | + const processedContent = context.transform(fileContent, filePath) |
| 60 | + |
| 61 | + // 弹出当前层级的包含文件集合 |
| 62 | + includedFilesStack.pop() |
| 63 | + |
| 64 | + // 如果文件经过了预处理,返回处理后的代码 |
| 65 | + if (processedContent) { |
| 66 | + return { |
| 67 | + type: 'CodeStatement', |
| 68 | + value: processedContent, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // 如果没有预处理指令,直接返回原始内容 |
| 73 | + return { |
| 74 | + type: 'CodeStatement', |
| 75 | + value: fileContent, |
| 76 | + } |
| 77 | + } |
| 78 | + catch (error) { |
| 79 | + // 确保出错时也弹出栈 |
| 80 | + includedFilesStack.pop() |
| 81 | + context.logger.error(`Error including file ${filePath}: ${error}`) |
| 82 | + return createProgramNode() |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + generate(node, comment) { |
| 87 | + if (node.type === 'IncludeStatement' && comment) { |
| 88 | + return `${comment.start} #include "${node.value}" ${comment.end}` |
| 89 | + } |
| 90 | + }, |
| 91 | + } |
| 92 | +}) |
0 commit comments