chore(release): 更新压缩脚本

This commit is contained in:
2025-12-27 17:55:09 +08:00
parent c422a51fb8
commit bc89550a28
4 changed files with 2570 additions and 50 deletions

164
mini-optimizer.config.js Normal file
View File

@@ -0,0 +1,164 @@
// mini-optimizer 极限压缩配置文件
module.exports = {
// JavaScript 压缩配置 - 极限压缩
minifyJs: {
// 启用压缩
enabled: true,
// 压缩选项
options: {
// 删除所有注释
comments: false,
// 删除 console 语句
drop_console: true,
// 删除 debugger 语句
drop_debugger: true,
// 启用最高级别的压缩
compress: {
// 启用所有压缩选项
passes: 2,
// 删除未使用的变量和函数
unused: true,
// 删除死代码
dead_code: true,
// 内联函数调用
inline: true,
// 折叠常量
collapse_vars: true,
// 合并变量
merge_vars: true,
// 简化表达式
simplify: true,
// 计算常量表达式
evaluate: true,
// 移除空块
drop_empty: true,
// 合并相同的函数
join_vars: true,
// 内联单引用的函数
inline_functions: true,
// 移除未使用的函数参数
unused_params: true
},
// 混淆选项
mangle: {
// 启用混淆
toplevel: true,
// 混淆变量名
keep_fnames: false,
// 混淆函数名
keep_classnames: false
}
}
},
// CSS 压缩配置 - 极限压缩
minifyCss: {
// 启用压缩
enabled: true,
// 压缩级别2 是最高级别
level: 2,
// 压缩选项
options: {
// 删除所有注释
comments: false,
// 合并选择器
mergeSelectors: true,
// 合并相同的规则
mergeRules: true,
// 移除空规则
removeEmpty: true,
// 移除未使用的选择器
removeUnused: true,
// 简化颜色值
convertColors: true,
// 缩短十六进制颜色
shorthandHex: true,
// 移除不必要的分号
removeLastSemicolon: true,
// 压缩字体权重
fontWeights: true,
// 压缩 URL
urls: true
}
},
// XS 文件压缩配置
minifyXs: {
enabled: true,
options: {
// 与 JS 压缩选项类似
comments: false,
drop_console: true,
drop_debugger: true,
compress: {
passes: 2,
unused: true,
dead_code: true,
inline: true,
collapse_vars: true,
merge_vars: true,
simplify: true,
evaluate: true,
drop_empty: true,
join_vars: true
},
mangle: {
toplevel: true,
keep_fnames: false,
keep_classnames: false
}
}
},
// XML/WXML 文件压缩配置
minifyXml: {
enabled: true,
options: {
// 移除所有空格
removeWhitespace: true,
// 移除所有注释
removeComments: true,
// 压缩属性值
minifyAttributes: true,
// 移除空属性
removeEmptyAttributes: true,
// 移除 CDATA 标签(如果可能)
removeCDATASectionsIfNotRequired: true,
// 合并相邻文本节点
collapseWhitespace: true
}
},
// JSON 文件压缩配置
minifyJson: {
enabled: true,
options: {
// 移除所有空格和换行
space: '',
// 移除所有注释
comments: false,
// 压缩对象键
minifyKeys: true,
// 移除空值
removeEmptyValues: true
}
},
// WXSS 文件压缩配置(与 CSS 相同)
minifyWxss: {
enabled: true,
level: 2,
options: {
comments: false,
mergeSelectors: true,
mergeRules: true,
removeEmpty: true,
removeUnused: true,
convertColors: true,
shorthandHex: true,
removeLastSemicolon: true,
fontWeights: true,
urls: true
}
}
};

2247
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
package.json Normal file
View File

@@ -0,0 +1,22 @@
{
"name": "help",
"version": "1.0.0",
"description": "本项目是为POCT检测分析平台客户定制开发的微信小程序用于展示和销售POCT检测分析平台产品提供在线购物、订单管理、用户中心等功能。",
"main": "app.js",
"scripts": {
"build": "node release.js -k",
"release": "node release.js"
},
"repository": {
"type": "git",
"url": "https://git.aigc-quickapp.com/Uniapp/mp-weixin-2811-xcx.aigc-quickapp.com.git"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mini-optimizer": "^1.0.3-beta.7"
},
"dependencies": {
"@babel/core": "^7.28.5"
}
}

View File

