mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
107 lines • 4.28 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.AssetResolver = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const debug_logger_1 = require("./debug-logger");
/**
* 资源解析器
* 用于定位UI资源文件
*/
class AssetResolver {
/**
* 获取UI资源文件路径
* @param relativePath 资源文件的相对路径
* @returns 完整的文件路径
*/
static getAssetPath(relativePath) {
// 编译后的结构 (当 outDir is './dist'):
// - TypeScript 编译到: dist/
// - UI 资源输出到: dist/ui-assets/
// 因此 __dirname 在编译后会是 dist/utils
// 首先检查相对于当前目录的路径 (支持从编译后的dist/utils目录访问)
const compiledPath = path.resolve(__dirname, './ui-assets', relativePath);
if (fs.existsSync(compiledPath)) {
debug_logger_1.logger.debug(`在编译输出 dist/ui-assets 目录找到资源: ${compiledPath}`);
return compiledPath;
}
// 尝试从当前源码位置直接访问已构建的dist/ui-assets (针对开发模式)
const directDistPath = path.resolve(__dirname, '../../dist/ui-assets', relativePath);
if (fs.existsSync(directDistPath)) {
debug_logger_1.logger.debug(`在源码相对路径的dist/ui-assets找到资源: ${directDistPath}`);
return directDistPath;
}
// 如果所有位置都找不到,记录警告并返回最可能的路径
debug_logger_1.logger.warn(`未找到资源文件: ${relativePath}`);
debug_logger_1.logger.debug(`尝试过以下路径:
- ${compiledPath} (基于编译后的位置)
- ${directDistPath} (直接从源码访问dist)`);
return compiledPath; // 返回第一个路径用于错误报告
}
/**
* 读取资源文件内容
* @param relativePath 相对路径
* @returns 文件内容,如果找不到则返回空字符串
*/
static getAssetContent(relativePath) {
const filePath = this.getAssetPath(relativePath);
try {
return fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf-8') : '';
}
catch (error) {
debug_logger_1.logger.error(`读取资源文件失败: ${filePath}`, error);
return '';
}
}
/**
* 获取JS资源
* @param relativePath JS文件的相对路径
* @returns JS内容
*/
static getJsAsset(relativePath) {
return this.getAssetContent(relativePath);
}
/**
* 获取CSS资源
* @param relativePath CSS文件的相对路径
* @returns CSS内容
*/
static getCssAsset(relativePath) {
return this.getAssetContent(relativePath);
}
}
exports.AssetResolver = AssetResolver;
//# sourceMappingURL=asset-resolver.js.map