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.

#151: a package that imports raea shares the composition's SDK. A package in the project's node_modules (or the person's plugins) importing "raea", "raea/<module>" or "@raea/<package>" resolves to the work folder's copy of that module, where the composition's own imports were pointed (engine/bundle edit.rs sdk_copy, by sdk/tsconfig.base.json's paths), so one SDK and one Clock. A package isn't rewritten as the edit's own files are, so its import resolved nothing and Rolldown left raea an outside global: 0.1.3 refused it before the first frame with "Can't find variable: raea" while the same file by a relative path drew. Checked: the issue's repro (vk-parts) refused with 0.1.3's shipped bundler, draws the bar at frame 29's width with this one; engine/bundle/tests/bundle.rs a_package_importing_the_sdk_gets_the_work_folders_copy (8/8 pass). docs/ecosystem.feature EC13

shinyobjectzd6d7a520a731
diff --git a/docs/ecosystem.feature b/docs/ecosystem.feature
index b5d2a2a5..eecf8d16 100644
--- a/docs/ecosystem.feature
+++ b/docs/ecosystem.feature
@@ -201,3 +201,11 @@ Feature: raea's plugin ecosystem: extending the engine and the app, made, shared
And may an extension restyle the app itself (a theme), or only draw inside its own frames?
And packages install per person, in every project (Shane 11-09: answered)
And will packages ever be sold through raea, or stay free with raea taking no part?
+
+ Scenario: EC13. A package that imports raea shares the composition's SDK (#151)
+ Given a package in the project's node_modules (or the person's plugins) that imports "raea", "raea/<module>" or
+ "@raea/<package>", its entry .tsx or plain ESM .js
+ Then the import is the SDK copy the composition's own files import (engine/bundle edit.rs sdk_copy): one SDK, so
+ its hooks read the Clock the engine drives, exactly as the same file imported by a relative path does
+ And never an outside global the bundle leaves unresolved ("Can't find variable: raea" before the first frame)
+ And engine/bundle/tests/bundle.rs a_package_importing_the_sdk_gets_the_work_folders_copy holds it
diff --git a/engine/bundle/src/edit.rs b/engine/bundle/src/edit.rs
index ecaca4a7..061ba4bf 100644
--- a/engine/bundle/src/edit.rs
+++ b/engine/bundle/src/edit.rs
@@ -43,6 +43,26 @@ impl EditResolve {
let imp = named.canonicalize().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")
}
+
+ /// 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)]
@@ -128,6 +148,12 @@ impl Plugin for EditResolve {
}
}
}
+ // 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);
diff --git a/engine/bundle/tests/bundle.rs b/engine/bundle/tests/bundle.rs
index f7164ec1..992c2cf2 100644
--- a/engine/bundle/tests/bundle.rs
+++ b/engine/bundle/tests/bundle.rs
@@ -212,3 +212,44 @@ fn bundles_a_composition_with_the_persons_plugins() {
assert!(b.js.contains("EDIT_BOTH") && !b.js.contains("PLUGIN_BOTH"), "the edit's own copy first");
let _ = std::fs::remove_dir_all(&d);
}
+
+/// A package that imports the SDK ("raea", "raea/<module>") gets the work folder's copy of it, the one the edit's own
+/// sources were pointed at, so the package's hooks read the same Clock (#151: a package in the project's node_modules
+/// importing `raea` was refused, "Can't find variable: raea", while the same file imported by a relative path drew).
+#[test]
+fn a_package_importing_the_sdk_gets_the_work_folders_copy() {
+ let d = std::env::temp_dir().join(format!("raea-bundle-pkg-sdk-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&d);
+ let w = |p: &str, s: &str| {
+ let f = d.join(p);
+ std::fs::create_dir_all(f.parent().unwrap()).unwrap();
+ std::fs::write(f, s).unwrap();
+ };
+ // raea's home: the SDK's sources and their names, the engine's node_modules under sdk/react/js
+ w("home/sdk/tsconfig.base.json", r#"{ "compilerOptions": { "paths": { "raea": ["./raea/src/prelude.ts"], "raea/clock": ["./app/src/clock.tsx"], "raea/*": ["./raea/src/*"], "@raea/app": ["./app/src/index.ts"], "@raea/app/*": ["./app/src/*"] } } }"#);
+ w("home/sdk/raea/src/prelude.ts", "export const prelude = 'SDK_ORIGINAL_PRELUDE';\n");
+ w("home/sdk/raea/src/easing.ts", "export const easing = 'SDK_ORIGINAL_EASING';\n");
+ w("home/sdk/app/src/clock.tsx", "export const clock = 'SDK_ORIGINAL_CLOCK';\n");
+ w("home/sdk/react/js/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
+ w("home/sdk/react/js/node_modules/react/index.js", "export const version = 'ENGINE_REACT';\n");
+ // the work folder raea makes: the SDK copied (motion/ and app/), the edit's source copied with its imports pointed
+ w("work/motion/prelude.ts", "export const prelude = 'WORK_PRELUDE';\n");
+ w("work/motion/easing.ts", "export const easing = 'WORK_EASING';\n");
+ w("work/app/clock.tsx", "export const clock = 'WORK_CLOCK';\n");
+ w("work/src/edit.tsx", "import { prelude } from '../motion/prelude';\nimport { bar } from 'vk-parts';\n(globalThis as any).out = [prelude, bar];\n");
+ w("work/entry.ts", "import './src/edit.tsx';\n");
+ // the edit's own package, importing the SDK by its names
+ w("edit/node_modules/vk-parts/package.json", r#"{ "name": "vk-parts", "version": "0.0.1", "main": "index.tsx", "peerDependencies": { "raea": "*" } }"#);
+ w("edit/node_modules/vk-parts/index.tsx", "import { prelude } from 'raea';\nimport { clock } from 'raea/clock';\nimport { easing } from 'raea/easing';\nexport const bar = [prelude, clock, easing].join(' ');\n");
+ #[cfg(unix)]
+ std::os::unix::fs::symlink(d.join("home/sdk/react/js/node_modules"), d.join("work/node_modules")).unwrap();
+ let edit = raea_bundle::edit::Edit { dir: d.join("edit"), js: d.join("home/sdk/react/js"), work: d.join("work") };
+ let b = raea_bundle::bundle_with(&d.join("work/entry.ts"), raea_bundle::Options { edit: Some(edit), ..Default::default() }).expect("bundles");
+ for want in ["WORK_PRELUDE", "WORK_CLOCK", "WORK_EASING"] {
+ assert!(b.js.contains(want), "{want}: the package's SDK import is the work folder's copy: {}", b.js);
+ }
+ assert!(!b.js.contains("SDK_ORIGINAL"), "one SDK, the copy, not its sources: {}", b.js);
+ // one copy of each module for the edit and the package alike
+ assert_eq!(b.js.matches("WORK_PRELUDE").count(), 1, "{}", b.js);
+ let _ = std::fs::remove_dir_all(&d);
+}