fix(scripts): version.json改为生成version.js(CommonJS),兼容微信小程序require限制
This commit is contained in:
@@ -5,8 +5,8 @@
|
|||||||
* 该脚本用于修复微信小程序的一些问题:
|
* 该脚本用于修复微信小程序的一些问题:
|
||||||
* 1. 将项目根目录下的 site.js 文件拷贝到 unpackage/dist/build/mp-weixin 目录下,如果文件存在则覆盖
|
* 1. 将项目根目录下的 site.js 文件拷贝到 unpackage/dist/build/mp-weixin 目录下,如果文件存在则覆盖
|
||||||
* 2. 将 project.config.json 及 project.private.config.json 拷贝到目标目录
|
* 2. 将 project.config.json 及 project.private.config.json 拷贝到目标目录
|
||||||
* 3. 生成 version.json(包含 Git 提交 ID 和构建时间)到目标目录
|
* 3. 生成 version.js(CommonJS 模块)和 version.json 到目标目录
|
||||||
* 4. 将 `const site = require("../site");` 和 `const __app_version = require("../version.json");`
|
* 4. 将 `const site = require("../site");` 和 `const __app_version = require("../version");`
|
||||||
* 追加到 unpackage/dist/build/mp-weixin/common/vendor.js 文件内容开头部分,
|
* 追加到 unpackage/dist/build/mp-weixin/common/vendor.js 文件内容开头部分,
|
||||||
* 如果文件开头已有则跳过
|
* 如果文件开头已有则跳过
|
||||||
*
|
*
|
||||||
@@ -71,34 +71,39 @@ async function commonPatch(mode = 'production') {
|
|||||||
await fsp.copyFile(srcSitePath, destSitePath);
|
await fsp.copyFile(srcSitePath, destSitePath);
|
||||||
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
||||||
|
|
||||||
// 3) 生成 version.json(包含 Git 提交 ID 和构建时间)
|
// 3) 生成 version.js(包含 Git 提交 ID 和构建时间,CommonJS 格式供 require 使用)
|
||||||
let commitId = 'unknown';
|
let commitId = 'unknown';
|
||||||
try {
|
try {
|
||||||
commitId = execSync('git rev-parse --short HEAD', { cwd }).toString().trim();
|
commitId = execSync('git rev-parse --short HEAD', { cwd }).toString().trim();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('无法获取 Git 提交 ID,使用 unknown 代替');
|
console.warn('无法获取 Git 提交 ID,使用 unknown 代替');
|
||||||
}
|
}
|
||||||
const versionJson = {
|
const versionData = {
|
||||||
commitId,
|
commitId,
|
||||||
buildTime: new Date().toISOString()
|
buildTime: new Date().toISOString()
|
||||||
};
|
};
|
||||||
const versionPath = path.join(destDir, 'version.json');
|
// 同时生成 version.js(供 require)和 version.json(供文件系统读取)
|
||||||
await fsp.writeFile(versionPath, JSON.stringify(versionJson, null, 2));
|
const versionJsPath = path.join(destDir, 'version.js');
|
||||||
console.log(`已生成: ${versionPath} (commitId: ${commitId})`);
|
await fsp.writeFile(versionJsPath, `module.exports = ${JSON.stringify(versionData, null, 2)};\n`);
|
||||||
|
console.log(`已生成: ${versionJsPath} (commitId: ${commitId})`);
|
||||||
|
|
||||||
|
const versionJsonPath = path.join(destDir, 'version.json');
|
||||||
|
await fsp.writeFile(versionJsonPath, JSON.stringify(versionData, null, 2));
|
||||||
|
console.log(`已生成: ${versionJsonPath} (commitId: ${commitId})`);
|
||||||
|
|
||||||
// 4) 处理 vendor.js:在开头追加 require 语句(如果还没有的话)
|
// 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}`);
|
||||||
console.warn(' → 请先执行 HBuilder 构建(uniapp 构建),产生 vendor.js 后再运行此脚本。');
|
console.warn(' → 请先执行 HBuilder 构建(uniapp 构建),产生 vendor.js 后再运行此脚本。');
|
||||||
console.warn(' → version.json 已生成,可在首页连击 5 次标题查看版本信息。');
|
console.warn(' → version.json/version.js 已生成,可在首页连击 5 次标题查看版本信息。');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");';
|
||||||
const versionLine = 'const __app_version = require("../version.json");';
|
const versionLine = 'const __app_version = require("../version");';
|
||||||
// 检查文件开头(例如前 1000 字符)是否已经包含这些 require 语句
|
// 检查文件开头(例如前 1000 字符)是否已经包含这些 require 语句
|
||||||
const inspectPrefix = vendorContent.slice(0, 1000);
|
const inspectPrefix = vendorContent.slice(0, 1000);
|
||||||
const hasSiteRequire = new RegExp(
|
const hasSiteRequire = new RegExp(
|
||||||
@@ -106,7 +111,7 @@ async function commonPatch(mode = 'production') {
|
|||||||
'm'
|
'm'
|
||||||
).test(inspectPrefix);
|
).test(inspectPrefix);
|
||||||
const hasVersionRequire = new RegExp(
|
const hasVersionRequire = new RegExp(
|
||||||
'^\\s*const\\s+__app_version\\s*=\\s*require\\s*\\([\'"]\\.\\./version\\.json[\'"]\\);?',
|
'^\\s*const\\s+__app_version\\s*=\\s*require\\s*\\([\'"]\\.\\./version[\'"]\\);?',
|
||||||
'm'
|
'm'
|
||||||
).test(inspectPrefix);
|
).test(inspectPrefix);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user