mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-24 02:01:35 +00:00
Implement a polyfill
attribute for imports
Allow using imported APIs under alternative names, such as prefixed names, for web APIs when the exact API differs across browsers.
This commit is contained in:
24
guide/src/reference/attributes/on-js-imports/polyfill.md
Normal file
24
guide/src/reference/attributes/on-js-imports/polyfill.md
Normal file
@ -0,0 +1,24 @@
|
||||
# Polyfilling APIs
|
||||
|
||||
In JS new APIs often have polyfills via different names in various contexts. For
|
||||
example the `AudioContext` API is known as `webkitAudioContext` in Safari at the
|
||||
time of this writing. The `polyfill` attribute indicates these alternative
|
||||
names.
|
||||
|
||||
For example to use `AudioContext` you might do:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
#[wasm_bindgen(polyfill = webkitAudioContext)]
|
||||
type AudioContext;
|
||||
|
||||
// methods on `AudioContext` ...
|
||||
}
|
||||
```
|
||||
|
||||
Whenever `AudioContext` is used it'll use `AudioContext` if the global namespace
|
||||
defines it or alternatively it'll fall back to `webkitAudioContext`.
|
||||
|
||||
Note that `polyfill` cannot be used with `module = "..."` or `js_namespace =
|
||||
...`, so it's basically limited to web-platform APIs today.
|
Reference in New Issue
Block a user