mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 06:36:08 +03:00
fix(es/module/cjs): Allow re-exports to be lazy (#3856)
This commit is contained in:
parent
4860ea2672
commit
f575b1bc48
@ -620,23 +620,24 @@ impl Fold for CommonJs {
|
||||
}
|
||||
};
|
||||
|
||||
let is_reexport = export.src.is_some()
|
||||
|| scope
|
||||
.idents
|
||||
.contains_key(&(orig.sym.clone(), orig.span.ctxt()));
|
||||
|
||||
drop(scope);
|
||||
|
||||
let old = self.in_top_level;
|
||||
|
||||
// When we are in top level we make import not lazy.
|
||||
let is_top_level = if lazy { !is_reexport } else { true };
|
||||
self.in_top_level = is_top_level;
|
||||
|
||||
let value = match imported {
|
||||
Some(ref imported) => Box::new(
|
||||
imported.clone().unwrap().make_member(orig.clone()),
|
||||
),
|
||||
Some(ref imported) => {
|
||||
let receiver = if lazy {
|
||||
Expr::Call(CallExpr {
|
||||
span: DUMMY_SP,
|
||||
callee: imported.clone().unwrap().as_callee(),
|
||||
args: vec![],
|
||||
type_args: Default::default(),
|
||||
})
|
||||
} else {
|
||||
Expr::Ident(imported.clone().unwrap())
|
||||
};
|
||||
Box::new(receiver.make_member(orig.clone()))
|
||||
}
|
||||
None => Box::new(Expr::Ident(orig.clone()).fold_with(self)),
|
||||
};
|
||||
|
||||
|
@ -3842,6 +3842,38 @@ function use() {
|
||||
"#
|
||||
);
|
||||
|
||||
// lazy_export_named
|
||||
test!(
|
||||
syntax(),
|
||||
|_| tr(Config {
|
||||
lazy: Lazy::Bool(true),
|
||||
..Default::default()
|
||||
}),
|
||||
lazy_export_named,
|
||||
r#"
|
||||
export { named1 } from "external";
|
||||
"#,
|
||||
r#"
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "named1", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _external().named1;
|
||||
}
|
||||
});
|
||||
function _external() {
|
||||
const data = require("external");
|
||||
_external = function() {
|
||||
return data;
|
||||
};
|
||||
return data;
|
||||
}
|
||||
"#
|
||||
);
|
||||
|
||||
// lazy_local_reexport_named
|
||||
test!(
|
||||
syntax(),
|
||||
|
Loading…
Reference in New Issue
Block a user