Skip to content

Commit

Permalink
fixup! refactor(compiler): drop regular imports when symbols can be d…
Browse files Browse the repository at this point in the history
…efer-loaded
  • Loading branch information
AndrewKushnir committed Aug 3, 2023
1 parent 7239334 commit d6a0ad6
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 16 deletions.
16 changes: 14 additions & 2 deletions packages/compiler-cli/src/ngtsc/annotations/common/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ export type CompileFactoryFn = (metadata: R3FactoryMetadata) => CompileResult;

export function compileNgFactoryDefField(metadata: R3FactoryMetadata): CompileResult {
const res = compileFactoryFunction(metadata);
return {name: 'ɵfac', initializer: res.expression, statements: res.statements, type: res.type};
return {
name: 'ɵfac',
initializer: res.expression,
statements: res.statements,
type: res.type,
deferrableImports: null
};
}

export function compileDeclareFactory(metadata: R3FactoryMetadata): CompileResult {
const res = compileDeclareFactoryFunction(metadata);
return {name: 'ɵfac', initializer: res.expression, statements: res.statements, type: res.type};
return {
name: 'ɵfac',
initializer: res.expression,
statements: res.statements,
type: res.type,
deferrableImports: null
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function compileInputTransformFields(inputs: ClassPropertyMapping<InputMa
name: `ngAcceptInputType_${input.classPropertyName}`,
type: outputAst.transplantedType(input.transform.type),
statements: [],
initializer: null
initializer: null,
deferrableImports: null
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export function createSourceSpan(node: ts.Node): ParseSourceSpan {
export function compileResults(
fac: CompileResult, def: R3CompiledExpression, metadataStmt: Statement|null, propName: string,
additionalFields: CompileResult[]|null,
deferrableImports?: Set<ts.ImportDeclaration>): CompileResult[] {
deferrableImports: Set<ts.ImportDeclaration>|null): CompileResult[] {
const statements = def.statements;
if (metadataStmt !== null) {
statements.push(metadataStmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,8 @@ export class ComponentDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileDeclareClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵcmp', inputTransformFields);
return compileResults(
fac, def, classMetadata, 'ɵcmp', inputTransformFields, null /* deferrableImports */);
}

compileLocal(
Expand All @@ -1055,7 +1056,8 @@ export class ComponentDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵcmp', inputTransformFields);
return compileResults(
fac, def, classMetadata, 'ɵcmp', inputTransformFields, null /* deferrableImports */);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ export class DirectiveDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵdir', inputTransformFields);
return compileResults(
fac, def, classMetadata, 'ɵdir', inputTransformFields, null /* deferrableImports */);
}

compilePartial(
Expand All @@ -227,7 +228,8 @@ export class DirectiveDecoratorHandler implements
compileDeclareClassMetadata(analysis.classMetadata).toStmt() :
null;

return compileResults(fac, def, classMetadata, 'ɵdir', inputTransformFields);
return compileResults(
fac, def, classMetadata, 'ɵdir', inputTransformFields, null /* deferrableImports */);
}

compileLocal(
Expand All @@ -239,7 +241,8 @@ export class DirectiveDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵdir', inputTransformFields);
return compileResults(
fac, def, classMetadata, 'ɵdir', inputTransformFields, null /* deferrableImports */);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,14 @@ export class NgModuleDecoratorHandler implements
initializer: ngModuleDef.expression,
statements: ngModuleDef.statements,
type: ngModuleDef.type,
deferrableImports: null,
},
{
name: 'ɵinj',
initializer: injectorDef.expression,
statements: injectorDef.statements,
type: injectorDef.type,
deferrableImports: null,
},
];
return res;
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,13 @@ export class InjectableDecoratorHandler implements
if (ɵprov === undefined) {
// Only add a new ɵprov if there is not one already
const res = compileInjectableFn(analysis.meta);
results.push(
{name: 'ɵprov', initializer: res.expression, statements: res.statements, type: res.type});
results.push({
name: 'ɵprov',
initializer: res.expression,
statements: res.statements,
type: res.type,
deferrableImports: null
});
}

return results;
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class PipeDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵpipe', null);
return compileResults(fac, def, classMetadata, 'ɵpipe', null, null /* deferrableImports */);
}

compilePartial(node: ClassDeclaration, analysis: Readonly<PipeHandlerData>): CompileResult[] {
Expand All @@ -194,7 +194,7 @@ export class PipeDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileDeclareClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵpipe', null);
return compileResults(fac, def, classMetadata, 'ɵpipe', null, null /* deferrableImports */);
}

compileLocal(node: ClassDeclaration, analysis: Readonly<PipeHandlerData>): CompileResult[] {
Expand All @@ -203,6 +203,6 @@ export class PipeDecoratorHandler implements
const classMetadata = analysis.classMetadata !== null ?
compileClassMetadata(analysis.classMetadata).toStmt() :
null;
return compileResults(fac, def, classMetadata, 'ɵpipe', null);
return compileResults(fac, def, classMetadata, 'ɵpipe', null, null /* deferrableImports */);
}
}
2 changes: 1 addition & 1 deletion packages/compiler-cli/src/ngtsc/transform/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export interface CompileResult {
initializer: Expression|null;
statements: Statement[];
type: Type;
deferrableImports?: Set<ts.ImportDeclaration>;
deferrableImports: Set<ts.ImportDeclaration>|null;
}

export interface ResolveResult<R> {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-cli/src/ngtsc/transform/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class IvyCompilationVisitor extends Visitor {
// so that we can pass it to the transform visitor that will drop
// corresponding regular import declarations.
for (const classResult of result) {
if (classResult.deferrableImports && classResult.deferrableImports.size > 0) {
if (classResult.deferrableImports !== null && classResult.deferrableImports.size > 0) {
classResult.deferrableImports.forEach(
importDecl => this.deferrableImports.add(importDecl));
}
Expand Down

0 comments on commit d6a0ad6

Please sign in to comment.