Skip to content

Commit d86331c

Browse files
authored
perf(site): update Text.js to 1.6.0 (#62269)
1 parent 82229fb commit d86331c

6 files changed

Lines changed: 62 additions & 20 deletions

File tree

‎bun.lock‎

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎services/site/package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"@solidjs/meta": "catalog:",
2828
"@solidjs/router": "catalog:",
2929
"@solidjs/start": "catalog:",
30-
"@textjs/app": "1.5.0",
31-
"@textjs/core": "1.5.0",
32-
"@textjs/opencode": "1.5.0",
33-
"@textjs/react": "1.5.0",
30+
"@textjs/app": "1.6.0",
31+
"@textjs/core": "1.6.0",
32+
"@textjs/opencode": "1.6.0",
33+
"@textjs/react": "1.6.0",
3434
"fuzzysort": "3.1.0",
3535
"nitro": "3.0.260903-beta",
3636
"number-flow": "0.6.2",

‎services/site/public/_headers‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
Cache-Control: public, max-age=31536000, immutable
1313
/textjs/picker-*
1414
Cache-Control: public, max-age=31536000, immutable
15+
# Yoga's WebAssembly, named by a hash of its bytes (textjs/script/build.ts).
16+
/textjs/yoga-*
17+
Cache-Control: public, max-age=31536000, immutable
1518
# Link preview cards, named by a hash of the PNG (script/og.ts).
1619
/og/*
1720
Cache-Control: public, max-age=31536000, immutable

‎services/site/textjs/script/build.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// anomalyco/textjs (MIT), which isn't published to npm; it is downloaded at the pinned commit into
77
// .upstream/. If the download fails the build still writes the picker, and try() falls back to a
88
// link to textjs.org/opencode.
9+
import { createHash } from "node:crypto"
910
import { mkdir, rm } from "node:fs/promises"
1011
import path from "node:path"
1112

@@ -14,6 +15,13 @@ const root = path.resolve(import.meta.dir, "..")
1415
const upstream = path.join(root, ".upstream")
1516
const out = path.resolve(root, "../public/textjs")
1617

18+
// Text.js 1.6 loads Yoga from a .wasm file and the bidi engine from its own chunk under the
19+
// `@textjs/client` resolve condition, which `text build` sets. Bun's bundler leaves the loader's
20+
// `new URL("@textjs/core/yoga.wasm", import.meta.url)` alone, so the entries load the file
21+
// themselves (`initYoga({ wasm })`) from a name hashed by its contents, defined into them below.
22+
const yoga = await Bun.file(new URL(import.meta.resolve("@textjs/core/yoga.wasm"))).bytes()
23+
const yogaName = `yoga-${createHash("sha256").update(yoga).digest("hex").slice(0, 8)}.wasm`
24+
1725
const lite = await fetchUpstream().then(
1826
() => true,
1927
(error) => {
@@ -29,22 +37,30 @@ const result = await Bun.build({
2937
format: "esm",
3038
splitting: true,
3139
minify: true,
40+
conditions: ["@textjs/client"],
3241
naming: { entry: "[name]-[hash].[ext]", chunk: "chunk-[hash].[ext]" },
33-
define: { "process.env.NODE_ENV": '"production"' },
42+
define: {
43+
"process.env.NODE_ENV": '"production"',
44+
__TEXTJS_YOGA__: JSON.stringify(`./${yogaName}`),
45+
},
3446
})
3547
if (!result.success) throw new AggregateError(result.logs, "textjs build failed")
48+
await Bun.write(path.join(out, yogaName), yoga)
3649
const entries = result.outputs.filter((output) => output.kind === "entry-point")
3750
const manifest = Object.fromEntries(
3851
entries.map((entry) => [path.basename(entry.path).split("-")[0], path.basename(entry.path)]),
3952
)
4053
await Bun.write(path.join(out, "manifest.json"), JSON.stringify(manifest))
41-
// What each app downloads: its entry plus every chunk it imports statically.
54+
// What each app downloads: its entry, every chunk it imports statically, and then the chunks it
55+
// fetches on demand (the bidi engine, the session dialogs), plus Yoga's wasm.
4256
const sizes = new Map(
4357
await Promise.all(
4458
result.outputs.map(async (output) => {
45-
const text = await Bun.file(output.path).text()
59+
const bytes = await Bun.file(output.path).bytes()
60+
const text = new TextDecoder().decode(bytes)
4661
const imports = [...text.matchAll(/from"\.\/([\w-]+\.js)"/g)].map((match) => match[1])
47-
return [path.basename(output.path), { bytes: text.length, gzip: Bun.gzipSync(text).length, imports }] as const
62+
const lazy = [...text.matchAll(/import\("\.\/([\w-]+\.js)"\)/g)].map((match) => match[1])
63+
return [path.basename(output.path), { bytes: bytes.length, gzip: Bun.gzipSync(text).length, imports, lazy }] as const
4864
}),
4965
),
5066
)
@@ -54,11 +70,21 @@ const closure = (file: string, seen = new Set<string>()): Set<string> => {
5470
sizes.get(file)?.imports.forEach((next) => closure(next, seen))
5571
return seen
5672
}
73+
const lazyClosure = (eager: string[]): Set<string> => {
74+
const seen = new Set<string>()
75+
const known = new Set(eager)
76+
for (const file of eager) sizes.get(file)?.lazy.forEach((next) => closure(next, seen))
77+
return new Set([...seen].filter((file) => !known.has(file)))
78+
}
5779
for (const [name, file] of Object.entries(manifest)) {
5880
const files = [...closure(file)]
59-
const kb = (key: "bytes" | "gzip") =>
60-
(files.reduce((sum, f) => sum + (sizes.get(f)?.[key] ?? 0), 0) / 1024).toFixed(1)
61-
console.log(`textjs: ${name} ${kb("bytes")} KB, ${kb("gzip")} KB gzip (${files.length} files)`)
81+
const kb = (key: "bytes" | "gzip", list: string[]) =>
82+
(list.reduce((sum, f) => sum + (sizes.get(f)?.[key] ?? 0), 0) / 1024).toFixed(1)
83+
console.log(`textjs: ${name} ${kb("bytes", files)} KB, ${kb("gzip", files)} KB gzip (${files.length} files)`)
84+
const lazy = [...lazyClosure(files)]
85+
if (lazy.length) console.log(`textjs: ${name} +${kb("gzip", lazy)} KB gzip on demand (${lazy.join(", ")})`)
86+
const yogaKb = `${(yoga.length / 1024).toFixed(1)} KB, ${(Bun.gzipSync(yoga).length / 1024).toFixed(1)} KB gzip`
87+
console.log(`textjs: ${name} +${yogaKb} yoga (${yogaName})`)
6288
}
6389

6490
async function fetchUpstream() {

‎services/site/textjs/src/lite.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { initYoga } from "@textjs/core"
12
import { renderToConsole } from "@textjs/react"
23
import { EmbeddedOpenCode } from "../.upstream/opencode-console/embed"
34
import { createDemoClient } from "../.upstream/opencode-console/lib/demo"
@@ -6,7 +7,13 @@ import { createDemoClient } from "../.upstream/opencode-console/lib/demo"
67
// in-memory backend scripts the agent, so nothing runs and nothing is installed. The source comes
78
// from anomalyco/textjs (apps/opencode-console) at the commit script/build.ts pins.
89

10+
// Text.js 1.6 fetches Yoga's WebAssembly instead of carrying it inline. The build names the file
11+
// after its contents (script/build.ts), and Bun doesn't resolve the loader's default
12+
// `new URL("@textjs/core/yoga.wasm", import.meta.url)`, so load it from next to the chunks.
13+
declare const __TEXTJS_YOGA__: string
14+
915
export async function start() {
16+
await initYoga({ wasm: new URL(__TEXTJS_YOGA__, import.meta.url) })
1017
// The app sets its own theme (OpenCode dark), like the native TUI.
1118
const root = await renderToConsole(<EmbeddedOpenCode client={createDemoClient()} onExit={() => root.unmount()} />, {
1219
responsive: true,

‎services/site/textjs/src/picker.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { initYoga } from "@textjs/core"
12
import { Box, renderToConsole, Select, Text, useInput, type SelectOption } from "@textjs/react"
23

34
// opencode.install.pick(): the install commands and desktop builds as a Text.js list in the
45
// DevTools console. The site passes the options, so this bundle holds no site data.
56

7+
// Text.js 1.6 fetches Yoga's WebAssembly instead of carrying it inline; script/build.ts hashes the
8+
// file and defines its name. See src/lite.tsx.
9+
declare const __TEXTJS_YOGA__: string
10+
611
type Input = { options: SelectOption<string>[]; selected?: string; dark: boolean }
712

8-
export function pick(input: Input) {
13+
export async function pick(input: Input) {
14+
await initYoga({ wasm: new URL(__TEXTJS_YOGA__, import.meta.url) })
915
return new Promise<string | undefined>((resolve) => {
1016
const done = (value?: string) => {
1117
root.then((app) => app.unmount())

0 commit comments

Comments
 (0)