【发布时间】:2020-10-18 09:17:43
【问题描述】:
Angular 10 中的 Tree Shaking 正在“摇晃”我的 AsyncPipe。
Angular 10 的 release notes blog entry 为 ng new 引入了新的 --strict 模式:
这样做的一件事是:
将您的应用配置为无副作用以启用更高级的 tree-shaking
官方文档说:
当您使用严格模式创建项目和工作区时,您将 请注意位于 src/app/ 目录中的附加 package.json 文件。 此文件通知工具和捆绑器此下的代码 目录没有非本地副作用。
这是package.json的内容:
{
"name": "heroes",
"private": true,
"description_1": "This is a special package.json file that is not used by package managers.",
"description_2": "It is used to tell the tools and bundlers whether the code under this directory is free of code with non-local side-effect. Any code that does have non-local side-effects can't be well optimized (tree-shaken) and will result in unnecessary increased payload size.",
"description_3": "It should be safe to set this option to 'false' for new applications, but existing code bases could be broken when built with the production config if the application code does contain non-local side-effects that the application depends on.",
"description_4": "To learn more about this file see: https://angular.io/config/app-package-json.",
"sideEffects": false
}
太棒了!我想。我更喜欢摇树。
但是AsyncPipe 震动了,我不知道为什么。我在一个大型网站的任何地方都使用它 - 我看不出它是如何优化它的。
它只在优化的--prod 构建中这样做。当我将其更改为 sideEffects: true 时,它又可以工作了。
【问题讨论】:
-
没人知道为什么?
-
我有同样的问题,但还不能在简单的项目设置中重现它。我会尽量压缩我目前的项目,直到找到根本原因。
标签: angular tree-shaking