Repository navigation
Description
Summary
When a module contains a TypeScript import-equals alias whose base is a default import (import X = Base.Y), esbuild's bundler drops the import Base from '...' statement entirely — while leaving value references to Base in the output. The result is a free identifier and a ReferenceError at runtime.
There is no warning and no error; the build exits 0.
Version
esbuild 0.28.1 (current latest at time of writing).
Minimal reproduction
Two files, no config, no package.json needed.
dep.js
export default class Base {
static hello() { return 'dep code is present'; }
}entry.ts
import Base from './dep.js';
import Alias = Base.SomeType;
export function make() {
return new Base();
}$ esbuild entry.ts --bundle --format=esm
Actual output
// entry.ts
function make() {
return new Base();
}
export {
make
};The import is gone, dep.js is not included in the bundle at all, and Base is left as an unbound free identifier. Running it:
return new Base();
^
ReferenceError: Base is not defined
Exit code is 0. result.warnings and result.errors are both empty.
Expected
Either the import is preserved (since Base is used as a value in new Base()), or the build fails / warns. Silently emitting a reference to an undefined variable seems clearly wrong.
For comparison, tsc preserves the import here — it knows Alias is type-only, erases just that declaration, and keeps import Base from './dep.js'.
Notes from narrowing it down
A few things that might help locate it:
- The alias does not need to be used. Deleting the
Aliasreference everywhere else still reproduces; the mere presence of theimport Alias = Base.SomeType;line is enough. Removing that one line fixes it. - Only
bundle: trueis affected.esbuild.transform()withloader: 'ts', andesbuild.build()withbundle: false, both correctly keepimport Base from './dep.js'. - Only a default import base is affected. A named or namespace base is fine, even with genuine runtime use:
import { ns } from './dep.js'; import Alias = ns.SomeType; export const k = ns.someEnum.Member; // works, ns import preserved
sideEffectsis irrelevant — reproduces with"sideEffects": false, without it, and with nopackage.jsonat all.- The dependency is resolved and read — it shows up in
metafile.inputs— it just contributes nothing to the output. - If the aliased path happens to exist as a real runtime value (e.g. the dep also has a merged
export namespace Base { ... }emitting an object), it does not reproduce. So it seems related to resolvingBase.SomeTypeagainst the module's actual exports and concluding the whole binding is type-only. - Possibly adjacent: import-is-undefined warning issued on TS import equals declaration #3271 covers the
import-is-undefinedwarning for an import-equals whose base is a local namespace. This case is different — the base is an imported default, and no warning fires at all.
Impact
This shipped a ReferenceError to production for us. The pattern is easy to write and looks harmless, tsc type-checks it fine, and nothing in the build output hints at a problem. The giveaway in a minified bundle is that the identifier stays unminified — esbuild won't rename what it treats as a global — which is a fairly subtle thing to notice.
Activity
fix evanw#4507: `import` assignment tree-shaking bug
Update dependency esbuild to v0.28.2 (#2)
Merge 'build(deps-dev): bump esbuild from 0.27.7 to 0.28.2 in /exampl…