fix(build): 解决 lodash.template 中 assignWith 函数缺失问题

- 添加 JavaScript 脚本修复 lodash.template 模块中的 assignWith 问题
- 提供 Shell 脚本支持 Linux/Mac 系统的自动修复功能
- 实现 assignWith 函数的简单 polyfill 版本以确保兼容性
- 添加补丁检测机制防止重复修补同一文件
- 在构建前自动运行修复脚本确保依赖完整性
This commit is contained in:
2026-04-01 10:03:38 +08:00
parent 4d13acacc2
commit df78ff29bd
3 changed files with 181 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env node
/**
* Fix lodash.template assignWith issue - Manual fix script
* Run this after npm/yarn install and before build
*
* Usage: node fix-lodash-manual.js
*/
const fs = require('fs');
const path = require('path');
const lodashTemplatePath = path.join(__dirname, 'node_modules', 'lodash.template', 'index.js');
if (!fs.existsSync(lodashTemplatePath)) {
console.error('❌ lodash.template not found at:', lodashTemplatePath);
console.error('Please run npm install or yarn install first');
process.exit(1);
}
let content = fs.readFileSync(lodashTemplatePath, 'utf8');
// Check if already patched
if (content.includes('/* LODASH_TEMPLATE_PATCHED */')) {
console.log('✓ lodash.template already patched');
process.exit(0);
}
// Check if assignWith is actually missing
if (content.includes('assignWith')) {
// Check if it's used but not defined
const hasFunctionDefinition = /function\s+assignWith|var\s+assignWith\s*=/.test(content);
if (hasFunctionDefinition) {
console.log('✓ assignWith is already defined in lodash.template');
process.exit(0);
}
}
console.log('🔧 Patching lodash.template...');
// Simple assignWith implementation
const assignWithImpl = `/* LODASH_TEMPLATE_PATCHED */
// assignWith polyfill for lodash.template
function assignWith(object, source, customizer) {
if (object == null) return object;
var props = Object.keys(Object(source));
for (var i = 0; i < props.length; i++) {
var key = props[i];
var value = source[key];
var assignedValue = customizer ? customizer(object[key], value, key, object, source) : value;
if (assignedValue !== undefined) {
object[key] = assignedValue;
}
}
return object;
}
`;
// Find first line break after comments
let insertPos = 0;
const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim() === ' */' || lines[i].trim() === '*/') {
insertPos = content.indexOf('\n', content.indexOf(lines[i])) + 1;
break;
}
}
if (insertPos === 0) {
insertPos = content.indexOf('\n') + 1;
}
const before = content.substring(0, insertPos);
const after = content.substring(insertPos);
content = before + '\n' + assignWithImpl + after;
fs.writeFileSync(lodashTemplatePath, content);
console.log('✅ Successfully patched lodash.template with assignWith function');
console.log(' File:', lodashTemplatePath);