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
?
1 Answer
Reset to default 8As 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.