-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add wasm_bindgen::script_url #3032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
27df7a9
d9dc1c5
34a58b4
64b2fef
787ffc9
1dd0b61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,6 +378,15 @@ impl<'a> Context<'a> { | |
// function. | ||
OutputMode::NoModules { global } => { | ||
js.push_str("const __exports = {};\n"); | ||
js.push_str("let script_src;\n"); | ||
js.push_str( | ||
"\ | ||
if (typeof document === 'undefined') { | ||
script_src = location.href; | ||
} else { | ||
script_src = document.currentScript.src; | ||
}\n", | ||
); | ||
js.push_str("let wasm;\n"); | ||
init = self.gen_init(needs_manual_start, None)?; | ||
footer.push_str(&format!("{} = Object.assign(init, __exports);\n", global)); | ||
|
@@ -701,13 +710,7 @@ impl<'a> Context<'a> { | |
), | ||
OutputMode::NoModules { .. } => "\ | ||
if (typeof input === 'undefined') { | ||
let src; | ||
if (typeof document === 'undefined') { | ||
src = location.href; | ||
} else { | ||
src = document.currentScript.src; | ||
} | ||
input = src.replace(/\\.js$/, '_bg.wasm'); | ||
input = script_src.replace(/\\.js$/, '_bg.wasm'); | ||
}" | ||
.to_string(), | ||
_ => "".to_string(), | ||
|
@@ -3426,6 +3429,22 @@ impl<'a> Context<'a> { | |
format!("wasm.{}", self.export_name_of(memory)) | ||
} | ||
|
||
Intrinsic::ScriptUrl => { | ||
assert_eq!(args.len(), 0); | ||
match self.config.mode { | ||
OutputMode::Web | ||
| OutputMode::Deno | ||
| OutputMode::Node { | ||
experimental_modules: true, | ||
} => format!("import.meta.url"), | ||
OutputMode::Node { | ||
experimental_modules: false, | ||
} => format!("__filename"), | ||
OutputMode::NoModules { .. } => format!("script_src"), | ||
OutputMode::Bundler { .. } => format!("__webpack_public_path__"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a skim of the docs, I don't this returns the path of the current file; I think it returns the path at which the root of the webpack project is hosted. So, this won't work. Webpack does seem to have some support for worker entrypoints, though: https://webpack.js.org/guides/web-workers/. |
||
} | ||
} | ||
|
||
Intrinsic::FunctionTable => { | ||
assert_eq!(args.len(), 0); | ||
let name = self.export_function_table()?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to use
format!
here, you should use.to_owned()
instead.