evanw / evanw/esbuild

Empty init functions generated for side-effect-free ESM modules

Open
#3,771 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

suboptimal-output
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

ESBuild seems to generate empty "module init" functions even for side-effect free ESM modules.

Given a module `src/utils/is-equal.mts`:

```ts
export function isEqual(a, b) {
// ...
}
```

What ends up in the generated bundle is (in the root scope):

```js
// src/utils/is-equal.mts
function isEqual(a, b) {
// ...
}
var init_is_equal = __esm({
"src/utils/is-equal.mts"() {
"use strict";
}
});
```

And then `init_is_equal` is called everywhere that `isEqual` is imported. What is causing this? I would not expect `init_is_equal` to be present. It does this for some modules but not others.

Config
- esbuild@0.21.3
- `platform: 'node', format: 'esm', target: 'node20'`
- Entire application is ESM and side-effect free (`"sideEffects: false"` in `package.json`) but some dependencies are CJS

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the side-effect-free module in src/utils/is-equal.mts and the stated platform, format, and target settings. Inspect the generated bundle around init_is_equal and compare modules that do and do not receive an init function. Done means identifying the cause and establishing the expected generated output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.