From 45a0992acc4f777bd3afc0c2997327555d573f4c Mon Sep 17 00:00:00 2001 From: Alex Malkevich Date: Tue, 2 Jan 2018 17:30:03 +0400 Subject: [PATCH] fix(preset): Make sure AOT will be able to collect metadata And also accept preseted component so further operations can be possible (like custom decorators) BREAKING CHANGE: Now module import module instead of `.withPreset` -> `.forComponent` by passing component to apply preset on as the first argument and preset component itself as second argument --- src/demo/my/my-custom.module.ts | 12 ++++++++---- src/demo/my/my.component.spec.ts | 2 +- src/demo/my/my.module.ts | 4 +++- src/preset/preset-default.module.ts | 16 +++++++++++++--- src/preset/preset-method.ts | 23 ++++++++++++++++++++++- src/preset/preset.module.ts | 19 +++++++++++++++++-- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/demo/my/my-custom.module.ts b/src/demo/my/my-custom.module.ts index 3d6be0f..c7fb1b3 100644 --- a/src/demo/my/my-custom.module.ts +++ b/src/demo/my/my-custom.module.ts @@ -1,16 +1,20 @@ -import { NgModule } from '@angular/core'; +import { ModuleWithProviders, NgModule, Type } from '@angular/core'; -import { createWithPresetMethodFor, PresetModule } from '../../preset'; +import { PresetModule } from '../../preset'; +import { providePresetFor } from '../../preset/preset-method'; import { MyComponentModule } from './my-component.module'; import { MyPreset } from './my-preset'; +import { MyComponent } from './my.component'; @NgModule({ imports: [ - PresetModule, MyComponentModule, + PresetModule.forComponent(MyComponent), ], exports: [MyComponentModule], }) export class MyCustomModule { - static withPreset = createWithPresetMethodFor(MyCustomModule); + static withPreset(presetType: Type): ModuleWithProviders { + return providePresetFor(MyCustomModule, presetType); + } } diff --git a/src/demo/my/my.component.spec.ts b/src/demo/my/my.component.spec.ts index 07b0426..15fa4e4 100644 --- a/src/demo/my/my.component.spec.ts +++ b/src/demo/my/my.component.spec.ts @@ -15,7 +15,7 @@ describe('MyComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [PresetDefaultModule.withPreset(MockPresetComponent)], + imports: [PresetDefaultModule.forComponent(MyComponent, MockPresetComponent)], declarations: [MyComponent, MockPresetComponent], }) .compileComponents(); diff --git a/src/demo/my/my.module.ts b/src/demo/my/my.module.ts index 01353cd..c112775 100644 --- a/src/demo/my/my.module.ts +++ b/src/demo/my/my.module.ts @@ -3,11 +3,13 @@ import { NgModule } from '@angular/core'; import { PresetDefaultModule } from '../../preset'; import { MyComponentModule } from './my-component.module'; import { MyPresetDefaultComponent } from './my-preset-default/my-preset-default.component'; +import { MyComponent } from './my.component'; @NgModule({ imports: [ MyComponentModule, - PresetDefaultModule.withPreset(MyPresetDefaultComponent), + PresetDefaultModule + .forComponent(MyComponent, MyPresetDefaultComponent), ], exports: [MyComponentModule], declarations: [MyPresetDefaultComponent], diff --git a/src/preset/preset-default.module.ts b/src/preset/preset-default.module.ts index 8c7e3d7..ae84b1a 100644 --- a/src/preset/preset-default.module.ts +++ b/src/preset/preset-default.module.ts @@ -1,7 +1,7 @@ import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; +import { ModuleWithProviders, NgModule, Type } from '@angular/core'; -import { createWithPresetMethodFor, WithPresetMethod } from './preset-method'; +import { providePresetFor } from './preset-method'; import { PresetService } from './preset.service'; @NgModule({ @@ -11,5 +11,15 @@ import { PresetService } from './preset.service'; providers: [PresetService], }) export class PresetDefaultModule { - static withPreset: WithPresetMethod = createWithPresetMethodFor(PresetDefaultModule); + + static forComponent(component: Type, presetType: Type): ModuleWithProviders { + return PresetDefaultModule.forComponents([component], presetType); + } + + static forComponents(components: Type[], presetType: Type): ModuleWithProviders { + return providePresetFor(PresetDefaultModule, presetType, [ + { provide: 'COMPS', useValue: components, multi: true }, + ]); + } + } diff --git a/src/preset/preset-method.ts b/src/preset/preset-method.ts index d27be64..6e0d215 100644 --- a/src/preset/preset-method.ts +++ b/src/preset/preset-method.ts @@ -1,9 +1,30 @@ -import { ANALYZE_FOR_ENTRY_COMPONENTS, ModuleWithProviders, Type } from '@angular/core'; +import { ANALYZE_FOR_ENTRY_COMPONENTS, ModuleWithProviders, Provider, Type } from '@angular/core'; import { PRESET_TYPES_TOKEN, PresetType } from './preset-token'; export type WithPresetMethod = (presetType: PresetType) => ModuleWithProviders; +export function providePreset(presetType: PresetType): Provider[] { + return [ + { provide: PRESET_TYPES_TOKEN, useValue: presetType, multi: true }, + { provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: presetType, multi: true }, + ]; +} + +export function providePresetFor( + moduleClass: Type, + presetType: PresetType, + additionalProviders: Provider[] = [], +): ModuleWithProviders { + return { + ngModule: moduleClass, + providers: [ + ...providePreset(presetType), + ...additionalProviders, + ], + }; +} + export function createWithPresetMethodFor( moduleClass: Type ): WithPresetMethod { diff --git a/src/preset/preset.module.ts b/src/preset/preset.module.ts index 5ebf237..5b02094 100644 --- a/src/preset/preset.module.ts +++ b/src/preset/preset.module.ts @@ -1,5 +1,5 @@ import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; +import { ModuleWithProviders, NgModule, Type } from '@angular/core'; import { PresetService } from './preset.service'; @@ -9,4 +9,19 @@ import { PresetService } from './preset.service'; declarations: [], providers: [PresetService], }) -export class PresetModule { } +export class PresetModule { + + static forComponent(component: Type): ModuleWithProviders { + return PresetModule.forComponents([component]); + } + + static forComponents(components: Type[]): ModuleWithProviders { + return { + ngModule: PresetModule, + providers: [ + { provide: 'COMPS', useValue: components, multi: true }, + ], + }; + } + +}