Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping all comments and console.logs with ng build --prod, possible?

I am using MEAN, Angular 2, Node/Express, Angular-CLI and ng build --prod to build my app and I have a travesty of commented out throwaway code and a billion debugging console.log statements throughout my app. Is there a way to have the build process remove all comments and console.log statements when it builds? The thought of doing it manually is scary!

like image 635
seanEd Avatar asked Feb 17 '17 21:02

seanEd


People also ask

What does ng build -- prod does?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.

Does angular remove console log in production?

Angular CLI. By using Angular CLI it is very easy to build your production app by using ng build --prod, but you will notice that console. log statements are not deleted from production files.

Does ng build default to production?

ng build now uses the production configuration by default. You no longer need to pass the --prod option to the command.


1 Answers

I have simple fix. Put this code in main.ts

if(env === 'prod') { // assuming you have env variable configured   // check if window exists, if you render backend window will not be available    if(window){       window.console.log = function(){};    } }   
like image 116
Sudhakar Avatar answered Oct 05 '22 06:10

Sudhakar