Skip to content

Commit

Permalink
feat(modules): split every directive into separate module
Browse files Browse the repository at this point in the history
And remove required static module methods.
This together allows greater tree-shaking abilities.
All changes to public API are done in deprecation manner - meaning nothing should be done to update.
However it is recommended to get rid of deprecated APIs to prepare for next major version.
  • Loading branch information
gund committed Mar 13, 2020
1 parent fb3a680 commit 5f2985b
Show file tree
Hide file tree
Showing 33 changed files with 286 additions and 143 deletions.
9 changes: 0 additions & 9 deletions projects/ng-dynamic-component/src/lib/component-injector.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InjectedComponent,
TestComponent as TestComponentBase,
TestModule,
} from '../test';
} from '../../test';
import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';

@Component({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { NgComponentOutlet } from '@angular/common';
import { ComponentRef, Directive, Host } from '@angular/core';

import { ComponentInjector } from './component-injector';
import {
DynamicComponentInjector,
DynamicComponentInjectorToken,
} from './token';

@Directive({
// tslint:disable-next-line: directive-selector
selector: '[ngComponentOutlet]',
exportAs: 'ndcComponentOutletInjector',
providers: [
{
provide: DynamicComponentInjectorToken,
useExisting: ComponentOutletInjectorDirective,
},
],
})
export class ComponentOutletInjectorDirective implements ComponentInjector {
export class ComponentOutletInjectorDirective
implements DynamicComponentInjector {
get componentRef(): ComponentRef<any> {
return (this.componentOutlet as any)._componentRef;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';

@NgModule({
imports: [CommonModule],
exports: [ComponentOutletInjectorDirective],
declarations: [ComponentOutletInjectorDirective],
})
export class ComponentOutletInjectorModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './token';
export * from './component-outlet-injector.directive';
export * from './component-outlet-injector.module';
20 changes: 20 additions & 0 deletions projects/ng-dynamic-component/src/lib/component-injector/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ComponentRef, InjectionToken } from '@angular/core';

export interface DynamicComponentInjector {
componentRef: ComponentRef<any> | null;
}

/**
* @deprecated Since v6.0.0 - Use {@link DynamicComponentInjector} instead
*/
export type ComponentInjector = DynamicComponentInjector;

export const DynamicComponentInjectorToken = new InjectionToken<
DynamicComponentInjector
>('DynamicComponentInjector');

/**
* @deprecated Since v6.0.0 - Use {@link DynamicComponentInjectorToken} instead
* and provide component class via `useExisting` instead of `useValue`
*/
export const COMPONENT_INJECTOR = DynamicComponentInjectorToken;
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { By } from '@angular/platform-browser';

import {
AnotherInjectedComponent,
getByPredicate,
InjectedComponent,
TestComponent as TestComponentBase,
TestModule,
getByPredicate,
} from '../test';
import { COMPONENT_INJECTOR } from './component-injector';
import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';
} from '../../test';
import {
ComponentOutletInjectorDirective,
DynamicComponentInjectorToken,
} from '../component-injector';
import { DynamicComponent } from '../dynamic.component';
import {
AttributesMap,
DynamicAttributesDirective,
} from './dynamic-attributes.directive';
import { DynamicComponent } from './dynamic.component';

const getInjectedComponentFrom = getByPredicate<InjectedComponent>(
By.directive(InjectedComponent),
Expand Down Expand Up @@ -51,7 +53,10 @@ describe('DynamicAttributesDirective', () => {
ComponentOutletInjectorDirective,
],
providers: [
{ provide: COMPONENT_INJECTOR, useValue: DynamicComponent },
{
provide: DynamicComponentInjectorToken,
useExisting: DynamicComponent,
},
],
}).compileComponents();

Expand Down Expand Up @@ -241,7 +246,10 @@ describe('DynamicAttributesDirective', () => {
ComponentOutletInjectorDirective,
],
providers: [
{ provide: COMPONENT_INJECTOR, useValue: DynamicComponent },
{
provide: DynamicComponentInjectorToken,
useExisting: DynamicComponent,
},
],
}).compileComponents();

Expand Down Expand Up @@ -292,7 +300,10 @@ describe('DynamicAttributesDirective', () => {
TestComponent,
],
providers: [
{ provide: COMPONENT_INJECTOR, useValue: DynamicComponent },
{
provide: DynamicComponentInjectorToken,
useExisting: DynamicComponent,
},
],
}).compileComponents();

Expand Down Expand Up @@ -336,7 +347,10 @@ describe('DynamicAttributesDirective', () => {
ComponentOutletInjectorDirective,
],
providers: [
{ provide: COMPONENT_INJECTOR, useValue: DynamicComponent },
{
provide: DynamicComponentInjectorToken,
useExisting: DynamicComponent,
},
],
}).compileComponents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import {
Type,
} from '@angular/core';

import { COMPONENT_INJECTOR, ComponentInjector } from './component-injector';
import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';
import {
ComponentOutletInjectorDirective,
DynamicComponentInjector,
DynamicComponentInjectorToken,
} from '../component-injector';

