evanw / evanw/esbuild

Tree-shaking doesn't work for ES decorators

Open
#4,334 0 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

A dead simple example:

Source code:

```ts
// a.js
/* @__PURE__ */
function dec() {
}

@dec
export class A1 {
}

export class A2 {
}

export class A3 {
}

// b.js
import { A2 } from './a';

export class B extends A2 {
}
```

Build command:

```bash
esbuild --bundle ./temp/b.js
```

The output:

```js
// temp/a.js
function dec() {
}
var A1 = @dec class {
};
var A2 = class {
};

// temp/b.js
var B = class extends A2 {
};
export {
B
};
```

The expected behavior is `dec` and `A1` are erased.

I'm not sure if I've misunderstood, but I've searched for a long time and haven't found the answer.

[Playground](https://esbuild.egoist.dev/#W1siaW5kZXgudHMiLHsiY29udGVudCI6ImltcG9ydCB7IEEyIH0gZnJvbSAnLi9hJztcblxuZXhwb3J0IGNsYXNzIEIgZXh0ZW5kcyBBMiB7XG59XG4ifV0sWyJlc2J1aWxkLmNvbmZpZy5qc29uIix7ImNvbnRlbnQiOiJ7XG4gIFwiZm9ybWF0XCI6IFwiZXNtXCIsXG4gIFwiY2RuVXJsXCI6IFwiaHR0cHM6Ly9lc20uc2hcIlxufSJ9XSxbImEudHMiLHsiY29udGVudCI6Ii8qIEBfX1BVUkVfXyAqL1xuZnVuY3Rpb24gZGVjKCkge1xufVxuXG5AZGVjXG5leHBvcnQgY2xhc3MgQTEge1xufVxuXG5leHBvcnQgY2xhc3MgQTIge1xufVxuXG5leHBvcnQgY2xhc3MgQTMge1xufVxuIn1dXQ==)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the issue with the shown temp/a.js and temp/b.js files and `esbuild --bundle ./temp/b.js`; the Playground provides the same case. Trace how ES decorators affect tree-shaking, then verify that the unused decorator and A1 are removed while A2 and B remain. Add a regression test for this example if the repository's test entry point is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript, typescript
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.