Context
In an Angular 9 project, I am working with two environments: production & mock.
In the Core Module, I check for mock environment.
If build is made with mock configuration I inject mocked services that return mocked data, so no external http requests are made.
If build is made with prod configuration, real services are injected.
I do it like this:
core.module.ts
@NgModule({
declarations: [],
providers: [],
imports: [BrowserModule, HttpClientModule],
exports: [],
})
export class CoreModule {}
country.service.proxy.ts
const countryServiceFactory = (
_http: HttpClient,
_errorUtil: ErrorUtilService
) => {
return isMock
? new ServiceMock()
: new Service(_http, _errorUtil);
};
@Injectable({
providedIn: CoreModule,
useFactory: countryServiceFactory,
})
export abstract class CountryServiceProxy {
abstract getCountries(): Observable<CountryWithLanguages[]>;
}
Where ServiceMock
and Service
implement the same interface.
This works.
Problem
Code is not tree shakeable. The result is that in my bundle (when I run ng build --prod
) even the mock services are included.
I want to switch each service from mock to prod during development.
Goal
How can I make Angular to bundle only the service that it is going to be used?
I am using:
Angular CLI: 9.0.4
Node: 13.6.0
OS: darwin x64
Ivy Workspace: Yes
Thank you! :)