Skip to content

Commit 7c85aee

Browse files
committed
fix: parse .js within "type": "commonjs" as ESM for now (#8470)
This PR partially reverts #8455. `.js` with the closest `package.json` with `"type": "commonjs"` will now be parsed as ESM for now as that caused tests in Vite repo to fail. For example: ``` [PARSE_ERROR] Error: Cannot use import statement outside a module ╭─[ node_modules/.pnpm/@babel[email protected]/node_modules/@babel/runtime/helpers/esm/slicedToArray.js:1:1 ] │ 1 │ import arrayWithHoles from "./arrayWithHoles.js"; │ ───┬── │ ╰──── ───╯ ```
1 parent 061a9b3 commit 7c85aee

File tree

7 files changed

+5
-48
lines changed

7 files changed

+5
-48
lines changed

crates/rolldown/src/utils/parse_to_ecma_ast.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ fn pure_esm_js_oxc_source_type(module_def_format: ModuleDefFormat) -> OxcSourceT
2222
debug_assert!(default_source_type.is_javascript());
2323
debug_assert!(!default_source_type.is_jsx());
2424
match module_def_format {
25-
ModuleDefFormat::Cjs | ModuleDefFormat::Cts | ModuleDefFormat::CjsPackageJson => {
26-
default_source_type.with_commonjs(true)
27-
}
25+
ModuleDefFormat::Cjs | ModuleDefFormat::Cts => default_source_type.with_commonjs(true),
2826
ModuleDefFormat::EsmMjs | ModuleDefFormat::EsmMts | ModuleDefFormat::EsmPackageJson => {
2927
default_source_type.with_module(true)
3028
}
31-
ModuleDefFormat::Unknown => {
29+
ModuleDefFormat::CjsPackageJson | ModuleDefFormat::Unknown => {
3230
// treat unknown format as ESM for now: https://github.com/rolldown/rolldown/issues/7009
3331
default_source_type.with_module(true)
3432
}

crates/rolldown/tests/rolldown/issues/7009/js_type_commonjs/_config.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/rolldown/tests/rolldown/issues/7009/js_type_commonjs/artifacts.snap

Lines changed: 0 additions & 17 deletions
This file was deleted.

crates/rolldown/tests/rolldown/issues/7009/js_type_commonjs/index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

crates/rolldown/tests/rolldown/issues/7009/js_type_commonjs/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4896,10 +4896,6 @@ expression: output
48964896
48974897
- main-!~{000}~.js => main-B4u3m95-.js
48984898
4899-
# tests/rolldown/issues/7009/js_type_commonjs
4900-
4901-
- main-!~{000}~.js => main-CO96sClh.js
4902-
49034899
# tests/rolldown/issues/7021
49044900
49054901
- main-!~{000}~.js => main-BuqYlu74.js

docs/in-depth/bundling-cjs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ If you find an issue that seems to be caused by this incompatibility, try using
200200

201201
If the heuristic is not working for you, you can use the code in the section above that handles both interpretations. If the import is in a dependency, we recommend to raise an issue to the dependency. In the meantime, you can use [`patch-package`](https://github.com/ds300/patch-package) or [`pnpm patch`](https://pnpm.io/cli/patch) or alternatives as an escape hatch.
202202

203-
### Strict Mode Applied to `.js` files without `"type": "commonjs"` in `package.json`
203+
### Strict Mode Applied to `.js` files
204204

205-
For files ending with `.js` that do not have `"type": "commonjs"` in the closest `package.json`, Rolldown incorrectly parses the file as ESM ([#7009](https://github.com/rolldown/rolldown/issues/7009)). This means that syntaxes only allowed in non-strict mode (sloppy mode) will be rejected.
205+
For files ending with `.js`, Rolldown parses the file as ESM ([#7009](https://github.com/rolldown/rolldown/issues/7009)) without falling back to CJS. This means that syntaxes only allowed in non-strict mode (sloppy mode) will be rejected.
206206

207-
For now, you can change the file extension to `.cjs` or add `"type": "commonjs"` to the closest `package.json` as a workaround.
207+
For now, you can change the file extension to `.cjs` as a workaround.
208208

209209
## Future Plans
210210

0 commit comments

Comments
 (0)