提取编译后的cocos ts文件

提取编译后的cocos ts文件

const fs = require('fs');
const path = require('path');

// 读取压缩文件
const inputFile = path.join(__dirname, 'assets/main/index.ee639.js');
const outputDir = path.join(__dirname, 'game/src');

// 确保输出目录存在
if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
}

console.log('开始解析文件:', inputFile);

try {
    const content = fs.readFileSync(inputFile, 'utf8');
    console.log('文件读取成功,大小:', content.length, '字节');
    
    // 增强的正则表达式,更好地匹配System.register模块
    const systemRegisterRegex = /System\.register\s*\(\s*([^,]+),\s*\[([^\]]+)\],\s*\(\s*function\s*\(([^)]+)\)\s*{([\s\S]*?)(?:}\s*\)\s*\)\s*;|}\s*\)\s*\);)/g;
    
    let match;
    let moduleCount = 0;
    
    console.log('开始提取System.register模块...');
    
    while ((match = systemRegisterRegex.exec(content)) !== null) {
        try {
            const [fullMatch, moduleName, dependencies, params, moduleBody] = match;
            
            // 解析模块名称,移除引号
            const cleanModuleName = moduleName.replace(/['"\\]/g, '').replace(/chunks:\/\/\/_virtual\//, '').replace(/[^a-zA-Z0-9._-]/g, '_');
            
            // 提取唯一标识符
            let uniqueId = '';
            const rfPushRegex = /_RF\.push\({}\s*,\s*["']([^"']+)["']\s*,\s*["'][^"']*["']/;
            const rfMatch = moduleBody.match(rfPushRegex);
            if (rfMatch && rfMatch[1]) {
                uniqueId = rfMatch[1];
            }
            
            // 构建新的模块内容
            const newModuleContent = `System.register(${moduleName}, [${dependencies}], (function(${params}) {${moduleBody}}));`;
            
            // 生成文件名
            let fileName;
            if (cleanModuleName.includes('.')) {
                fileName = cleanModuleName;
            } else {
                fileName = cleanModuleName + '.js';
            }
            
            const outputPath = path.join(outputDir, fileName);
            
            // 写入文件
            fs.writeFileSync(outputPath, newModuleContent, 'utf8');
            
            // 记录依赖关系
            const deps = dependencies.replace(/['"\\]/g, '').split(',').map(d => d.trim()).filter(d => d);
            
            console.log(`模块 ${moduleCount + 1} 提取成功:`);
            console.log(`  文件名: ${fileName}`);
            console.log(`  模块名: ${cleanModuleName}`);
            console.log(`  唯一ID: ${uniqueId || '未找到'}`);
            console.log(`  依赖数: ${deps.length}`);
            console.log(`  文件路径: ${outputPath}`);
            console.log('-------------------');
            
            moduleCount++;
            
            // 每处理50个模块输出一次进度
            if (moduleCount > 0 && moduleCount % 50 === 0) {
                console.log(`已处理 ${moduleCount} 个模块,继续处理...`);
            }
            
        } catch (err) {
            console.error('处理单个模块时出错:', err.message);
        }
    }
    
    console.log(`解析完成!成功提取了 ${moduleCount} 个模块。`);
    
    // 创建模块依赖分析文件
    const analysisFile = path.join(outputDir, 'modules_analysis.txt');
    fs.writeFileSync(analysisFile, `模块提取分析报告\n` +
                               `=================\n` +
                               `提取时间: ${new Date().toLocaleString()}\n` +
                               `源文件: ${inputFile}\n` +
                               `提取模块数: ${moduleCount}\n`, 'utf8');
    
    console.log(`分析报告已保存到: ${analysisFile}`);
    
} catch (error) {
    console.error('解析过程中出错:', error.message);
    console.error('错误详情:', error.stack);
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容