From 9342a6ccc7f59e5de481577be61859cf5372be0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 12 Jul 2022 12:17:09 +0900 Subject: [PATCH] feat(next-swc/auto-cjs): Detect `__esModule` (#38181) ## 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 --- packages/next-swc/crates/core/src/auto_cjs/mod.rs | 6 ++++++ packages/next-swc/crates/core/src/lib.rs | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/next-swc/crates/core/src/auto_cjs/mod.rs b/packages/next-swc/crates/core/src/auto_cjs/mod.rs index 5f35aa8cc1b7..7e49265de7e5 100644 --- a/packages/next-swc/crates/core/src/auto_cjs/mod.rs +++ b/packages/next-swc/crates/core/src/auto_cjs/mod.rs @@ -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; + } + } } diff --git a/packages/next-swc/crates/core/src/lib.rs b/packages/next-swc/crates/core/src/lib.rs index 60fcb7ce18af..98190b487a62 100644 --- a/packages/next-swc/crates/core/src/lib.rs +++ b/packages/next-swc/crates/core/src/lib.rs @@ -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);