Skip to content

Read imports and exports from the AST instead of the module record - #127

Merged
nstepien merged 2 commits into
mainfrom
imports-from-ast
Nov 6, 2026
Merged

nstepien merged 2 commits into
mainfrom
imports-from-ast

Conversation

@nstepien

@nstepien nstepien commented Nov 4, 2026

Copy link
Copy Markdown
Owner

parseFile read a module's imports and exports from the module record that parseSync returns. Converting that record to JS objects takes longer than reading the same declarations from the top-level statements of the program, so it now loops over program.body and never reads .module.

Benchmarks

Medians with Vite 8.3.5, main against this branch. The JS, CSS, and final source maps are identical.

Workload main This branch
Vite build of a synthetic project: 40 components × 8 templates × 5 tokens from one tokens.ts 31.9 ms 27.4 ms (−14%)
Cold dev server transforms of those 40 components 39.2 ms 35.0 ms (−11%)
Vite build of react-data-grid's src 21.0 ms 20.7 ms

Parsing alone goes from 3.60 to 2.41 ms for the synthetic project's 42 files, and from 6.43 to 5.79 ms for react-data-grid's 48 files. parseSync(…).program without the module record takes as long as this branch: the loop costs 7–11 µs per pass over those files.

Parity

On 123 files (react-data-grid, the fixtures, the synthetic project, and an edge-case file), the loop finds the same imports and exports as the module record, except where neither can resolve a value:

  • Exported functions and classes, which the loop skips, as only variables have static values
  • export { importedName as name }, which oxc treats as an indirect export, while the loop records it, though the root scope has no imports to resolve it from
  • A module that fails to parse, like JSX in a .js file read from disk: the program is empty, while the module record still lists what came before the error

Alternatives

  • this.parse(): in Rolldown and Vite, it calls parseSync with lang: 'js' and no file name, and throws on parse errors. It was as fast, but the language has to be passed by hand, and an imported module that fails to parse would fail the importer's transform with an error that names no file. On main, that only warns that the value can't be resolved.
  • Collecting them in SCOPE_VISITOR: it would save the loop's few microseconds, but it also visits the import and export declarations nested in TypeScript namespace, declare module, and declare global blocks, which open no scope.

Tests

A new test covers the forms the loop skips or names differently: imports and exports named by strings resolve, and default imports, destructured exports, and re-exports are skipped with a warning. It passes on main too. The uncovered lines are unchanged, with 3 fewer uncovered branches.

AGENTS.md now says where imports and exports are read from, and why.

🤖 Generated with Claude Code

parseFile read a module's imports and exports from the module record
that parseSync returns. Converting that record to JS objects takes
longer than reading the same declarations from the top-level statements
of the program, so it now loops over program.body and never reads
.module.

Medians with Vite 8.3.5, main against this branch, with identical JS,
CSS, and final source maps:
- Vite build of a synthetic project (40 components, 8 templates each,
  5 tokens per template from one tokens.ts): 31.9 ms to 27.4 ms (-14%)
- Cold dev server transforms of those components: 39.2 ms to 35.0 ms
  (-11%)
- Vite build of react-data-grid's src: 21.0 ms to 20.7 ms

A new test covers the forms the loop skips or names differently:
imports and exports named by strings resolve, and default imports,
destructured exports, and re-exports are skipped with a warning, as on
main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@nstepien
nstepien marked this pull request as ready for review November 6, 2026 22:44
@nstepien
nstepien merged commit 1ace514 into main Nov 6, 2026
12 checks passed
@nstepien
nstepien deleted the imports-from-ast branch November 6, 2026 22:47
nstepien added a commit that referenced this pull request Nov 7, 2026
The parse cache only stored finished parses. The transforms of the
modules that import the same module run concurrently: in a build, they
all wait for `this.load()` of that module and resume together, so each
one missed the cache and read and parsed the file again. In a project of
400 components that interpolate values of one `tokens.ts`, it was parsed
400 times per build, and in a cold dev server too.

The cache now holds the pending parse of each module read from disk,
stored as soon as it is requested, so the file is read and parsed once.
The parse of a module being transformed is stored as is. `parseFile`
only parses now, and `loadParsedFile` returns the cached or pending
parse of a module read from disk, like `loadImportedModule` does for the
imported modules of a transform (#124). Caching the promise is safe for
the same reason: a parse never waits for a value to resolve, so it can't
wait for itself.

**Benchmarks**

Medians with Vite 8.3.5, #127 against this branch. The JS, CSS, and
final source maps are identical.

| Workload | #127 | This branch |
|---|---|---|
| Vite build of a synthetic project: 40 components × 8 templates × 5
tokens from one `tokens.ts` | 28.5 ms | 23.1 ms (−19%) |
| The same with 400 components | 224.0 ms | 171.6 ms (−23%) |
| Cold dev server transforms of the 40 components | 35.9 ms | 31.8 ms
(−11%) |
| Cold dev server transforms of the 400 components | 386.1 ms | 339.9 ms
(−12%) |
| Vite build of react-data-grid's `src`, which reads no module from disk
| 20.7 ms | 21.0 ms |

`tokens.ts` is now read and parsed once per build and per cold dev
server, instead of 400 times.

**Other effects**

- A read that fails is cached like a parse, until `buildEnd` or
`watchChange` clears it. It fails the transform that interpolates the
module's values either way.
- In the dev server, a read that finishes after `watchChange` forgot the
module can no longer store its stale parse, as the pending parse is
cached before the read starts.

**Alternative**

`parse` from `rolldown/utils`, which parses on another thread, was also
measured. On its own, it gave up to 11% on these builds, the most where
the duplicate parses overlapped, and 0–4% on cold dev transforms. On top
of this branch, it gave at most 4%, while Rolldown advises preferring
`parseSync`, so it's left out.

The tests pass unchanged, with the same uncovered lines and branches as
#127. `AGENTS.md` now says why the parse is cached while pending.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
nstepien added a commit that referenced this pull request Nov 7, 2026
Main reworked the import handling underneath this branch: imports read
from the top-level statements (#127), parses cached while pending (#128),
imports as bindings of the module-level scope with default imports
through `*default*` (#131), values resolved without promises (#133), and
TypeScript namespaces given a scope of their own (#130). src/index.ts was
taken from main and this branch's features ported onto it:

- Namespace imports are `NamespaceImport` bindings, and member chains
  (`ns.a.b`) resolve through the exports of each namespace.
- Exports are `moduleExports` (local, re-export, namespace) plus
  `exportStarSources`, read from the top-level statements, and
  `resolveExport` follows them with ESM precedence and ambiguity.
  Exporting an imported binding re-exports it, as in the spec. Exported
  functions, classes, enums, and destructured bindings are recorded, so
  that they take precedence over `export *`.
- Type-only imports and exports are skipped through `importKind` and
  `exportKind`, as the module record is no longer read.
- Main's `isCssTag`, `bindingValues`, and `MaybePromise` resolution are
  kept. An import resolves through `resolveExport` and imports the
  stylesheet of the module that declares the value.

Main's decisions win where they differ from this branch:

- TypeScript namespaces are not supported: the namespace-name and
  `import x =` bindings are dropped, along with the typed fixture's
  namespace cases, as main's namespace test covers containment. Enums
  still shadow.
- Templates exported by default get no `default` name, so their class
  names hash like main's.

Main's import-forms test now extracts the namespace member and the
re-export it skipped. Two fixture cases cover the export of a namespace
import and a member of a local object.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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