evanw / evanw/esbuild

Useless repeated imports when using external modules

Open
#475 44 comments 55 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

## Setup
```sh
npx esbuild --version
# 0.7.17

echo 'export const x = 1' > a.js
echo 'import {x} from "./a.js"; console.log(x);' > b.js
echo 'import {x} from "./a.js"; console.log(x);' > c.js
echo 'import "./b.js"; import "./c.js";' > d.js
```

## Actual behavior
```
$ npx esbuild --bundle --format=esm --external:"`pwd`/a.js" d.js
// b.js
import {x} from "./a.js";
console.log(x);

// c.js
import {x as x2} from "./a.js";
console.log(x2);
```
And
```
$ npx esbuild --bundle --minify --format=esm --external:"`pwd`/a.js" d.js
import{x as o}from"./a.js";console.log(o);import{x as m}from"./a.js";console.log(m);
```
**NB:** `x` is imported twice, once as `x` and once as `x2`

## Expected behavior
```
$ npx esbuild --bundle --format=esm --external:"`pwd`/a.js" d.js
// b.js
import {x} from "./a.js";
console.log(x);

// c.js
console.log(x);
```
And
```
$ npx esbuild --bundle --minify --format=esm --external:"`pwd`/a.js" d.js
import{x as o}from"./a.js";console.log(o);console.log(o);
```
**NB:** `x` is imported only once

Contributor guide

No contributing guide indexed for this repository

Research direction

Re-run the provided esbuild 0.7.17 reproduction with external a.js and inspect the bundling path for repeated imports from the same external module. Compare the non-minified and minified output with the expected output, where the external export is imported once and both uses share it.

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.