dep时将aspectService编译到features/index中

This commit is contained in:
pqcqaq 2024-11-25 23:28:52 +08:00
parent d80b59c6d9
commit b744286df7
2 changed files with 193 additions and 6 deletions

View File

@ -583,11 +583,27 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
if (dependencies.length > 0) {
const importStatements = [];
const fdNames = [];
const adNames = [];
dependencies.forEach((dep, idx) => {
const fdName = `${(0, string_1.firstLetterUpperCase)(briefNames[idx])}FeatureDict`;
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("FeatureDict"), factory.createIdentifier(fdName))])), factory.createStringLiteral(dep), undefined));
const adName = `${(0, string_1.firstLetterUpperCase)(briefNames[idx])}AspectDict`;
// 导入FeatureDict和AspectDict
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("FeatureDict"), factory.createIdentifier(fdName))])), factory.createStringLiteral(dep), undefined), factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("AspectDict"), factory.createIdentifier(adName))])), factory.createStringLiteral(`${dep}/es/aspects/AspectDict`), undefined));
fdNames.push(fdName);
adNames.push(adName);
});
// 导入自己的AspectDictimport { AspectDict } from '../aspects/AspectDict';
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("AspectDict"), factory.createIdentifier("ProjectAspectDict"))])), factory.createStringLiteral("../aspects/AspectDict"), undefined),
// import { createService } from 'oak-frontend-base/es/aspects/AspectService';
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, undefined, factory.createIdentifier("createService"))])), factory.createStringLiteral("oak-frontend-base/es/aspects/AspectService"), undefined));
// 创建一个这样的type: type MergeAspectDict = ProjectAspectDict & GenernalAspectDict<EntityDict>;
// 除了ProjectAspectDict还有其他的AspectDict需要<EntityDict>参数
const adTypeDeclaration = factory.createTypeAliasDeclaration(undefined, factory.createIdentifier("MergeAspectDict"), undefined, factory.createIntersectionTypeNode([
factory.createTypeReferenceNode(factory.createIdentifier("ProjectAspectDict"), undefined),
...adNames.map(ad => {
return factory.createTypeReferenceNode(factory.createIdentifier(ad), [factory.createTypeReferenceNode(factory.createIdentifier("EntityDict"), undefined)]);
})
]));
let i = 0;
while (true) {
const stmt = statements[i];
@ -599,7 +615,7 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
const stmt3 = statements[i - 1], stmt4 = statements[i];
(0, assert_1.default)(ts.isImportDeclaration(stmt3) && ts.isFunctionDeclaration(stmt4));
const { name, parameters } = stmt4;
(0, assert_1.default)(ts.isIdentifier(name) && name.text === 'create' && parameters.length === 1);
(0, assert_1.default)(name && ts.isIdentifier(name) && name.text === 'create' && parameters.length === 1);
const [param] = parameters;
const { name: paramName, type } = param;
(0, assert_1.default)(ts.isIdentifier(paramName) && paramName.text === 'features' && ts.isTypeReferenceNode(type));
@ -611,9 +627,43 @@ function outputFeatureIndex(dependencies, briefNames, sourceFile, printer, filen
]))
])
});
const functionBlock = stmt4.body;
(0, assert_1.default)(functionBlock && ts.isBlock(functionBlock));
let returnI = 0;
while (true) {
const stmt = functionBlock.statements[returnI];
if (ts.isReturnStatement(stmt)) {
break;
}
returnI++;
}
// 在return之前插入const aspect = createService<EntityDict, MergeAspectDict>(cache);
const aspectDeclaration = factory.createVariableStatement(undefined, factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier("aspect"), undefined, undefined, factory.createCallExpression(factory.createIdentifier("createService"), [
factory.createTypeReferenceNode(factory.createIdentifier("EntityDict"), undefined),
factory.createTypeReferenceNode(factory.createIdentifier("MergeAspectDict"), undefined)
], [factory.createIdentifier("cache")]))], ts.NodeFlags.Const));
const returnStmt = functionBlock.statements[returnI];
(0, assert_1.default)(returnStmt.expression && ts.isObjectLiteralExpression(returnStmt.expression));
// 在里面添加aspect
const { properties } = returnStmt.expression;
Object.assign(returnStmt.expression, {
properties: [
...properties,
factory.createShorthandPropertyAssignment(factory.createIdentifier("aspect"))
]
});
// 把aspectDeclaration 插入到return之前
const newFunctionStatements = [
...functionBlock.statements.slice(0, returnI),
aspectDeclaration,
...functionBlock.statements.slice(returnI)
];
const newBlock = factory.createBlock(newFunctionStatements);
Object.assign(functionBlock, newBlock);
statements2 = [
...statements.slice(0, i),
...importStatements,
adTypeDeclaration,
...statements.slice(i)
];
if (isModule) {

View File

@ -1084,10 +1084,13 @@ function outputFeatureIndex(
if (dependencies.length > 0) {
const importStatements: ts.Statement[] = [];
const fdNames: string[] = [];
const adNames: string[] = [];
dependencies.forEach(
(dep, idx) => {
const fdName = `${firstLetterUpperCase(briefNames[idx])}FeatureDict`;
const adName = `${firstLetterUpperCase(briefNames[idx])}AspectDict`;
// 导入FeatureDict和AspectDict
importStatements.push(
factory.createImportDeclaration(
undefined,
@ -1102,24 +1105,95 @@ function outputFeatureIndex(
),
factory.createStringLiteral(dep),
undefined
),
factory.createImportDeclaration(
undefined,
factory.createImportClause(
false,
undefined,
factory.createNamedImports([factory.createImportSpecifier(
false,
factory.createIdentifier("AspectDict"),
factory.createIdentifier(adName)
)])
),
factory.createStringLiteral(`${dep}/es/aspects/AspectDict`),
undefined
)
);
fdNames.push(fdName);
adNames.push(adName);
}
);
// 导入自己的AspectDictimport { AspectDict } from '../aspects/AspectDict';
importStatements.push(
factory.createImportDeclaration(
undefined,
factory.createImportClause(
false,
undefined,
factory.createNamedImports([factory.createImportSpecifier(
false,
factory.createIdentifier("AspectDict"),
factory.createIdentifier("ProjectAspectDict")
)])
),
factory.createStringLiteral("../aspects/AspectDict"),
undefined
),
// import { createService } from 'oak-frontend-base/es/aspects/AspectService';
factory.createImportDeclaration(
undefined,
factory.createImportClause(
false,
undefined,
factory.createNamedImports([factory.createImportSpecifier(
false,
undefined,
factory.createIdentifier("createService"),
)])
),
factory.createStringLiteral("oak-frontend-base/es/aspects/AspectService"),
undefined
)
);
// 创建一个这样的type: type MergeAspectDict = ProjectAspectDict & GenernalAspectDict<EntityDict>;
// 除了ProjectAspectDict还有其他的AspectDict需要<EntityDict>参数
const adTypeDeclaration = factory.createTypeAliasDeclaration(
undefined,
factory.createIdentifier("MergeAspectDict"),
undefined,
factory.createIntersectionTypeNode([
factory.createTypeReferenceNode(
factory.createIdentifier("ProjectAspectDict"),
undefined
),
...adNames.map(ad => {
return factory.createTypeReferenceNode(
factory.createIdentifier(ad),
[factory.createTypeReferenceNode(
factory.createIdentifier("EntityDict"),
undefined
)]
);
})
])
)
let i = 0;
while (true) {
const stmt = statements[i];
if (ts.isFunctionDeclaration(stmt)) {
break;
}
i ++;
i++;
}
const stmt3 = statements[i-1], stmt4 = statements[i];
const stmt3 = statements[i - 1], stmt4 = statements[i];
assert(ts.isImportDeclaration(stmt3) && ts.isFunctionDeclaration(stmt4));
const { name, parameters } = stmt4;
assert(ts.isIdentifier(name!) && name.text === 'create' && parameters.length === 1);
assert(name && ts.isIdentifier(name) && name.text === 'create' && parameters.length === 1);
const [param] = parameters;
const { name: paramName, type } = param;
assert(ts.isIdentifier(paramName) && paramName.text === 'features' && ts.isTypeReferenceNode(type!));
@ -1134,9 +1208,72 @@ function outputFeatureIndex(
])
});
const functionBlock: ts.Block | undefined = stmt4.body;
assert(functionBlock && ts.isBlock(functionBlock));
let returnI = 0;
while (true) {
const stmt = functionBlock.statements[returnI];
if (ts.isReturnStatement(stmt)) {
break;
}
returnI++;
}
// 在return之前插入const aspect = createService<EntityDict, MergeAspectDict>(cache);
const aspectDeclaration = factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[factory.createVariableDeclaration(
factory.createIdentifier("aspect"),
undefined,
undefined,
factory.createCallExpression(
factory.createIdentifier("createService"),
[
factory.createTypeReferenceNode(
factory.createIdentifier("EntityDict"),
undefined
),
factory.createTypeReferenceNode(
factory.createIdentifier("MergeAspectDict"),
undefined
)
],
[factory.createIdentifier("cache")]
)
)],
ts.NodeFlags.Const
)
)
const returnStmt = functionBlock.statements[returnI] as ts.ReturnStatement;
assert(returnStmt.expression && ts.isObjectLiteralExpression(returnStmt.expression));
// 在里面添加aspect
const { properties } = returnStmt.expression;
Object.assign(returnStmt.expression, {
properties: [
...properties,
factory.createShorthandPropertyAssignment(
factory.createIdentifier("aspect"),
)
]
});
// 把aspectDeclaration 插入到return之前
const newFunctionStatements = [
...functionBlock.statements.slice(0, returnI),
aspectDeclaration,
...functionBlock.statements.slice(returnI)
];
const newBlock = factory.createBlock(newFunctionStatements);
Object.assign(functionBlock, newBlock);
statements2 = [
...statements.slice(0, i),
...importStatements,
adTypeDeclaration,
...statements.slice(i)
];
if (isModule) {
@ -1322,7 +1459,7 @@ function outputIntializeFeatures(
)
)
)
});
});
}
const result = printer.printList(