2026-05-21 09:08:59 +08:00

31 lines
1.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.

/**
* Monorepo 路径修复
*
* Cool Admin 的 LocationUtil.getRunPath() 通过 Error stack trace 中的
* node_modules 位置反推项目根目录。在 pnpm monorepo 中node_modules
* 被提升到根目录,导致解析到 GPU_GUARD_MONOREPO/dist 而非
* GPU_GUARD_MONOREPO/packages/backend/dist(或src),使得模块配置无法加载。
*
* 本脚本通过 --require 或 NODE_OPTIONS 在所有模块加载前修补 getRunPath。
*/
const path = require('path');
// 在开发模式(ts-node)下,模块从 src 目录加载
// 在生产模式下,模块从 dist 目录加载
const isDev = process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'development';
const backendRoot = isDev
? path.resolve(__dirname, 'src')
: path.resolve(__dirname, 'dist');
// 直接加载并修补 LocationUtil 单例
try {
const locationModule = require('@cool-midway/core/dist/util/location');
if (locationModule && locationModule.default) {
locationModule.default.getRunPath = () => backendRoot;
locationModule.default.distPath = backendRoot;
console.log('[monorepo-fix] Cool Admin getRunPath 已修补为:', backendRoot);
}
} catch (e) {
console.warn('[monorepo-fix] 修补失败:', e.message);
}