fix: CJS detection regression for packages with "export" in comments#3793
Merged
bartlomieju merged 2 commits intomainfrom Apr 28, 2026
Merged
fix: CJS detection regression for packages with "export" in comments#3793bartlomieju merged 2 commits intomainfrom
bartlomieju merged 2 commits intomainfrom
Conversation
The content-based heuristic (`code.includes("export ")`) was fooled by
CJS files containing the word "export" in comments — e.g.
`@opentelemetry/api`'s CJS entry has a comment:
// TODO: Remove ProxyTracerProvider export in the next major version.
This caused the CJS shim to be skipped, leaving `exports` undefined
when Vite's ESModulesEvaluator ran the file.
Replace the naive string check with two-layer detection:
1. Package.json "type" field (Node.js semantics): .cjs is always CJS,
.js is CJS unless the nearest package.json has "type": "module".
Results are cached per directory.
2. ESM statement regex fallback: for dual CJS/ESM packages that ship
ESM in .js without "type": "module" (e.g. @opentelemetry/api),
detect actual ESM syntax (export/import statements) including
minified forms like `import{` and `export*from`.
Also adds a test that imports from a real CJS npm package (qs) in
node_modules, since the existing CJS test fixtures were local ESM
files that never exercised the shim path.
Merged
bartlomieju
added a commit
that referenced
this pull request
Apr 28, 2026
## Summary - Bump `@fresh/core` 2.3.1 → 2.3.2 - Bump `@fresh/plugin-vite` 1.1.0 → 1.1.1 - Bump `@fresh/init` 2.3.1 → 2.3.2 - Bump `@fresh/update` 2.3.1 → 2.3.2 Patch release for the CJS detection regression fix (#3793).
2 tasks
bartlomieju
added a commit
that referenced
this pull request
Apr 28, 2026
## Summary Reverts two commits that broke CJS package handling in dev mode: - Reverts #3793 (CJS detection regex fix) - Reverts #3767 (removal of Babel CJS→ESM transform) The simplified CJS shim introduced in #3767 has two fundamental issues: 1. **Detection**: the heuristic (`code.includes("export ")`) is fooled by comments containing "export" 2. **Named exports**: the shim only provides `export default module.exports`, so `import { trace } from "@opentelemetry/api"` returns `undefined` (#3797) The Babel-based transform handled both correctly. Bringing it back until we can replace it with a proper solution using `deno_ast`'s CJS parser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3794
Fixes a regression from #3767 where CJS npm packages fail with
exports is not definedin dev mode.code.includes("export ")) was fooled by the word "export" appearing in comments of CJS files — e.g.@opentelemetry/apihas// TODO: Remove ProxyTracerProvider export in the next major version.which caused the CJS shim to be skipped entirelytypefield (Node.js semantics):.cjs→ always CJS,.js→ CJS unless nearestpackage.jsonhas"type": "module". Results cached per directory..jswithout"type": "module"(e.g.@opentelemetry/api,preact-render-to-string), detects actual ESM syntax including minified forms likeimport{andexport*fromqs) innode_modules— the existing "CJS test" fixtures were local ESM files that never exercised the shim path