feat(scripts): 构建时自动生成 version.json 并注入到 vendor.js
This commit is contained in:
@@ -4,9 +4,11 @@
|
|||||||
*
|
*
|
||||||
* 该脚本用于修复微信小程序的一些问题:
|
* 该脚本用于修复微信小程序的一些问题:
|
||||||
* 1. 将项目根目录下的 site.js 文件拷贝到 unpackage/dist/build/mp-weixin 目录下,如果文件存在则覆盖
|
* 1. 将项目根目录下的 site.js 文件拷贝到 unpackage/dist/build/mp-weixin 目录下,如果文件存在则覆盖
|
||||||
* 2. 将 `const site = require("../site.js");` 这段代码追加到
|
* 2. 将 project.config.json 及 project.private.config.json 拷贝到目标目录
|
||||||
* unpackage/dist/build/mp-weixin/common/vendor.js 文件内容开头部分,
|
* 3. 生成 version.json(包含 Git 提交 ID 和构建时间)到目标目录
|
||||||
* 如果这个文件开头已经有了这行代码,则不追加
|
* 4. 将 `const site = require("../site");` 和 `const __app_version = require("../version.json");`
|
||||||
|
* 追加到 unpackage/dist/build/mp-weixin/common/vendor.js 文件内容开头部分,
|
||||||
|
* 如果文件开头已有则跳过
|
||||||
*
|
*
|
||||||
* 使用:
|
* 使用:
|
||||||
* node fix-wechat-miniapp.js # 打补丁并创建 ZIP 文件(默认 mode=production)
|
* node fix-wechat-miniapp.js # 打补丁并创建 ZIP 文件(默认 mode=production)
|
||||||
@@ -22,6 +24,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const fsp = fs.promises;
|
const fsp = fs.promises;
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
|
||||||
const { openFileDirectory } = require('./open-directory');
|
const { openFileDirectory } = require('./open-directory');
|
||||||
const { createZipWithSystemCommand } = require('./create-zip');
|
const { createZipWithSystemCommand } = require('./create-zip');
|
||||||
@@ -68,7 +71,22 @@ async function commonPatch(mode = 'production') {
|
|||||||
await fsp.copyFile(srcSitePath, destSitePath);
|
await fsp.copyFile(srcSitePath, destSitePath);
|
||||||
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
||||||
|
|
||||||
// 2) 处理 vendor.js:在开头追加 require 语句(如果还没有的话)
|
// 3) 生成 version.json(包含 Git 提交 ID 和构建时间)
|
||||||
|
let commitId = 'unknown';
|
||||||
|
try {
|
||||||
|
commitId = execSync('git rev-parse --short HEAD', { cwd }).toString().trim();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('无法获取 Git 提交 ID,使用 unknown 代替');
|
||||||
|
}
|
||||||
|
const versionJson = {
|
||||||
|
commitId,
|
||||||
|
buildTime: new Date().toISOString()
|
||||||
|
};
|
||||||
|
const versionPath = path.join(destDir, 'version.json');
|
||||||
|
await fsp.writeFile(versionPath, JSON.stringify(versionJson, null, 2));
|
||||||
|
console.log(`已生成: ${versionPath} (commitId: ${commitId})`);
|
||||||
|
|
||||||
|
// 4) 处理 vendor.js:在开头追加 require 语句(如果还没有的话)
|
||||||
const vendorExists = await exists(vendorPath);
|
const vendorExists = await exists(vendorPath);
|
||||||
if (!vendorExists) {
|
if (!vendorExists) {
|
||||||
console.warn(`目标 vendor.js 不存在,跳过追加 require。路径: ${vendorPath}`);
|
console.warn(`目标 vendor.js 不存在,跳过追加 require。路径: ${vendorPath}`);
|
||||||
@@ -78,20 +96,24 @@ async function commonPatch(mode = 'production') {
|
|||||||
let vendorContent = await fsp.readFile(vendorPath, 'utf8');
|
let vendorContent = await fsp.readFile(vendorPath, 'utf8');
|
||||||
|
|
||||||
const requireLine = 'const site = require("../site");';
|
const requireLine = 'const site = require("../site");';
|
||||||
// 检查文件开头(例如前 5 行或前 400 字符)是否已经包含该 require 语句
|
const versionLine = 'const __app_version = require("../version.json");';
|
||||||
const inspectPrefix = vendorContent.slice(0, 400);
|
// 检查文件开头(例如前 1000 字符)是否已经包含这些 require 语句
|
||||||
const requireRegex = new RegExp(
|
const inspectPrefix = vendorContent.slice(0, 1000);
|
||||||
// 匹配可能存在的引号或分号差异
|
const hasSiteRequire = new RegExp(
|
||||||
'^\\s*const\\s+site\\s*=\\s*require\\s*\\([\'"]\\.\\./site[\'"]\\);?',
|
'^\\s*const\\s+site\\s*=\\s*require\\s*\\([\'"]\\.\\./site[\'"]\\);?',
|
||||||
'm'
|
'm'
|
||||||
);
|
).test(inspectPrefix);
|
||||||
|
const hasVersionRequire = new RegExp(
|
||||||
|
'^\\s*const\\s+__app_version\\s*=\\s*require\\s*\\([\'"]\\.\\./version\\.json[\'"]\\);?',
|
||||||
|
'm'
|
||||||
|
).test(inspectPrefix);
|
||||||
|
|
||||||
if (requireRegex.test(inspectPrefix)) {
|
if (hasSiteRequire && hasVersionRequire) {
|
||||||
console.log('vendor.js 开头已包含 require("../site") 语句,跳过追加。');
|
console.log('vendor.js 开头已包含 require 语句,跳过追加。');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 把 requireLine + newline 插入文件最前面。
|
// 把 require 语句 + newline 插入文件最前面。
|
||||||
// 保留原始换行风格(若可能)
|
// 保留原始换行风格(若可能)
|
||||||
const eol = vendorContent.includes('\r\n') ? '\r\n' : '\n';
|
const eol = vendorContent.includes('\r\n') ? '\r\n' : '\n';
|
||||||
let prefix = '';
|
let prefix = '';
|
||||||
@@ -102,9 +124,13 @@ async function commonPatch(mode = 'production') {
|
|||||||
vendorContent = vendorContent.slice(1);
|
vendorContent = vendorContent.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newContent = prefix + requireLine + eol + vendorContent;
|
const linesToInject = [requireLine];
|
||||||
|
if (!hasVersionRequire) {
|
||||||
|
linesToInject.push(versionLine);
|
||||||
|
}
|
||||||
|
const newContent = prefix + linesToInject.join(eol) + eol + vendorContent;
|
||||||
await fsp.writeFile(vendorPath, newContent, 'utf8');
|
await fsp.writeFile(vendorPath, newContent, 'utf8');
|
||||||
console.log(`已在 vendor.js 开头追加: ${requireLine}`);
|
console.log(`已在 vendor.js 开头追加: ${linesToInject.join(', ')}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('发生错误:', err);
|
console.error('发生错误:', err);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user