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:
Alex Crichton
2018-09-28 13:17:37 -07:00
parent 11bcaf42d5
commit 3c14f7a6eb
11 changed files with 172 additions and 2 deletions

View 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.