Files
shaneandshinyobjectz 28af820668 PA13, PA14: raea playable, and playables in the public engine (#264)
* PA13, PA14: raea playable, and playables in the public engine

Playables move from the private tree into the public engine (Shane 11-28: "public totally part of the engine"):
sdk/playable (raea/playable for authors, the browser host, check.ts, parity.ts, tony.ts), examples/playables and
docs/playables.feature, with raea/playable and @raea/playable back in sdk/tsconfig.base.json.

`raea playable GAME.tsx -o DIR` (apps/playable) replaces the Bun pipeline (playable.sh, bundle.ts, lint.ts, walk.ts,
alias.ts). It lints the source, renders each composition through `raea run` for its shapes and 3D kinds, bundles the
game and the host once with raea's own bundler, and writes each network's page or ZIP against its 5 MB cap.
engine/bundle gains `minify`, `inline` (inline.rs: pictures, sounds, fonts and .glb as data URLs, .bin as a
Uint8Array) and package-folder aliases (`three/=DIR`). brick-rush writes all nine targets in 7 s (1,384 KB a page);
mobile-ads' tiles and gates export; all play in WebKit, the Google and TikTok packages included.

check.ts lets a network's own page tags through; parity.ts draws in WebKit (chrome.ts is gone), 6.8/255 on
brick-rush frame 300. The craft guide motion/playable.md is `raea guide playable`. bench/live-3d skips brick-rush with
its real reason (PA18).

* tools/layers.toml: raea-app-playable in apps (PA13)

* Fonts travel with a playable, and an end card in a brand's type

An imported .ttf, .otf, .woff or .woff2 (and .jpeg) is its path in a native render (runtime/bundle rewrite.rs,
engine/bundle script.rs) and a data URL in a playable (inline.rs), so an @font-face names the same file either way.
<EndCard> takes font, color and button. Made for the real-brand playables in mobile-ads (ACANA, OLIPOP, HelloFresh),
which carry their brands' own type; the guide and docs/playables.feature say how.

* The preview page tells raea's review its ad events (PA16)

Inside a frame, the preview network posts {raeaPlayable: kind} to its parent on install and end, so the review can show
them beside the game.

* PA16 status: the review plays playables (raea-platform #99), on a host of their own

* The preview page tells the review when it's ready, seen or hidden, and the first touch (PA16)

Beside install and end, the preview build inside a frame posts ready (the game started), viewable and hidden (its page
shown or put away) and touch (the player took over from the game's own hand), so the review and the desktop show them.

* PA16 status: built in cloud (raea-platform #99, #101) and desktop (raea-feed #124), and raea show --playable

* raea playable says what the source can't have even when the game won't render for the lint

A fetch or a URL often stops the walk's render too, and the lint answered with the render's end ("lint.ts ended with
exit 1") and dropped the refusals it had found. Now the source's refusals are the answer, and a render that fails
for another reason is named by its error line.

* PA16 status: play.raea.io is live, checked with the ACANA export

* skills: raea-skills main ae81d0f (#135 notes hook, #136 and #137 the playable guide and raea playable in cli.md)

tools/skills.sh gen with a raea built at this branch and the feed app at main: every reference and guide copy matches it.

* raea-cli: export-game and playable dispatched in a line each, as the verbs around them, so the crate stays at the size gate's 2000 lines

* dupes: the playable's copies folded into what they copied

spec is raea-runtime-bundle's own (now pub), inline.rs's base64 is the base64 crate as raea's other crates use it,
the store and card JSON are json!, and the bundle FFI reads minify and inline as it reads z3 and stamps. tools/dupes.sh:
293 exact, 57 near, as allowed.

---------

Co-authored-by: shinyobjectz <[email protected]>
2026-11-28 13:53:48 -08:00

256 lines
17 KiB
Rust

//! A work folder bundled as raea's bundle_fresh lays it out: tsconfig `paths`, the automatic JSX runtime from
//! node_modules, process.env.NODE_ENV defined, and the same bytes from any folder (lightspeed T3: a bundle that names
//! its work folder changes every function the incremental bake compares).
use std::path::{Path, PathBuf};
fn folder(name: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("raea-bundle-test-{}-{name}", 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();
};
w("tsconfig.json", r#"{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "react", "baseUrl": ".", "paths": { "@lib/*": ["./lib/*"] } } }"#);
w("node_modules/react/package.json", r#"{ "name": "react", "version": "0.0.0", "exports": { "./jsx-runtime": "./jsx-runtime.js" } }"#);
w("node_modules/react/jsx-runtime.js", "export const jsx = (t, p) => ({ t, p }); export const jsxs = jsx;");
w("lib/greet.ts", "export const greet = (n: string): string => `hello ${n}`;");
w("entry.tsx", "import { greet } from \"@lib/greet\";\nconst el = <div title={greet(\"raea\")} />;\n(globalThis as any).out = [el, process.env.NODE_ENV];\n");
d
}
#[test]
fn bundles_a_work_folder_the_same_from_anywhere() {
let (a, b) = (folder("a"), folder("b-longer-name"));
let ja = raea_bundle::bundle(&a.join("entry.tsx")).expect("bundles").js;
let jb = raea_bundle::bundle(&b.join("entry.tsx")).expect("bundles").js;
for d in [&a, &b] {
let _ = std::fs::remove_dir_all(d);
}
assert!(ja.contains("hello ${n}"), "the paths alias resolved: {ja}");
assert!(ja.contains("\"production\""), "NODE_ENV defined: {ja}");
assert!(ja.contains("({ t, p })") || ja.contains("t,\n") || ja.contains("jsx"), "the automatic runtime from node_modules: {ja}");
assert!(!ja.contains("<div"), "JSX compiled");
assert!(!ja.contains(&*std::env::temp_dir().to_string_lossy()) && !ja.contains("raea-bundle-test"), "no folder named: {ja}");
assert_eq!(ja, jb, "the same bytes from two folders");
}
#[test]
fn reports_a_missing_import() {
let d = folder("missing");
std::fs::write(d.join("entry.tsx"), "import { x } from \"./nowhere\";\nconsole.log(x);\n").unwrap();
let e = raea_bundle::bundle(Path::new(&d.join("entry.tsx"))).expect_err("can't resolve it");
let _ = std::fs::remove_dir_all(&d);
assert!(e.contains("nowhere"), "names what it couldn't find: {e}");
}
/// CSS (A2): a global stylesheet with an @import and nesting, and a CSS module with `composes`, imported by the entry
fn css_folder(name: &str) -> PathBuf {
let d = folder(name);
let w = |p: &str, s: &str| std::fs::write(d.join(p), s).unwrap();
w("base.css", "body { margin: 0 }\n");
w("site.css", "@import \"./base.css\";\n.card { color: red; & .title { font-weight: 700 } }\n");
w("card.module.css", ".shell { padding: 4px }\n.card { composes: shell; background: oklch(70% 0.1 200) }\n");
w("entry.tsx", "import \"./site.css\";\nimport s from \"./card.module.css\";\n(globalThis as any).out = s.card;\n");
d
}
#[test]
fn bundles_css_imports_and_modules() {
let (a, b) = (css_folder("css-a"), css_folder("css-b-other"));
let ba = raea_bundle::bundle(&a.join("entry.tsx")).expect("bundles");
let bb = raea_bundle::bundle(&b.join("entry.tsx")).expect("bundles");
for d in [&a, &b] {
let _ = std::fs::remove_dir_all(d);
}
let css = &ba.css;
// the order the modules run: site.css (base.css inlined first), then the module
let (base, card, shell) = (css.find("margin: 0").unwrap_or_else(|| panic!("@import inlined: {css:?} js: {}", ba.js)), css.find(".card").unwrap(), css.find("shell").unwrap());
assert!(base < card && card < shell, "in import order: {css}");
assert!(css.contains(".card .title"), "nesting written out: {css}");
assert!(!css.contains("oklch"), "oklch() lowered for Stylo: {css}");
// the module's classes: its file, the class, its CSS's hash; `composes` adds the composed class
let class = regex_like(&ba.js, "card-module_card_").expect("the module's class map in the script");
assert!(css.contains(&format!(".{class}")), "the class {class} in the CSS: {css}");
assert!(ba.js.contains("card-module_shell_"), "composes names the composed class: {}", ba.js);
assert_eq!(ba, bb, "the same script and CSS from two folders");
}
/// the first token in `s` starting with `prefix`
fn regex_like(s: &str, prefix: &str) -> Option<String> {
let i = s.find(prefix)?;
Some(s[i..].chars().take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '-').collect())
}
#[test]
fn no_css_is_empty() {
let d = folder("no-css");
let b = raea_bundle::bundle(&d.join("entry.tsx")).expect("bundles");
let _ = std::fs::remove_dir_all(&d);
assert_eq!(b.css, "");
}
/// A script's bundle with the engine's resolution (script.rs, as react/js/bundle.ts did on bun's)
#[test]
fn bundles_a_script_with_the_engines_resolution() {
let d = std::env::temp_dir().join(format!("raea-bundle-script-{}", 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();
};
w("js/shims/test.ts", "export const test = (n: string) => 'TEST_SHIM:' + n;\n");
w("js/shims/node/zlib.ts", "export const gzip = () => 'ZLIB_SHIM';\n");
w("js/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
w("js/node_modules/react/index.js", "export const version = 'ENGINE_REACT';\n");
// the fake SDK laid out as the real one (sdk/raea/src under sdk/), with the import names in sdk/tsconfig.base.json's
// paths, which is where the resolver reads "raea" and "raea/<module>" from (script.rs, docs/sdk.feature S1d; #90)
w("sdk/raea/src/index.tsx", "export * from \"./prelude\";\nexport const motion = 'MOTION_INDEX';\n");
w("sdk/raea/src/ecs.ts", "export const ecs = 'MOTION_ECS';\n");
w("sdk/raea/src/prelude.ts", "export const prelude = 'MOTION_PRELUDE';\n");
w("sdk/tsconfig.base.json", r#"{ "compilerOptions": { "paths": { "raea": ["./raea/src/index.tsx"], "raea/*": ["./raea/src/*"] } } }"#);
w("lib/aliased.ts", "export const aliased = 'ALIASED';\n");
// the script's own folder carries another react: the engine's must win
w("work/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
w("work/node_modules/react/index.js", "export const version = 'OTHER_REACT';\n");
w("work/entry.ts", r#"
import { motion } from "raea";
import { ecs } from "raea/ecs";
import { prelude } from "raea";
import { ecs as ecs2 } from "raea/ecs";
import { test } from "bun:test";
import { gzip } from "zlib";
import fs from "node:fs";
import { aliased } from "~lib";
import { version } from "react";
(globalThis as any).out = [motion, ecs, prelude, ecs2 === ecs, test("x"), gzip(), aliased, version, typeof fs, FLAG];
"#);
let script = raea_bundle::script::Script {
js: d.join("js"),
motion: d.join("sdk/raea/src"),
shell: d.join("nope.ts"),
aliases: [("~lib".to_string(), d.join("lib/aliased.ts"))].into_iter().collect(),
stubs: vec!["fs".into()],
};
let o = raea_bundle::Options { z3: false, script: Some(script.clone()), defines: vec![("FLAG".into(), "\"ON\"".into())], edit: None, stamps: false, ..Default::default() };
let b = raea_bundle::bundle_with(&d.join("work/entry.ts"), o).expect("bundles");
for want in ["MOTION_INDEX", "MOTION_ECS", "MOTION_PRELUDE", "TEST_SHIM:", "ZLIB_SHIM", "ALIASED", "ENGINE_REACT", "isn't available in raea's V8", "\"ON\""] {
assert!(b.js.contains(want), "{want} in the bundle: {}", b.js);
}
assert!(!b.js.contains("OTHER_REACT"), "one React: the engine's");
// a Node built-in raea doesn't carry, not listed as a stub: the bundle fails, naming it
w("work/bad.ts", "import { createServer } from \"http\";\nconsole.log(createServer);\n");
let e = raea_bundle::bundle_with(&d.join("work/bad.ts"), raea_bundle::Options { z3: false, script: Some(script), defines: vec![], edit: None, stamps: false, ..Default::default() }).expect_err("http isn't carried");
assert!(e.contains("\"http\" is a Node built-in") && e.contains("--node-stub http"), "{e}");
let _ = std::fs::remove_dir_all(&d);
}
/// A composition's bundle sees the edit's own packages (edit.rs): its sources are copied to a work folder whose
/// node_modules is the engine's, so a package only the edit installed (motion/react) was missing. The edit's package
/// importing react gets the engine's React, not the edit's own copy.
#[test]
fn bundles_a_composition_with_the_edits_packages() {
let d = std::env::temp_dir().join(format!("raea-bundle-edit-{}", 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();
};
w("js/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
w("js/node_modules/react/index.js", "export const version = 'ENGINE_REACT';\n");
// the edit's folder: its own react, and a package (with a subpath export, as motion/react) that imports react
w("edit/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
w("edit/node_modules/react/index.js", "export const version = 'EDIT_REACT';\n");
w("edit/node_modules/anim/package.json", r#"{ "name": "anim", "exports": { "./react": "./react.js" } }"#);
w("edit/node_modules/anim/react.js", "import { version } from 'react';\nexport const anim = 'EDIT_ANIM with ' + version;\n");
// the work folder raea makes: the edit's source copied, node_modules the engine's
w("work/src/edit.tsx", "import { anim } from 'anim/react';\nimport { version } from 'react';\n(globalThis as any).out = [anim, version];\n");
w("work/entry.ts", "import './src/edit.tsx';\n");
#[cfg(unix)]
std::os::unix::fs::symlink(d.join("js/node_modules"), d.join("work/node_modules")).unwrap();
let edit = raea_bundle::edit::Edit { dir: d.join("edit"), js: d.join("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");
assert!(b.js.contains("EDIT_ANIM with ") && b.js.contains("ENGINE_REACT"), "the edit's package, the engine's React: {}", b.js);
assert!(!b.js.contains("EDIT_REACT"), "one React: the engine's");
// without it, as before: the edit's package isn't found, and Rolldown leaves it an external global (undefined when the
// composition runs)
let before = raea_bundle::bundle_with(&d.join("work/entry.ts"), raea_bundle::Options::default()).expect("bundles");
assert!(!before.js.contains("EDIT_ANIM"), "anim/react only resolves from the edit's folder");
let _ = std::fs::remove_dir_all(&d);
}
/// A package the edit's folders don't have resolves from the person's plugins (RAEA_PLUGINS, ~/.raea/plugins: what the
/// raea app's Plugins page installs), after the edit's own: an edit that installed the same package keeps its copy.
#[test]
fn bundles_a_composition_with_the_persons_plugins() {
let d = std::env::temp_dir().join(format!("raea-bundle-plugins-{}", 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();
};
w("js/node_modules/react/package.json", r#"{ "name": "react", "main": "index.js" }"#);
w("js/node_modules/react/index.js", "export const version = 'ENGINE_REACT';\n");
w("plugins/node_modules/grain/package.json", r#"{ "name": "grain", "main": "index.js", "raea": {} }"#);
w("plugins/node_modules/grain/index.js", "import { version } from 'react';\nexport const grain = 'PLUGIN_GRAIN with ' + version;\n");
w("plugins/node_modules/both/package.json", r#"{ "name": "both", "main": "index.js" }"#);
w("plugins/node_modules/both/index.js", "export const both = 'PLUGIN_BOTH';\n");
w("edit/node_modules/both/package.json", r#"{ "name": "both", "main": "index.js" }"#);
w("edit/node_modules/both/index.js", "export const both = 'EDIT_BOTH';\n");
w("work/src/edit.tsx", "import { grain } from 'grain';\nimport { both } from 'both';\n(globalThis as any).out = [grain, both];\n");
w("work/entry.ts", "import './src/edit.tsx';\n");
#[cfg(unix)]
std::os::unix::fs::symlink(d.join("js/node_modules"), d.join("work/node_modules")).unwrap();
// SAFETY: only this test reads RAEA_PLUGINS
unsafe { std::env::set_var("RAEA_PLUGINS", d.join("plugins")) };
let edit = raea_bundle::edit::Edit { dir: d.join("edit"), js: d.join("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");
assert!(b.js.contains("PLUGIN_GRAIN with ") && b.js.contains("ENGINE_REACT"), "the plugin's package, the engine's React: {}", b.js);
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);
}