简体   繁体   English

如何修复 angular 中的 ng build 项目

[英]How to fix ng build project in angular

How to fix the ng build project in angular without editing in angular.json如何在 angular.json 中编辑 angular 中的ng build project

here's the error这是错误

Your global Angular CLI version (8.3.16) is greater than your local
version (8.1.3). The local Angular CLI version is used.

To disable this warning use "ng config -g cli.warnings.versionMismatch false".

chunk {0} runtime-es2015.52e4031f7f0d4c5547f5.js (runtime) 2.25 kB [entry] [rendered]
chunk {1} main-es2015.4e8665ff7ceef4e442ee.js (main) 4.62 MB [initial] [rendered]
chunk {2} polyfills-es2015.556027bb8b3cb9b5932e.js (polyfills) 173 kB [initial] [rendered]
chunk {3} styles.26e824d23ac01d2a1ba0.css (styles) 695 kB [initial] [rendered]
chunk {4} 4-es2015.823687dfc617d3f0d9c2.js () 3.43 kB  [rendered]
chunk {5} 5-es2015.f2106b47214935964e1d.js () 2.76 kB  [rendered]
Date: 2019-11-12T03:49:07.167Z - Hash: a28b3171138b993d899e - Time: 134373ms

WARNING in budgets, maximum exceeded for initial. Budget 2 MB was exceeded by 3.47 MB.

ERROR in budgets, maximum exceeded for initial. Budget 5 MB was exceeded by 483 kB.

在此处输入图像描述

thanks in advance.提前致谢。

I got an error when it comes to 92% and the error is in the above.当涉及到 92% 时,我得到了一个错误,错误在上面。

Update: I see your edit question.更新:我看到你的编辑问题。 For short answer you can only fix by using webpack-bundle-analyzer to analyze the bundle size and see which package affect your bundle size and remove it对于简短的回答,您只能通过使用webpack-bundle-analyzer来分析包大小并查看哪个 package 影响您的包大小并将其删除

You can see in angular.json there is the config for budget look like this您可以在 angular.json 中看到预算配置如下所示

"budgets": [
    {
        "type": "initial",
        "maximumWarning": "2mb",
        "maximumError": "5mb"
    },
    {
        "type": "anyComponentStyle",
        "maximumWarning": "6kb",
        "maximumError": "10kb"
    }
],

Remove this and you can build without error.删除它,您可以毫无错误地构建。 But I would not recommend you to remove it because this config will let you know the size of your application and you can improve the performance of the app by remove unnecessary code to reduce the size.但我不建议您删除它,因为此配置会让您知道应用程序的大小,并且您可以通过删除不必要的代码以减小大小来提高应用程序的性能。

You can have more information from here: https://angular.io/guide/build#configuring-size-budgets您可以从这里获得更多信息: https://angular.io/guide/build#configuring-size-budgets

I wouldn't suggest changing the budget sizes as they are meant just for this - warn you your bundles are growing too big.我不建议更改预算大小,因为它们仅用于此目的 - 警告您您的捆绑包变得太大。

You may consider 2 thing:你可以考虑两件事:

  • Lazy loaded modules for parts of the application that are not visible in the initial screen.在初始屏幕中不可见的应用程序部分的延迟加载模块。 You may also combine this with some pre-loading strategy to avoid consequent delays in bundle fetching.您也可以将其与一些预加载策略结合使用,以避免随之而来的捆绑获取延迟。
  • If you've already done the above then most probably the problem is in some (maybe new) import that's adding a lot of code.如果您已经完成了上述操作,那么很可能问题出在某些(可能是新的)导入中,这会添加大量代码。 Some new library you may be importing in multiple places.您可能要在多个地方导入一些新库。 I've seen this when we started using an external component and imported it in 3 different places.当我们开始使用外部组件并将其导入 3 个不同的地方时,我已经看到了这一点。 The solution was to put the component in a lazy-loaded module and import in the modules that needed it.解决方案是将组件放在延迟加载的模块中,然后导入需要它的模块。

Hope this helps.希望这可以帮助。

You need to edit your angular.json file.您需要编辑angular.json文件。 because this angular project generated with default.因为这个 angular 项目默认生成。 so in you console appearing warning.所以在你的控制台中出现警告。 find above line in your angular.json file to modify it.在您的 angular.json 文件中找到上面的行来修改它。

 "budgets": [
        {
           "type": "initial",
           "maximumWarning": "2mb",
           "maximumError": "5mb"
        }
   ]

angular.json angular.json

"budgets": [
   {
      "type": "initial",
      "maximumWarning": "3mb", <=== increase this value 
      "maximumError": "5mb" <==== may also increase this
   }
]

Budgets here indicates limits for bundle sizes that should not be exceeded as its affects performance.此处的预算表示捆绑大小的限制,因为它会影响性能,因此不应超过这些限制。

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

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