Skip to content

armgit / raea

Public Imported from GitHub
0

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.

12.2 KB
Raw Blame
//! A composition's bundle (raea's bundle_fresh), with the edit's own packages: its sources are copied into a
//! work folder whose node_modules is the engine's, so `import { motion } from "motion/react"` found nothing the edit
//! had installed. A bare import from the edit's sources resolves from the edit's own folder first (its node_modules,
//! then its parents', as Node and Vite would), then the engine's; the packages the engine carries (react, mediabunny,
//! bevy-react...) resolve to its copies from any package, so a bundle has one React however the edit installed its own.
//! A package the edit's folders don't have resolves from the person's plugins last (~/.raea/plugins, RAEA_PLUGINS:
//! what the raea app's Plugins page installs for every project, docs/ecosystem.feature EC1), so a component a person
//! installed once imports in any composition.
use rolldown_plugin::{HookResolveIdArgs, HookResolveIdOutput, HookResolveIdReturn, HookUsage, Plugin, PluginContext, PluginContextResolveOptions};
use std::borrow::Cow;
use std::path::{Path, PathBuf};

/// Where a composition's packages come from
#[derive(Debug, Clone, Default)]
pub struct Edit {
    /// the edit's own folder (not the work folder's copy)
    pub dir: PathBuf,
    /// sdk/react/js: the engine's node_modules
    pub js: PathBuf,
    /// the work folder (the entry's): the edit's copied sources and the SDK's
    pub work: PathBuf,
}

#[derive(Debug)]
pub struct EditResolve(Edit);

impl EditResolve {
    /// its folders as the resolver names modules: symlinks resolved (the Mac's temp folder is /var, a link to /private/var),
    /// and on Windows without canonicalize()'s verbatim `\\?\` prefix, which the resolver never writes: with it no
    /// importer was ever under the work folder, so a live game's own `import "react"` went to the engine's React instead of
    /// the live runtime its tsconfig paths name, and `raea play` threw "not a function" before the first frame
    pub fn new(e: Edit) -> Self {
        let real = |p: PathBuf| dunce::canonicalize(&p).map(raea_io_home::plain).unwrap_or(p);
        EditResolve(Edit { dir: real(e.dir), js: real(e.js), work: real(e.work) })
    }

    /// whether an importer is one of the edit's own sources: under the work folder and not in a package. The importer
    /// is named as real as the folders, since the resolver writes a path as it was given: on GitHub's Windows runners
    /// TEMP is the 8.3 `C:\Users\RUNNER~1\..`, which canonicalize() spells `runneradmin`, so no importer was under the
    /// work folder and every live play threw "not a function" (the engine's React, with no `__h`) before its first frame
    fn from_edit(&self, imp: &str) -> bool {
        let named = Path::new(imp);
        let imp = dunce::canonicalize(&named).map(raea_io_home::plain).unwrap_or_else(|_| named.to_path_buf());
        imp.starts_with(&self.0.work) && !imp.components().any(|c| c.as_os_str() == "node_modules")
    }

    /// whether an importer is the edit's own code: its sources (src/) or what it imports from outside its folder (ext/),
    /// not the SDK's copies beside them
    fn own_source(&self, imp: &str) -> bool {
        let named = Path::new(imp);
        let imp = dunce::canonicalize(&named).map(raea_io_home::plain).unwrap_or_else(|_| named.to_path_buf());
        self.from_edit(&imp.to_string_lossy()) && ["src", "ext"].iter().any(|d| imp.starts_with(self.0.work.join(d)))
    }

