UNPKG

module-migration-tool

Version:

分析项目文件依赖并迁移到新项目的工具

55 lines (47 loc) 1.3 kB
/** * CSS文件分析器 */ const fs = require("fs") /** * 分析CSS文件依赖 * @param {string} filePath - 文件路径 * @param {string} content - 文件内容 * @returns {string[]} 依赖路径数组 */ function analyzeCSS(filePath, content) { const dependencies = [] // 匹配@import语句 const importRegex = /@import\s+(?:url\()?\s*['"]([^'"]+)['"](\s*\))?\s*;/g let match while ((match = importRegex.exec(content)) !== null) { dependencies.push(match[1]) } // 匹配url()引用 const urlRegex = /url\(\s*['"]?([^'")]+)['"]?\s*\)/g while ((match = urlRegex.exec(content)) !== null) { // 排除已经由@import找到的依赖 const url = match[1] if (!dependencies.includes(url) && !url.startsWith("data:")) { dependencies.push(url) } } return dependencies } /** * 从文件中分析CSS依赖 * @param {string} filePath - 文件路径 * @returns {string[]} 依赖路径数组 */ function analyzeFile(filePath) { try { const content = fs.readFileSync(filePath, "utf-8") return analyzeCSS(filePath, content) } catch (error) { console.error(`读取文件 ${filePath} 失败:`, error.message) return [] } } module.exports = { analyzeCSS, analyzeFile, }