WHAT I WANT TO DO
My app is a lib that aims to be installed into another app as a module. I have a loggerService in my lib:
@Injectable({'providedIn': 'root'})
export class Logger {
log(message: string) {
console.log(message)
}
}
Basically, when I build the lib and check the /dist folder, I want my loggerService to be removed from the build.
WHAT I HAVE TRIED (all failures) :(
- Force the removal of the service in tsconfig.lib.json file:
"exclude": ["src/lib/shared/services/logger/logger.service.ts"],
- Empty file replacement inside the angular.json file for the build of the main-app which will make use of my lib :
"projects": {
"main-app": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": {
"replace": "src/lib/shared/services/logger/logger.service.ts"
"with": ''
}
}
}
}
}
}
}
- Empty file replacement inside the angular.json file for the build of the lib itself:
"projects": {
"my-lib": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": {
"replace": "src/lib/shared/services/logger/logger.service.ts"
"with": ''
}
}
}
}
}
}
}
build ERROR --> cannot use fileReplacements inside the production bracket like for the main-app.
- Remove the
{"providedIn": 'root'}
from the loggerService file:
@Injectable()
export class Logger {
So I was hoping to see the service completely removed from the build since it has not been declared inside any NgModule nor component.
RELATED QUESTIONS
· Remove service from build: This question was asked 3 years ago but never got a working answer. :/
· Remove interceptor from build: same as above, never got a satisfying answer
Looks like it is not that easy since Angular Ivy compiler detects that the service is being used by some component or service so you cannot exclude that service in a mandatory way. Is there some alternative structure of the code to achieve the desired result?
Note I want to completely remove the Logger service for security reasons and not just enable it in development mode :)