Skip to content

Commit

Permalink
fix(compiler-cli): allow non-array imports for standalone component i…
Browse files Browse the repository at this point in the history
…n local compilation mode (#51819)

Currently the compiler in local mode assumes that the standalone component imports are array expressions. This is not always true as they can be const variables as well. This change allow non-array expressions for standalone component imports field and passes that expression to the downstream tools such as deps tracker to compute the component's deps in runtime.

PR Close #51819
  • Loading branch information
pmvald authored and pkozlowski-opensource committed Sep 20, 2023
1 parent 19c3dc1 commit 5b66330
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Expand Up @@ -468,10 +468,7 @@ export class ComponentDecoratorHandler implements
viewProviders: wrappedViewProviders,
i18nUseExternalIds: this.i18nUseExternalIds,
relativeContextFilePath,
rawImports: (rawImports !== null && ts.isArrayLiteralExpression(rawImports) &&
rawImports.elements.length > 0) ?
new WrappedNodeExpr(rawImports) :
undefined,
rawImports: rawImports !== null ? new WrappedNodeExpr(rawImports) : undefined,
},
typeCheckMeta: extractDirectiveTypeCheckMeta(node, inputs, this.reflector),
classMetadata: this.includeClassMetadata ?
Expand Down
30 changes: 28 additions & 2 deletions packages/compiler-cli/test/ngtsc/local_compilation_spec.ts
Expand Up @@ -276,7 +276,33 @@ runInEachFileSystem(() => {
'dependencies: i0.ɵɵgetComponentDepsFactory(MainComponent, [SomeThing, forwardRef(() => SomeThing2)])');
});

it('should not generate ɵɵgetComponentDepsFactory for standalone component with empty imports',
it('should generate ɵɵgetComponentDepsFactory with raw non-array imports as second param for component def dependencies - for standalone component with non-empty imports',
() => {
env.write('test.ts', `
import {Component, forwardRef} from '@angular/core';
import {SomeThing} from 'some-where';
import {SomeThing2} from 'some-where2';
const NG_IMPORTS = [SomeThing, forwardRef(()=>SomeThing2)];
@Component({
standalone: true,
imports: NG_IMPORTS,
selector: 'test-main',
template: '<span>Hello world!</span>',
})
export class MainComponent {
}
`);

env.driveMain();
const jsContents = env.getContents('test.js');

expect(jsContents)
.toContain('dependencies: i0.ɵɵgetComponentDepsFactory(MainComponent, NG_IMPORTS)');
});

it('should generate ɵɵgetComponentDepsFactory with empty array as secon d arg for standalone component with empty imports',
() => {
env.write('test.ts', `
import {Component} from '@angular/core';
Expand All @@ -295,7 +321,7 @@ runInEachFileSystem(() => {
const jsContents = env.getContents('test.js');

expect(jsContents)
.toContain('dependencies: i0.ɵɵgetComponentDepsFactory(MainComponent)');
.toContain('dependencies: i0.ɵɵgetComponentDepsFactory(MainComponent, [])');
});

it('should not generate ɵɵgetComponentDepsFactory for standalone component with no imports',
Expand Down

0 comments on commit 5b66330

Please sign in to comment.