Skip to content

Commit 7697dea

Browse files
committed
Return absolute URLs and add node tests
1 parent 66b5b91 commit 7697dea

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'a> Context<'a> {
384384
if (typeof document === 'undefined') {
385385
script_src = location.href;
386386
} else {
387-
script_src = document.currentScript.src;
387+
script_src = new URL(document.currentScript.src, location.href).toString();
388388
}\n",
389389
);
390390
js.push_str("let wasm;\n");
@@ -3131,7 +3131,7 @@ impl<'a> Context<'a> {
31313131
} => "import.meta.url",
31323132
OutputMode::Node {
31333133
experimental_modules: false,
3134-
} => "__filename",
3134+
} => "require('url').pathToFileURL(__filename)",
31353135
OutputMode::NoModules { .. } => "script_src",
31363136
};
31373137
Ok(format!("new URL('{}', {}).toString()", path, base))

crates/cli/tests/wasm-bindgen/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn default_module_path_target_no_modules() {
263263
if (typeof document === 'undefined') {
264264
script_src = location.href;
265265
} else {
266-
script_src = document.currentScript.src;
266+
script_src = new URL(document.currentScript.src, location.href).toString();
267267
}",
268268
));
269269
assert!(contents.contains(

crates/macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
1919
}
2020

2121
/// This macro adds a linked module by module, raw_module or inline_js attribute.
22-
/// It expands to a String containing a link to that module. This link may be relative and
23-
/// is suitable for dynamic imports, or creating workers or worklets:
22+
/// It expands to a String containing a URL to that module. This URL can be used
23+
/// to create workers or worklets, for example:
2424
/// ```
2525
/// use web_sys::Worker;
2626
/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));

tests/wasm/link_to.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fs = require('fs');
2+
const url = require('url');
3+
4+
exports.read_file = (str) => fs.readFileSync(url.fileURLToPath(str), "utf8");

tests/wasm/link_to.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen_test::*;
3+
4+
#[wasm_bindgen(module = "/tests/wasm/link_to.js")]
5+
extern "C" {
6+
#[wasm_bindgen(catch)]
7+
fn read_file(url: &str) -> Result<String, JsValue>;
8+
}
9+
10+
#[wasm_bindgen_test]
11+
fn test_module() {
12+
let link = wasm_bindgen::link_to!(module = "/tests/wasm/linked_module.js");
13+
assert_eq!(read_file(&link).unwrap(), "// linked module\n");
14+
}
15+
16+
#[wasm_bindgen_test]
17+
fn test_raw_module() {
18+
let link = wasm_bindgen::link_to!(raw_module = "not-found.js");
19+
assert!(read_file(&link).is_err());
20+
}
21+
22+
#[wasm_bindgen_test]
23+
fn test_inline_js() {
24+
// Test two invocations to ensure that snippet indices from different
25+
// Program structs are offset correctly.
26+
let link1 = wasm_bindgen::link_to!(inline_js = "// inline js 1\n");
27+
let link2 = wasm_bindgen::link_to!(inline_js = "// inline js 2\n");
28+
assert_eq!(read_file(&link1).unwrap(), "// inline js 1\n");
29+
assert_eq!(read_file(&link2).unwrap(), "// inline js 2\n");
30+
}

tests/wasm/linked_module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// linked module

tests/wasm/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod intrinsics;
3232
pub mod js_keywords;
3333
pub mod js_objects;
3434
pub mod jscast;
35+
pub mod link_to;
3536
pub mod math;
3637
pub mod no_shims;
3738
pub mod node;

0 commit comments

Comments
 (0)