fix: 修复在node_modules下的引用处理时出现的部分问题

This commit is contained in:
Pan Qiancheng 2025-11-07 15:23:55 +08:00
parent 32675a9080
commit 2114f959e6
3 changed files with 36 additions and 16 deletions

View File

@ -13,7 +13,7 @@ declare const Schema: Record<string, {
inModi: boolean;
relations: false | string[];
extendsFrom: string[];
importAttrFrom: Record<string, [string, string | undefined, string]>;
importAttrFrom: Record<string, [string, string | undefined, string, string]>;
}>;
export declare function constructAttributes(entity: string): ts.PropertyAssignment[];
export declare function translateLocaleObject(locale: ts.ObjectLiteralExpression): Record<string, any>;

View File

@ -238,11 +238,16 @@ function addImportedFrom(moduleName, name, node) {
* @param projectRoot 项目根目录默认为当前目录
* @returns 新的相对导入路径
*/
function resolveCompiledImportPath(sourceFilePath, outputFilePath, importPath, projectRoot = '.') {
function resolveCompiledImportPath(sourceFilePath, outputFilePath, importPath, projectRoot = '.', relative) {
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
if (!importPath.startsWith('.')) {
return importPath;
}
if (sourceFilePath.startsWith("node_modules")) {
return importPath.startsWith('.') ? (relative
? path_1.default.join(relative, importPath).replace(/\\/g, '/')
: path_1.default.join('..', importPath).replace(/\\/g, '/')) : importPath;
}
// 1. 获取源文件所在目录
const sourceDir = path_1.default.dirname(sourceFilePath);
// 2. 解析原始导入路径,得到目标文件的绝对路径(相对于项目根目录)
@ -294,7 +299,7 @@ function analyzeExternalAttrImport(node, program, importAttrFrom, relativePath)
const sourceFilePath = declaration.getSourceFile().fileName;
(0, lodash_1.assign)(importAttrFrom, {
// [name]: [importFromRelatively, propertyName],
[name]: [importFrom, propertyName, sourceFilePath],
[name]: [importFrom, propertyName, sourceFilePath, relativePath],
});
}
else if (ts.isTypeAliasDeclaration(declaration)) {
@ -3816,12 +3821,12 @@ function _outputBaseSchema(outputDir, printer) {
// else {
// fromExternalImportAttrs[from] = [[attr, propertyName]];
// }
const [from, propertyName, sourceFilePath] = importAttrFrom[attr];
const [from, propertyName, sourceFilePath, relative] = importAttrFrom[attr];
// ===== 在这里进行路径转换 =====
const outputFilePath = path_1.default.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
// 使用路径解析算法计算新的导入路径
const newImportPath = resolveCompiledImportPath(path_1.default.relative(process.cwd(), sourceFilePath), path_1.default.relative(process.cwd(), outputFilePath), from, // 原始导入路径
".");
".", relative);
// console.log('resolve import path:', {
// sourceFilePath: PathLib.relative(process.cwd(), sourceFilePath),
// outputFilePath: PathLib.relative(process.cwd(), outputFilePath),

View File

@ -34,7 +34,7 @@ const Schema: Record<string, {
inModi: boolean;
relations: false | string[];
extendsFrom: string[];
importAttrFrom: Record<string, [string, string | undefined, string]>;
importAttrFrom: Record<string, [string, string | undefined, string, string]>;
}> = {};
const OneToMany: Record<string, Array<[string, string, boolean]>> = {};
const ManyToOne: Record<string, Array<[string, string, boolean]>> = {};
@ -361,12 +361,26 @@ function resolveCompiledImportPath(
sourceFilePath: string,
outputFilePath: string,
importPath: string,
projectRoot: string = '.'
projectRoot: string = '.',
relative?: string,
): string {
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
if (!importPath.startsWith('.')) {
return importPath;
}
if (!importPath.startsWith('.')) {
return importPath;
}
if (sourceFilePath.startsWith("node_modules")) {
// 如果是node_modules下的文件保持原有的处理逻辑
return importPath.startsWith('.') ? (relative
? PathLib.join(
relative,
importPath
).replace(/\\/g, '/')
: PathLib.join(
'..',
importPath
).replace(/\\/g, '/')) : importPath;
}
// 1. 获取源文件所在目录
const sourceDir = PathLib.dirname(sourceFilePath);
@ -396,7 +410,7 @@ function resolveCompiledImportPath(
function analyzeExternalAttrImport(
node: ts.TypeReferenceNode,
program: ts.Program,
importAttrFrom: Record<string, [string, string | undefined, string]>,
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
relativePath?: string,
) {
const checker = program.getTypeChecker();
@ -436,7 +450,7 @@ function analyzeExternalAttrImport(
assign(importAttrFrom, {
// [name]: [importFromRelatively, propertyName],
[name]: [importFrom, propertyName, sourceFilePath],
[name]: [importFrom, propertyName, sourceFilePath, relativePath],
});
}
else if (ts.isTypeAliasDeclaration(declaration)) {
@ -961,7 +975,7 @@ function analyzeSchemaDefinition(
referencedSchemas: string[],
schemaAttrs: ts.TypeElement[],
enumAttributes: Record<string, string[]>,
importAttrFrom: Record<string, [string, string | undefined, string]>,
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
relativePath?: string
) {
let hasEntityAttr = false;
@ -1191,7 +1205,7 @@ function analyzeReferenceSchemaFile(
referencedSchemas: string[],
schemaAttrs: ts.TypeElement[],
enumAttributes: Record<string, string[]>,
importAttrFrom: Record<string, [string, string | undefined, string]>,
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
relativePath: string) {
let result: ReturnType<typeof analyzeSchemaDefinition> | undefined;
ts.forEachChild(sourceFile!, (node) => {
@ -1259,7 +1273,7 @@ function analyzeEntity(filename: string, path: string, program: ts.Program, rela
let actionType = 'crud';
let _static = false;
const enumAttributes: Record<string, string[]> = {};
const importAttrFrom: Record<string, [string, string | undefined, string]> = {};
const importAttrFrom: Record<string, [string, string | undefined, string, string]> = {};
let localeDef: ts.ObjectLiteralExpression | undefined = undefined;
let extendsFrom: string[] | undefined;
// let relationHierarchy: ts.ObjectLiteralExpression | undefined = undefined;
@ -6987,7 +7001,7 @@ function _outputBaseSchema(outputDir: string, printer: ts.Printer) {
// else {
// fromExternalImportAttrs[from] = [[attr, propertyName]];
// }
const [from, propertyName, sourceFilePath] = importAttrFrom[attr];
const [from, propertyName, sourceFilePath, relative] = importAttrFrom[attr];
// ===== 在这里进行路径转换 =====
const outputFilePath = PathLib.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
@ -6998,6 +7012,7 @@ function _outputBaseSchema(outputDir: string, printer: ts.Printer) {
PathLib.relative(process.cwd(), outputFilePath),
from, // 原始导入路径
".",
relative
);
// console.log('resolve import path:', {