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 回调语法错误
This commit is contained in:
107
vue.config.js
107
vue.config.js
@@ -1,6 +1,9 @@
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
const { zionUniMpLoadPackagePlugin } = require('zion-uniapp-mp-load-package/webpack');
|
||||
|
||||
console.log('[vue.config] 已加载');
|
||||
|
||||
// 打印出process.env
|
||||
// for (let key in process.env) {
|
||||
// console.log(key, process.env[key]);
|
||||
@@ -29,6 +32,107 @@ try {
|
||||
console.log('NODE_ENV 环境变量未设置,默认使用开发模式');
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义 webpack 插件:在通用产物(vendor.js)中内联注入版本数据
|
||||
* 利用 emit 钩子在资源写入磁盘前处理,支持 HMR(每次重编译都会自动注入)
|
||||
* 适用于所有平台(mp-weixin / h5 等)
|
||||
*/
|
||||
class VersionInjectPlugin {
|
||||
constructor() {
|
||||
this._cachedCommitId = null;
|
||||
}
|
||||
apply(compiler) {
|
||||
compiler.hooks.emit.tapAsync('VersionInjectPlugin', (compilation, callback) => {
|
||||
const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
|
||||
const platform = process.env.UNI_PLATFORM || '';
|
||||
|
||||
// 获取 Git 提交 ID(缓存,避免每次 HMR 都执行)
|
||||
if (!this._cachedCommitId) {
|
||||
try {
|
||||
this._cachedCommitId = execSync('git rev-parse --short HEAD', { cwd: __dirname }).toString().trim();
|
||||
} catch (e) {
|
||||
this._cachedCommitId = 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
const versionData = { commitId: this._cachedCommitId, buildTime: new Date().toISOString() };
|
||||
const inlineVersion = JSON.stringify(versionData);
|
||||
|
||||
// 尝试注入到 vendor.js(mp-weixin / mp-* 等平台)
|
||||
const vendorKey = 'common/vendor.js';
|
||||
const vendorAsset = compilation.assets[vendorKey];
|
||||
if (vendorAsset) {
|
||||
let content = typeof vendorAsset.source === 'function' ? vendorAsset.source() : vendorAsset._value;
|
||||
if (typeof content !== 'string') content = content.toString();
|
||||
|
||||
// 清理旧注入
|
||||
content = content.replace(/^\s*const\s+site\s*=\s*require\s*\(['"]\.\.\/site(\.js)?['"]\);?\s*\r?\n/gm, '');
|
||||
content = content.replace(/^\s*const\s+__app_version\s*=\s*(require\s*\(['"]\.\.\/version(\.json|\.js)?['"]\)|.+);?\s*\r?\n/gm, '');
|
||||
|
||||
// 只注入版本信息(通用),site.js require 仅在 mp-weixin 生产构建时注入
|
||||
const lines = [`const __app_version = ${inlineVersion};`];
|
||||
if (platform === 'mp-weixin' && mode === 'production') {
|
||||
lines.unshift('const site = require("../site.js");');
|
||||
}
|
||||
|
||||
const eol = content.includes('\r\n') ? '\r\n' : '\n';
|
||||
let prefix = '';
|
||||
if (content.charCodeAt(0) === 0xfeff) {
|
||||
prefix = '\uFEFF';
|
||||
content = content.slice(1);
|
||||
}
|
||||
|
||||
content = prefix + lines.join(eol) + eol + content;
|
||||
|
||||
compilation.assets[vendorKey] = {
|
||||
source: () => content,
|
||||
size: () => content.length
|
||||
};
|
||||
|
||||
console.log(`[VersionInject] ✅ vendor.js 已注入 (platform=${platform}, mode=${mode}, commit=${this._cachedCommitId})`);
|
||||
} else {
|
||||
// H5 等平台可能没有 common/vendor.js,改为尝试注入到 index.html
|
||||
// #ifdef H5
|
||||
const htmlKey = 'index.html';
|
||||
const htmlAsset = compilation.assets[htmlKey];
|
||||
if (htmlAsset) {
|
||||
let html = typeof htmlAsset.source === 'function' ? htmlAsset.source() : htmlAsset._value;
|
||||
if (typeof html !== 'string') html = html.toString();
|
||||
// 在 </head> 前注入 script 标签
|
||||
const scriptTag = `<script>var __app_version = ${inlineVersion};</script>`;
|
||||
if (!html.includes('__app_version')) {
|
||||
html = html.replace('</head>', `${scriptTag}\n</head>`);
|
||||
compilation.assets[htmlKey] = {
|
||||
source: () => html,
|
||||
size: () => html.length
|
||||
};
|
||||
console.log(`[VersionInject] ✅ index.html 已注入 (platform=${platform}, commit=${this._cachedCommitId})`);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
|
||||
// 生产模式下(仅 mp-weixin),额外执行外部脚本完成 site.js 拷贝 + ZIP 打包
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
compiler.hooks.afterEmit.tapAsync('MpWeixinZipPack', (compilation, callback) => {
|
||||
const platform = process.env.UNI_PLATFORM || '';
|
||||
if (platform !== 'mp-weixin') return callback();
|
||||
|
||||
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);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出配置
|
||||
module.exports = {
|
||||
productionSourceMap: false,
|
||||
@@ -118,5 +222,8 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 所有模式下添加版本注入插件(适用于所有平台)
|
||||
config.plugins.push(new VersionInjectPlugin());
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user