Skip to content

Commit

Permalink
fix: make sure that we can use various exts as an entrypoint when bun…
Browse files Browse the repository at this point in the history
…dling (#425)

* fix(base): `mjs` extension support

* stamp: polishing

* chore: make sure that we can use various exts as an entrypoint when bundling
  • Loading branch information
nyannyacha authored Oct 7, 2024
1 parent 5bd54ed commit 5f87d9a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 51 deletions.
23 changes: 13 additions & 10 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,28 +270,33 @@ where
let drop_token = CancellationToken::default();

let base_dir_path = std::env::current_dir().map(|p| p.join(&service_path))?;
let base_url = Url::from_directory_path(&base_dir_path).unwrap();

let is_user_worker = conf.is_user_worker();
let Ok(mut main_module_url) = Url::from_directory_path(&base_dir_path) else {
bail!(
"malformed base directory: {}",
base_dir_path.to_string_lossy()
);
};

let potential_exts = vec!["ts", "tsx", "js", "jsx"];
let mut main_module_url = base_url.join("index.ts")?;
static POTENTIAL_EXTS: &[&str] = &["ts", "tsx", "js", "mjs", "jsx"];

for potential_ext in potential_exts {
main_module_url = base_url.join(format!("index.{}", potential_ext).as_str())?;
for ext in POTENTIAL_EXTS.iter() {
main_module_url = main_module_url.join(format!("index.{}", ext).as_str())?;
if main_module_url.to_file_path().unwrap().exists() {
break;
}
}

let is_user_worker = conf.is_user_worker();
let is_some_entry_point = maybe_entrypoint.is_some();

if is_some_entry_point {
main_module_url = Url::parse(&maybe_entrypoint.unwrap())?;
}

let mut net_access_disabled = false;
let mut allow_net = None;
let mut allow_remote_modules = true;

if is_user_worker {
let user_conf = conf.as_user_worker().unwrap();

Expand Down Expand Up @@ -329,9 +334,7 @@ where
emitter_factory.set_decorator_type(maybe_decorator);

if let Some(jsx_import_source_config) = maybe_jsx_import_source_config.clone() {
emitter_factory
.set_jsx_import_source(jsx_import_source_config)
.await;
emitter_factory.set_jsx_import_source(jsx_import_source_config);
}

emitter_factory.set_import_map(load_import_map(import_map_path.clone())?);
Expand Down
4 changes: 4 additions & 0 deletions crates/base/test_cases/eszip-various-ext/serve/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deno.serve(req => {
console.log(req.headers.get("content-type"));
return Response.json(<div>meow</div>);
});
4 changes: 4 additions & 0 deletions crates/base/test_cases/eszip-various-ext/serve/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deno.serve(req => {
console.log(req.headers.get("content-type"));
return new Response("meow");
});
4 changes: 4 additions & 0 deletions crates/base/test_cases/eszip-various-ext/serve/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deno.serve((req: Request) => {
console.log(req.headers.get("content-type"));
return new Response("meow");
});
4 changes: 4 additions & 0 deletions crates/base/test_cases/eszip-various-ext/serve/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deno.serve((req: Request) => {
console.log(req.headers.get("content-type"));
return Response.json(<div>meow</div>);
});
118 changes: 78 additions & 40 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#[path = "../src/utils/integration_test_helper.rs"]
mod integration_test_helper;

use deno_config::JsxImportSourceConfig;
use http_v02 as http;
use hyper_v014 as hyper;
use reqwest_v011 as reqwest;
use sb_graph::{emitter::EmitterFactory, generate_binary_eszip, EszipPayloadKind};
use url::Url;

use std::{
borrow::Cow,
Expand Down Expand Up @@ -2428,51 +2430,40 @@ async fn test_js_entrypoint() {
TerminationToken::new()
);
}
}

let get_eszip_buf = |path: &'static str| async move {
generate_binary_eszip(
PathBuf::from(path),
#[allow(clippy::arc_with_non_send_sync)]
Arc::new(EmitterFactory::new()),
None,
None,
None,
)
.await
.unwrap()
.into_bytes()
};
#[tokio::test]
#[serial]
async fn test_should_be_able_to_bundle_against_various_exts() {
let get_eszip_buf = |path: &str| {
let path = path.to_string();
let mut emitter_factory = EmitterFactory::new();

{
let buf = get_eszip_buf("./test_cases/eszip-js/serve-js/index.js").await;
let client = Client::new();
let req = client
.request(
Method::POST,
format!("http://localhost:{}/meow", NON_SECURE_PORT),
)
.body(buf);
emitter_factory.set_jsx_import_source(JsxImportSourceConfig {
default_specifier: Some("https://esm.sh/preact".to_string()),
default_types_specifier: None,
module: "jsx-runtime".to_string(),
base_url: Url::from_file_path(std::env::current_dir().unwrap()).unwrap(),
});

integration_test!(
"./test_cases/main_eszip",
NON_SECURE_PORT,
"",
None,
None,
Some(req),
None,
(|resp| async {
let resp = resp.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let msg = resp.text().await.unwrap();
assert_eq!(msg, "meow");
}),
TerminationToken::new()
);
}
async {
generate_binary_eszip(
PathBuf::from(path),
#[allow(clippy::arc_with_non_send_sync)]
Arc::new(emitter_factory),
None,
None,
None,
)
.await
.unwrap()
.into_bytes()
}
};

{
let buf = get_eszip_buf("./test_cases/eszip-js/npm-supabase-js/index.js").await;
let buf = get_eszip_buf("./test_cases/eszip-various-ext/npm-supabase/index.js").await;

let client = Client::new();
let req = client
.request(
Expand All @@ -2498,6 +2489,53 @@ async fn test_js_entrypoint() {
TerminationToken::new()
);
}

let test_serve_simple_fn = |ext: &'static str, expected: &'static [u8]| {
let ext = ext.to_string();
let expected = expected.to_vec();

async move {
let buf = get_eszip_buf(&format!(
"./test_cases/eszip-various-ext/serve/index.{}",
ext
))
.await;

let client = Client::new();
let req = client
.request(
Method::POST,
format!("http://localhost:{}/meow", NON_SECURE_PORT),
)
.body(buf);

integration_test!(
"./test_cases/main_eszip",
NON_SECURE_PORT,
"",
None,
None,
Some(req),
None,
(|resp| async move {
let resp = resp.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let msg = resp.bytes().await.unwrap();
assert_eq!(msg, expected);
}),
TerminationToken::new()
);
}
};

test_serve_simple_fn("ts", b"meow").await;
test_serve_simple_fn("js", b"meow").await;
test_serve_simple_fn("mjs", b"meow").await;

static REACT_RESULT: &str = r#"{"type":"div","props":{"children":"meow"},"__k":null,"__":null,"__b":0,"__e":null,"__c":null,"__v":-1,"__i":-1,"__u":0}"#;

test_serve_simple_fn("jsx", REACT_RESULT.as_bytes()).await;
test_serve_simple_fn("tsx", REACT_RESULT.as_bytes()).await;
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/sb_graph/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl EmitterFactory {
.unwrap()
}

pub async fn set_jsx_import_source(&mut self, config: JsxImportSourceConfig) {
pub fn set_jsx_import_source(&mut self, config: JsxImportSourceConfig) {
self.jsx_import_source_config = Some(config);
}

Expand Down

0 comments on commit 5f87d9a

Please sign in to comment.