简体   繁体   English

@角/料桶进口成本

[英]@angular/material barrel import cost

Using the latest Angular & CLI versions (~6.1.6), and the latest @angular/material library (~6.4.7), I'm finding a pretty significant performance penalty when using an import from @angular/material's primary entry point ( public_api.ts ): 使用最新的Angular和CLI版本(〜6.1.6),以及最新的@ angular / material库(〜6.4.7),当使用从@ angular / material的主要入口点进行导入时,我发现性能下降非常明显( public_api.ts ):

import { MatButtonModule }  from '@angular/material';

If I update the import statement to use the secondary endpoint: 如果我更新import语句以使用辅助端点:

import { MatButtonModule }  from '@angular/material/button';

My vendor bundle is reduced by ~2.6MB, and page render speed decreases by ~200ms. 我的供应商捆绑包减少了约2.6MB,页面渲染速度降低了约200ms。

With @angular/cli 's tree shaking abilities, the ultimate production build size is equivalent with either import statement. 具有@angular/cli的摇树能力,最终生产版本的大小与任一import语句相同。 As a result, I feel people often favor the development semantics of the terser import statement, not realizing/understanding the dev-time performance penalty. 结果,我觉得人们经常偏爱terser import语句的开发语义,而不是意识到/理解dev-time性能损失。

Why isn't the performance penalty of 3rd party "barrel importing" discussed more? 为什么不更多讨论第三方“桶进口”的性能损失? I've had a really hard time finding any documentation on the decision making process and/or performance implications of these choices. 我很难找到有关这些选择的决策过程和/或性能影响的任何文档。

So when you import barrel, webpack will add all exports from the barrel to your bundle.js. 因此,当您导入桶时,webpack会将桶的所有导出添加到bundle.js中。 When you import a specific file like below 当您导入如下所示的特定文件时

import { MatButtonModule }  from '@angular/material/button';

Will be imported only button file. 将仅导入按钮文件。

But the webpack 2 release came with built-in support for ES2015 modules (alias harmony modules) as well as unused module export detection. 但是webpack 2版本附带对ES2015模块(别名和声模块)的内置支持以及未使用的模块导出检测。 The new webpack 4 release expands on this capability with a way to provide hints to the compiler via the "sideEffects" package.json property to denote which files in your project are "pure" and therefore safe to prune if unused. 新的webpack 4版本在此功能上进行了扩展,并通过“ sideEffects” package.json属性向编译器提供了提示,以指示项目中的哪些文件是“纯”的,因此如果不使用它们可以安全地进行修剪。

There are several tips to get rid of "dead code"( imports which included in your bundle but not used) 有一些技巧可以消除“死代码”(捆绑包中包含但未使用的导入)

  • Use ES2015 module syntax (ie import and export). 使用ES2015模块语法(即导入和导出)。
  • Add a "sideEffects" property to your project's package.json file. 在项目的package.json文件中添加一个“ sideEffects”属性。
  • Use production mode configuration option to enable various optimizations including minification and tree shaking . 使用生产模式配置选项可启用各种优化,包括缩小和摇树

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

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