rewrite import.meta

This commit is contained in:
velzie 2024-08-09 17:41:34 -04:00
parent a12e1bb539
commit 066777fe94
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
6 changed files with 43 additions and 24 deletions

View file

@ -51,6 +51,7 @@ fn get_config(scramjet: &Object) -> Config {
wrapfn: get_str(config, "wrapfn"), wrapfn: get_str(config, "wrapfn"),
importfn: get_str(config, "importfn"), importfn: get_str(config, "importfn"),
rewritefn: get_str(config, "rewritefn"), rewritefn: get_str(config, "rewritefn"),
metafn: get_str(config, "metafn"),
} }
} }

View file

@ -118,6 +118,7 @@ fn dorewrite(source_text: &str) -> String {
wrapfn: "$wrap".to_string(), wrapfn: "$wrap".to_string(),
importfn: "$import".to_string(), importfn: "$import".to_string(),
rewritefn: "$rewrite".to_string(), rewritefn: "$rewrite".to_string(),
metafn: "$meta".to_string(),
}, },
) )
.as_slice(), .as_slice(),

View file

@ -38,6 +38,7 @@ pub struct Config {
pub wrapfn: String, pub wrapfn: String,
pub importfn: String, pub importfn: String,
pub rewritefn: String, pub rewritefn: String,
pub metafn: String,
pub encode: EncodeFn, pub encode: EncodeFn,
} }
@ -58,6 +59,23 @@ impl Rewriter {
}); });
} }
} }
fn walk_member_expression(&mut self, it: &Expression) -> bool {
if match it {
Expression::Identifier(s) => {
self.rewrite_ident(&s.name, s.span);
true
}
Expression::StaticMemberExpression(s) => self.walk_member_expression(&s.object),
Expression::ComputedMemberExpression(s) => self.walk_member_expression(&s.object),
_ => false,
} {
return true;
}
// TODO: WE SHOULD PROBABLY WALK THE REST OF THE TREE
// walk::walk_expression(self, it);
false
}
} }
impl<'a> Visit<'a> for Rewriter { impl<'a> Visit<'a> for Rewriter {
@ -79,28 +97,7 @@ impl<'a> Visit<'a> for Rewriter {
// we need to rewrite `new Something` to `new (wrapfn(Something))` instead of `new wrapfn(Something)`, that's why there's weird extra code here // we need to rewrite `new Something` to `new (wrapfn(Something))` instead of `new wrapfn(Something)`, that's why there's weird extra code here
fn visit_new_expression(&mut self, it: &oxc_ast::ast::NewExpression<'a>) { fn visit_new_expression(&mut self, it: &oxc_ast::ast::NewExpression<'a>) {
match &it.callee { self.walk_member_expression(&it.callee);
Expression::Identifier(s) => {
self.rewrite_ident(&s.name, s.span);
return;
}
Expression::StaticMemberExpression(s) => match &s.object {
Expression::Identifier(s) => {
self.rewrite_ident(&s.name, s.span);
return;
}
_ => {}
},
Expression::ComputedMemberExpression(s) => match &s.object {
Expression::Identifier(s) => {
self.rewrite_ident(&s.name, s.span);
return;
}
_ => {}
},
_ => {}
}
walk::walk_new_expression(self, it);
} }
fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) { fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) {
self.jschanges.push(JsChange::GenericChange { self.jschanges.push(JsChange::GenericChange {
@ -253,6 +250,15 @@ impl<'a> Visit<'a> for Rewriter {
// then no, don't walk it, we don't care // then no, don't walk it, we don't care
} }
fn visit_meta_property(&mut self, it: &oxc_ast::ast::MetaProperty<'a>) {
if it.meta.name == "import" {
self.jschanges.push(JsChange::GenericChange {
span: it.span,
text: format!("{}(\"{}\")", self.config.metafn, self.base),
});
}
}
fn visit_assignment_expression(&mut self, it: &oxc_ast::ast::AssignmentExpression<'a>) { fn visit_assignment_expression(&mut self, it: &oxc_ast::ast::AssignmentExpression<'a>) {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match &it.left { match &it.left {

View file

@ -1,11 +1,20 @@
import { config, encodeUrl } from "../shared"; import { config, encodeUrl } from "../shared";
export default function (client, self) { export default function (client, self) {
self[config.importfn] = function (base) { self[config.importfn] = function (base: string) {
return function (url) { return function (url: string) {
const resolved = new URL(url, base).href; const resolved = new URL(url, base).href;
return Function(`return import("${encodeUrl(resolved)}")`)(); return Function(`return import("${encodeUrl(resolved)}")`)();
}; };
}; };
self[config.metafn] = function (base: string) {
return {
url: base,
resolve: function (url: string) {
return new URL(url, base).href;
},
};
};
} }

View file

@ -15,6 +15,7 @@ export class ScramjetController {
trysetfn: "$scramjet$tryset", trysetfn: "$scramjet$tryset",
importfn: "$scramjet$import", importfn: "$scramjet$import",
rewritefn: "$scramjet$rewrite", rewritefn: "$scramjet$rewrite",
metafn: "$scramjet$meta",
wasm: "/scramjet.wasm.js", wasm: "/scramjet.wasm.js",
shared: "/scramjet.shared.js", shared: "/scramjet.shared.js",
worker: "/scramjet.worker.js", worker: "/scramjet.worker.js",

1
src/types.d.ts vendored
View file

@ -18,6 +18,7 @@ interface ScramjetConfig {
trysetfn: string; trysetfn: string;
importfn: string; importfn: string;
rewritefn: string; rewritefn: string;
metafn: string;
wasm: string; wasm: string;
shared: string; shared: string;
worker: string; worker: string;