Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SystemJS imports #6879

Merged
merged 3 commits into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -57,7 +57,7 @@ System.register([
case 0:
return [
4,
import("./test") // TWO
_context.import("./test") // TWO
];
case 1:
req = _state.sent();
Expand All @@ -78,7 +78,7 @@ System.register([
case 0:
return [
4,
import("./test") // THREE
_context.import("./test") // THREE
];
case 1:
req = _state.sent();
Expand All @@ -100,7 +100,7 @@ System.register([
case 0:
return [
4,
import("./test") // FOUR
_context.import("./test") // FOUR
];
case 1:
req = _state.sent();
Expand All @@ -120,7 +120,7 @@ System.register([
case 0:
return [
4,
import("./test") // FIVE
_context.import("./test") // FIVE
];
case 1:
req = _state.sent();
Expand Down
Expand Up @@ -57,7 +57,7 @@ System.register([
case 0:
return [
4,
import("./test") // TWO
_context.import("./test") // TWO
];
case 1:
req = _state.sent();
Expand All @@ -78,7 +78,7 @@ System.register([
case 0:
return [
4,
import("./test") // THREE
_context.import("./test") // THREE
];
case 1:
req = _state.sent();
Expand All @@ -100,7 +100,7 @@ System.register([
case 0:
return [
4,
import("./test") // FOUR
_context.import("./test") // FOUR
];
case 1:
req = _state.sent();
Expand All @@ -120,7 +120,7 @@ System.register([
case 0:
return [
4,
import("./test") // FIVE
_context.import("./test") // FIVE
];
case 1:
req = _state.sent();
Expand Down
Expand Up @@ -29,30 +29,30 @@ 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
;
})
});
_export("cl2", cl2 = class cl2 {
constructor(){
this.p = {
m: /*#__PURE__*/ _async_to_generator(function*() {
const req = yield import('./test') // FOUR
const req = yield _context.import('./test') // FOUR
;
})
};
}
});
_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() {
Expand Down
Expand Up @@ -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;
}();
Expand All @@ -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;
}());
Expand Down
Expand Up @@ -28,7 +28,7 @@ System.register([
_class_call_check(this, D);
}
return D.prototype.method = function() {
import("./0");
_context.import("./0");
}, D;
}());
}
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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());
});
}
Expand All @@ -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());
});
}
Expand Down
Expand Up @@ -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());
});
}
});
Expand Down
Expand Up @@ -5,7 +5,7 @@ System.register([], function(_export, _context) {
setters: [],
execute: function() {
if (_context.meta.foo) {
import.meta.foo();
_context.meta.foo();
}
}
};
Expand Down
Expand Up @@ -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();
}
};
});
30 changes: 25 additions & 5 deletions crates/swc_ecma_transforms_module/src/system_js.rs
Expand Up @@ -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) => {
Expand All @@ -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 => {
Expand All @@ -574,9 +589,12 @@ impl Fold for SystemJs {
_ => Expr::MetaProp(meta_prop_expr),
},
Expr::Await(await_expr) => {
let await_expr = await_expr.fold_children_with(self);
vasilev-alex marked this conversation as resolved.
Show resolved Hide resolved

if self.enter_async_fn == 0 {
self.tla = true;
}

Expr::Await(await_expr)
}
Expr::This(this_expr) => {
Expand All @@ -585,7 +603,7 @@ impl Fold for SystemJs {
}
Expr::This(this_expr)
}
_ => expr.fold_children_with(self),
_ => expr,
}
}

Expand All @@ -610,6 +628,8 @@ impl Fold for SystemJs {
}

fn fold_prop(&mut self, prop: Prop) -> Prop {
let prop = prop.fold_children_with(self);
kdy1 marked this conversation as resolved.
Show resolved Hide resolved

match prop {
Prop::Shorthand(shorthand) => Prop::KeyValue(KeyValueProp {
key: PropName::Ident(shorthand.clone()),
Expand Down
29 changes: 29 additions & 0 deletions crates/swc_ecma_transforms_module/tests/system_js.rs
Expand Up @@ -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")]
Expand Down