export interface AttributesMap {
[key: string]: string;
Expand All @@ -35,10 +38,6 @@ export class DynamicAttributesDirective implements DoCheck {
ngComponentOutletNdcDynamicAttributes: AttributesMap;

private attrsDiffer = this.differs.find({}).create<string, string>();
private componentInjector = this.injector.get(
this.componentInjectorType,
null,
);
private lastCompType: Type<any>;
private lastAttrActions: AttributeActions;

Expand Down Expand Up @@ -78,11 +77,12 @@ export class DynamicAttributesDirective implements DoCheck {
private renderer: Renderer2,
private differs: KeyValueDiffers,
private injector: Injector,
@Inject(COMPONENT_INJECTOR)
private componentInjectorType: Type<ComponentInjector>,
@Inject(DynamicComponentInjectorToken)
@Optional()
private componentInjector?: DynamicComponentInjector,
@Host()
private componentOutletInjector: ComponentOutletInjectorDirective,
@Optional()
private componentOutletInjector?: ComponentOutletInjectorDirective,
) {}

ngDoCheck(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { DynamicAttributesDirective } from './dynamic-attributes.directive';

@NgModule({
imports: [CommonModule],
exports: [DynamicAttributesDirective],
declarations: [DynamicAttributesDirective],
})
export class DynamicAttributesModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dynamic-attributes.directive';
export * from './dynamic-attributes.module';
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ import {
ViewContainerRef,
} from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Subject } from 'rxjs';

import {
ComponentInjectorComponent,
InjectedComponent,
MockedInjectedComponent,
TestComponent,
TestModule,
MockedInjectedComponent,
} from '../test';
import { COMPONENT_INJECTOR } from './component-injector';
import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';
} from '../../test';
import {
ComponentOutletInjectorDirective,
DynamicComponentInjectorToken,
} from '../component-injector';
import { IoFactoryService } from '../io';
import { WindowRefToken, WindowRefService } from '../window-ref';
import {
DirectiveRef,
DynamicDirectiveDef,
dynamicDirectiveDef,
DynamicDirectivesDirective,
DynamicDirectiveDef,
} from './dynamic-directives.directive';
import { IoFactoryService } from './io-factory.service';
import { WindowRefService, WINDOW_REF } from './window-ref.service';
import { By } from '@angular/platform-browser';

@Directive({ selector: 'mock' })
class MockDirective
Expand Down Expand Up @@ -108,8 +110,11 @@ describe('Directive: DynamicDirectives', () => {
ComponentOutletInjectorDirective,
],
providers: [
{ provide: COMPONENT_INJECTOR, useValue: ComponentInjectorComponent },
{ provide: WINDOW_REF, useValue: window },
{
provide: DynamicComponentInjectorToken,
useExisting: ComponentInjectorComponent,
},
{ provide: WindowRefToken, useValue: window },
WindowRefService,
IoFactoryService,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import {
ViewRef,
} from '@angular/core';

import { COMPONENT_INJECTOR, ComponentInjector } from './component-injector';
import { ComponentOutletInjectorDirective } from './component-outlet-injector.directive';
import { IoFactoryService } from './io-factory.service';
import { InputsType, IoService, OutputsType } from './io.service';
import { getCtorType } from './util';
import { WindowRefService } from './window-ref.service';
import {
ComponentOutletInjectorDirective,
DynamicComponentInjector,
DynamicComponentInjectorToken,
} from '../component-injector';
import { InputsType, IoFactoryService, IoService, OutputsType } from '../io';
import { getCtorType } from '../util';
import { WindowRefService } from '../window-ref';

export interface DynamicDirectiveDef<T> {
type: Type<T>;
Expand Down Expand Up @@ -64,11 +66,6 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
@Output()
ndcDynamicDirectivesCreated = new EventEmitter<DirectiveRef<any>[]>();

private componentInjector = this.injector.get(
this.componentInjectorType,
null,
);

private lastCompInstance: any;

private get directives() {
Expand Down Expand Up @@ -118,15 +115,15 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
.create<DynamicDirectiveDef<any>>((_, def) => def.type);

constructor(
private injector: Injector,
private iterableDiffers: IterableDiffers,
private ioFactoryService: IoFactoryService,
private windowRef: WindowRefService,
@Inject(COMPONENT_INJECTOR)
private componentInjectorType: Type<ComponentInjector>,
@Inject(DynamicComponentInjectorToken)
@Optional()
private componentInjector?: DynamicComponentInjector,
@Host()
@Optional()
private componentOutletInjector: ComponentOutletInjectorDirective,
private componentOutletInjector?: ComponentOutletInjectorDirective,
) {}

ngDoCheck(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { DynamicDirectivesDirective } from './dynamic-directives.directive';

@NgModule({
imports: [CommonModule],
exports: [DynamicDirectivesDirective],
declarations: [DynamicDirectivesDirective],
})
export class DynamicDirectivesModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dynamic-directives.directive';
export * from './dynamic-directives.module';
Loading

0 comments on commit 5f2985b

Please sign in to comment.