fix(es/module): Fix SystemJS imports (#6879)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/5922.
This commit is contained in:
Alex Vasilev 2023-02-07 19:07:46 +03:00 committed by GitHub
parent 74070935a9
commit 85fffdad02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 74 additions and 27 deletions

View File

@ -57,7 +57,7 @@ System.register([
case 0:
return [
4,
import("./test") // TWO
_context.import("./test") // TWO
];
case 1:
req = _state.sent();
@ -78,7 +78,7 @@ System.register([
case 0:
return [
4,
import("./test") // THREE
_context.import("./test") // THREE
];
case 1:
req = _state.sent();
@ -100,7 +100,7 @@ System.register([
case 0:
return [
4,
import("./test") // FOUR
_context.import("./test") // FOUR
];
case 1:
req = _state.sent();
@ -120,7 +120,7 @@ System.register([
case 0:
return [
4,
import("./test") // FIVE
_context.import("./test") // FIVE
];
case 1:
req = _state.sent();

View File

@ -57,7 +57,7 @@ System.register([
case 0:
return [
4,
import("./test") // TWO
_context.import("./test") // TWO
];
case 1:
req = _state.sent();
@ -78,7 +78,7 @@ System.register([
case 0:
return [
4,
import("./test") // THREE
_context.import("./test") // THREE
];
case 1:
req = _state.sent();
@ -100,7 +100,7 @@ System.register([
case 0:
return [
4,
import("./test") // FOUR
_context.import("./test") // FOUR
];
case 1:
req = _state.sent();
@ -120,7 +120,7 @@ System.register([
case 0:
return [
4,
import("./test") // FIVE
_context.import("./test") // FIVE
];
case 1:
req = _state.sent();

View File

@ -29,14 +29,14 @@ System.register([
_export("cl1", cl1 = class cl1 {
m() {
return _async_to_generator(function*() {
const req = yield import('./test') // TWO
const req = yield _context.import('./test') // TWO
;
})();
}
});
_export("obj", obj = {
m: /*#__PURE__*/ _async_to_generator(function*() {
const req = yield import('./test') // THREE
const req = yield _context.import('./test') // THREE
;
})
});
@ -44,7 +44,7 @@ System.register([
constructor(){
this.p = {
m: /*#__PURE__*/ _async_to_generator(function*() {
const req = yield import('./test') // FOUR
const req = yield _context.import('./test') // FOUR
;
})
};
@ -52,7 +52,7 @@ System.register([
});
_export("l", l = function() {
var _ref = _async_to_generator(function*() {
const req = yield import('./test') // FIVE
const req = yield _context.import('./test') // FIVE
;
});
return function l() {

View File

@ -39,7 +39,7 @@ System.register([
}
var _proto = C.prototype;
_proto.method = function method() {
var loadAsync = import("./0");
var loadAsync = _context.import("./0");
};
return C;
}();
@ -50,7 +50,7 @@ System.register([
}
var _proto = D.prototype;
_proto.method = function method() {
var loadAsync = import("./0");
var loadAsync = _context.import("./0");
};
return D;
}());

View File

@ -28,7 +28,7 @@ System.register([
_class_call_check(this, D);
}
return D.prototype.method = function() {
import("./0");
_context.import("./0");
}, D;
}());
}

View File

@ -18,7 +18,7 @@ System.register([], function(_export, _context) {
System.register([], function(_export, _context) {
"use strict";
async function foo() {
class C extends (await import("./0")).B {
class C extends (await _context.import("./0")).B {
}
var c = new C();
c.print();

View File

@ -15,7 +15,7 @@ System.register([], function(_export, _context) {
System.register([], function(_export, _context) {
"use strict";
async function foo() {
class C extends (await import("./0")).B {
class C extends (await _context.import("./0")).B {
}
new C().print();
}

View File

@ -56,7 +56,7 @@ System.register([], function(_export, _context) {
console.log(Zero.foo());
}, async (err)=>{
console.log(err);
let one = await import("./1");
let one = await _context.import("./1");
console.log(one.backup());
});
}
@ -69,7 +69,7 @@ System.register([], function(_export, _context) {
console.log(Zero.foo());
}, async (err)=>{
console.log(err);
let one = await import("./1");
let one = await _context.import("./1");
console.log(one.backup());
});
}

View File

@ -43,7 +43,7 @@ System.register([], function(_export, _context) {
_context.import("./0"), this.myModule.then((Zero)=>{
console.log(Zero.foo());
}, async (err)=>{
console.log(err), console.log((await import("./1")).backup());
console.log(err), console.log((await _context.import("./1")).backup());
});
}
});

View File

@ -5,7 +5,7 @@ System.register([], function(_export, _context) {
setters: [],
execute: function() {
if (_context.meta.foo) {
import.meta.foo();
_context.meta.foo();
}
}
};

View File

@ -3,7 +3,7 @@ System.register([], function(_export, _context) {
return {
setters: [],
execute: function() {
_context.meta.foo && import.meta.foo();
_context.meta.foo && _context.meta.foo();
}
};
});

View File

@ -537,7 +537,25 @@ impl SystemJs {
impl Fold for SystemJs {
noop_fold_type!();
fn fold_call_expr(&mut self, expr: CallExpr) -> CallExpr {
let expr = expr.fold_children_with(self);
match expr.callee {
Callee::Import(_) => CallExpr {
callee: self
.context_ident
.clone()
.make_member(quote_ident!("import"))
.as_callee(),
..expr
},
_ => expr,
}
}
fn fold_expr(&mut self, expr: Expr) -> Expr {
let expr = expr.fold_children_with(self);
match expr {
Expr::Ident(ident) => self.fold_module_name_ident(ident),
Expr::Assign(assign) => {
@ -562,10 +580,7 @@ impl Fold for SystemJs {
.as_callee(),
..call
}),
_ => Expr::Call(CallExpr {
args: call.args.fold_with(self),
..call
}),
_ => Expr::Call(call),
},
Expr::MetaProp(meta_prop_expr) => match meta_prop_expr.kind {
MetaPropKind::ImportMeta => {
@ -577,6 +592,7 @@ impl Fold for SystemJs {
if self.enter_async_fn == 0 {
self.tla = true;
}
Expr::Await(await_expr)
}
Expr::This(this_expr) => {
@ -585,7 +601,7 @@ impl Fold for SystemJs {
}
Expr::This(this_expr)
}
_ => expr.fold_children_with(self),
_ => expr,
}
}
@ -610,6 +626,8 @@ impl Fold for SystemJs {
}
fn fold_prop(&mut self, prop: Prop) -> Prop {
let prop = prop.fold_children_with(self);
match prop {
Prop::Shorthand(shorthand) => Prop::KeyValue(KeyValueProp {
key: PropName::Ident(shorthand.clone()),

View File

@ -142,6 +142,35 @@ test!(
});"#
);
test!(
syntax(),
|tester| tr(
tester,
Config {
..Default::default()
}
),
imports,
r#"
import.meta.url;
import.meta.fn();
await import('./test2');
"#,
r#"
System.register([], function(_export, _context) {
"use strict";
return {
setters: [],
execute: async function() {
_context.meta.url;
_context.meta.fn();
await _context.import('./test2');
}
};
});
"#
);
// TODO: test get-module-name-option, tla
#[testing::fixture("tests/fixture/systemjs/**/input.mjs")]