简体   繁体   English

条件环境变量摇摇欲坠

[英]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. 在下面的代码中,当我运行ng build --prod并在environment.prod.ts设置ng build --prod evironment.showDevTools = false ,我希望console.log不会包含在最终的构建文件中,但不幸的是它包含在其中。 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 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 . 我必须在package.json添加一个名为"sideEffects": false的字段。 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 参考: https : //webpack.js.org/guides/tree-shaking/,https : //github.com/stereobooster/package.json#sideeffects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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