Skip to content

Optimize constant ESM import bindings - #98603

Draft
lukesandberg wants to merge 6 commits into
canaryfrom
optimize-constant-esm-imports
Draft

Optimize constant ESM import bindings#98603
lukesandberg wants to merge 6 commits into
canaryfrom
optimize-constant-esm-imports

Conversation

@lukesandberg

@lukesandberg lukesandberg commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

What?

Optimize Turbopack's generated code for named and default ESM imports whose bindings are known not to change after module evaluation. These imports are captured once in a generated local, while live bindings continue to read through the module namespace.

Why?

Turbopack currently preserves every imported binding as a namespace property access at each use. That is necessary for genuinely live bindings, but adds repeated property reads and generated code for the common case where an export is constant.

How?

The import code generator follows statically known re-export chains and reuses the existing export-liveness analysis. Capture eligibility is a cached Turbo Task keyed by module, export, and chunking context, so repeated uses share the re-export walk and circuit-breaker lookup. It emits a compact, deterministic local binding only when the resolved export is constant and evaluation order is safe. Besides removing repeated property reads, direct calls through these locals no longer need the (0, namespace.property)(...) form used to avoid binding the namespace as this. Circuit breakers, mutable or live exports, assignment targets, namespace imports, externals, and unknown export shapes retain the conservative namespace access.

Snapshot and execution coverage verifies named and default imports, re-exports, mangled export keys, call semantics, live updates, and circular-import safety.

Verification

  • cargo nextest run -p turbopack-tests --no-fail-fast (412 passed)
  • cargo check -p turbopack-ecmascript
  • cargo fmt --all -- --check

Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Failing test suites

Commit: 981232b | About building and testing Next.js

pnpm test-dev-turbo test/development/app-hmr/hmr.test.ts (turbopack) (job)

  • app-dir-hmr > filesystem changes > can navigate cleanly to a page that requires a change in the Webpack runtime (DD)
Expand output

● app-dir-hmr › filesystem changes › can navigate cleanly to a page that requires a change in the Webpack runtime

expect(received).toEqual(expected) // deep equality

Expected: ArrayContaining [{"message": StringContaining "[Fast Refresh] done in", "source": "log"}]
Received: [{"message": "%cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold", "source": "info"}, {"message": "[HMR] connected", "source": "log"}, {"message": "Next.js hydrate callback fired", "source": "log"}, {"message": "Next.js hydrate callback fired", "source": "log"}]

  192 |       const logs = await browser.log()
  193 |       // TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver)
> 194 |       expect(logs).toEqual(
      |                    ^
  195 |         expect.arrayContaining([
  196 |           {
  197 |             message: expect.stringContaining('[Fast Refresh] done in'),

  at Object.toEqual (development/app-hmr/hmr.test.ts:194:20)

Other failing CI jobs

Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs Outdated
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs Outdated
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs
vercel-fleet-prod Bot and others added 2 commits September 12, 2026 22:14
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs Outdated
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs Outdated
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs Outdated
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs Outdated
Comment thread turbopack/crates/turbopack-ecmascript/src/references/esm/binding.rs
vercel-fleet-prod Bot and others added 2 commits September 13, 2026 19:27
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant