Skip to content

Commit

Permalink
Return absolute URLs and add node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Sep 26, 2022
1 parent 66b5b91 commit 7697dea
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl<'a> Context<'a> {
if (typeof document === 'undefined') {
script_src = location.href;
} else {
script_src = document.currentScript.src;
script_src = new URL(document.currentScript.src, location.href).toString();
}\n",
);
js.push_str("let wasm;\n");
Expand Down Expand Up @@ -3131,7 +3131,7 @@ impl<'a> Context<'a> {
} => "import.meta.url",
OutputMode::Node {
experimental_modules: false,
} => "__filename",
} => "require('url').pathToFileURL(__filename)",
OutputMode::NoModules { .. } => "script_src",
};
Ok(format!("new URL('{}', {}).toString()", path, base))
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn default_module_path_target_no_modules() {
if (typeof document === 'undefined') {
script_src = location.href;
} else {
script_src = document.currentScript.src;
script_src = new URL(document.currentScript.src, location.href).toString();
}",
));
assert!(contents.contains(
Expand Down
4 changes: 2 additions & 2 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
}

/// This macro adds a linked module by module, raw_module or inline_js attribute.
/// It expands to a String containing a link to that module. This link may be relative and
/// is suitable for dynamic imports, or creating workers or worklets:
/// It expands to a String containing a URL to that module. This URL can be used
/// to create workers or worklets, for example:
/// ```
/// use web_sys::Worker;
/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
Expand Down
4 changes: 4 additions & 0 deletions tests/wasm/link_to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const fs = require('fs');
const url = require('url');

exports.read_file = (str) => fs.readFileSync(url.fileURLToPath(str), "utf8");
30 changes: 30 additions & 0 deletions tests/wasm/link_to.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "/tests/wasm/link_to.js")]
extern "C" {
#[wasm_bindgen(catch)]
fn read_file(url: &str) -> Result<String, JsValue>;
}

#[wasm_bindgen_test]
fn test_module() {
let link = wasm_bindgen::link_to!(module = "/tests/wasm/linked_module.js");
assert_eq!(read_file(&link).unwrap(), "// linked module\n");
}

#[wasm_bindgen_test]
fn test_raw_module() {
let link = wasm_bindgen::link_to!(raw_module = "not-found.js");
assert!(read_file(&link).is_err());
}

#[wasm_bindgen_test]
fn test_inline_js() {
// Test two invocations to ensure that snippet indices from different
// Program structs are offset correctly.
let link1 = wasm_bindgen::link_to!(inline_js = "// inline js 1\n");
let link2 = wasm_bindgen::link_to!(inline_js = "// inline js 2\n");
assert_eq!(read_file(&link1).unwrap(), "// inline js 1\n");
assert_eq!(read_file(&link2).unwrap(), "// inline js 2\n");
}
1 change: 1 addition & 0 deletions tests/wasm/linked_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// linked module
1 change: 1 addition & 0 deletions tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod intrinsics;
pub mod js_keywords;
pub mod js_objects;
pub mod jscast;
pub mod link_to;
pub mod math;
pub mod no_shims;
pub mod node;
Expand Down

0 comments on commit 7697dea

Please sign in to comment.