最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论