@@ -1,6 +1,10 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { execSync } = require('child_process'); const { execSync } = require('child_process');
// 直接引用本地安装的 mini-optimizer 模块
const Optimizer = require('mini-optimizer').default;
// 导入 mini-optimizer 配置文件
const optimizerConfig = require('./mini-optimizer.config.js');
// 定义项目根目录 // 定义项目根目录
const rootDir = __dirname; const rootDir = __dirname;
@@ -8,7 +12,7 @@ const rootDir = __dirname;
const distDir = path.join(rootDir, 'dist'); const distDir = path.join(rootDir, 'dist');
// 定义要排除的目录和文件 // 定义要排除的目录和文件
const excludeDirs = ['node_modules', '.git', 'dist', '.vscode']; const excludeDirs = ['node_modules', '.git', 'dist', '.vscode'];
const excludeFiles = ['.gitignore', 'package-lock.json', 'yarn.lock', 'release.js', 'README.md']; const excludeFiles = ['.gitignore', 'package.json', 'package-lock.json', 'yarn.lock', 'release.js', 'mini-optimizer.config.js', 'babel.config.js', 'README.md'];
// 获取当前日期用于生成zip文件名格式YYYY-MM-DD // 获取当前日期用于生成zip文件名格式YYYY-MM-DD
function getCurrentDate() { function getCurrentDate() {
@@ -138,8 +142,9 @@ function compressJson(content) {
} }
} }
// 递归复制目录并压缩文件wxml, js, css, json
function copyAndCompressDir(sourceDir, targetDir) { // 递归复制目录(只复制文件,不进行压缩)
function copyDir(sourceDir, targetDir) {
// 创建目标目录 // 创建目标目录
if (!fs.existsSync(targetDir)) { if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true }); fs.mkdirSync(targetDir, { recursive: true });
@@ -147,63 +152,140 @@ function copyAndCompressDir(sourceDir, targetDir) {
const files = fs.readdirSync(sourceDir); const files = fs.readdirSync(sourceDir);
files.forEach(file => { for (const file of files) {
const sourcePath = path.join(sourceDir, file); const sourcePath = path.join(sourceDir, file);
const targetPath = path.join(targetDir, file); const targetPath = path.join(targetDir, file);
const stats = fs.statSync(sourcePath); const stats = fs.statSync(sourcePath);
// 跳过排除的目录和文件 // 跳过排除的目录和文件
if (excludeDirs.includes(file) || excludeFiles.includes(file)) { if (excludeDirs.includes(file) || excludeFiles.includes(file)) {
return; continue;
} }
if (stats.isDirectory()) { if (stats.isDirectory()) {
// 递归处理子目录 // 递归处理子目录
copyAndCompressDir(sourcePath, targetPath); copyDir(sourcePath, targetPath);
} else { } else {
const extname = path.extname(file); // 直接复制文件
let compressedContent; fs.copyFileSync(sourcePath, targetPath);
let content; console.log(`Copied: ${sourcePath}`);
}
}
}
// 使用 mini-optimizer API 对 dist 目录进行统一压缩
async function compressDistDir(targetDir) {
console.log('Start compressing files in dist directory using mini-optimizer API...');
// 创建优化器实例,使用配置文件中的极限压缩选项
const optimizer = new Optimizer(targetDir, 'wx', optimizerConfig);
// 是否启用兼容模式 - 开启后,会使用自定义压缩函数处理压缩失败的文件
const enableCompatMode = false;
// 记录哪些成功被压缩的文件
const compressedFiles = [];
const unCompressedFiles = [];
// 递归遍历 dist 目录,压缩所有需要的文件类型
async function compressDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
try { if (stats.isDirectory()) {
// 根据文件类型选择不同的处理方式 // 递归处理子目录
if (extname === '.wxml') { await compressDir(filePath);
// 压缩wxml文件 } else {
content = fs.readFileSync(sourcePath, { encoding: 'utf8' }); const extname = path.extname(file);
compressedContent = compressWxml(content);
fs.writeFileSync(targetPath, compressedContent, { encoding: 'utf8' }); try {
console.log(`Compressed and copied: ${sourcePath}`); // 读取文件内容
} else if (extname === '.js') { const content = fs.readFileSync(filePath, { encoding: 'utf8' });
// 压缩js文件, 还是有问题,会出现编译错误。暂时不适用压缩 let compressedContent;
content = fs.readFileSync(sourcePath, { encoding: 'utf8' });
// compressedContent = compressJs(content); // 使用 mini-optimizer 压缩不同类型的文件
fs.writeFileSync(targetPath, content, { encoding: 'utf8' }); if (extname === '.wxml' || extname === '.xml') {
console.log(`Compressed and copied: ${sourcePath}`); // 压缩xml和wxml文件 - 禁用属性值压缩以避免Babel解析错误
} else if (extname === '.css') { try {
// 压缩css文件 compressedContent = optimizer.optimize_xml(content, { minifyAttributes: false });
content = fs.readFileSync(sourcePath, { encoding: 'utf8' }); compressedFiles.push(filePath);
compressedContent = compressCss(content); } catch (error) {
fs.writeFileSync(targetPath, compressedContent, { encoding: 'utf8' }); if (!enableCompatMode) {
console.log(`Compressed and copied: ${sourcePath}`); throw error;
} else if (extname === '.json') { }
// 压缩json文件 compressedContent = compressWxml(content);
content = fs.readFileSync(sourcePath, { encoding: 'utf8' }); compressedFiles.push(filePath);
compressedContent = compressJson(content); }
fs.writeFileSync(targetPath, compressedContent, { encoding: 'utf8' }); } else if (extname === '.js') {
console.log(`Compressed and copied: ${sourcePath}`); // 压缩js文件
} else { try {
// 直接复制其他文件 compressedContent = optimizer.optimize_js(content);
fs.copyFileSync(sourcePath, targetPath); compressedFiles.push(filePath);
console.log(`Copied: ${sourcePath}`); } catch (error) {
if (!enableCompatMode) {
throw error;
}
compressedContent = compressJs(content);
compressedFiles.push(filePath);
}
} else if (extname === '.css' || extname === '.wxss') {
// 压缩css和wxss文件
try {
compressedContent = await optimizer.optimize_css(content);
compressedFiles.push(filePath);
} catch (error) {
if (!enableCompatMode) {
throw error;
}
compressedContent = compressCss(content);
compressedFiles.push(filePath);
}
} else if (extname === '.json') {
// 压缩json文件
try {
compressedContent = optimizer.optimize_json(content);
compressedFiles.push(filePath);
} catch (error) {
if (!enableCompatMode) {
throw error;
}
compressedContent = compressJson(content);
compressedFiles.push(filePath);
}
} else if (extname === '.xs') {
// 压缩xs文件
compressedContent = optimizer.optimize_xs(content);
compressedFiles.push(filePath);
} else {
// 跳过不需要压缩的文件
continue;
}
// 写入压缩后的内容
fs.writeFileSync(filePath, compressedContent, { encoding: 'utf8' });
console.log(`Compressed: ${filePath}`);
} catch (error) {
console.error(`Error compressing file ${filePath}:`, error.message);
unCompressedFiles.push(filePath);
// 发生错误时,保留原始文件
console.log(`Kept original file: ${filePath}`);
} }
} catch (error) {
console.error(`Error processing file ${sourcePath}:`, error.message);
// 发生错误时,直接复制原始文件
fs.copyFileSync(sourcePath, targetPath);
console.log(`Fallback: Copied original file ${sourcePath}`);
} }
} }
}); }
// 开始压缩整个dist目录
await compressDir(targetDir);
// 打印压缩成功的文件列表
console.log('Compressed files:', compressedFiles);
// 打印未压缩的文件列表
console.log('Uncompressed files:', unCompressedFiles);
console.log('Files compressed successfully using mini-optimizer API!');
} }
// 使用系统命令创建zip压缩包 // 使用系统命令创建zip压缩包
@@ -253,7 +335,7 @@ function cleanDistDir(distDir) {
} }
// 主函数 // 主函数
function main() { async function main() {
try { try {
// 解析命令行参数 // 解析命令行参数
const args = process.argv.slice(2); const args = process.argv.slice(2);
@@ -268,10 +350,15 @@ function main() {
console.log('Created dist directory'); console.log('Created dist directory');
} }
// 复制并压缩文件到dist目录 // 第一步:复制所有文件到dist目录(不压缩)
console.log('Start copying and compressing files...'); console.log('Start copying files...');
copyAndCompressDir(rootDir, distDir); copyDir(rootDir, distDir);
console.log('Files copied and compressed successfully!'); console.log('Files copied successfully!');
// 第二步使用mini-optimizer对dist目录进行统一压缩
console.log('Start compressing files in dist directory...');
await compressDistDir(distDir);
console.log('Files compressed successfully!');
// 生成zip文件名格式POCT检测分析平台-定制化-YYYY-MM-DD-mp-weixin.zip // 生成zip文件名格式POCT检测分析平台-定制化-YYYY-MM-DD-mp-weixin.zip
const currentDate = getCurrentDate(); const currentDate = getCurrentDate();