mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
130 lines • 5.48 kB
JavaScript
;
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.JSONParser = void 0;
const path = __importStar(require("path"));
const debug_logger_1 = require("../utils/debug-logger");
class JSONParser {
constructor() {
// No dependencies needed for pure text analysis
}
async parse(content, filePath) {
try {
const jsonContent = JSON.parse(content);
const dependencies = new Set();
// --- Process app.json specific fields ---
this.processPages(jsonContent, dependencies);
this.processSubPackages(jsonContent, dependencies);
this.processTabBar(jsonContent, dependencies);
// --- Process component.json specific fields ---
this.processUsingComponents(jsonContent, dependencies);
this.processComponentGenerics(jsonContent, dependencies);
return Array.from(dependencies);
}
catch (e) {
if (e instanceof SyntaxError) {
debug_logger_1.logger.error(`Error parsing JSON file ${filePath}: ${e.message}`);
}
else {
debug_logger_1.logger.warn(`Error processing JSON file ${filePath}: ${e.message}`);
}
// Don't re-throw parsing errors, just return empty
return [];
}
}
processPages(content, dependencies) {
if (content.pages && Array.isArray(content.pages)) {
for (const pagePath of content.pages) {
if (typeof pagePath === 'string') {
// Add the page path as root-relative
dependencies.add('/' + pagePath);
}
}
}
}
processSubPackages(content, dependencies) {
const subpackages = content.subPackages || content.subpackages;
if (subpackages && Array.isArray(subpackages)) {
for (const subpackage of subpackages) {
const root = subpackage.root;
const subPages = subpackage.pages;
if (typeof root === 'string' && Array.isArray(subPages)) {
for (const pagePath of subPages) {
if (typeof pagePath === 'string') {
const fullPagePath = '/' + path.posix.join(root, pagePath);
dependencies.add(fullPagePath);
}
}
}
}
}
}
processTabBar(content, dependencies) {
var _a;
if (((_a = content.tabBar) === null || _a === void 0 ? void 0 : _a.list) && Array.isArray(content.tabBar.list)) {
for (const item of content.tabBar.list) {
if (item && typeof item.iconPath === 'string') {
dependencies.add(item.iconPath);
}
if (item && typeof item.selectedIconPath === 'string') {
dependencies.add(item.selectedIconPath);
}
}
}
}
processUsingComponents(content, dependencies) {
if (content.usingComponents && typeof content.usingComponents === 'object') {
for (const [_componentName, componentPath] of Object.entries(content.usingComponents)) {
if (typeof componentPath === 'string' && !componentPath.startsWith('plugin://')) {
dependencies.add(componentPath);
}
}
}
}
processComponentGenerics(content, dependencies) {
if (content.componentGenerics && typeof content.componentGenerics === 'object') {
for (const genericName in content.componentGenerics) {
const genericInfo = content.componentGenerics[genericName];
if (typeof genericInfo === 'object' && genericInfo.default) {
if (typeof genericInfo.default === 'string') {
dependencies.add(genericInfo.default);
}
}
}
}
}
}
exports.JSONParser = JSONParser;
//# sourceMappingURL=json-parser.js.map