Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 501
Ole Avatar asked Mar 30 '18 18:03

Ole


1 Answers

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.

like image 91
bersling Avatar answered Sep 28 '22 01:09

bersling