mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
101 lines • 4.73 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.findMiniProgramEntryPoints = findMiniProgramEntryPoints;
const path = __importStar(require("path"));
const analyzer_1 = require("../analyzer/analyzer");
const command_init_1 = require("../utils/command-init");
const debug_logger_1 = require("../utils/debug-logger");
const filetypes_1 = require("../utils/filetypes");
/**
* Finds potential entry points for a Mini Program project.
* Includes app, pages, subPackages, and components found via usingComponents.
*
* @param projectRoot The absolute path to the root of the entire project.
* @returns A promise resolving to an array of entry file paths relative to the projectRoot.
*/
async function findMiniProgramEntryPoints(projectRoot) {
var _a;
debug_logger_1.logger.debug('[EntryFinder] 使用 analyzeProject 结果生成入口点(递归)。');
debug_logger_1.logger.debug(`[EntryFinder] Project Root: ${projectRoot}`);
// 通过统一的初始化流程获取 appJson、别名、排除规则等上下文
const cliOptions = { project: projectRoot };
const context = await (0, command_init_1.initializeCommandContext)(cliOptions);
if (!context.appJsonContent || Object.keys(context.appJsonContent).length === 0) {
debug_logger_1.logger.warn('[EntryFinder] 未找到有效的 app.json 内容,返回空入口列表。');
return [];
}
try {
const { projectStructure } = await (0, analyzer_1.analyzeProject)(projectRoot, {
...context,
includeAssets: false,
});
const nodeMap = new Map(projectStructure.nodes.map((n) => [n.id, n]));
const allowedExts = new Set(filetypes_1.COMPONENT_DEFINITION_FILE_TYPES);
const entries = new Set();
// 1) 从非 Module(App/Package/Page/Component)到 Module 的直接结构链接
for (const link of projectStructure.links) {
if (link.type !== 'Structure')
continue;
const source = nodeMap.get(link.source);
const target = nodeMap.get(link.target);
if (!source || !target)
continue;
if (source.type !== 'Module' && target.type === 'Module' && ((_a = target.properties) === null || _a === void 0 ? void 0 : _a.absolutePath)) {
const abs = target.properties.absolutePath;
const ext = path.extname(abs).toLowerCase().slice(1);
if (allowedExts.has(ext)) {
entries.add(path.relative(projectRoot, abs));
}
}
}
// 2) essentialFiles 直接加入(来自初始化上下文)
if (Array.isArray(context.essentialFiles)) {
for (const abs of context.essentialFiles) {
const ext = path.extname(abs).toLowerCase().slice(1);
if (allowedExts.has(ext)) {
entries.add(path.relative(projectRoot, abs));
}
}
}
debug_logger_1.logger.info(`[EntryFinder] 基于第一层结构关联与必要文件生成入口文件数: ${entries.size}`);
return Array.from(entries);
}
catch (e) {
debug_logger_1.logger.error(`[EntryFinder] analyzeProject 执行失败: ${e.message}`);
return [];
}
}
//# sourceMappingURL=entry-finder.js.map