UNPKG

mp-lens

Version:

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

188 lines 8.48 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.FileParser = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const alias_resolver_1 = require("../utils/alias-resolver"); const debug_logger_1 = require("../utils/debug-logger"); const path_resolver_1 = require("../utils/path-resolver"); // Import specialized parsers with corrected paths relative to src/analyzer/ const javascript_parser_1 = require("./javascript-parser"); const json_parser_1 = require("./json-parser"); const wxml_parser_1 = require("./wxml-parser"); const wxss_parser_1 = require("./wxss-parser"); /** * FileParser: Orchestrates parsing of different file types in a WeChat Mini Program. * Handles file reading and path resolution, then delegates text analysis to specialized parsers. */ class FileParser { constructor(projectRoot, options) { const actualRoot = options.miniappRoot || projectRoot; if (options.miniappRoot) { debug_logger_1.logger.debug(`FileParser using custom miniapp root: ${options.miniappRoot}`); } const aliasResolver = new alias_resolver_1.AliasResolver(actualRoot); const hasAliasConfig = aliasResolver.initialize(); if (hasAliasConfig) { debug_logger_1.logger.debug('Alias configuration detected, automatically enabling alias resolution'); debug_logger_1.logger.debug('Alias configuration:', aliasResolver.getAliases()); } // Instantiate PathResolver this.pathResolver = new path_resolver_1.PathResolver(projectRoot, options, aliasResolver, hasAliasConfig); // Instantiate specialized parsers - they now only handle text analysis this.javaScriptParser = new javascript_parser_1.JavaScriptParser(); this.wxmlParser = new wxml_parser_1.WXMLParser(); this.wxssParser = new wxss_parser_1.WXSSParser(); this.jsonParser = new json_parser_1.JSONParser(); } /** * Parses a single file by reading its content and delegating text analysis to the appropriate parser. * Handles path resolution centrally. * Returns a list of absolute paths to the file's dependencies. */ async parseFile(filePath) { const ext = path.extname(filePath).toLowerCase(); try { // Read file content once at the top level const content = fs.readFileSync(filePath, 'utf-8'); let rawDependencies = []; // Delegate text analysis to specialized parsers switch (ext) { case '.js': case '.ts': case '.wxs': // WXS files are JavaScript, use the same parser rawDependencies = await this.javaScriptParser.parse(content, filePath); break; case '.wxml': rawDependencies = await this.wxmlParser.parse(content, filePath); break; case '.wxss': rawDependencies = await this.wxssParser.parse(content, filePath); break; case '.json': rawDependencies = await this.jsonParser.parse(content, filePath); break; case '.png': case '.jpg': case '.jpeg': case '.gif': case '.svg': return []; // Image files have no dependencies default: debug_logger_1.logger.trace(`Unsupported file type for parsing: ${filePath}`); return []; } // Resolve all raw dependency paths to absolute paths const resolvedDependencies = []; for (const rawPath of rawDependencies) { const resolvedPath = this.resolveDependencyPath(rawPath, filePath, ext); if (resolvedPath) { resolvedDependencies.push(resolvedPath); } } return resolvedDependencies; } catch (e) { // Centralized error handling for file reading or parsing issues debug_logger_1.logger.warn(`Error parsing file ${filePath}: ${e.message}`); return []; // Return empty array on error } } /** * Resolves a raw dependency path to an absolute path based on file type context */ resolveDependencyPath(rawPath, sourcePath, sourceExt) { // Determine allowed extensions based on source file type and dependency context let allowedExtensions; switch (sourceExt) { case '.js': case '.ts': allowedExtensions = ['.js', '.ts', '.json']; break; case '.wxs': allowedExtensions = ['.wxs']; // WXS files can only import other WXS files break; case '.wxml': // WXML can reference .wxml (import/include), .wxs, and image files // Determine type based on path characteristics if (this.isImagePath(rawPath)) { allowedExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp']; } else if (rawPath.includes('.wxs') || rawPath.endsWith('.wxs')) { allowedExtensions = ['.wxs']; } else { // Default to WXML for import/include allowedExtensions = ['.wxml']; } break; case '.wxss': // WXSS can import other WXSS files or reference image files if (this.isImagePath(rawPath)) { allowedExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp']; } else { allowedExtensions = ['.wxss']; } break; case '.json': // JSON files can reference various file types depending on context // For pages and components, we need to find all related files if (this.isImagePath(rawPath)) { allowedExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp']; } else { // For pages/components, try to find the main file first allowedExtensions = ['.js', '.ts', '.wxml', '.wxss', '.json']; } break; default: allowedExtensions = []; } return this.pathResolver.resolveAnyPath(rawPath, sourcePath, allowedExtensions); } /** * Determines if a path refers to an image file based on its extension */ isImagePath(filePath) { const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp']; const ext = path.extname(filePath).toLowerCase(); // Strictly check if the file extension is an image extension return imageExtensions.includes(ext); } } exports.FileParser = FileParser; //# sourceMappingURL=file-parser.js.map