Files
lucky_shop/scripts/mp-weixin.patch.js
ZF sun 3bff91e421 refactor: 统一版本信息注入方式
- feat(vue.config): 新增 VersionInjectPlugin,在 webpack emit 钩子中内联注入 __app_version 到 vendor.js / index.html
- refactor(scripts): mp-weixin.patch.js 精简为仅拷贝 site.js + ZIP 打包
- feat(index): 首页连击5次显示构建版本信息
- fix(util): 修复 openWxWorkServiceChat fail 回调语法错误
2026-05-11 09:36:57 +08:00

107 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* fix-wechat-miniapp.js
*
* 该脚本用于微信小程序的构建后处理(仅在生产构建时由 vue.config.js 触发):
* 1. 将 project.config.json 及 project.private.config.json 拷贝到构建产物目录
* 2. 将 site.js 拷贝到构建产物目录
* 3. 创建 ZIP 压缩包
*
* 注意:version.js 生成和 vendor.js 注入已由 webpack 插件 (VersionInjectPlugin) 在 emit 钩子中自动处理。
*
* 使用:
* node fix-wechat-miniapp.js # 拷贝文件并创建 ZIP(默认 mode=production)
* node fix-wechat-miniapp.js --no-zip # 只拷贝文件,不创建 ZIP
*/
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
const { openFileDirectory } = require('./open-directory');
const { createZipWithSystemCommand } = require('./create-zip');
async function copyAssets(mode = 'production') {
try {
const cwd = path.join(__dirname, '..');
const destDir = path.join(cwd, 'unpackage', 'dist', mode === 'production' ? 'build' : 'dev', 'mp-weixin');
await ensureDir(destDir);
// 复制 project.config.json 和 project.private.config.json
const configFiles = ['project.config.json', 'project.private.config.json'];
for (const fileName of configFiles) {
const srcPath = path.join(cwd, fileName);
const destPath = path.join(destDir, fileName);
if (await exists(srcPath)) {
await fsp.copyFile(srcPath, destPath);
console.log(`已拷贝: ${srcPath} -> ${destPath}`);
} else {
console.warn(`源文件不存在,跳过复制: ${srcPath}`);
}
}
// 复制 site.js
const srcSitePath = path.join(cwd, 'site.js');
if (await exists(srcSitePath)) {
await fsp.copyFile(srcSitePath, path.join(destDir, 'site.js'));
console.log(`已拷贝: ${srcSitePath} -> ${destDir}`);
} else {
console.error(`源文件不存在: ${srcSitePath}`);
process.exitCode = 2;
}
} catch (err) {
console.error('发生错误:', err);
process.exitCode = 1;
}
}
async function main() {
const argv = process.argv.slice(2);
const options = {
noZip: argv.includes('--no-zip'),
mode: 'production'
};
const modeIndex = argv.indexOf('--mode');
if (modeIndex !== -1 && modeIndex + 1 < argv.length) {
options.mode = argv[modeIndex + 1];
}
// 1) 拷贝文件
await copyAssets(options.mode);
// 2) 创建 ZIP(仅 production 模式,且未指定 --no-zip)
if (!options.noZip && options.mode === 'production') {
const cwd = path.join(__dirname, '..');
const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', 'mp-weixin');
const destDir = path.join(cwd, 'unpackage', 'dist', 'build');
const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir);
console.log(`ZIP 文件路径: ${zipFilePath}`);
await openFileDirectory(zipFilePath);
} else {
console.log('跳过创建 ZIP 文件和打开目录');
}
}
async function exists(p) {
try {
await fsp.access(p, fs.constants.F_OK);
return true;
} catch (e) {
return false;
}
}
async function ensureDir(dirPath) {
try {
await fsp.mkdir(dirPath, { recursive: true });
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
}
main();