You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit aafac72
Browse filesBrowse the repository at this point in the historyBrowse files
fix(core): verify standalone component imports in JiT (#45777)
This commits adds verifications assuring that items imported into
standalone components are one of:
- standalone component / directive / pipe;
- NgModule;
- forwardRef resolving to one of the above.
It explicitly disallows modules with providers.
PR Close#45777
getDependencyTypeForError(depType)}, imported from "${
214
+
stringifyForError(
215
+
importingType)}", is not standalone. Did you forget to add the standalone: true flag?`);
216
+
}
217
+
}else{
218
+
// it can be either a module with provider or an unknown (not annotated) type
219
+
if(isModuleWithProviders(depType)){
220
+
thrownewError(`A module with providers was imported from "${
221
+
stringifyForError(
222
+
importingType)}". Modules with providers are not supported in standalone components imports.`);
223
+
}else{
224
+
thrownewError(`The "${stringifyForError(depType)}" type, imported from "${
225
+
stringifyForError(
226
+
importingType)}", must be a standalone component / directive / pipe or an NgModule. Did you forget to add the required @Component / @Directive / @Pipe or @NgModule annotation?`);
227
+
}
228
+
}
229
+
}
230
+
}
231
+
189
232
/**
190
233
* Build memoized `directiveDefs` and `pipeDefs` functions for the component definition of a
191
234
* standalone component, which process `imports` and filter out directives and pipes. The use of
@@ -205,8 +248,9 @@ function getStandaloneDefFunctions(type: Type<any>, imports: Type<any>[]): {
it('should error when forwardRef does not resolve to a truthy value',()=>{
431
+
@Component({
432
+
selector: 'test',
433
+
standalone: true,
434
+
imports: [forwardRef(()=>null)],
435
+
template: '',
436
+
})
437
+
classTestComponent{
438
+
}
439
+
expect(()=>{
440
+
TestBed.createComponent(TestComponent);
441
+
})
442
+
.toThrowError(
443
+
'Expected forwardRef function, imported from "TestComponent", to return a standalone entity or NgModule but got "null".');
444
+
});
445
+
446
+
it('should error when a non-standalone component is imported',()=>{
447
+
@Component({
448
+
selector: 'not-a-standalone',
449
+
template: '',
450
+
})
451
+
classNonStandaloneCmp{
452
+
}
453
+
454
+
@Component({
455
+
selector: 'standalone',
456
+
standalone: true,
457
+
template: '',
458
+
imports: [NonStandaloneCmp],
459
+
})
460
+
classStandaloneCmp{
461
+
}
462
+
463
+
expect(()=>{
464
+
TestBed.createComponent(StandaloneCmp);
465
+
})
466
+
.toThrowError(
467
+
'The "NonStandaloneCmp" component, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
468
+
});
469
+
470
+
it('should error when a non-standalone directive is imported',()=>{
471
+
@Directive({selector: '[not-a-standalone]'})
472
+
classNonStandaloneDirective{
473
+
}
474
+
475
+
@Component({
476
+
selector: 'standalone',
477
+
standalone: true,
478
+
template: '',
479
+
imports: [NonStandaloneDirective],
480
+
})
481
+
classStandaloneCmp{
482
+
}
483
+
484
+
expect(()=>{
485
+
TestBed.createComponent(StandaloneCmp);
486
+
})
487
+
.toThrowError(
488
+
'The "NonStandaloneDirective" directive, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
489
+
});
490
+
491
+
it('should error when a non-standalone pipe is imported',()=>{
492
+
@Pipe({name: 'not-a-standalone'})
493
+
classNonStandalonePipe{
494
+
}
495
+
496
+
@Component({
497
+
selector: 'standalone',
498
+
standalone: true,
499
+
template: '',
500
+
imports: [NonStandalonePipe],
501
+
})
502
+
classStandaloneCmp{
503
+
}
504
+
505
+
expect(()=>{
506
+
TestBed.createComponent(StandaloneCmp);
507
+
})
508
+
.toThrowError(
509
+
'The "NonStandalonePipe" pipe, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
510
+
});
511
+
512
+
it('should error when an unknown type is imported',()=>{
513
+
classSthElse{}
514
+
515
+
@Component({
516
+
selector: 'standalone',
517
+
standalone: true,
518
+
template: '',
519
+
imports: [SthElse],
520
+
})
521
+
classStandaloneCmp{
522
+
}
523
+
524
+
expect(()=>{
525
+
TestBed.createComponent(StandaloneCmp);
526
+
})
527
+
.toThrowError(
528
+
'The "SthElse" type, imported from "StandaloneCmp", must be a standalone component / directive / pipe or an NgModule. Did you forget to add the required @Component / @Directive / @Pipe or @NgModule annotation?');
529
+
});
530
+
531
+
it('should error when a module with providers is imported',()=>{
532
+
@NgModule()
533
+
classOtherModule{
534
+
}
535
+
536
+
@NgModule()
537
+
classLibModule{
538
+
staticforComponent(){
539
+
return{ngModule: OtherModule};
540
+
}
541
+
}
542
+
543
+
@Component({
544
+
standalone: true,
545
+
template: '',
546
+
// we need to import a module with a provider in a nested array since module with providers
547
+
// are disallowed on the type level
548
+
imports: [[LibModule.forComponent()]],
549
+
})
550
+
classStandaloneCmp{
551
+
}
552
+
553
+
expect(()=>{
554
+
TestBed.createComponent(StandaloneCmp);
555
+
})
556
+
.toThrowError(
557
+
'A module with providers was imported from "StandaloneCmp". Modules with providers are not supported in standalone components imports.');
0 commit comments