Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
//! A bundle that carries its files (docs/playables.feature PA13): a picture, sound or model a module imports becomes a
//! data URL in the script, and a `.bin` table its bytes, so the one page loads nothing (an ad network refuses a page
//! that makes a request). Ahead of the script resolver, which otherwise hands media back as their paths.
use rolldown_common::ModuleType;
use rolldown_plugin::{HookLoadArgs, HookLoadOutput, HookLoadReturn, HookUsage, Plugin, SharedLoadPluginContext};
use std::borrow::Cow;
use std::path::Path;
/// The files carried, by extension, with their media type
const TYPES: &[(&str, &str)] = &[
("png", "image/png"),
("jpg", "image/jpeg"),
("jpeg", "image/jpeg"),
("webp", "image/webp"),
("gif", "image/gif"),
("svg", "image/svg+xml"),
("mp3", "audio/mpeg"),
("m4a", "audio/mp4"),
("ogg", "audio/ogg"),
("wav", "audio/wav"),
("glb", "model/gltf-binary"),
("woff2", "font/woff2"),
("woff", "font/woff"),
("ttf", "font/ttf"),
("otf", "font/otf"),
];
#[derive(Debug)]
pub struct Inline;
/// Standard base64, padded (the base64 crate, as raea's other crates use it)
pub fn base64(bytes: &[u8]) -> String {
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(bytes)
}
impl Plugin for Inline {
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("raea:inline")
}
fn register_hook_usage(&self) -> HookUsage {
HookUsage::Load
}
async fn load(&self, _ctx: SharedLoadPluginContext, args: &HookLoadArgs<'_>) -> HookLoadReturn {
let path = args.id.split(['?', '#']).next().unwrap_or(args.id);
let Some(ext) = Path::new(path).extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase) else { return Ok(None) };
let code = if ext == "bin" {
let b = std::fs::read(path)?;
format!("export default Uint8Array.from(atob({:?}), (c) => c.charCodeAt(0));\n", base64(&b))
} else if let Some((_, mime)) = TYPES.iter().find(|(e, _)| *e == ext) {
let b = std::fs::read(path)?;
format!("export default {:?};\n", format!("data:{mime};base64,{}", base64(&b)))
} else {
return Ok(None);
};
Ok(Some(HookLoadOutput { code: code.into(), module_type: Some(ModuleType::Js), ..Default::default() }))
}
}
#[cfg(test)]
mod tests {
#[test]
fn base64_pads_as_the_standard_does() {
assert_eq!(super::base64(b""), "");
assert_eq!(super::base64(b"f"), "Zg==");
assert_eq!(super::base64(b"fo"), "Zm8=");
assert_eq!(super::base64(b"foo"), "Zm9v");
assert_eq!(super::base64(b"foobar"), "Zm9vYmFy");
}
}