mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
238 lines • 10.7 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.analyzeWxmlForPurge = analyzeWxmlForPurge;
exports.isSafeClassExpression = isSafeClassExpression;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Could not find a declaration file for module '@wxml/parser'
const parser_1 = require("@wxml/parser");
const fs = __importStar(require("fs"));
const debug_logger_1 = require("../../utils/debug-logger");
const wxml_path_1 = require("../../utils/wxml-path");
/**
* Analyzes a WXML file and its imports/includes to extract tags, static classes,
* and dynamic class expressions for PurgeCSS.
*
* @param initialWxmlFilePath Path to the initial WXML file to analyze
* @param pathResolver Instance of PathResolver to resolve import/include paths
* @param visited Set of already visited files (to prevent infinite loops and redundant work)
* @returns Promise<WxmlPurgeAnalysisResult>
*/
async function analyzeWxmlForPurge(initialWxmlFilePath, pathResolver, visited = new Set()) {
const aggregatedResult = {
wxmlFilePaths: new Set(),
tagNames: new Set(),
staticClassNames: new Set(),
dynamicClassValues: new Set(),
riskyDynamicClassPatterns: [], // Initialize new field
};
await _analyzeWxmlRecursiveForPurge(initialWxmlFilePath, pathResolver, visited, aggregatedResult);
return aggregatedResult;
}
async function _analyzeWxmlRecursiveForPurge(currentWxmlFilePath, pathResolver, visited, aggregatedResult) {
if (visited.has(currentWxmlFilePath)) {
return;
}
visited.add(currentWxmlFilePath);
aggregatedResult.wxmlFilePaths.add(currentWxmlFilePath);
try {
const content = fs.readFileSync(currentWxmlFilePath, 'utf-8');
const ast = (0, parser_1.parse)(content);
collectWxmlData(ast, currentWxmlFilePath, aggregatedResult);
const importPaths = extractImportPaths(ast); // Reuse existing import path extraction
for (const importPath of importPaths) {
try {
const resolvedPath = pathResolver.resolveAnyPath(importPath, currentWxmlFilePath, ['wxml']);
if (resolvedPath) {
await _analyzeWxmlRecursiveForPurge(resolvedPath, pathResolver, visited, aggregatedResult);
}
}
catch (err) {
debug_logger_1.logger.warn(`Error processing import ${importPath} in ${currentWxmlFilePath} for purge analysis: ${err}`);
}
}
}
catch (err) {
debug_logger_1.logger.error(`Error analyzing WXML file ${currentWxmlFilePath} for purge analysis: ${err}`);
}
}
/**
* Checks if a WXML class expression (the content inside {{...}}) is safe.
* Safe: simple literals, or ternary operators yielding simple literals.
* Unsafe: contains '+' for concatenation, unless it's part of a more complex, unhandled safe pattern.
* @param expression The content of the class binding, e.g., "condition ? 'classA' : 'classB'" or "'prefix-' + varName"
*/
function isSafeClassExpression(expression) {
const trimmedExpression = expression.trim();
// Regex for: condition ? 'literal' : 'literal' OR condition ? "literal" : "literal"
// Allows for empty strings as literals e.g. condition ? 'my-class' : ''
// It also allows for non-literal conditions.
const safeTernaryRegex = /^(?:[^?]+)\s*\?\s*('[^']*'|"[^"]*")\s*:\s*('[^']*'|"[^"]*")$/;
if (safeTernaryRegex.test(trimmedExpression)) {
return true;
}
// Regex for simple string literal: 'literal' OR "literal"
const simpleLiteralRegex = /^('[^']*'|"[^"]*")$/;
if (simpleLiteralRegex.test(trimmedExpression)) {
return true;
}
// If it's not a safe ternary or simple literal, and contains '+', it's considered risky.
if (trimmedExpression.includes('+')) {
return false;
}
// Default to safe if no '+' is found and it's not an explicitly identified risky pattern.
// This means expressions like `{{ myObject.classKey }}` or `{{ [classA, classB] }}`
// would be considered "safe" by this specific rule if they don't use '+'.
return true;
}
/**
* Collects tags, static classes, and dynamic class attributes from the AST.
*/
function collectWxmlData(node, wxmlFilePath, // current wxml file path for context if needed
result) {
var _a;
if (node.type === 'WXElement') {
result.tagNames.add(node.name);
const attrs = (_a = node.startTag) === null || _a === void 0 ? void 0 : _a.attributes; // Type assertion for safety
if (attrs && Array.isArray(attrs)) {
for (const attr of attrs) {
if (attr.key === 'class' && typeof attr.value === 'string') {
const classValue = attr.value;
// Split by space for static classes, but keep {{...}} intact
const classParts = classValue.match(/\{\{[^}]*\}\}|[^{}\s]+/g) || [];
for (const part of classParts) {
if (part.startsWith('{{') && part.endsWith('}}')) {
const innerExpression = part.substring(2, part.length - 2);
if (isSafeClassExpression(innerExpression)) {
result.dynamicClassValues.add(part);
// Try to extract literal strings from within SAFE {{...}} as potential static classes
const literalRegex = /['"]([^'"]+)['"]/g;
let match;
while ((match = literalRegex.exec(part)) !== null) {
if (match[1]) {
match[1]
.trim()
.split(/\\s+/)
.filter(Boolean)
.forEach((cls) => result.staticClassNames.add(cls));
}
}
}
else {
// This is a risky dynamic class pattern
result.riskyDynamicClassPatterns.push({
filePath: wxmlFilePath,
expression: part,
});
// Optionally, log here or ensure it's logged by the caller
debug_logger_1.logger.warn(`Risky dynamic class pattern found in ${wxmlFilePath}: ${part}`);
}
}
else {
// Static class name
part
.trim()
.split(/\s+/)
.filter(Boolean)
.forEach((cls) => result.staticClassNames.add(cls));
}
}
}
else if (attr.key &&
attr.key.startsWith('generic:') &&
attr.value &&
typeof attr.value === 'string') {
// Handle generic component values as potential tags
result.tagNames.add(attr.value);
}
}
}
}
// Recursively process children
if (node.type === 'WXElement' && Array.isArray(node.children)) {
for (const child of node.children) {
collectWxmlData(child, wxmlFilePath, result);
}
}
// Handle Program/body
if (node.type === 'Program' && Array.isArray(node.body)) {
for (const item of node.body) {
collectWxmlData(item, wxmlFilePath, result);
}
}
}
/**
* Extracts import and include paths from the AST
*
* @param ast WXML AST node or Program
* @returns Array of import/include paths
*/
function extractImportPaths(ast) {
const importPaths = [];
findImportTags(ast, importPaths);
return importPaths;
}
/**
* Recursively finds import and include tags in the AST
*
* @param ast WXML AST node or Program
* @param importPaths Array to store found paths
*/
function findImportTags(ast, importPaths) {
var _a;
if (ast.type === 'WXElement' && (ast.name === 'import' || ast.name === 'include')) {
// Find src attribute from startTag.attributes per wxml-parser AST docs
const attrs = (_a = ast.startTag) === null || _a === void 0 ? void 0 : _a.attributes;
if (attrs && Array.isArray(attrs)) {
const srcAttr = attrs.find((attr) => attr.key === 'src');
if (srcAttr && srcAttr.value) {
importPaths.push((0, wxml_path_1.normalizeWxmlImportPath)(srcAttr.value));
}
}
}
// Recursively process children
if (ast.type === 'WXElement' && Array.isArray(ast.children)) {
for (const child of ast.children) {
findImportTags(child, importPaths);
}
}
// Handle Program/body
if (ast.type === 'Program' && Array.isArray(ast.body)) {
for (const node of ast.body) {
findImportTags(node, importPaths);
}
}
}
//# sourceMappingURL=analyzeWxmlForPurge.js.map