1

We have a service which works as an HTTP interceptor in our development environment. It adds some special debug and authentication headers to ease the development.

However, we don't want this service to appear in production or even to be added to the compiled source code.

How do we completely remove some Angular service from production build?

Are there some kind of compiler directives which we could use?

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
  • You could register a different provider for that service based on environment. See if this helps: https://stackoverflow.com/questions/39942118/how-to-inject-different-service-based-on-certain-build-environment-in-angular2 – Wand Maker Apr 28 '18 at 15:21
  • I can configure the provider using factory function and conditional operator with reference to environment variable, but the service will be still referenced in my code. Will it be added to the final build? I'm not sure if compiler (tree shaker) is smart enough to remove unused service this way. – Slava Fomin II Apr 28 '18 at 15:28
  • No it is not, woudl have to use NOOP service implementation of preprocess sources first to remove usages of this service - then tree shaker would do its stuff. – Antoniossss Mar 22 '22 at 09:41

0 Answers0