Repository: swc-project/swc (rust)

Issue:
Bug: _interop_require_wildcard helper missing
### Describe the bug

`_interop_require_wildcard` is missing, if has `module` configured to `commonjs`

### Input code

```typescript
const a = require('foo')
console.log(a)
import('other')
```


### Config

```json
{
  "jsc": {
    "target": "es2020",
    "parser": {
      "syntax": "typescript",
    },
  },
  "isModule": "unknown",
  "module": {
    "type": "commonjs"
  },
}
```


### Playground link

https://play.swc.rs/?version=1.3.66&code=H4sIAAAAAAAAA0vOzysuUUhUsFUoSi0szSxK1VBPy89X1%2BRKBkrk56Tq5eSnayRqcmXmFuQXlWio55dkpBYBpQEdbRHiOAAAAA%3D%3D&config=H4sIAAAAAAAAAz2NSwqAMAwF956iZO1CXHoHD1FqED9NSxNREe9u6m%2BT5A3Dy1EYAyM7aMyhpwaxqUfRDMh1VVdQPjzaxJh%2BTwnvJHbLpuwR2aUhymufed0DBm5Dt8yYvYUmCivdEvgPf4%2B1JEsueB9oZHgqzgsyPIPvowAAAA%3D%3D

### Expected behavior

`_interop_require_wildcard` presents

### Actual behavior

`_interop_require_wildcard` is missing

### Version

1.3.66

### Additional context

_No response_

Pull request:
fix(es/module): Skip Script transformations for CJS and AMD
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
- fix #7650 

Patch:
diff --git a/crates/swc_ecma_transforms_module/src/amd.rs b/crates/swc_ecma_transforms_module/src/amd.rs
index d0b79aa078..769ac7bf95 100644
--- a/crates/swc_ecma_transforms_module/src/amd.rs
+++ b/crates/swc_ecma_transforms_module/src/amd.rs
@@ -129,8 +129,8 @@ where
 {
     noop_visit_mut_type!();
 
-    fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
-        if let Some(first) = n.first() {
+    fn visit_mut_module(&mut self, n: &mut Module) {
+        if let Some(first) = n.body.first() {
             if self.module_id.is_none() {
                 self.module_id = self.get_amd_module_id_from_comments(first.span());
             }
@@ -139,13 +139,13 @@ where
         let import_interop = self.config.import_interop();
 
         let mut strip = ModuleDeclStrip::new(self.const_var_kind);
-        n.visit_mut_with(&mut strip);
+        n.body.visit_mut_with(&mut strip);
 
-        let mut stmts: Vec<Stmt> = Vec::with_capacity(n.len() + 4);
+        let mut stmts: Vec<Stmt> = Vec::with_capacity(n.body.len() + 4);
 
         // "use strict";
         if self.config.strict_mode {
-            stmts.push(clone_first_use_directive(n, true).unwrap_or_else(use_strict));
+            stmts.push(clone_first_use_directive(&n.body, true).unwrap_or_else(use_strict));
         }
 
         let ModuleDeclStrip {
@@ -169,7 +169,7 @@ where
                 .map(From::from),
         );
 
-        stmts.extend(n.take().into_iter().filter_map(|item| match item {
+        stmts.extend(n.body.take().into_iter().filter_map(|item| match item {
             ModuleItem::Stmt(stmt) if !stmt.is_directive() => Some(stmt),
             _ => None,
         }));
@@ -256,7 +256,7 @@ where
             .as_arg(),
         );
 
-        *n = vec![
+        n.body = vec![
             quote_ident!(DUMMY_SP.apply_mark(self.unresolved_mark), "define")
                 .as_call(DUMMY_SP, amd_call_args)
                 .into_stmt()
@@ -264,6 +264,10 @@ where
         ];
     }
 
+    fn visit_mut_script(&mut self, _: &mut Script) {
+        // skip script
+    }
+
     fn visit_mut_expr(&mut self, n: &mut Expr) {
         match n {
             Expr::Call(CallExpr {
diff --git a/crates/swc_ecma_transforms_module/src/common_js.rs b/crates/swc_ecma_transforms_module/src/common_js.rs
index 4654f3654f..34167a1f63 100644
--- a/crates/swc_ecma_transforms_module/src/common_js.rs
+++ b/crates/swc_ecma_transforms_module/src/common_js.rs
@@ -90,7 +90,7 @@ where
 {
     noop_visit_mut_type!();
 
-    fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
+    fn visit_mut_module(&mut self, n: &mut Module) {
         let import_interop = self.config.import_interop();
 
         let mut module_map = Default::default();
@@ -98,7 +98,7 @@ where
         let mut has_ts_import_equals = false;
 
         // handle `import foo = require("mod")`
-        n.iter_mut().for_each(|item| {
+        n.body.iter_mut().for_each(|item| {
             if let ModuleItem::ModuleDecl(module_decl) = item {
                 *item = self.handle_ts_import_equals(
                     module_decl.take(),
@@ -109,16 +109,16 @@ where
         });
 
         let mut strip = ModuleDeclStrip::new(self.const_var_kind);
-        n.visit_mut_with(&mut strip);
+        n.body.visit_mut_with(&mut strip);
 
-        let mut stmts: Vec<ModuleItem> = Vec::with_capacity(n.len() + 6);
+        let mut stmts: Vec<ModuleItem> = Vec::with_capacity(n.body.len() + 6);
 
-        stmts.extend(clone_first_use_directive(n, false).map(From::from));
+        stmts.extend(clone_first_use_directive(&n.body, false).map(From::from));
 
         // "use strict";
         if self.config.strict_mode {
             stmts.push(
-                clone_first_use_directive(n, true)
+                clone_first_use_directive(&n.body, true)
                     .unwrap_or_else(use_strict)
                     .into(),
             );
@@ -155,7 +155,7 @@ where
             .map(From::from),
         );
 
-        stmts.extend(n.take().into_iter().filter(|item| match item {
+        stmts.extend(n.body.take().into_iter().filter(|item| match item {
             ModuleItem::Stmt(stmt) => !stmt.is_directive(),
             _ => false,
         }));
@@ -184,7 +184,11 @@ where
             self.config.allow_top_level_this,
         ));
 
-        *n = stmts;
+        n.body = stmts;
+    }
+
+    fn visit_mut_script(&mut self, _: &mut Script) {
+        // skip script
     }
 
     fn visit_mut_expr(&mut self, n: &mut Expr) {


Test patch:
diff --git a/crates/swc/tests/fixture/issues-7xxx/7650/input/.swcrc b/crates/swc/tests/fixture/issues-7xxx/7650/input/.swcrc
new file mode 100644
index 0000000000..96df861930
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7650/input/.swcrc
@@ -0,0 +1,12 @@
+{
+    "jsc": {
+        "target": "es2020",
+        "parser": {
+            "syntax": "typescript"
+        }
+    },
+    "isModule": "unknown",
+    "module": {
+        "type": "commonjs"
+    }
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-7xxx/7650/input/index.js b/crates/swc/tests/fixture/issues-7xxx/7650/input/index.js
new file mode 100644
index 0000000000..045b0b8d01
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7650/input/index.js
@@ -0,0 +1,3 @@
+const a = require('foo')
+console.log(a)
+import('other')
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-7xxx/7650/output/index.js b/crates/swc/tests/fixture/issues-7xxx/7650/output/index.js
new file mode 100644
index 0000000000..65ebb655a3
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7650/output/index.js
@@ -0,0 +1,3 @@
+const a = require('foo');
+console.log(a);
+import('other');