fix(ivy): force new imports for .d.ts files (#25080)
When ngtsc encounters a reference to a type (for example, a Component type listed in an NgModule declarations array), it traces the import of that type and attempts to determine the best way to refer to it. In the event the type is defined in the same file where a reference is being generated, the identifier of the type is used. If the type was imported, ngtsc has a choice. It can use the identifier from the original import, or it can write a new import to the module where the type came from. ngtsc has a bug currently when it elects to rely on the user's import. When writing a .d.ts file, the user's import may have been elided as the type was not referred to from the type side of the program. Thus, in .d.ts files ngtsc must always assume the import may not exist, and generate a new one. In .js output the import is guaranteed to still exist, so it's preferable for ngtsc to continue using the existing import if one is available. This commit changes how @angular/compiler writes type definitions, and allows it to use a different expression to write a type definition than is used to write the value. This allows ngtsc to specify that types in type definitions should always be imported. A corresponding change to the staticallyResolve() Reference system allows the choice of which type of import to use when generating an Expression from a Reference. PR Close #25080
This commit is contained in:

committed by
Igor Minar

parent
f902b5ec59
commit
ed7aa1c3e5
@ -86,5 +86,6 @@ export {R3DependencyMetadata, R3FactoryMetadata, R3ResolvedDependencyType} from
|
||||
export {compileInjector, compileNgModule, R3InjectorMetadata, R3NgModuleMetadata} from './render3/r3_module_compiler';
|
||||
export {compilePipeFromMetadata, R3PipeMetadata} from './render3/r3_pipe_compiler';
|
||||
export {makeBindingParser, parseTemplate} from './render3/view/template';
|
||||
export {R3Reference} from './render3/util';
|
||||
export {compileComponentFromMetadata, compileDirectiveFromMetadata, parseHostBindings} from './render3/view/compiler';
|
||||
// This file only reexports content of the `src` folder. Keep it that way.
|
@ -15,7 +15,7 @@ import {OutputContext} from '../util';
|
||||
|
||||
import {R3DependencyMetadata, compileFactoryFunction} from './r3_factory';
|
||||
import {Identifiers as R3} from './r3_identifiers';
|
||||
import {convertMetaToOutput, mapToMapExpression} from './util';
|
||||
import {R3Reference, convertMetaToOutput, mapToMapExpression} from './util';
|
||||
|
||||
export interface R3NgModuleDef {
|
||||
expression: o.Expression;
|
||||
@ -40,17 +40,17 @@ export interface R3NgModuleMetadata {
|
||||
/**
|
||||
* An array of expressions representing the directives and pipes declared by the module.
|
||||
*/
|
||||
declarations: o.Expression[];
|
||||
declarations: R3Reference[];
|
||||
|
||||
/**
|
||||
* An array of expressions representing the imports of the module.
|
||||
*/
|
||||
imports: o.Expression[];
|
||||
imports: R3Reference[];
|
||||
|
||||
/**
|
||||
* An array of expressions representing the exports of the module.
|
||||
*/
|
||||
exports: o.Expression[];
|
||||
exports: R3Reference[];
|
||||
|
||||
/**
|
||||
* Whether to emit the selector scope values (declarations, imports, exports) inline into the
|
||||
@ -68,9 +68,9 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
|
||||
const expression = o.importExpr(R3.defineNgModule).callFn([mapToMapExpression({
|
||||
type: moduleType,
|
||||
bootstrap: o.literalArr(bootstrap),
|
||||
declarations: o.literalArr(declarations),
|
||||
imports: o.literalArr(imports),
|
||||
exports: o.literalArr(exports),
|
||||
declarations: o.literalArr(declarations.map(ref => ref.value)),
|
||||
imports: o.literalArr(imports.map(ref => ref.value)),
|
||||
exports: o.literalArr(exports.map(ref => ref.value)),
|
||||
})]);
|
||||
|
||||
const type = new o.ExpressionType(o.importExpr(R3.NgModuleDef, [
|
||||
@ -148,7 +148,7 @@ function accessExportScope(module: o.Expression): o.Expression {
|
||||
return new o.ReadPropExpr(selectorScope, 'exported');
|
||||
}
|
||||
|
||||
function tupleTypeOf(exp: o.Expression[]): o.Type {
|
||||
const types = exp.map(type => o.typeofExpr(type));
|
||||
function tupleTypeOf(exp: R3Reference[]): o.Type {
|
||||
const types = exp.map(ref => o.typeofExpr(ref.type));
|
||||
return exp.length > 0 ? o.expressionType(o.literalArr(types)) : o.NONE_TYPE;
|
||||
}
|
@ -47,3 +47,8 @@ export function typeWithParameters(type: o.Expression, numParams: number): o.Exp
|
||||
}
|
||||
return o.expressionType(type, null, params);
|
||||
}
|
||||
|
||||
export interface R3Reference {
|
||||
value: o.Expression;
|
||||
type: o.Expression;
|
||||
}
|
||||
|
Reference in New Issue
Block a user