简体   繁体   English

静态打字稿类方法树是否可以通过汇总摇动?

[英]Are static typescript class methods tree shakeable by rollup?

Suppose we create a typescript class with two static methods:假设我们使用两个静态方法创建一个打字稿类:

export class Utilities {
      static methodFoo() { return 'foo'}
      static methodBoo() { return 'boo'}
} 

Later someone imports our class from the npm package @scope/utilities and uses only methodFoo like this后来有人从 npm 包@scope/utilities导入我们的类,并且只使用methodFoo像这样

import {Utilities} from '@scope/utilities';

let pityTheFoo = Utilities.methodFoo();

If we use rollup to publish the above as an optimized / 'treeshaken' module, will rollup be able to shave off methodBoo ?如果我们使用 rollup 将上述内容作为优化/'treeshaken' 模块发布,rollup 是否能够去除methodBoo

As of November 2019, no, static methods aren't treeshaken.截至 2019 年 11 月,不,静态方法不是 treeshaken。 Rollup Issue #349 was raised for it, where even the tool creator was sympathetic to having the feature .为它提出了汇总问题 #349即使是工具创建者也对拥有该功能表示同情 The issue closed in an auto-cleanup due to inactivity since.由于此后不活动,该问题在自动清理中关闭

As for testing the behavior, you can easily do it yourself: just build a class with a static method that's never used, use rollup.js (or angular or something that includes rollup) and examine the output.至于测试行为,您可以自己轻松完成:只需使用从未使用过的静态方法构建一个类,使用 rollup.js(或 angular 或包含 rollup 的东西)并检查输出。

I'm struggling myself with this, because how are you then going to build treeshakable utils?我正在为此苦苦挣扎,因为您将如何构建可摇晃的实用程序? Just do it like rxjs and export all functions individually?就像 rxjs 一样,单独导出所有函数? Then you'll loose all the namespacing, which doesn't seem ideal to me either... I'm guessing your question originated from that thought?然后你会失去所有的命名空间,这对我来说似乎也不理想......我猜你的问题源于那个想法?

So as far as I know you currently either export plain functions or your utils won't be treeshaken.因此,据我所知,您目前要么导出普通函数,要么您的实用程序不会被摇摇欲坠。

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

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