Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
fix(preset): Make sure AOT will be able to collect metadata
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alex Malkevich committed Jan 2, 2018
1 parent 2580ef5 commit db7c939
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/demo/my/my-custom.module.ts
Original file line number Diff line number Diff line change
@@ -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<MyPreset>(MyCustomModule);
static withPreset(presetType: Type<MyPreset>): ModuleWithProviders {
return providePresetFor(MyCustomModule, presetType);
}
}
2 changes: 1 addition & 1 deletion src/demo/my/my.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('MyComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [PresetDefaultModule.withPreset(MockPresetComponent)],
imports: [PresetDefaultModule.forComponent(MyComponent, MockPresetComponent)],
declarations: [MyComponent, MockPresetComponent],
})
.compileComponents();
Expand Down
4 changes: 3 additions & 1 deletion src/demo/my/my.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
16 changes: 13 additions & 3 deletions src/preset/preset-default.module.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -11,5 +11,15 @@ import { PresetService } from './preset.service';
providers: [PresetService],
})
export class PresetDefaultModule {
static withPreset: WithPresetMethod = createWithPresetMethodFor(PresetDefaultModule);

static forComponent(component: Type<any>, presetType: Type<any>): ModuleWithProviders {
return PresetDefaultModule.forComponents([component], presetType);
}

static forComponents(components: Type<any>[], presetType: Type<any>): ModuleWithProviders {
return providePresetFor(PresetDefaultModule, presetType, [
{ provide: 'COMPS', useValue: components, multi: true },
]);
}

}
23 changes: 22 additions & 1 deletion src/preset/preset-method.ts
Original file line number Diff line number Diff line change
@@ -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<T = any> = (presetType: PresetType<T>) => ModuleWithProviders;

export function providePreset<T>(presetType: PresetType<T>): Provider[] {
return [
{ provide: PRESET_TYPES_TOKEN, useValue: presetType, multi: true },
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: presetType, multi: true },
];
}

export function providePresetFor<T>(
moduleClass: Type<any>,
presetType: PresetType<T>,
additionalProviders: Provider[] = [],
): ModuleWithProviders {
return {
ngModule: moduleClass,
providers: [
...providePreset(presetType),
...additionalProviders,
],
};
}

export function createWithPresetMethodFor<T = any>(
moduleClass: Type<any>
): WithPresetMethod<T> {
Expand Down
19 changes: 17 additions & 2 deletions src/preset/preset.module.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,4 +9,19 @@ import { PresetService } from './preset.service';
declarations: [],
providers: [PresetService],
})
export class PresetModule { }
export class PresetModule {

static forComponent(component: Type<any>): ModuleWithProviders {
return PresetModule.forComponents([component]);
}

static forComponents(components: Type<any>[]): ModuleWithProviders {
return {
ngModule: PresetModule,
providers: [
{ provide: 'COMPS', useValue: components, multi: true },
],
};
}

}

0 comments on commit db7c939

Please sign in to comment.