feat(vue.config): H5 生产构建后自动打包 ZIP

This commit is contained in:
2026-05-11 10:59:36 +08:00
parent c1fbabe07b
commit 6033cc2fa0

View File

@@ -135,22 +135,83 @@ class VersionInjectPlugin {
callback();
});
// 生产模式下(仅 mp-weixin),额外执行外部脚本完成 site.js 拷贝 + ZIP 打包
// 生产构建后处理:mp-weixin 拷贝 site.js + ZIP;H5 自动打包 ZIP
if (process.env.NODE_ENV === 'production') {
compiler.hooks.afterEmit.tapAsync('MpWeixinZipPack', (compilation, callback) => {
compiler.hooks.afterEmit.tapAsync('ProductionZipPack', (compilation, callback) => {
const platform = process.env.UNI_PLATFORM || '';
if (platform !== 'mp-weixin') return callback();
const UNI_SCRIPT = process.env.UNI_SCRIPT ?? '';
console.log('\n🚀 mp-weixin 生产构建完成,运行补丁脚本(site.js 拷贝 + ZIP 打包)...');
try {
execSync(`"${process.execPath}" scripts/mp-weixin.patch.js --mode production`, { cwd: __dirname, stdio: 'inherit' });
} catch (e) {
console.error('补丁脚本执行失败:', e.message);
if (platform === 'mp-weixin') {
console.log('\n🚀 mp-weixin 生产构建完成,运行补丁脚本(site.js 拷贝 + ZIP 打包)...');
try {
execSync(`"${process.execPath}" scripts/mp-weixin.patch.js --mode production`, { cwd: __dirname, stdio: 'inherit' });
} catch (e) {
console.error('补丁脚本执行失败:', e.message);
}
} else if (platform === 'h5' && UNI_SCRIPT && this._cachedCommitId) {
this._zipH5Build(UNI_SCRIPT, this._cachedCommitId);
}
callback();
});
}
}
_zipH5Build(UNI_SCRIPT, commitId) {
// 计算输出目录名(与 UNI_OUTPUT_DIR 逻辑一致)
let baseSuffix = '';
try {
const manifest = require('./manifest.json');
if (manifest?.h5?.router?.base) {
baseSuffix = manifest.h5.router.base.replace(/^\/+|\/+$/g, '').replace(/\//g, '.');
}
} catch (e) {}
let domain = '';
if (process.env.UNI_APP_DOMAIN) {
try {
domain = new URL(process.env.UNI_APP_DOMAIN).hostname;
} catch (e) {
domain = process.env.UNI_APP_DOMAIN.replace(/^https?:\/\//, '').replace(/\/.*$/, '');
}
} else {
domain = UNI_SCRIPT.replace(/^h5-/, '');
}
const outputDirName = baseSuffix ? `${domain}-${baseSuffix}` : domain;
// 上海时区时间(UTC+8)
const now = new Date();
const shanghaiOffset = 8 * 60;
const shanghaiTime = new Date(now.getTime() + shanghaiOffset * 60 * 1000);
const ts = shanghaiTime.toISOString().replace(/[^0-9]/g, '').slice(0, 14);
const buildDir = process.env.NODE_ENV === 'production' ? 'build' : 'dev';
const sourceDir = path.join(__dirname, 'unpackage', 'dist', buildDir, 'h5', outputDirName);
const destDir = path.join(__dirname, 'unpackage', 'dist', buildDir, 'h5');
const zipFileName = `${outputDirName}-${commitId}-${ts}.zip`;
const zipFilePath = path.join(destDir, zipFileName);
console.log(`\n📦 H5 构建完成,打包 ZIP: ${zipFileName}...`);
let command;
const sourcePath = sourceDir.replace(/'/g, "''");
const destPath = zipFilePath.replace(/'/g, "''");
if (process.platform === 'win32') {
command = `powershell -Command "Compress-Archive -Path '${sourcePath}\\*' -DestinationPath '${destPath}' -Force"`;
} else {
const sourceBase = path.dirname(sourceDir);
const sourceName = path.basename(sourceDir);
command = `cd '${sourceBase}' && zip -r '${zipFilePath}' '${sourceName}' -x '*/node_modules/*' '*.git/*' '*.DS_Store'`;
}
try {
execSync(command, { stdio: 'inherit' });
console.log(`✅ H5 ZIP 文件已创建: ${zipFileName}`);
} catch (e) {
console.error('H5 ZIP 打包失败:', e.message);
}
}
}
}
}
// 导出配置