Skip to content

Commit

Permalink
refactor(compiler): add a factory for component dependencies in local…
Browse files Browse the repository at this point in the history
… compilation mode
  • Loading branch information
pmvald committed Jul 17, 2023
1 parent e7b6395 commit 3bf5afb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,15 @@ export class ComponentDecoratorHandler implements
if (analysis.template.errors !== null && analysis.template.errors.length > 0) {
return [];
}

const meta: R3ComponentMetadata<R3TemplateDependency> = {
...analysis.meta,
// No resolution is done in local compilation mode, therefore we pass an empty array here.
declarationListEmitMode: DeclarationListEmitMode.Direct,
declarations: [],
declarationListEmitMode: (!analysis.meta.isStandalone || analysis.rawImports !== null) ?
DeclarationListEmitMode.RuntimeResolved :
DeclarationListEmitMode.Direct,
declarations: EMPTY_ARRAY,
};

const fac = compileNgFactoryDefField(toFactoryMetadata(meta, FactoryTarget.Component));
const def = compileComponentFromMetadata(meta, pool, makeBindingParser());
const inputTransformFields = compileInputTransformFields(analysis.inputs);
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler/src/render3/partial/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ function compileUsedDependenciesMetadata(meta: R3ComponentMetadata<R3TemplateDep
generateForwardRef :
(expr: o.Expression) => expr;

if (meta.declarationListEmitMode === DeclarationListEmitMode.RuntimeResolved) {
throw new Error(`Unsupported emit mode`);
}

return toOptionalLiteralArray(meta.declarations, decl => {
switch (decl.kind) {
case R3TemplateDependencyKind.Directive:
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler/src/render3/r3_identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ export class Identifiers {
static resolveDocument: o.ExternalReference = {name: 'ɵɵresolveDocument', moduleName: CORE};
static resolveBody: o.ExternalReference = {name: 'ɵɵresolveBody', moduleName: CORE};

static getComponentDepsFactory:
o.ExternalReference = {name: 'ɵɵgetComponentDepsFactory', moduleName: CORE};

static defineComponent: o.ExternalReference = {name: 'ɵɵdefineComponent', moduleName: CORE};
static declareComponent: o.ExternalReference = {name: 'ɵɵngDeclareComponent', moduleName: CORE};

Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/render3/view/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export const enum DeclarationListEmitMode {
* ```
*/
ClosureResolved,

RuntimeResolved,
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler/src/render3/view/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,15 @@ export function compileComponentFromMetadata(
definitionMap.set('template', templateFn);
}

if (meta.declarations.length > 0) {
if (meta.declarationListEmitMode !== DeclarationListEmitMode.RuntimeResolved &&
meta.declarations.length > 0) {
definitionMap.set(
'dependencies',
compileDeclarationList(
o.literalArr(meta.declarations.map(decl => decl.type)), meta.declarationListEmitMode));
} else if (meta.declarationListEmitMode === DeclarationListEmitMode.RuntimeResolved) {
definitionMap.set(
'dependencies', o.importExpr(R3.getComponentDepsFactory).callFn([meta.type.value]));
}

if (meta.encapsulation === null) {
Expand Down Expand Up @@ -338,6 +342,8 @@ function compileDeclarationList(
// directives: function () { return [MyDir].map(ng.resolveForwardRef); }
const resolvedList = list.prop('map').callFn([o.importExpr(R3.resolveForwardRef)]);
return o.fn([], [new o.ReturnStatement(resolvedList)]);
case DeclarationListEmitMode.RuntimeResolved:
throw new Error(`Unsupported with an array of pre-resolved dependencies`);
}
}

Expand Down

0 comments on commit 3bf5afb

Please sign in to comment.