mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-28 20:21:35 +00:00
Implement the local JS snippets RFC
This commit is an implementation of [RFC 6] which enables crates to inline local JS snippets into the final output artifact of `wasm-bindgen`. This is accompanied with a few minor breaking changes which are intended to be relatively minor in practice: * The `module` attribute disallows paths starting with `./` and `../`. It requires paths starting with `/` to actually exist on the filesystem. * The `--browser` flag no longer emits bundler-compatible code, but rather emits an ES module that can be natively loaded into a browser. Otherwise be sure to check out [the RFC][RFC 6] for more details, and otherwise this should implement at least the MVP version of the RFC! Notably at this time JS snippets with `--nodejs` or `--no-modules` are not supported and will unconditionally generate an error. [RFC 6]: https://github.com/rustwasm/rfcs/pull/6 Closes #1311
This commit is contained in:
42
tests/headless/snippets.rs
Normal file
42
tests/headless/snippets.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_test::*;
|
||||
|
||||
#[wasm_bindgen(module = "/tests/headless/snippets1.js")]
|
||||
extern {
|
||||
fn get_two() -> u32;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_get_two() {
|
||||
assert_eq!(get_two(), 2);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(inline_js = "export function get_three() { return 3; }")]
|
||||
extern {
|
||||
fn get_three() -> u32;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_get_three() {
|
||||
assert_eq!(get_three(), 3);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(inline_js = "let a = 0; export function get() { a += 1; return a; }")]
|
||||
extern {
|
||||
#[wasm_bindgen(js_name = get)]
|
||||
fn duplicate1() -> u32;
|
||||
}
|
||||
|
||||
#[wasm_bindgen(inline_js = "let a = 0; export function get() { a += 1; return a; }")]
|
||||
extern {
|
||||
#[wasm_bindgen(js_name = get)]
|
||||
fn duplicate2() -> u32;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn duplicate_inline_not_unified() {
|
||||
assert_eq!(duplicate1(), 1);
|
||||
assert_eq!(duplicate2(), 1);
|
||||
assert_eq!(duplicate1(), 2);
|
||||
assert_eq!(duplicate2(), 2);
|
||||
}
|
Reference in New Issue
Block a user