自 Angular CLI 6 起,ng build --prod 不会缩小/丑化/删除注释 [英] ng build --prod does NOT minify / uglify / remove comments since Angular CLI 6

查看:25
本文介绍了自 Angular CLI 6 起,ng build --prod 不会缩小/丑化/删除注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我升级了我的 Angular 应用程序以使用 Angular CLI 版本 6.x,编译它以用于生产(使用 ng build --prod,像往常一样)不产生缩小的 js.这导致 vendor.js 的大小非常大(在我的例子中几乎是 10 MB).

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.js size (in my case is almost 10 MB).

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

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

推荐答案

问题出在 angular.json 文件中.

在键 projects.MY_PROJECT_NAME.architect.build.configurations.production 下,当您创建时,我遗漏了 production 配置中通常默认出现的所有选项一个新的角度项目.

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

为了解决问题,production 配置应该如下所示:

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

"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
},

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

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

这篇关于自 Angular CLI 6 起,ng build --prod 不会缩小/丑化/删除注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