javascript - Are static typescript class methods tree shakeable by rollup? - Stack Overflow

Suppose we create a typescript class with two static methods:export class Utilities {static methodFoo()

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

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?

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

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?

Share asked Mar 30, 2018 at 18:34 OleOle 47.3k70 gold badges237 silver badges443 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

As of November 2019, no, static methods aren't treeshaken. Rollup Issue #349 was raised for it, where even the tool creator was sympathetic to having the feature. 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.

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? 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742404473a4437565.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信