feat(es/optimization): Support default imports for const modules (#7604)

This commit is contained in:
廖应龙 2023-06-30 10:12:45 +08:00 committed by GitHub
parent efc1afd96a
commit ac02b84918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -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);
}
};
}

View File

@ -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);
"#
);