const fs = require('fs'); const path = require('path'); // 定义要压缩的目录 const rootDir = __dirname; // 定义wxml压缩函数 function compressWxml(content) { // 移除HTML注释 content = content.replace(//g, ''); // 移除多余的空白字符(空格、换行、制表符) // 保留标签内的必要空格 content = content.replace(/\s+/g, ' '); // 移除标签前后的空格 content = content.replace(/\s*\s*/g, '>'); return content; } // 递归遍历目录并压缩wxml文件 function traverseDir(dir) { const files = fs.readdirSync(dir); files.forEach(file => { const filePath = path.join(dir, file); const stats = fs.statSync(filePath); if (stats.isDirectory()) { // 跳过node_modules和其他不需要处理的目录 if (file !== 'node_modules' && file !== '.git' && file !== 'dist') { traverseDir(filePath); } } else if (path.extname(file) === '.wxml') { // 读取并压缩wxml文件 const content = fs.readFileSync(filePath, 'utf8'); const compressedContent = compressWxml(content); // 写回压缩后的内容 fs.writeFileSync(filePath, compressedContent, 'utf8'); console.log(`Compressed: ${filePath}`); } }); } // 开始压缩 console.log('Start compressing wxml files...'); traverseDir(rootDir); console.log('All wxml files compressed successfully!');