Question: How not to include mock services in Angular2 production bundle

Question

How not to include mock services in Angular2 production bundle

Answers 0
Added at 2016-12-29 23:12
Tags
Question

When building an angular 2 app for production we use

ng build --prod --w --aot 

But our mock services also get bundled and minified probably because we have

import {XMockService} from "./xxx-mock.service";

and that prevents the tree shaking from dropping the unused service. Here is our simplified app.module.ts which conditionally lazy-loads the mock service when environment.mock=true

I would imagine this is a pretty common scenario but I can't find any answers for it.

Thanks in advance and happy holidays! :-)

app.module.ts:

import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core";
import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import {AppComponent} from "./app.component";
import {environment} from "../environments/environment";
import {XService} from "./xxx.service";
import {XMockService} from "./xxx-mock.service";

let importedModules: Array<any> = [
  XService
];

if (environment.mock) {
  importedModules.push(
    {provide: XService, useClass: XMockService}
  );
} else {
  importedModules.push(
    XService
  );
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: importedModules,
  bootstrap: [AppComponent]
})

export class AppModule {}
Answers to

How not to include mock services in Angular2 production bundle

Source Show
◀ Wstecz