0

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) :(

  1. Force the removal of the service in tsconfig.lib.json file:
"exclude": ["src/lib/shared/services/logger/logger.service.ts"],
  1. 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": ''
                }
            }
        }
    }
}
}
}
  1. 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.

  1. 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 :)

Santironhacker
  • 722
  • 6
  • 11
  • What is the exact error you got from using file replacements? How about replacing your logger implementation with NOOP service for prod? Class will be there, but will do nothing. – Antoniossss Mar 22 '22 at 09:39
  • *So I was hoping to see the service completely removed from the build since it has not been declared inside any NgModule nor component.* I think it is not about module declaration but rather Type references in the rest of the code – Antoniossss Mar 22 '22 at 09:43
  • Do you have your service referenced in `public-api.ts` inside `src` directory of your lib project? You can try removing (or commenting it out) when building the dist. – Misha Mashina Mar 22 '22 at 19:49
  • @Antoniossss --> 1. For security reasons, I don't want the prod build to include the service. Even an empty one. No error is thrown upon file replacement since no replacement was effectively done. – Santironhacker Mar 24 '22 at 02:41
  • @Antoniossss --> 2. Type references: exactly, Angular still compiles the service if it is used in other classes, even if it had not been declared in any module / component / "{providedIn: root}". – Santironhacker Mar 24 '22 at 02:46
  • @MishaMashina Well spotted. Unfortunately the ```public-api.ts``` just has my main module exported: ```export * from ./lib/my-main-module.module.ts*.``` But the service is not declared in there neither in the imported _submodules / components / {providedIn: root}_ syntax. But it still gets compiled because it is referenced in some _service/components_ that make use of it. What's the workaround? – Santironhacker Mar 24 '22 at 02:50
  • @Santironhacker why wouldnt it, you are using it after all. You can try to preprocess your sourcefiles to remove all references. I dont think there is a built-in method of removing supossedly unwanted code from the codebase. You also dont have to a have anything declare in the bodule as you can still use eg exported members, static method, exported constants etc in your code. NG modules has little to nothing with it. – Antoniossss Mar 24 '22 at 10:23
  • Thanks for the idea @Antoniossss. In order to preprocess the files to remove the reference it means I would need to refactor all components / services where my LoggerService is used. But I still want to use it in DEV mode so not sure how to delete the reference just for PROD. – Santironhacker Mar 28 '22 at 01:31
  • just preprocess only for the prod build – Antoniossss Mar 28 '22 at 03:12

0 Answers0