Strip custom logging statements from production build


#1

For an Ionic2 project I’m using a custom Logger util class.
On places were I want to do error and debug logging, I do the following:

import { Logger }          from '../utils/logger';
...
this.logger = new Logger("AuthService");
...
this.logger.debug("Logging in");

Prior to Ionic 2 RC.0 I had a Webpack setup and for my production build I could remove all the debug-statements out of the code.
I did this by wrapping the debug statement in a if (DEBUG) {} block.
In Webpack I defined the following:

new DefinePlugin({
  'DEBUG': process.env.NODE_ENV !== 'production',
  ...
}

The uglify task would remove all the ‘dead’ code, so no debug statements in my code.

Ionic RC.0 (and up)
Now I’ve upgraded everything to the latest release (RC.3) and using Rollup for my build.
I tried Webpack again once the Ionic team re-introduced it, but found that my final package was a bit bigger.

I try to accomplish te same in the new setup but still with no luck.

I tried the following:

  • rollup-plugin-strip
    https://github.com/rollup/rollup-plugin-strip
    But somehow can’t get it to work with my custom logger (it just doesn’t pick up this.logger.debug statements, and I’ve defined it in the plugin config.
  • as a test (haven’t looked for a DefinePlugin alternative for Rollup yet) wrapped my debug statements in if (false) {} but the ngc compiler raises an error on this: ``Unreachable code detected.```

Are there any best practises already or does someone experiencing the same issues?