    /// A package's import of the SDK by its names ("raea", "raea/<module>", "@raea/<package>"): the work folder's copy
    /// of that module, where the edit's own imports of it were pointed (runtime/bundle's rewrite_into: sdk/raea/src's
    /// files at motion/, a package's at <package>/ beside it), so a package and the composition share one SDK and its
    /// hooks read the Clock the engine drives. A package isn't rewritten, so its import resolved nothing from its own
    /// folder and Rolldown left `raea` an outside global: "Can't find variable: raea" before the first frame (#151)
    fn sdk_copy(&self, spec: &str) -> Option<PathBuf> {
        if !(spec == "raea" || spec.starts_with("raea/") || spec.starts_with("@raea/")) {
            return None;
        }
        // sdk/react/js's grandparent
        let sdk = self.0.js.ancestors().nth(2)?;
        let f = raea_io_home::sdk::resolve(sdk, spec)?;
        let mut parts = f.strip_prefix(sdk).ok()?.components();
        let pkg = parts.next()?.as_os_str().to_string_lossy().into_owned();
        // its src/
        parts.next()?;
        let copy = self.0.work.join(if pkg == "raea" { "motion" } else { pkg.as_str() }).join(parts.as_path());
        copy.is_file().then_some(copy)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn its_folders_are_named_as_the_resolver_names_modules() {
        assert_eq!(raea_io_home::plain(PathBuf::from(r"\\?\C:\Temp\raea-cli-1")), PathBuf::from(r"C:\Temp\raea-cli-1"));
        assert_eq!(raea_io_home::plain(PathBuf::from(r"\\?\UNC\server\share")), PathBuf::from(r"\\?\UNC\server\share"));
        assert_eq!(raea_io_home::plain(PathBuf::from("/private/var/x")), PathBuf::from("/private/var/x"));
        // a real folder: canonicalized on Windows that was \\?\C:\..., which no importer starts with
        let dir = std::env::temp_dir();
        let r = EditResolve::new(Edit { dir: dir.clone(), js: dir.clone(), work: dir.clone() });
        assert!(!r.0.work.display().to_string().starts_with(r"\\?\"), "{}", r.0.work.display());
        assert!(dir.join("entry.tsx").starts_with(&r.0.work) || cfg!(unix), "{} under {}", dir.display(), r.0.work.display());
    }

    /// an importer named by another spelling of the work folder (a symlink here; an 8.3 short name on Windows) is still
    /// the edit's, and a package under it isn't
    #[test]
    fn an_importer_by_another_name_for_the_work_folder_is_the_edits() {
        let d = std::env::temp_dir().join(format!("raea-edit-alias-{}", std::process::id()));
        let work = d.join("work");
        std::fs::create_dir_all(work.join("node_modules/pkg")).unwrap();
        std::fs::write(work.join("entry.tsx"), "").unwrap();
        std::fs::write(work.join("node_modules/pkg/index.js"), "").unwrap();
        let alias = d.join("alias");
        #[cfg(unix)]
        std::os::unix::fs::symlink(&work, &alias).unwrap();
        #[cfg(windows)]
        std::os::windows::fs::symlink_dir(&work, &alias).unwrap_or_else(|_| std::fs::create_dir_all(&alias).unwrap());
        let r = EditResolve::new(Edit { dir: work.clone(), js: work.clone(), work: alias.clone() });
        if alias.is_symlink() {
            assert!(r.from_edit(&work.join("entry.tsx").to_string_lossy()), "{} under {}", work.display(), r.0.work.display());
        }
        assert!(r.from_edit(&alias.join("entry.tsx").to_string_lossy()));
        assert!(!r.from_edit(&alias.join("node_modules").join("pkg").join("index.js").to_string_lossy()));
        let _ = std::fs::remove_dir_all(&d);
    }

    /// three's stand-in is for the edit's own code (src/, ext/): never the SDK's copies beside it, nor a package
    #[test]
    fn the_edits_own_code_is_its_sources_and_what_it_brings_in() {
        let work = std::env::temp_dir().join(format!("raea-edit-own-{}", std::process::id()));
        for f in ["src/scene.tsx", "ext/lib.ts", "r3f/input/vanilla.ts", "playable/host/main.tsx", "src/node_modules/pkg/index.js"] {
            std::fs::create_dir_all(work.join(f).parent().unwrap()).unwrap();
            std::fs::write(work.join(f), "").unwrap();
        }
        let r = EditResolve::new(Edit { dir: work.clone(), js: work.clone(), work: work.clone() });
        let own = |f: &str| r.own_source(&work.join(f).to_string_lossy());
        assert!(own("src/scene.tsx") && own("ext/lib.ts"));
        assert!(!own("r3f/input/vanilla.ts") && !own("playable/host/main.tsx") && !own("src/node_modules/pkg/index.js"));
        let _ = std::fs::remove_dir_all(&work);
    }
}

/// The person's plugins (the raea app's Plugins page installs them here with npm): RAEA_PLUGINS, else ~/.raea/plugins.
pub fn plugins() -> PathBuf {
    raea_io_home::env_path("RAEA_PLUGINS", ".raea/plugins")
}

/// `spec` resolved by Rolldown's resolver as if imported from a file in `dir`
async fn resolve_from(ctx: &PluginContext, spec: &str, dir: &Path) -> anyhow::Result<Option<HookResolveIdOutput>> {
    let importer = dir.join("_raea.js");
    let opts = PluginContextResolveOptions { skip_self: true, ..Default::default() };
    match ctx.resolve(spec, Some(&importer.to_string_lossy()), Some(opts)).await? {
        Ok(r) => Ok(Some(HookResolveIdOutput::from_id(r.id.as_str()))),
        Err(_) => Ok(None),
    }
}

/// Packages a composition's bundle takes from raea in place of their own, by the stand-in module under raea's home:
/// ones that need what a native render's JS hasn't (a DOM, web workers) to do what raea's engine does itself.
/// troika-three-text (drei's <Text>) lays text out in a worker onto a canvas; raea's R3F reader draws its Text as
/// raea's own text (sdk/r3f/src/input/troika.ts, text.ts; docs/r3f.feature R3F13).
const STAND_INS: &[(&str, &str)] = &[("troika-three-text", "sdk/r3f/src/input/troika.ts")];

/** three as the edit's own code imports it: three with its renderer a stand-in that draws nothing and records what it
 *  renders, for <Three program> (sdk/r3f/src/input/vanilla.ts, program.ts; docs/r3f.feature R3F19), at the work
 *  folder's copy of the r3f package so it imports the edit's three. Three's own modules, its addons and the SDK's
 *  copies (the playable host's real WebGLRenderer) import three as it is. */
const THREE_STAND_INS: &[(&str, &str)] = &[("three", "r3f/input/vanilla.ts"), ("three/webgpu", "r3f/input/vanilla-webgpu.ts")];

impl Plugin for EditResolve {
    fn name(&self) -> Cow<'static, str> {
        Cow::Borrowed("raea:edit")
    }

    fn register_hook_usage(&self) -> HookUsage {
        HookUsage::ResolveId
    }

    async fn resolve_id(&self, ctx: &PluginContext, args: &HookResolveIdArgs<'_>) -> HookResolveIdReturn {
        let e = &self.0;
        let spec = args.specifier;
        let Some(imp) = args.importer else { return Ok(None) };
        // raea's home is sdk/react/js's grandparent's parent
        if let Some((_, own)) = STAND_INS.iter().find(|(p, _)| *p == spec) {
            if let Some(home) = e.js.ancestors().nth(3) {
                let f = home.join(own);
                if f.is_file() {
                    return Ok(Some(HookResolveIdOutput::from_id(f.to_string_lossy().as_ref())));
                }
            }
        }
        if let Some((_, own)) = THREE_STAND_INS.iter().find(|(p, _)| *p == spec) {
            let f = e.work.join(own);
            if self.own_source(imp) && f.is_file() {
                return Ok(Some(HookResolveIdOutput::from_id(f.to_string_lossy().as_ref())));
            }
        }
        // a package importing the SDK: the copy the edit's own sources use (the edit's own were pointed at it already)
        if !self.from_edit(imp) {
            if let Some(f) = self.sdk_copy(spec) {
                return Ok(Some(HookResolveIdOutput::from_id(f.to_string_lossy().as_ref())));
            }
        }
        let bare = spec.starts_with('@') || spec.starts_with(|c: char| c.is_ascii_lowercase());
        if !bare || spec.starts_with("@raea/") || spec.contains(':') {
            return Ok(None);
        }
        let pkg = if spec.starts_with('@') { spec.splitn(3, '/').take(2).collect::<Vec<_>>().join("/") } else { spec.split('/').next().unwrap_or(spec).to_string() };
        let engine = crate::script::SINGLETONS.contains(&pkg.as_str()) && e.js.join("node_modules").join(&pkg).exists();
        let from_edit = self.from_edit(imp);
        match (from_edit, engine) {
            // the edit's sources: the engine's copy by the work folder's own resolution (its tsconfig paths point a live
            // bundle's react at the live runtime)
            (true, true) => Ok(None),
            (true, false) => match resolve_from(ctx, spec, &e.dir).await? {
                Some(r) => Ok(Some(r)),
                None => resolve_from(ctx, spec, &plugins()).await,
            },
            // a package (the edit's or the engine's) importing one the engine carries: the engine's
            (false, true) => resolve_from(ctx, spec, &e.js).await,
            (false, false) => Ok(None),
        }
    }
}