fix(es/module/cjs): Allow re-exports to be lazy (#3856)

This commit is contained in:
Guillaume Malette 2022-03-07 15:39:45 -05:00 committed by GitHub
parent 4860ea2672
commit f575b1bc48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 12 deletions

View File

@ -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); drop(scope);
let old = self.in_top_level; 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 { let value = match imported {
Some(ref imported) => Box::new( Some(ref imported) => {
imported.clone().unwrap().make_member(orig.clone()), 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)), None => Box::new(Expr::Ident(orig.clone()).fold_with(self)),
}; };

View File

@ -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 // lazy_local_reexport_named
test!( test!(
syntax(), syntax(),