admin管理员组

文章数量:1336631

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.

本文标签: javascriptAre static typescript class methods tree shakeable by rollupStack Overflow