fix(es/modules): Allow using dynamic import with a name from another import (#3390)

This commit is contained in:
RiESAEX 2022-01-28 14:36:20 +08:00 committed by GitHub
parent 6690354f79
commit 1dcc188dd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -506,13 +506,21 @@ impl Scope {
Expr::Call(CallExpr {
span,
callee: Callee::Import(_),
args,
mut args,
..
}) if !folder.config().ignore_dynamic
// TODO: import assertion
&& args.len() == 1 =>
{
folder.make_dynamic_import(span, args)
let expr = match *(args.pop().unwrap().expr) {
Expr::Ident(ident) => match Self::fold_ident(folder, ident) {
Ok(expr) => expr,
Err(ident) => Expr::Ident(ident),
},
expr => expr,
};
folder.make_dynamic_import(span, vec![expr.as_arg()])
}
Expr::Call(CallExpr {

View File

@ -4283,6 +4283,41 @@ test!(
}
"
);
test!(
syntax(),
|_| tr(Config {
..Default::default()
}),
issue_3246_1,
"import { foo } from 'bar';
import(foo);
",
r#""use strict";
var _bar = require("bar");
Promise.resolve().then(function() {
return _interopRequireWildcard(require(_bar.foo));
});
"#
);
test!(
syntax(),
|_| tr(Config {
..Default::default()
}),
issue_3246_2,
"import foo from 'bar';
import(foo);
",
r#""use strict";
var _bar = _interopRequireDefault(require("bar"));
Promise.resolve().then(function() {
return _interopRequireWildcard(require(_bar.default));
});
"#
);
test!(
syntax(),