evanw / evanw/esbuild

missing JS minification

Open
#3,964 0 comments 2 reactions 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

Original code:
```js
import { DEV } from "./constants";

function reportError() {
if (DEV) {
const message = "some useful message"
throw new Error(message)
} else {
throw new Error("ERROR_CODE x2242");
}
}

export { reportError };
```
bundled & minified output (`--minify --bundle --format=esm`):

```js
function E(){if(!1){let r="some useful message";throw new Error(r)}else throw new Error("ERROR_CODE x2242")}export{E as reportError};
```

expected output:
```js
function E(){throw new Error("ERROR_CODE x2242")}export{E as reportError};
```

Works perfectly when `DEV` boolean is defined in the same file
```js
const DEV = false

function reportError() {
if (DEV) {
const message = "some useful message"
throw new Error(message)
} else {
throw new Error("ERROR_CODE x2242");
}
}
export { reportError };
```
or when there's no variable declaration inside `if (DEV)` condition

```js
import { DEV } from "./constants";

function reportError() {
if (DEV) {
throw new Error("some useful message")
} else {
throw new Error("ERROR_CODE x2242");
}
}

export { reportError };
```

minimal repro link https://esbuild.github.io/try/#YgAwLjI0LjAALS1taW5pZnkgLS1idW5kbGUgLS1mb3JtYXQ9ZXNtAGUAZW50cnkuanMAaW1wb3J0IHsgREVWIH0gZnJvbSAiLi9jb25zdGFudHMiOwoKZnVuY3Rpb24gcmVwb3J0RXJyb3IoKSB7CiAgaWYgKERFVikgewogICAgIGNvbnN0IG1lc3NhZ2UgPSAic29tZSB1c2VmdWwgbWVzc2FnZSIKICAgICB0aHJvdyBuZXcgRXJyb3IobWVzc2FnZSkKICB9IGVsc2UgewogICAgdGhyb3cgbmV3IEVycm9yKCJFUlJPUl9DT0RFIHgyMjQyIik7CiAgfQp9CgpleHBvcnQgeyByZXBvcnRFcnJvciB9OwoAAGNvbnN0YW50cwBleHBvcnQgY29uc3QgREVWID0gZmFsc2U7

or try to build @sveltejs for `browser`.

Thanks @evanw 🙏

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked esbuild playground reproduction and run the shown command: --minify --bundle --format=esm. Compare the generated output with the expected output, including the case where DEV is imported from constants and the case where it is declared locally. Done means the unreachable branch and its local message are removed in the imported-constant case without changing the exported reportError behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.