diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index c10f21522792..e25f1c316dfb 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -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"); @@ -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)) diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index 0bd6944a2c1d..dfd36a154c60 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -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")); diff --git a/tests/wasm/link_to.js b/tests/wasm/link_to.js new file mode 100644 index 000000000000..3028a6ae8a60 --- /dev/null +++ b/tests/wasm/link_to.js @@ -0,0 +1,4 @@ +const fs = require('fs'); +const url = require('url'); + +exports.read_file = (str) => fs.readFileSync(url.fileURLToPath(str), "utf8"); diff --git a/tests/wasm/link_to.rs b/tests/wasm/link_to.rs new file mode 100644 index 000000000000..bfa5c0e6e34e --- /dev/null +++ b/tests/wasm/link_to.rs @@ -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; +} + +#[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"); +} diff --git a/tests/wasm/linked_module.js b/tests/wasm/linked_module.js new file mode 100644 index 000000000000..b979535b28e5 --- /dev/null +++ b/tests/wasm/linked_module.js @@ -0,0 +1 @@ +// linked module diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs index 51e6dcafed56..599528619572 100644 --- a/tests/wasm/main.rs +++ b/tests/wasm/main.rs @@ -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;