UNPKG

mp-lens

Version:

微信小程序分析工具 (Unused Code, Dependencies, Visualization)

170 lines 6.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigLoader = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Loads the mp-lens specific configuration file (e.g., mp-lens.config.js). * It does NOT handle loading tsconfig.json or other project-specific config files. */ class ConfigLoader { /** * 从指定路径加载配置文件 * @param configPath 配置文件路径,如果未提供,将自动搜索 * @param projectRoot 项目根目录 * @returns 配置对象或null(未找到配置文件) */ static async loadConfig(configPath, projectRoot = process.cwd()) { // 如果提供了具体的配置文件路径,直接尝试加载 if (configPath) { return this.loadConfigFile(path.resolve(projectRoot, configPath)); } // 默认配置文件名称(支持多种格式) const possibleConfigs = ['mp-lens.config.js', 'mp-lens.config.ts', 'mp-lens.config.json']; // 从项目根目录查找配置文件 for (const configName of possibleConfigs) { const fullPath = path.join(projectRoot, configName); if (fs.existsSync(fullPath)) { console.log(`找到配置文件: ${fullPath}`); return this.loadConfigFile(fullPath); } } console.log(`未找到配置文件,将使用默认配置`); return null; } /** * 根据文件类型加载配置文件 * @param filePath 配置文件路径 * @returns 配置对象 */ static async loadConfigFile(filePath) { try { const ext = path.extname(filePath).toLowerCase(); // 处理不同类型的配置文件 switch (ext) { case '.js': return this.loadJavaScriptConfig(filePath); case '.ts': return this.loadTypeScriptConfig(filePath); case '.json': return this.loadJsonConfig(filePath); default: console.warn(`不支持的配置文件格式: ${ext}`); return null; } } catch (error) { console.error(`加载配置文件失败: ${error.message}`); return null; } } /** * 加载JSON格式的配置文件 */ static loadJsonConfig(filePath) { try { const content = fs.readFileSync(filePath, 'utf-8'); return JSON.parse(content); } catch (error) { console.error(`解析JSON配置文件失败: ${error.message}`); return null; } } /** * 加载JavaScript格式的配置文件 */ static async loadJavaScriptConfig(filePath) { try { // 删除可能的缓存,以确保获取最新的配置 const absolutePath = path.resolve(filePath); // eslint-disable-next-line @typescript-eslint/no-var-requires delete require.cache[absolutePath]; // 动态导入JavaScript配置文件 // eslint-disable-next-line @typescript-eslint/no-var-requires const config = require(absolutePath); // 如果配置导出为函数,则执行它 if (typeof config === 'function') { return await config(); } // 处理ES模块导出(有default属性) if (config && config.default) { if (typeof config.default === 'function') { return await config.default(); } return config.default; } return config; } catch (error) { console.error(`加载JavaScript配置文件失败: ${error.message}`); return null; } } /** * 加载TypeScript格式的配置文件 * 注意:需要项目中安装ts-node才能直接执行TypeScript文件 */ static async loadTypeScriptConfig(filePath) { try { // 尝试注册ts-node try { // 直接引用ts-node模块 // eslint-disable-next-line @typescript-eslint/no-var-requires const tsNode = require('ts-node'); tsNode.register({ transpileOnly: true, compilerOptions: { module: 'commonjs', }, }); } catch (e) { const tsNodeError = e; console.error(`加载TypeScript配置需要安装ts-node: ${tsNodeError.message}`); return null; } // 使用与JavaScript相同的加载逻辑 return this.loadJavaScriptConfig(filePath); } catch (error) { console.error(`加载TypeScript配置文件失败: ${error.message}`); return null; } } } exports.ConfigLoader = ConfigLoader; //# sourceMappingURL=config-loader.js.map