evanw / evanw/esbuild

Dynamic method names prevent tree shaking

Open
#3,589 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Using dynamic class method names prevents the class to be discarded by tree shaking when it is not used.

Example:

```js
// input.js

const method = 'name';

class A {
greet() {
console.log('hello from A');
}

[method]() {
return 'A';
}
}

class B {
greet() {
console.log('hello from B');
}
}

new B().greet();
```

I have tried to add various `/* @__PURE__ */` annotations, but the class A does not get tree shaken.
The above example is deliberately simple to demonstrate the issue, but the context where it came from is that any class that declares a custom inspect function like below prevents treeshaking:

```js
[Symbol.for('nodejs.util.inspect.custom')]() {
return '...';
}
```

Here is a link to the playground to reproduce the issue.
https://esbuild.github.io/try/#dAAwLjE5LjExAC0tdHJlZS1zaGFraW5nPXRydWUAY29uc3QgbWV0aG9kID0gJ21ldGhvZCc7CgpjbGFzcyBBIHsKICBncmVldCgpIHsKICAgIGNvbnNvbGUubG9nKCdoZWxsbyBmcm9tIEEnKTsKICB9CiAgCiAgW21ldGhvZF0oKSB7CiAgICByZXR1cm4gJ0EnOwogIH0KfQoKY2xhc3MgQiB7CiAgZ3JlZXQoKSB7CiAgICBjb25zb2xlLmxvZygnaGVsbG8gZnJvbSBCJyk7CiAgfQp9CgpuZXcgQigpLmdyZWV0KCk7

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked esbuild playground and the provided input.js reproduction, then inspect tree-shaking behavior for computed class method names. Done means the unused class A is discarded while used class B remains, and the custom inspect example no longer prevents tree shaking.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.