evanw / evanw/esbuild

Support tree-shaking with split declaration and initialization

Open
#2,196 0 comments 1 reaction 0 assignees View on GitHub
suboptimal-output
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

TLDR: I'd like the following to tree-shake:

```js
// index.js
export { x } from './file.js';

// file.js
let x, y;
x = 1;
y = 2;
export { x, y };
```

- - -

TS warns when accessing a private (either `#priv` or `private _priv`) on a class outside of the class's lexical scope. This was supposed to be solved by the static initialize block syntax, but the only way to export a function that access private fields is to split the declaration and initiailzation:

```js
export let foo;

export class Bar {
private _foo = 1;

static {
foo = (bar) => bar._foo;
}
}
```

Even when this is downleveled and minified, we'd still have the split:

```js
var t, o=class{constructor(){this._foo=1}};
(()=>{t=e=>e._foo})();
export{t as foo};
```

Terser can eliminate the static initialize block's IIFE, but we still end up with:

```js
var t, o=class{constructor(){this._foo=1}};
t=e=>e._foo;
export{t as foo};
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.