-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.module.ts
152 lines (143 loc) 路 4.33 KB
/
app.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import {
HTTP_INTERCEPTORS,
HttpClient,
HttpClientModule,
} from '@angular/common/http';
import { APP_INITIALIZER, NgModule, PLATFORM_ID } from '@angular/core';
import {
BrowserModule,
BrowserTransferStateModule,
傻getDOM,
} from '@angular/platform-browser';
import { ServiceWorkerModule } from '@angular/service-worker';
import { routerReducer } from '@ngrx/router-store';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { PrebootModule } from 'preboot';
// NGRX store
import { EffectsModule } from '@ngrx/effects';
import {
MetaReducer,
StoreModule,
USER_PROVIDED_META_REDUCERS,
} from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { LocalStorageService } from '@services/local-storage.service';
import * as fromApplication from '@store/application/application.reducer';
import { LOCAL_STORAGE_KEY, STORAGE_KEYS } from '@store/meta/app.tokens';
import { storageMetaReducer } from '@store/meta/storage.metareducer';
import { RouterEffects } from '@store/router/router.effects';
// Core
import { environment } from 'environments/environment';
import { AppComponent } from './app.component';
// Routes
import { AppRoutingModule, GoogleAnalyticsEffects } from 'routes';
// Modules
import { ErrorModule } from './modules/error.module';
// Interceptor
import { AppHttpInterceptor } from './interceptors/http.interceptor';
import { getInitialState } from './ssr/tokens';
/**
* Custom TranslateService Loader to load the given language
* @param {HttpClient} http HttpClient to 'mock' the call to the assets i18n json file
* @return {TranslateHttpLoader} JSON
*/
export function createTranslateLoader(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
/**
* factory meta-reducer configuration function
*/
export function getMetaReducers(
saveKeys: string[],
localStorageKey: string,
storageService: LocalStorageService,
): MetaReducer<any>[] {
return [storageMetaReducer(saveKeys, localStorageKey, storageService)];
}
/**
* Marks an class as an NgModule so it could be configured
*/
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [
AppRoutingModule,
BrowserModule.withServerTransition({ appId: 'serverApp' }),
BrowserTransferStateModule,
EffectsModule.forRoot([GoogleAnalyticsEffects, RouterEffects]),
ErrorModule,
HttpClientModule,
PrebootModule.withConfig({ appRoot: 'app-root' }),
StoreModule.forRoot(
{
applicationState: fromApplication.Applicationreducer,
routerState: routerReducer,
},
{ initialState: getInitialState },
),
StoreDevtoolsModule.instrument(),
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.production,
}),
TranslateModule.forRoot({
loader: {
deps: [HttpClient],
provide: TranslateLoader,
useFactory: createTranslateLoader,
},
}),
],
providers: [
{
provide: STORAGE_KEYS,
useValue: ['applicationState', 'dummyState', 'routerState'],
},
{ provide: LOCAL_STORAGE_KEY, useValue: '__app_storage__' },
{
deps: [STORAGE_KEYS, LOCAL_STORAGE_KEY, LocalStorageService],
provide: USER_PROVIDED_META_REDUCERS,
useFactory: getMetaReducers,
},
{
multi: true,
provide: HTTP_INTERCEPTORS,
useClass: AppHttpInterceptor,
},
{
deps: [DOCUMENT, PLATFORM_ID],
multi: true,
provide: APP_INITIALIZER,
useFactory: /* istanbul ignore next */ function(
document: HTMLDocument,
platformId: Object,
): Function {
return () => {
if (isPlatformBrowser(platformId)) {
const dom = 傻getDOM();
const styles: any[] = Array.prototype.slice.apply(
dom.querySelectorAll(
document,
`style[ng-transition]`,
),
);
styles.forEach(el => {
// Remove ng-transition attribute to prevent Angular appInitializerFactory
// to remove server styles before preboot complete
el.removeAttribute('ng-transition');
});
document.addEventListener('PrebootComplete', () => {
// After preboot complete, remove the server scripts
setTimeout(() =>
styles.forEach(el => dom.remove(el)),
);
});
}
};
},
},
],
})
/* istanbul ignore next */
export class AppModule {}