Repository: swc-project/swc (rust)

Issue:
Const module default imports
### Describe the feature

Default imports from const modules shouldn't panic. Instead, they should be substituted by the `"default"` key of the const module's definition
```typescript
import something from 'my-mod';
console.log(something);
```
given the following config:
```json
{
  "jsc": {
    "transform": {
      "constModules": {
        "my-mod": { "default": "\"substitution\"" }
      }
    }
  }
}
```
should be transpiled to
```typescript
console.log("substitution");
```
The panic can be found here:
https://github.com/swc-project/swc/blob/be33efaecdd00491ab68d31e65365c447d42d646/crates/swc_ecma_transforms_optimization/src/const_modules.rs#L124

More than happy to open a PR if nobody is working on this

### Babel plugin or link to the feature description

_No response_

### Additional context

[The spec treats default exports as exports with an `[[ExportName]]` of `"default"`](https://tc39.es/ecma262/multipage/ecmascript-language-scripts-and-modules.html#sec-exports)

Pull request description:
feat(es/optimization): Support `default` imports for const modules
Fixes #7601

<!-- 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):**


Patch:
diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs
index 5137c055b8..c8d4b3b0f8 100644
--- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs
+++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs
@@ -6,7 +6,7 @@ use std::{
 use dashmap::DashMap;
 use once_cell::sync::Lazy;
 use rustc_hash::FxHashMap;
-use swc_atoms::JsWord;
+use swc_atoms::{js_word, JsWord};
 use swc_common::{
     errors::HANDLER,
     sync::Lrc,
@@ -120,8 +120,18 @@ impl VisitMut for ConstModules {
                             ImportSpecifier::Namespace(ref s) => {
                                 self.scope.namespace.insert(s.local.to_id());
                             }
-                            ImportSpecifier::Default(..) => {
-                                panic!("const_module does not support default import")
+                            ImportSpecifier::Default(ref s) => {
+                                let imported = &s.local.sym;
+                                let default_import_key = js_word!("default");
+                                let value =
+                                    entry.get(&default_import_key).cloned().unwrap_or_else(|| {
+                                        panic!(
+                                            "The requested const_module `{}` does not provide \
+                                             default export",
+                                            import.src.value
+                                        )
+                                    });
+                                self.scope.imported.insert(imported.clone(), value);
                             }
                         };
                     }


Test patch:
diff --git a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
index 50d4beb350..52793e908c 100644
--- a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
+++ b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
@@ -147,3 +147,16 @@ console.log({ bar });
 console.log({ bar: true });
 "#
 );
+
+test!(
+    ::swc_ecma_parser::Syntax::default(),
+    |tester| tr(tester, &[("my-mod", &[("default", "true")])]),
+    default_import_issue_7601,
+    r#"
+import something from 'my-mod';
+console.log(something);
+"#,
+    r#"
+console.log(true);
+"#
+);