UNPKG

mp-lens

Version:

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

128 lines 5.79 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.loadTsConfigTypes = loadTsConfigTypes; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const debug_logger_1 = require("./debug-logger"); // Helper function to recursively find all files in a directory function getAllFilesRecursive(dirPath, fileList = []) { try { const entries = fs.readdirSync(dirPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.resolve(dirPath, entry.name); if (entry.isDirectory()) { getAllFilesRecursive(fullPath, fileList); } else if (entry.isFile()) { fileList.push(fullPath); } } } catch (error) { debug_logger_1.logger.warn(`Error reading directory for types ${dirPath}: ${error.message}`); } return fileList; } /** * Loads tsconfig.json, parses the types array (root level first, then compilerOptions.types), * and returns a list of absolute paths for project-local type definition files/directories. * It filters out module names (like "miniprogram-api-typings"). * * @param projectRoot The absolute path to the project root. * @returns An array of absolute paths to essential type files derived from tsconfig. */ function loadTsConfigTypes(projectRoot) { var _a; const tsConfigPath = path.resolve(projectRoot, 'tsconfig.json'); const essentialTypeFiles = []; if (!fs.existsSync(tsConfigPath)) { debug_logger_1.logger.debug('tsconfig.json not found, skipping types parsing.'); return essentialTypeFiles; } try { const tsConfigContent = fs.readFileSync(tsConfigPath, 'utf-8'); // Basic JSON parsing, potentially improve with a more robust parser that handles comments const tsConfig = JSON.parse(tsConfigContent); // Check root-level 'types' first, then fallback to 'compilerOptions.types' let types = undefined; if (Array.isArray(tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.types)) { types = tsConfig.types; } else if (Array.isArray((_a = tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.types)) { types = tsConfig.compilerOptions.types; } else { // Skip the loop below if types is undefined } if (Array.isArray(types)) { // Check if types was successfully assigned for (const typeRef of types) { if (typeof typeRef === 'string') { // Refined check: Consider anything starting with '.' or containing '/' or '\' as a path. const pathSeparatorRegex = /[\\/]/; const isLikelyPath = typeRef.startsWith('.') || pathSeparatorRegex.test(typeRef); if (isLikelyPath) { const potentialPath = path.resolve(projectRoot, typeRef); // Resolve path only if likely a path try { const stats = fs.statSync(potentialPath); if (stats.isFile()) { essentialTypeFiles.push(potentialPath); } else if (stats.isDirectory()) { const filesInDir = getAllFilesRecursive(potentialPath); essentialTypeFiles.push(...filesInDir); } } catch (err) { // Ignore if path doesn't exist or can't be accessed debug_logger_1.logger.warn(`Could not resolve tsconfig type path '${typeRef}': ${err.message}`); } } else { // Assume it's a module name (like "miniprogram-api-typings") and ignore } } } } // No else needed here, already logged if no array was found } catch (error) { debug_logger_1.logger.error(`Failed to read or parse tsconfig.json at ${tsConfigPath}: ${error.message}`); } // Return unique paths return [...new Set(essentialTypeFiles)]; } //# sourceMappingURL=tsconfig-helper.js.map