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.

@shared/ imports render: both bundlers resolve the workspace's shared/ (LY2)

shinyobjectzac7c37f25668
diff --git a/docs/layout.feature b/docs/layout.feature
index da94ac68..a2fe3742 100644
--- a/docs/layout.feature
+++ b/docs/layout.feature
@@ -52,7 +52,11 @@ Feature: One project layout: compositions, components, controllers, assets, arti
When a project's composition imports "@shared/components/desk"
Then the engine's loader resolves it to the workspace's shared/components/desk.tsx (walking up to .raea/workspace),
as it resolves a relative import, with the same extensions and index files
- And a render, `raea check`, `raea context` and the app's Compositions page all read it the same way
+ And a render, `raea check`, `raea context` and the app's Compositions page all read it the same way: the bundler
+ that copies an edit (runtime/bundle outside.rs) copies what it imports from shared/ beside it, and the one that
+ bundles a script where it lives (engine/bundle script.rs) resolves it from the importer (raea_io_home::shared).
+ 0.1.7's render left @shared/ to the bundler, which took it for a package: "Can't find variable:
+ _shared_components_badge" before the first frame, though tsc passed and raea's skill says to import this way
And TypeScript resolves it too: the project's tsconfig (made by `raea project new`, kept by `raea doctor --fix`)
maps @shared/* to ../../shared/*, so an editor and tsc follow the import
And @shared/ outside a workspace is refused with what to do (UnknownLibrary's message names shared/)
diff --git a/engine/bundle/src/script.rs b/engine/bundle/src/script.rs
index 240af47d..9463c6e0 100644
--- a/engine/bundle/src/script.rs
+++ b/engine/bundle/src/script.rs
@@ -89,6 +89,11 @@ impl Plugin for ScriptResolve {
"@raea/shell" => return Ok(path_id(&s.shell)),
_ => {}
}
+ // the workspace's shared code (docs/layout.feature LY2), from where the importer is: not a package
+ if spec.starts_with(raea_io_home::shared::PREFIX) {
+ let dir = args.importer.map(Path::new).and_then(Path::parent).unwrap_or(Path::new("."));
+ return raea_io_home::shared::file(dir, spec).map(|f| path_id(&f)).map_err(|e| anyhow::anyhow!("{e} (imported by {importer})"));
+ }
// the SDK by its names (docs/sdk.feature S1, S1d): "raea" is its prelude, "raea/<module>" a module, "@raea/<package>"
// a package, each where sdk/tsconfig.base.json's paths put it (sdk/ is the folder above sdk/raea/src). An
// `@raea/` name the list doesn't have is left to the rest (@raea/ui's own folders)
diff --git a/io/home/src/lib.rs b/io/home/src/lib.rs
index 0d5d816e..036fa878 100644
--- a/io/home/src/lib.rs
+++ b/io/home/src/lib.rs
@@ -2,6 +2,7 @@
//! models, the fonts), and where its per-user caches and logs go on each platform.
pub mod sdk;
+pub mod shared;
/// What a folder takes on disk, as `du -sh` says it (`links`: through a link to its files), or "?".
pub fn disk_use(dir: &std::path::Path, links: bool) -> String {
@@ -139,6 +140,16 @@ pub fn src_on(s: &str, windows: bool) -> String {
if s.as_bytes().get(1) == Some(&b':') { format!("file:///{}", s.replace('\\', "/")) } else { s.to_string() }
}
+/// The import path from a file in folder `from` to the file `to` (both relative to one folder): ./x, ../y/z.
+pub fn import_path(from: &std::path::Path, to: &std::path::Path) -> String {
+ let (a, b): (Vec<_>, Vec<_>) = (from.components().collect(), to.components().collect());
+ let common = a.iter().zip(&b).take_while(|(x, y)| x == y).count();
+ let mut parts = vec![".."; a.len() - common].into_iter().map(String::from).collect::<Vec<_>>();
+ parts.extend(b[common..].iter().map(|c| c.as_os_str().to_string_lossy().into_owned()));
+ let p = parts.join("/");
+ if p.starts_with("..") { p } else { format!("./{p}") }
+}
+
/// A local file as a page names it in a URL's path: its path on Unix; on Windows `/C:/x/y` (the path of the file URL
/// file:///C:/x/y, `src`'s, which a page's file:/// base joins it to). Written `C:\x`, a URL reads `c` as its scheme
/// and CSS reads the backslashes as escapes (#40, #41, #42).
diff --git a/io/home/src/shared.rs b/io/home/src/shared.rs
new file mode 100644
index 00000000..21ef8076
--- /dev/null
+++ b/io/home/src/shared.rs
@@ -0,0 +1,71 @@
+//! A workspace's shared/ (docs/layout.feature LY2): what every project in it imports as `@shared/<kind>/<name>`. The
+//! engine's loader (engine/modules) and both bundlers (engine/bundle's script resolution, runtime/bundle's copy of an
+//! edit) find it the same way. A render once left `@shared/...` to the bundler, which read it as a package and threw
+//! "Can't find variable: _shared_components_badge" before the first frame, though tsc passed (0.1.7).
+use std::path::{Component, Path, PathBuf};
+
+/// The prefix a project imports its workspace's shared files by: `@shared/components/desk`.
+pub const PREFIX: &str = "@shared/";
+
+/// The shared/ of the workspace `dir` is in: under the nearest folder above it holding .raea/workspace, or that folder
+/// itself when it has no shared/ yet (a workspace from before shared/ keeps its kinds at its root until `raea workspace
+/// tidy`, as raea_app_registry::shared_dir reads it).
+pub fn of(dir: &Path) -> Option<PathBuf> {
+ let ws = dir.ancestors().find(|d| d.join(".raea/workspace").is_file())?;
+ Some(if ws.join("shared").is_dir() { ws.join("shared") } else { ws.to_path_buf() })
+}
+
+/// The script a path names as an import does: the file itself, with a script or JSON extension, or a folder's index.
+pub fn script(want: &Path) -> Option<PathBuf> {
+ let with = |ext: &str| PathBuf::from(format!("{}{ext}", want.display()));
+ let tries = [want.to_path_buf(), with(".tsx"), with(".ts"), with(".jsx"), with(".js"), with(".mjs"), with(".json"), want.join("index.tsx"), want.join("index.ts"), want.join("index.js")];
+ tries.into_iter().find(|t| t.is_file())
+}
+
+/// The file `@shared/<rest>` names for a source in `dir`, or why there's none (for a message naming its importer).
+pub fn file(dir: &Path, spec: &str) -> Result<PathBuf, String> {
+ let rest = spec.strip_prefix(PREFIX).ok_or_else(|| format!("`{spec}` isn't an {PREFIX} import"))?;
+ let Some(shared) = of(dir) else {
+ return Err(format!("`{spec}` names the workspace's shared/, and {} is in no workspace (no .raea/workspace above it): import the file by a relative path, or copy it into the project", dir.display()));
+ };
+ for part in Path::new(rest).components() {
+ if !matches!(part, Component::Normal(_)) {
+ return Err(format!("`{spec}` climbs out of the workspace's shared/"));
+ }
+ }
+ let want = shared.join(rest);
+ script(&want).ok_or_else(|| format!("`{spec}` isn't there: no {} (tried .tsx, .ts, .jsx, .js, .mjs, .json and a folder's index)", want.display()))
+}
+
+/// The file an import names for a source in `dir`: a relative one beside it (None when it isn't there), an `@shared/`
+/// one in the workspace's shared/ (Err when it isn't: a bundler would take the name for a package's).
+pub fn import(dir: &Path, spec: &str) -> Result<Option<PathBuf>, String> {
+ if spec.starts_with(PREFIX) { file(dir, spec).map(Some) } else { Ok(script(&dir.join(spec))) }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn an_import_of_shared_finds_the_workspace_s_file_from_any_depth() {
+ let t = std::env::temp_dir().join(format!("raea-home-shared-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&t);
+ for (p, s) in [(".raea/workspace", "id = \"ws\"\n"), ("shared/components/badge.tsx", ""), ("shared/components/kit/index.ts", ""), ("projects/p/compositions/edit.tsx", "")] {
+ std::fs::create_dir_all(t.join(p).parent().unwrap()).unwrap();
+ std::fs::write(t.join(p), s).unwrap();
+ }
+ let from = t.join("projects/p/compositions");
+ assert_eq!(file(&from, "@shared/components/badge").unwrap(), t.join("shared/components/badge.tsx"));
+ assert_eq!(file(&from, "@shared/components/badge.tsx").unwrap(), t.join("shared/components/badge.tsx"));
+ assert_eq!(file(&from, "@shared/components/kit").unwrap(), t.join("shared/components/kit/index.ts"));
+ assert!(file(&from, "@shared/components/none").unwrap_err().contains("isn't there"));
+ assert!(file(&from, "@shared/../projects/p/compositions/edit").unwrap_err().contains("climbs out"));
+ assert!(file(&std::env::temp_dir(), "@shared/components/badge").unwrap_err().contains("in no workspace"));
+ // a workspace from before shared/: its kinds at its root
+ std::fs::rename(t.join("shared/components"), t.join("components")).unwrap();
+ std::fs::remove_dir_all(t.join("shared")).unwrap();
+ assert_eq!(file(&from, "@shared/components/badge").unwrap(), t.join("components/badge.tsx"));
+ std::fs::remove_dir_all(&t).unwrap();
+ }
+}
diff --git a/runtime/bundle/src/outside.rs b/runtime/bundle/src/outside.rs
index 7ae5cc1f..46dd8042 100644
--- a/runtime/bundle/src/outside.rs
+++ b/runtime/bundle/src/outside.rs
@@ -1,9 +1,9 @@
use super::*;
use std::collections::BTreeSet;
-/// A source the edit reaches outside its own folder by a relative import (content/anything/paint/shots.tsx's
-/// ../../../examples/objects/wake): `file` where it is, `copy` where bundle_fresh puts it, relative to the work folder
-/// (ext/raea/<its path from raea's home>, or ext/project/<its path from the project's git root>).
+/// A source the edit reaches outside its folder by a relative import (../../../examples/objects/wake) or as shared code
+/// (@shared/components/badge, docs/layout.feature LY2): `file` where it is, `copy` where bundle_fresh puts it under the
+/// work folder (ext/raea/<from raea's home>, ext/project/<from the project's git root>, ext/shared/<from shared/>).
#[derive(Debug, Clone, PartialEq)]
pub struct Outside {
pub file: PathBuf,
@@ -11,24 +11,21 @@ pub struct Outside {
}
fn specs() -> regex::Regex {
- regex::Regex::new(r#"(\bfrom\s+)(["'])(\.[^"']*)(["'])"#).unwrap()
+ regex::Regex::new(r#"(\bfrom\s+)(["'])(\.[^"']*|@shared/[^"']*)(["'])"#).unwrap()
}
fn absolute(p: &Path) -> PathBuf {
normalize(&std::path::absolute(p).unwrap_or_else(|_| p.to_path_buf()))
}
-/// The file a relative `spec` names from `base`, as the bundler would find it: itself, with a script or JSON extension,
-/// or a folder's index
+/// The file a relative or @shared/ `spec` names from `base`, as the bundler would find it: itself, with a script or JSON
+/// extension, or a folder's index
fn resolve(base: &Path, spec: &str) -> Option<PathBuf> {
- let p = normalize(&base.join(spec));
- let with = |ext: &str| PathBuf::from(format!("{}{ext}", p.display()));
- let tries = [p.clone(), with(".tsx"), with(".ts"), with(".jsx"), with(".js"), with(".json"), p.join("index.tsx"), p.join("index.ts"), p.join("index.js")];
- tries.into_iter().find(|t| t.is_file())
+ raea_io_home::shared::import(base, spec).ok().flatten().map(|p| normalize(&p))
}
-/// Where an edit may import from outside its folder, each with its name under ext/: raea's home, and the project's
-/// root (the nearest folder above the edit holding .git) when that is somewhere else
+/// Where an edit may import from outside its folder, each with its name under ext/: raea's home, the project's root
+/// (the nearest folder above the edit holding .git) when elsewhere, and its workspace's shared/ (LY2)
fn roots(dir: &Path) -> Vec<(&'static str, PathBuf)> {
let home = absolute(&raea_io_home::home());
let mut out = vec![("raea", home.clone())];
@@ -37,6 +34,9 @@ fn roots(dir: &Path) -> Vec<(&'static str, PathBuf)> {
out.push(("project", git.to_path_buf()));
}
}
+ if let Some(shared) = raea_io_home::shared::of(dir) {
+ out.push(("shared", absolute(&shared)));
+ }
out
}
@@ -64,8 +64,8 @@ pub fn outside_in(dir: &Path, roots: &[(&'static str, PathBuf)]) -> R<Vec<Outsid
if MEDIA_EXTS.contains(&ext.as_str()) {
continue;
}
- // one the bundler can't find either: left for it to name
- let Some(t) = resolve(&base, spec) else { continue };
+ // one the bundler can't find either: left for it to name (but an @shared/ one, which it takes for a package)
+ let Some(t) = raea_io_home::shared::import(&base, spec).map_err(|e| format!("{}: {e}", f.display()))?.map(|p| normalize(&p)) else { continue };
if t.starts_with(dir) || sdk.iter().any(|s| t.starts_with(s)) || t.components().any(|c| c.as_os_str() == "node_modules") || !seen.insert(t.clone()) {
continue;
}
@@ -95,19 +95,9 @@ pub fn point_outside(src: &str, from_dir: &Path, at: &Path, dir: &Path, outs: &[
None => None,
});
match target {
- Some(t) => format!("{}{}{}{}", &c[1], &c[2], relative(at, &t), &c[4]),
+ Some(t) => format!("{}{}{}{}", &c[1], &c[2], raea_io_home::import_path(at, &t), &c[4]),
None => c[0].to_string(),
}
})
.into_owned()
}
-
-/// The import path from a file in `from` to `to`, both under the work folder
-fn relative(from: &Path, to: &Path) -> String {
- let (a, b): (Vec<_>, Vec<_>) = (from.components().collect(), to.components().collect());
- let common = a.iter().zip(&b).take_while(|(x, y)| x == y).count();
- let mut parts = vec![".."; a.len() - common].into_iter().map(String::from).collect::<Vec<_>>();
- parts.extend(b[common..].iter().map(|c| c.as_os_str().to_string_lossy().into_owned()));
- let p = parts.join("/");
- if p.starts_with("..") { p } else { format!("./{p}") }
-}
diff --git a/runtime/bundle/src/tests.rs b/runtime/bundle/src/tests.rs
index d3f47620..29e211af 100644
--- a/runtime/bundle/src/tests.rs
+++ b/runtime/bundle/src/tests.rs
@@ -69,6 +69,17 @@ fn outside_copies_what_the_edit_imports_beyond_its_folder() {
w("y.ts", "export const y = 0;\n");
let e = outside_in(&dir, &roots).unwrap_err();
assert!(e.contains("lib/c.ts") && e.contains("outside raea's home"), "{e}");
+ // @shared/ (LY2), as raea's skill says to import: 0.1.7 left it to the bundler, which took it for a package
+ w(".raea/workspace", "");
+ w("shared/components/badge.tsx", "export { TONE } from \"./tone\";\n");
+ w("shared/components/tone.ts", "export const TONE = 1;\n");
+ w("proj/edit/a.tsx", "import { TONE } from \"@shared/components/badge\";\n");
+ let roots = vec![("project", root.clone()), ("shared", t.join("shared"))];
+ let outs = outside_in(&dir, &roots).unwrap();
+ assert_eq!(outs.iter().map(|o| o.copy.to_str().unwrap()).collect::<Vec<_>>(), ["ext/shared/components/badge.tsx", "ext/shared/components/tone.ts"]);
+ assert!(point_outside("import { TONE } from \"@shared/components/badge\";", &dir, Path::new("src"), &dir, &outs).contains("\"../ext/shared/components/badge.tsx\""));
+ w("proj/edit/a.tsx", "import { N } from \"@shared/components/nope\";\n");
+ assert!(outside_in(&dir, &roots).unwrap_err().contains("a.tsx: `@shared/components/nope` isn't there"));
std::fs::remove_dir_all(&t).unwrap();
}