mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-19 16:01:23 +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:
Cargo.toml
crates
backend
cli-support
cli
src
bin
macro-support
macro
shared
src
webidl
src
examples
raytrace-parallel
without-a-bundler-no-modules
without-a-bundler
guide/src
tests/headless
@ -94,25 +94,46 @@ impl TryToTokens for ast::Program {
|
||||
shared::SCHEMA_VERSION,
|
||||
shared::version()
|
||||
);
|
||||
let encoded = encode::encode(self)?;
|
||||
let mut bytes = Vec::new();
|
||||
bytes.push((prefix_json.len() >> 0) as u8);
|
||||
bytes.push((prefix_json.len() >> 8) as u8);
|
||||
bytes.push((prefix_json.len() >> 16) as u8);
|
||||
bytes.push((prefix_json.len() >> 24) as u8);
|
||||
bytes.extend_from_slice(prefix_json.as_bytes());
|
||||
bytes.extend_from_slice(&encode::encode(self)?);
|
||||
bytes.extend_from_slice(&encoded.custom_section);
|
||||
|
||||
let generated_static_length = bytes.len();
|
||||
let generated_static_value = syn::LitByteStr::new(&bytes, Span::call_site());
|
||||
|
||||
// We already consumed the contents of included files when generating
|
||||
// the custom section, but we want to make sure that updates to the
|
||||
// generated files will cause this macro to rerun incrementally. To do
|
||||
// that we use `include_str!` to force rustc to think it has a
|
||||
// dependency on these files. That way when the file changes Cargo will
|
||||
// automatically rerun rustc which will rerun this macro. Other than
|
||||
// this we don't actually need the results of the `include_str!`, so
|
||||
// it's just shoved into an anonymous static.
|
||||
let file_dependencies = encoded.included_files
|
||||
.iter()
|
||||
.map(|file| {
|
||||
let file = file.to_str().unwrap();
|
||||
quote! { include_str!(#file) }
|
||||
});
|
||||
|
||||
(quote! {
|
||||
#[allow(non_upper_case_globals)]
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[link_section = "__wasm_bindgen_unstable"]
|
||||
#[doc(hidden)]
|
||||
#[allow(clippy::all)]
|
||||
pub static #generated_static_name: [u8; #generated_static_length] =
|
||||
*#generated_static_value;
|
||||
pub static #generated_static_name: [u8; #generated_static_length] = {
|
||||
#[doc(hidden)]
|
||||
static _INCLUDED_FILES: &[&str] = &[#(#file_dependencies),*];
|
||||
|
||||
*#generated_static_value
|
||||
};
|
||||
|
||||
})
|
||||
.to_tokens(tokens);
|
||||
|
||||
|
Reference in New Issue
Block a user