evanw / evanw/esbuild

Math is a side-effect

Open
#3,988 5 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

constants with math operations are not being tree-shaken

INPUT
```js
const FOO = 1 + 2
```

OUTPUT
```js
// entry.js
var FOO = 1 + 2;
```

https://esbuild.github.io/try/#YgAwLjI0LjAALS1idW5kbGUgLS1mb3JtYXQ9ZXNtAGUAZW50cnkuanMAY29uc3QgRk9PID0gMSArIDI

Which in my case leaves a bunch of unused constants from imported but unused libraries.

```js
// lib/memory.mjs
var REG_SIZE = 4;
var STRING_SIZE = 2 * REG_SIZE;

// vendor/pako/pako.js
var MIN_MATCH$1 = 3;
var MAX_MATCH$1 = 258;
var LENGTH_CODES$1 = 29;
var LITERALS$1 = 256;
var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
var D_CODES$1 = 30;
var HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
var static_ltree = /* @__PURE__ */ new Uint32Array((L_CODES$1 + 2) * 2);
var static_dtree = /* @__PURE__ */ new Uint32Array(D_CODES$1 * 2);
var _length_code = /* @__PURE__ */ new Uint32Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
var LENGTH_CODES = 29;
var LITERALS = 256;
var L_CODES = LITERALS + 1 + LENGTH_CODES;
var HEAP_SIZE = 2 * L_CODES + 1;
var MIN_MATCH = 3;
var MAX_MATCH = 258;
var MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;

// site/git.ts
var UTF_0 = 0 + 48;
var UTF_1 = 1 + 48;
```

CODE

And there is no way to prevent this without making them to an iife afaik.

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the entry.js example from the linked esbuild try case and compare the bundled output with the reported result. Trace the tree-shaking path for unused constant expressions, then add coverage showing the expected handling of the math-based constants and verify the existing suite.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.