Skip to content

fix(transformer): use require not import in CommonJS files#18226

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/01-19-fix_transformer_use_require_not_import_in_commonjs_files
Jan 19, 2026
Merged

fix(transformer): use require not import in CommonJS files#18226
graphite-app[bot] merged 1 commit intomainfrom
om/01-19-fix_transformer_use_require_not_import_in_commonjs_files

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Jan 19, 2026

Transformer was deciding whether to insert an import statement or require() call depending on source_type.is_script(). That returns false for CommonJS, which was resulting in import statements being inserted in CommonJS files. Fix it by checking source_type.is_module() instead.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Jan 19, 2026
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 19, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing om/01-19-fix_transformer_use_require_not_import_in_commonjs_files (c4fa5d5) with main (8fe1e8e)1

Summary

✅ 38 untouched benchmarks
⏩ 7 skipped benchmarks2

Footnotes

  1. No successful run was found on main (9a5e3e2) during the generation of this report, so 8fe1e8e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@overlookmotel overlookmotel marked this pull request as ready for review January 19, 2026 11:23
Copilot AI review requested due to automatic review settings January 19, 2026 11:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the transformer where CommonJS files were incorrectly receiving import statements instead of require() calls. The issue was that the code was checking source_type.is_script() which returns false for CommonJS files (they have ModuleKind::CommonJS, not ModuleKind::Script). The fix changes the check to source_type.is_module(), which correctly identifies ES Modules vs everything else (Script, CommonJS).

Changes:

  • Swapped the logic in JSX transformer and module imports to use is_module() instead of is_script()
  • Removed an unused helper method is_script() from JsxImpl
  • Fixed test snapshot showing one additional passing test

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
crates/oxc_transformer/src/jsx/jsx_impl.rs Swapped conditional logic from is_script() to is_module() for determining whether to use AutomaticModule vs AutomaticScript bindings, and removed the unused is_script() helper method
crates/oxc_transformer/src/common/module_imports.rs Fixed the conditional logic to use is_module() instead of is_script() when deciding whether to insert import statements or require statements
tasks/coverage/snapshots/semantic_babel.snap Updated test snapshot showing one additional passing test and removed the error for a CommonJS file that was incorrectly using import symbol flags

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@overlookmotel overlookmotel self-assigned this Jan 19, 2026
@overlookmotel
Copy link
Member Author

overlookmotel commented Jan 19, 2026

I think this should fix the problem. Am running monitor-oxc to make sure.

https://github.com/oxc-project/monitor-oxc/actions/runs/21135903246

@overlookmotel
Copy link
Member Author

Yes, the fix works.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Jan 19, 2026
Copy link
Member Author

overlookmotel commented Jan 19, 2026

Merge activity

graphite-app bot pushed a commit that referenced this pull request Jan 19, 2026
Transformer was deciding whether to insert an `import` statement or `require()` call depending on `source_type.is_script()`. That returns `false` for CommonJS, which was resulting in `import` statements being inserted in CommonJS files. Fix it by checking `source_type.is_module()` instead.
@graphite-app graphite-app bot force-pushed the om/01-19-fix_transformer_use_require_not_import_in_commonjs_files branch from c4fa5d5 to 90e5f08 Compare January 19, 2026 12:43
Transformer was deciding whether to insert an `import` statement or `require()` call depending on `source_type.is_script()`. That returns `false` for CommonJS, which was resulting in `import` statements being inserted in CommonJS files. Fix it by checking `source_type.is_module()` instead.
@graphite-app graphite-app bot force-pushed the om/01-19-fix_transformer_use_require_not_import_in_commonjs_files branch from 90e5f08 to 645c3f0 Compare January 19, 2026 12:47
@graphite-app graphite-app bot merged commit 645c3f0 into main Jan 19, 2026
21 checks passed
@graphite-app graphite-app bot deleted the om/01-19-fix_transformer_use_require_not_import_in_commonjs_files branch January 19, 2026 12:54
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jan 19, 2026
overlookmotel added a commit that referenced this pull request Jan 19, 2026
### 🐛 Bug Fixes

- f1e2dc0 semantic: No error in `check_function_redeclaration` for
CommonJS files (#18231) (overlookmotel)
- 645c3f0 transformer: Use `require` not `import` in CommonJS files
(#18226) (overlookmotel)
- ee9f6a4 mangler: Use `retain` instead of `truncate` to remove empty
frequency slots (#18225) (Dunqing)

### ⚡ Performance

- 52073d9 semantic: Use cheaper test for source type (#18235)
(overlookmotel)

Co-authored-by: overlookmotel <557937+overlookmotel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants