简体   繁体   中英

Conditional environment variables tree-shaking

Currently I am trying to remove unused code based on the environment variable. In the code below when I run ng build --prod and set evironment.showDevTools = false in environment.prod.ts then I expect the console.log not to be included in the final build file, but unfortunately it is. What am I doing wrong?

import { Component } from '@angular/core';
import { environment } from '../environments/environment';

if (environment.showDevTools) {
  console.log('showing dev-tools');
}

@Component({
  selector: 'app-component',
  templateUrl: './app.html',
  styleUrls: ['app.scss']
})
export class AppComponent {

}

Part of main.js

,ut=n("AytR");ut.a.showDevTools&&console.log("showing dev-tools");var st=function(){

I had to add a field called "sideEffects": false to the package.json . So it currently looks like this:

{
  "name": "myApp",
  "version": "1.0.0",
  "sideEffects": false,
  "scripts": {
    "ng": "ng",
    "test": "ng test",
    ...
  },
}

References: https://webpack.js.org/guides/tree-shaking/ , https://github.com/stereobooster/package.json#sideeffects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM