typescript 自 Angular CLI 6 起,ng build --prod 不会缩小/丑化/删除注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51926816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:35:28  来源:igfitidea点击:

ng build --prod does NOT minify / uglify / remove comments since Angular CLI 6

angulartypescriptangular-climinifyangular-cli-v6

提问by Francesco Borzi

Since I've upgraded my Angular app to use Angular CLI version 6.x, compiling it for production (using ng build --prod, as usual) does not produce minified js. This result in very big vendor.jssize (in my case is almost 10 MB).

由于我已经将我的 Angular 应用程序升级为使用 Angular CLI 版本6.x,因此编译它用于生产(ng build --prod像往常一样使用 )不会产生缩小的 js。这导致非常大的vendor.js大小(在我的情况下几乎是 10 MB)。

If I open the resulting vendor.jsfile, I can clearly see that the code is not minified and the comments are not removed.

如果我打开生成的vendor.js文件,我可以清楚地看到代码没有缩小,注释也没有删除。

回答by Francesco Borzi

The issue is in angular.jsonfile.

问题在angular.json文件中。

Under the key projects.MY_PROJECT_NAME.architect.build.configurations.production, I was missing all the options that normally comes by default in the productionconfiguration when you create a new angular project.

在 keyprojects.MY_PROJECT_NAME.architect.build.configurations.production下,production当您创建新的 angular 项目时,我错过了配置中通常默认提供的所有选项。

This is how the productionconfiguration should look like in order to fix the issue:

production为了解决这个问题,配置应该是这样的:

"production": {
  "fileReplacements": [
    {
      "replace": "src/environments/environment.ts",
      "with": "src/environments/environment.prod.ts"
    }
  ],
  "optimization": true,
  "outputHashing": "all",
  "sourceMap": false,
  "extractCss": true,
  "namedChunks": false,
  "aot": true,
  "extractLicenses": true,
  "vendorChunk": false,
  "buildOptimizer": true
},

For some reasons, after upgrading from previous Angular CLI versions, my productionconfiguration only had the fileReplacementskey. Adding the other properties shown above (optimization, outputHashing, etc...) solved the issue for me.

由于某些原因,从以前的 Angular CLI 版本升级后,我的production配置只有fileReplacements密钥。添加上面显示的其他属性(optimizationoutputHashing等...)为我解决了这个问题。