Skip to content

Commit

Permalink
feat(next-swc/auto-cjs): Detect __esModule (#38181)
Browse files Browse the repository at this point in the history
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

---

 - Closes #38117
  • Loading branch information
kdy1 committed Jul 12, 2022
1 parent 883a52e commit 9342a6c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/next-swc/crates/core/src/auto_cjs/mod.rs
Expand Up @@ -30,4 +30,10 @@ impl Visit for CjsFinder {
e.obj.visit_with(self);
e.prop.visit_with(self);
}

fn visit_str(&mut self, s: &Str) {
if s.value.contains("__esModule") {
self.found = true;
}
}
}
5 changes: 3 additions & 2 deletions packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -202,8 +202,9 @@ impl TransformOptions {
pub fn patch(mut self, fm: &SourceFile) -> Self {
self.swc.swcrc = false;

let should_enable_commonjs =
self.swc.config.module.is_none() && fm.src.contains("module.exports") && {
let should_enable_commonjs = self.swc.config.module.is_none()
&& (fm.src.contains("module.exports") || fm.src.contains("__esModule"))
&& {
let syntax = self.swc.config.jsc.syntax.unwrap_or_default();
let target = self.swc.config.jsc.target.unwrap_or_else(EsVersion::latest);

Expand Down

0 comments on commit 9342a6c

Please sign in to comment.