lecodes-cli 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.js +883 -507
- package/package.json +9 -4
- package/runtime/sdk/canvas/Canvas.ts +86 -0
- package/runtime/sdk/compile/aspectMacro.ts +42 -0
- package/runtime/sdk/compile/assetMacro.ts +44 -0
- package/runtime/sdk/compile/bundler.ts +173 -0
- package/runtime/sdk/compile/compileProject.ts +84 -0
- package/runtime/sdk/compile/header.ts +46 -0
- package/runtime/sdk/compile/index.ts +12 -0
- package/runtime/sdk/compile/sourcemap.ts +25 -0
- package/runtime/sdk/core/Aspect.ts +2 -2
- package/runtime/sdk/core/signals.ts +277 -0
- package/runtime/sdk/g2/CharacterController2D.ts +5 -4
- package/runtime/sdk/g2/Node2D.ts +1 -1
- package/runtime/sdk/g2/Physics2D.ts +22 -12
- package/runtime/sdk/g2/Scene2D.ts +12 -0
- package/runtime/sdk/g2/{Shape.ts → Shape2D.ts} +10 -10
- package/runtime/sdk/g2/Sprite.ts +7 -2
- package/runtime/sdk/g2/Texture2D.ts +4 -2
- package/runtime/sdk/g2/{Trigger.ts → Trigger2D.ts} +9 -9
- package/runtime/sdk/gl/CharacterController.ts +101 -0
- package/runtime/sdk/gl/Node.ts +11 -2
- package/runtime/sdk/gl/Physics.ts +65 -41
- package/runtime/sdk/gl/Scene.ts +18 -0
- package/runtime/sdk/gl/Shape.ts +98 -0
- package/runtime/sdk/gl/Texture.ts +2 -2
- package/runtime/sdk/gl/Trigger.ts +46 -0
- package/runtime/sdk/gl/physicsEvents.ts +20 -0
- package/runtime/sdk/inject.ts +12 -5
- package/runtime/sdk/runtime/camera.ts +31 -0
- package/runtime/sdk/runtime/device.ts +26 -0
- package/runtime/sdk/runtime/share.ts +9 -0
- package/runtime/sdk/ui/UI.ts +5 -1
- package/runtime/sdk/ui/UIButton.ts +11 -5
- package/runtime/sdk/ui/UIContainer.ts +28 -13
- package/runtime/sdk/ui/UIImage.ts +5 -0
- package/runtime/sdk/ui/UIInput.ts +10 -0
- package/runtime/sdk/ui/UINode.ts +206 -8
- package/runtime/sdk/ui/UIScreen.ts +10 -5
- package/runtime/sdk/ui/UIScrollable.ts +10 -5
- package/runtime/sdk/ui/UISpacer.ts +4 -0
- package/runtime/sdk/ui/UIText.ts +21 -5
- package/runtime/sdk/ui/UIVideo.ts +5 -0
- package/runtime/sdk/ui/UIVirtualizedList.ts +5 -0
- package/runtime/sdk/ui/UIWidget.ts +10 -5
- package/runtime/sdk/canvas/Label.ts +0 -57
- package/runtime/sdk/gl/Collider.ts +0 -29
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lecodes-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Clone, edit and push LeCodes projects from your machine.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,23 +21,28 @@
|
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"vendor-runtime": "bun scripts/vendor-runtime.ts",
|
|
24
|
-
"build": "bun scripts/vendor-runtime.ts && bun build src/index.ts --target=node --outfile dist/index.js --external @letary/chisel --external lecodes-renderer --banner=\"#!/usr/bin/env node\"",
|
|
24
|
+
"build": "bun scripts/vendor-runtime.ts && bun build src/index.ts --target=node --outfile dist/index.js --external @letary/chisel --external lecodes-renderer --external lecodes-design --banner=\"#!/usr/bin/env node\"",
|
|
25
25
|
"typecheck": "tsc --noEmit",
|
|
26
26
|
"dev": "bun run src/index.ts",
|
|
27
27
|
"prepublishOnly": "bun run build"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@letary/chisel": "^0.
|
|
30
|
+
"@letary/chisel": "^0.2.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"lecodes-
|
|
33
|
+
"lecodes-design": "^0.1.0",
|
|
34
|
+
"lecodes-renderer": "^0.4.0"
|
|
34
35
|
},
|
|
35
36
|
"peerDependenciesMeta": {
|
|
37
|
+
"lecodes-design": {
|
|
38
|
+
"optional": true
|
|
39
|
+
},
|
|
36
40
|
"lecodes-renderer": {
|
|
37
41
|
"optional": true
|
|
38
42
|
}
|
|
39
43
|
},
|
|
40
44
|
"devDependencies": {
|
|
45
|
+
"lecodes-design": "workspace:*",
|
|
41
46
|
"lecodes-renderer": "workspace:*",
|
|
42
47
|
"sdk": "workspace:*"
|
|
43
48
|
}
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
// new Sprite({ texture: c }) // Canvas is a valid texture source everywhere
|
|
22
22
|
|
|
23
23
|
import { Texture2D } from "../g2/Texture2D"
|
|
24
|
+
import { File } from "../runtime/fetch"
|
|
24
25
|
|
|
25
26
|
// Opcodes — MUST stay in sync with docs/canvas-contract.md.
|
|
26
27
|
const OP = {
|
|
@@ -30,6 +31,7 @@ const OP = {
|
|
|
30
31
|
BEGIN_PATH: 14, CLOSE_PATH: 15, MOVE_TO: 16, LINE_TO: 17, QUADRATIC_TO: 18, BEZIER_TO: 19,
|
|
31
32
|
ARC: 20, RECT: 21, ROUND_RECT: 22, FILL: 23, STROKE: 24,
|
|
32
33
|
FILL_RECT: 25, STROKE_RECT: 26, CLEAR_RECT: 27, FILL_TEXT: 28, STROKE_TEXT: 29,
|
|
34
|
+
DRAW_IMAGE: 30,
|
|
33
35
|
} as const
|
|
34
36
|
|
|
35
37
|
const ALIGN = { left: 0, center: 1, right: 2, start: 3, end: 4 } as const
|
|
@@ -140,6 +142,31 @@ export class Canvas {
|
|
|
140
142
|
fillText(text: string, x: number, y: number, maxWidth = 0): this { this._cmd.push(OP.FILL_TEXT, this._ref(text), x, y, maxWidth); return this }
|
|
141
143
|
strokeText(text: string, x: number, y: number, maxWidth = 0): this { this._cmd.push(OP.STROKE_TEXT, this._ref(text), x, y, maxWidth); return this }
|
|
142
144
|
|
|
145
|
+
// --- images ---
|
|
146
|
+
/** Blit a `Bitmap` (a snapshot from `toBitmap()`) onto this canvas. All coordinates are LOGICAL —
|
|
147
|
+
* including the source sub-rect in the 9-arg form (it's scaled by the bitmap's pixelRatio here).
|
|
148
|
+
* Records a DRAW_IMAGE op, so the blit runs at bake time: keep the Bitmap alive until the next
|
|
149
|
+
* `update()`/`toBitmap()`/`texture()`. Three browser-style forms:
|
|
150
|
+
* drawImage(bmp, dx, dy) — whole bitmap at its natural size
|
|
151
|
+
* drawImage(bmp, dx, dy, dw, dh) — whole bitmap, scaled into dw×dh
|
|
152
|
+
* drawImage(bmp, sx, sy, sw, sh, dx, dy, dw, dh) — a sub-rect of the bitmap, scaled into dw×dh */
|
|
153
|
+
drawImage(img: Bitmap, dx: number, dy: number): this
|
|
154
|
+
drawImage(img: Bitmap, dx: number, dy: number, dw: number, dh: number): this
|
|
155
|
+
drawImage(img: Bitmap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): this
|
|
156
|
+
drawImage(img: Bitmap, a: number, b: number, c?: number, d?: number, e?: number, f?: number, g?: number, h?: number): this {
|
|
157
|
+
const pr = img.pixelRatio
|
|
158
|
+
let sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number
|
|
159
|
+
if (e === undefined) { // (dx, dy) or (dx, dy, dw, dh): the whole bitmap
|
|
160
|
+
sx = 0; sy = 0; sw = img.pixelWidth; sh = img.pixelHeight
|
|
161
|
+
dx = a; dy = b; dw = c ?? img.width; dh = d ?? img.height
|
|
162
|
+
} else { // (sx, sy, sw, sh, dx, dy, dw, dh): a sub-rect (logical → device px)
|
|
163
|
+
sx = a * pr; sy = b * pr; sw = c! * pr; sh = d! * pr
|
|
164
|
+
dx = e; dy = f!; dw = g!; dh = h!
|
|
165
|
+
}
|
|
166
|
+
this._cmd.push(OP.DRAW_IMAGE, img._surfaceId, sx, sy, sw, sh, dx, dy, dw, dh)
|
|
167
|
+
return this
|
|
168
|
+
}
|
|
169
|
+
|
|
143
170
|
/** Measure `text` in the current font on THIS platform. The one synchronous round-trip. */
|
|
144
171
|
measureText(text: string): CanvasTextMetrics {
|
|
145
172
|
const o = _creatorCanvas.measureText(text, this._font)
|
|
@@ -168,6 +195,26 @@ export class Canvas {
|
|
|
168
195
|
return this
|
|
169
196
|
}
|
|
170
197
|
|
|
198
|
+
/** Copy the current pixels into a standalone, immutable `Bitmap` — independent of this canvas, so
|
|
199
|
+
* later drawing/`reset()` never touches it. Draw it back with `drawImage()`, export it with
|
|
200
|
+
* `toDataURL()`, or use it to "flatten" an ever-growing drawing so re-bakes stay O(1):
|
|
201
|
+
* const snap = canvas.toBitmap()
|
|
202
|
+
* canvas.reset(); canvas.drawImage(snap, 0, 0) // one op replaces the whole history
|
|
203
|
+
* // …keep drawing on top; snap.destroy() once it's been drawn in. */
|
|
204
|
+
toBitmap(): Bitmap {
|
|
205
|
+
return new Bitmap(_creatorCanvas.snapshot(this._bakeSurface()), this.width, this.height, this.pixelRatio)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Encode the current pixels to an image `File` — 'image/png' (default) or 'image/jpeg'. The File
|
|
209
|
+
* is a host buffer handle {id,name,size}: append it to a FormData to upload, load it as a texture
|
|
210
|
+
* (`Texture2D.load(file)`), or hand it to a share/save sheet. */
|
|
211
|
+
toFile(name = "canvas.png", type = "image/png"): Promise<File> {
|
|
212
|
+
const surfaceId = this._bakeSurface()
|
|
213
|
+
return new Promise<File>((res, rej) => {
|
|
214
|
+
_creatorCanvas.toFile(surfaceId, type, (bufferId, size) => res(new File(bufferId, name, size)), rej)
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
171
218
|
destroy(): void {
|
|
172
219
|
if (this._surfaceId) _creatorCanvas.destroySurface(this._surfaceId)
|
|
173
220
|
this._tex2d?.destroy()
|
|
@@ -182,3 +229,42 @@ export class Canvas {
|
|
|
182
229
|
/** Duck-type guard so consumers can accept a Canvas anywhere a texture source is expected. */
|
|
183
230
|
export const isCanvas = (v: unknown): v is Canvas =>
|
|
184
231
|
typeof v === "object" && v !== null && (v as { __canvas?: unknown }).__canvas === true
|
|
232
|
+
|
|
233
|
+
/** An immutable RGBA8 snapshot of a canvas, produced by `Canvas.toBitmap()`. It owns its own baked
|
|
234
|
+
* surface (a copy), so the source canvas can keep drawing or `reset()` without affecting it. Use it
|
|
235
|
+
* to stamp/blit back (`canvas.drawImage(bitmap, …)`), to export (`bitmap.toDataURL()`), or as a
|
|
236
|
+
* cheap "layer" you composite. Call `destroy()` when done — it holds a native surface. */
|
|
237
|
+
export class Bitmap {
|
|
238
|
+
readonly __bitmap = true as const
|
|
239
|
+
/** @internal — a standalone `_creatorCanvas` surface (RGBA8), independent of any Canvas. */
|
|
240
|
+
readonly _surfaceId: number
|
|
241
|
+
/** Logical width, matching the Canvas it was captured from (pixelRatio is crispness, not size). */
|
|
242
|
+
readonly width: number
|
|
243
|
+
readonly height: number
|
|
244
|
+
readonly pixelRatio: number
|
|
245
|
+
|
|
246
|
+
constructor(surfaceId: number, width: number, height: number, pixelRatio: number) {
|
|
247
|
+
this._surfaceId = surfaceId
|
|
248
|
+
this.width = width
|
|
249
|
+
this.height = height
|
|
250
|
+
this.pixelRatio = pixelRatio
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Device-pixel dimensions of the stored bitmap (`width * pixelRatio`, rounded). */
|
|
254
|
+
get pixelWidth(): number { return Math.round(this.width * this.pixelRatio) }
|
|
255
|
+
get pixelHeight(): number { return Math.round(this.height * this.pixelRatio) }
|
|
256
|
+
|
|
257
|
+
/** Encode to an image `File` — 'image/png' (default) or 'image/jpeg'. See `Canvas.toFile`. */
|
|
258
|
+
toFile(name = "canvas.png", type = "image/png"): Promise<File> {
|
|
259
|
+
return new Promise<File>((res, rej) => {
|
|
260
|
+
_creatorCanvas.toFile(this._surfaceId, type, (bufferId, size) => res(new File(bufferId, name, size)), rej)
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Free the native surface. The bitmap must not be drawn or encoded afterwards. */
|
|
265
|
+
destroy(): void { _creatorCanvas.destroySurface(this._surfaceId) }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Duck-type guard for a Bitmap (see `isCanvas`). */
|
|
269
|
+
export const isBitmap = (v: unknown): v is Bitmap =>
|
|
270
|
+
typeof v === "object" && v !== null && (v as { __bitmap?: unknown }).__bitmap === true
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Compile-time injection of an aspect's runtime name. A user aspect carries its accessor name in the
|
|
2
|
+
// (type-erased) generic — `class Health extends Aspect<'health', Sprite> {…}` — so neither the
|
|
3
|
+
// bundler nor the runtime can see it after types are stripped. This pass injects the matching
|
|
4
|
+
// `static aspect = "health"` so node.aspect()/node.health work at runtime, exactly like the SDK's
|
|
5
|
+
// built-in aspects declare it by hand.
|
|
6
|
+
//
|
|
7
|
+
// Runs on PROJECT files only (the SDK's own aspects already declare `static aspect`). Idempotent:
|
|
8
|
+
// skips a class that already declares one. String-based, like assetMacro — no parser dependency.
|
|
9
|
+
|
|
10
|
+
// `class X extends Aspect<'name', …>` up to the class body's opening brace. Group 3 = the name.
|
|
11
|
+
const ASPECT_RE = /\bclass\s+\w+\s+extends\s+Aspect\s*<\s*(["'])((?:[^"'\\]|\\.)*)\1[^{};]*>\s*\{/g
|
|
12
|
+
|
|
13
|
+
/** Cheap pre-check so we don't scan files that define no aspects. */
|
|
14
|
+
export const hasAspectExtends = (src: string): boolean => /\bextends\s+Aspect\s*</.test(src)
|
|
15
|
+
|
|
16
|
+
/** Inject `static aspect = "<name>"` into each `class … extends Aspect<'<name>', …>`. */
|
|
17
|
+
export const injectAspectNames = (src: string): string => {
|
|
18
|
+
let out = ""
|
|
19
|
+
let last = 0
|
|
20
|
+
ASPECT_RE.lastIndex = 0
|
|
21
|
+
let m: RegExpExecArray | null
|
|
22
|
+
while ((m = ASPECT_RE.exec(src)) !== null) {
|
|
23
|
+
const braceEnd = m.index + m[0].length // index just past the opening `{`
|
|
24
|
+
if (!classBodyDeclaresAspect(src, braceEnd)) {
|
|
25
|
+
out += src.slice(last, braceEnd) + ` static aspect = ${JSON.stringify(m[2])};`
|
|
26
|
+
last = braceEnd
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return last === 0 ? src : out + src.slice(last)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Does the class body starting just after `from` (its `{`) already declare a `static aspect`? */
|
|
33
|
+
const classBodyDeclaresAspect = (src: string, from: number): boolean => {
|
|
34
|
+
let depth = 1
|
|
35
|
+
let i = from
|
|
36
|
+
for (; i < src.length && depth > 0; i++) {
|
|
37
|
+
const c = src[i]
|
|
38
|
+
if (c === "{") depth++
|
|
39
|
+
else if (c === "}") depth--
|
|
40
|
+
}
|
|
41
|
+
return /\bstatic\b[^;{}()=]*\baspect\b/.test(src.slice(from, i))
|
|
42
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Ported from packages/lecodes-cli/src/compile/assetMacro.ts (itself from the backend). The
|
|
2
|
+
// compile-time `asset('./x.png')` macro is desugared into a real import so it goes through the same
|
|
3
|
+
// loader/resolver as a hand-written import.
|
|
4
|
+
|
|
5
|
+
// Matches `asset("./img.png")` / `asset('./img.png')` calls whose argument is a single string
|
|
6
|
+
// literal (handling escaped chars inside the literal).
|
|
7
|
+
const ASSET_RE = /\basset\(\s*(["'])((?:[^"'\\]|\\.)*)\1\s*\)/g
|
|
8
|
+
|
|
9
|
+
/** Cheap pre-check so we don't run the regex over every file. */
|
|
10
|
+
export const hasAssetMacro = (src: string): boolean => src.includes("asset(")
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Desugars `asset('./path.png')` into the exact import the user could have written by hand.
|
|
14
|
+
* Identical paths within a file map to one identifier. Only string-literal arguments are supported.
|
|
15
|
+
*/
|
|
16
|
+
export const desugarAssetMacros = (src: string, filePath: string): string => {
|
|
17
|
+
const byPath = new Map<string, string>()
|
|
18
|
+
let i = 0
|
|
19
|
+
|
|
20
|
+
const out = src.replace(ASSET_RE, (_match, _quote, path) => {
|
|
21
|
+
let name = byPath.get(path)
|
|
22
|
+
if (!name) {
|
|
23
|
+
name = `__asset_${i++}`
|
|
24
|
+
byPath.set(path, name)
|
|
25
|
+
}
|
|
26
|
+
return name
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
if (/\basset\(/.test(out)) {
|
|
30
|
+
throw new Error(`asset() expects a string literal, found a dynamic argument in: ${filePath}`)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (byPath.size === 0) return src
|
|
34
|
+
|
|
35
|
+
const imports = [ ...byPath ]
|
|
36
|
+
.map(([ path, name ]) => `import ${name} from ${JSON.stringify(path)};`)
|
|
37
|
+
.join("\n")
|
|
38
|
+
|
|
39
|
+
// APPENDED, never prepended. ESM imports hoist, so their position is semantically irrelevant —
|
|
40
|
+
// but lines added at the TOP would shift every original line, and the bundle's source map would
|
|
41
|
+
// then disagree with the file the editor shows: inspector jumps and error-overlay stack frames
|
|
42
|
+
// land N lines off in exactly (and only) the files that use asset(). Appending adds no offsets.
|
|
43
|
+
return out + "\n" + imports
|
|
44
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"
|
|
2
|
+
import { spawnSync } from "node:child_process"
|
|
3
|
+
import { createRequire } from "node:module"
|
|
4
|
+
import { dirname, join, relative } from "node:path"
|
|
5
|
+
import { fileURLToPath } from "node:url"
|
|
6
|
+
import { desugarAssetMacros, hasAssetMacro } from "./assetMacro"
|
|
7
|
+
import { hasAspectExtends, injectAspectNames } from "./aspectMacro"
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* The user-project bundler. Project files live in an in-memory map; the whole SDK is delivered via
|
|
11
|
+
* chisel's `inject` so user code uses globals (Scene, Sprite, toast, …) with NO imports. chisel — the
|
|
12
|
+
* Rust method-granular bundler (@letary/chisel) — tree-shakes the injected SDK down to what the
|
|
13
|
+
* project references, including unused class *methods*. It is the only bundler (the esbuild fallback
|
|
14
|
+
* is gone). Bare imports are a hard error (chisel's resolver enforces it).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export type MemoryFiles = Record<string, string>
|
|
18
|
+
|
|
19
|
+
/** Locate the SDK inject entry: an explicit override (LECODES_SDK_INJECT — used by the published CLI,
|
|
20
|
+
* which vendors the SDK source), else the source in-repo. chisel needs the SOURCE tree (not a bundle). */
|
|
21
|
+
const resolveInject = (): string => {
|
|
22
|
+
const override = process.env.LECODES_SDK_INJECT
|
|
23
|
+
if (override && existsSync(override)) return override
|
|
24
|
+
const here = dirname(fileURLToPath(import.meta.url)) // …/sdk/src/compile — portable (Node + Bun)
|
|
25
|
+
const src = join(here, "..", "inject.ts")
|
|
26
|
+
if (existsSync(src)) return src
|
|
27
|
+
throw new Error("sdk: could not locate the inject source (LECODES_SDK_INJECT / src/inject.ts)")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const normalize = (path: string): string => {
|
|
31
|
+
const parts: string[] = []
|
|
32
|
+
for (const part of path.split("/")) {
|
|
33
|
+
if (!part || part === ".") continue
|
|
34
|
+
if (part === "..") parts.pop()
|
|
35
|
+
else parts.push(part)
|
|
36
|
+
}
|
|
37
|
+
return "/" + parts.join("/")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---- chisel (Rust) bundler -------------------------------------------------------------------
|
|
41
|
+
//
|
|
42
|
+
// chisel is fed an in-memory file map over JSON-stdio: the SDK source under `/__sdk/…` (injected via
|
|
43
|
+
// `/__sdk/inject.ts`), the project files at their own paths, and a `define` map. It does
|
|
44
|
+
// method-granular tree-shaking + (when minify is on) compress + mangle. Assets/shaders/svg reach it
|
|
45
|
+
// as `asset()` macros desugared to default imports; aspect names are injected as `static aspect`.
|
|
46
|
+
|
|
47
|
+
const CHISEL_DEFINE: Record<string, string> = { RAD2DEG: "57.29577951308232", DEG2RAD: "0.017453292519943295" }
|
|
48
|
+
|
|
49
|
+
// Methods the native host invokes by name have no in-bundle caller, so method-DCE can't see them.
|
|
50
|
+
// The SDK prefixes its framework / host-facing methods with `_` (`_emitTouchStart`, `_emitClick`,
|
|
51
|
+
// `_onAnimationComplete`, `_emitAnim`, …), so keep every underscore-prefixed method on classes that survive.
|
|
52
|
+
const CHISEL_KEEP = [ "_*" ]
|
|
53
|
+
|
|
54
|
+
/** The SDK source root (parent of `inject.ts`). chisel needs the source tree, not a prebuilt bundle. */
|
|
55
|
+
const resolveSdkSrc = (): string => {
|
|
56
|
+
const inject = resolveInject()
|
|
57
|
+
if (!inject.endsWith("inject.ts")) {
|
|
58
|
+
throw new Error(`chisel needs the SDK source tree (src/inject.ts); got ${inject}. Set LECODES_SDK_INJECT to the source.`)
|
|
59
|
+
}
|
|
60
|
+
return dirname(inject)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const isSdkSource = (path: string) =>
|
|
64
|
+
/\.[tj]s$/.test(path) && !path.endsWith(".d.ts") &&
|
|
65
|
+
!path.includes("/compile/") && !path.includes("/tests/") && !path.includes("/__tests__/")
|
|
66
|
+
|
|
67
|
+
// The SDK tree doesn't change within a process — read it once.
|
|
68
|
+
const sdkFilesCache = new Map<string, MemoryFiles>()
|
|
69
|
+
const collectSdkFiles = (sdkSrc: string): MemoryFiles => {
|
|
70
|
+
const cached = sdkFilesCache.get(sdkSrc)
|
|
71
|
+
if (cached) return cached
|
|
72
|
+
const out: MemoryFiles = {}
|
|
73
|
+
const walk = (dir: string) => {
|
|
74
|
+
for (const name of readdirSync(dir)) {
|
|
75
|
+
const p = join(dir, name)
|
|
76
|
+
if (statSync(p).isDirectory()) walk(p)
|
|
77
|
+
else if (isSdkSource(p)) out["/__sdk/" + relative(sdkSrc, p).split("\\").join("/")] = readFileSync(p, "utf8")
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
walk(sdkSrc)
|
|
81
|
+
sdkFilesCache.set(sdkSrc, out)
|
|
82
|
+
return out
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Locate the chisel binary: an explicit override (LECODES_CHISEL_BIN), else the prebuilt binary from
|
|
86
|
+
* the @letary/chisel npm package (which resolves the platform via optional @letary/chisel-<platform>). */
|
|
87
|
+
const resolveChiselBin = (): string => {
|
|
88
|
+
const override = process.env.LECODES_CHISEL_BIN
|
|
89
|
+
if (override) {
|
|
90
|
+
if (existsSync(override)) return override
|
|
91
|
+
throw new Error(`LECODES_CHISEL_BIN points at a missing file: ${override}`)
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const bin = createRequire(import.meta.url)("@letary/chisel").binaryPath() as string | null
|
|
95
|
+
if (bin && existsSync(bin)) return bin
|
|
96
|
+
} catch { /* @letary/chisel (or its platform binary) not installed */ }
|
|
97
|
+
throw new Error("chisel binary not found. Install @letary/chisel, or set LECODES_CHISEL_BIN.")
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const bundleProjectChisel = async (
|
|
101
|
+
entrypoint: string,
|
|
102
|
+
files: MemoryFiles,
|
|
103
|
+
format: "esm" | "iife",
|
|
104
|
+
sourcemap: boolean,
|
|
105
|
+
): Promise<{ code: string, map: string | null }> => {
|
|
106
|
+
const entry = normalize(entrypoint)
|
|
107
|
+
// Per-file source transforms (project files only — SDK aspects declare their own `static aspect`):
|
|
108
|
+
// - desugar `asset('./x')` → a default import that resolves to the synthetic asset module
|
|
109
|
+
// - inject `static aspect = '<name>'` from `class … extends Aspect<'<name>', …>`
|
|
110
|
+
const projFiles: MemoryFiles = {}
|
|
111
|
+
for (const [ k, v ] of Object.entries(files)) {
|
|
112
|
+
const path = normalize(k)
|
|
113
|
+
let src = v
|
|
114
|
+
if (/\.[tj]sx?$/.test(path)) {
|
|
115
|
+
if (hasAssetMacro(src)) src = desugarAssetMacros(src, path)
|
|
116
|
+
if (hasAspectExtends(src)) src = injectAspectNames(src)
|
|
117
|
+
}
|
|
118
|
+
projFiles[path] = src
|
|
119
|
+
}
|
|
120
|
+
if (!projFiles[entry]) throw new Error(`Entrypoint not found: ${entry}`)
|
|
121
|
+
|
|
122
|
+
const sdkFiles = collectSdkFiles(resolveSdkSrc())
|
|
123
|
+
const minify = process.env.LECODES_CHISEL_MINIFY !== "0" // shipped bundles minify by default
|
|
124
|
+
const input = JSON.stringify({
|
|
125
|
+
files: { ...sdkFiles, ...projFiles },
|
|
126
|
+
entry,
|
|
127
|
+
inject: [ "/__sdk/inject.ts" ],
|
|
128
|
+
format,
|
|
129
|
+
minify,
|
|
130
|
+
fuse: process.env.LECODES_CHISEL_FUSE === "1",
|
|
131
|
+
define: CHISEL_DEFINE,
|
|
132
|
+
keep: CHISEL_KEEP,
|
|
133
|
+
sourcemap,
|
|
134
|
+
// Reactive-UI desugaring (memoized children .map → __uiMap + auto-wrapped signal reads in
|
|
135
|
+
// UIText/style args). Only fires on free references to the injected globals, i.e. user code.
|
|
136
|
+
// An older chisel binary ignores the unknown field — bundles still work, children just
|
|
137
|
+
// rebuild per change instead of reusing nodes.
|
|
138
|
+
reactive_ui: process.env.LECODES_CHISEL_REACTIVE_UI !== "0",
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
const bin = resolveChiselBin()
|
|
142
|
+
const r = spawnSync(bin, [], { input, encoding: "utf8", maxBuffer: 256 * 1024 * 1024 })
|
|
143
|
+
if (r.status === 2 || r.error) {
|
|
144
|
+
throw new Error(`chisel failed: ${r.error?.message ?? r.stderr ?? "unknown error"}`)
|
|
145
|
+
}
|
|
146
|
+
let parsed: { code?: string, map?: string, error?: string }
|
|
147
|
+
try {
|
|
148
|
+
parsed = JSON.parse(r.stdout || "{}")
|
|
149
|
+
} catch {
|
|
150
|
+
throw new Error(`chisel produced invalid output: ${(r.stdout || r.stderr || "").slice(0, 200)}`)
|
|
151
|
+
}
|
|
152
|
+
if (parsed.error) throw new Error(`chisel: ${parsed.error}`)
|
|
153
|
+
if (parsed.code == null) throw new Error("chisel produced no code")
|
|
154
|
+
return { code: parsed.code, map: parsed.map ?? null }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Bundle the project `files` (path → source) starting at `entrypoint`, with the SDK injected.
|
|
159
|
+
* `format` is "esm" by default (what the platform ships); "iife" produces a self-running script.
|
|
160
|
+
*/
|
|
161
|
+
export const bundleProject = async (
|
|
162
|
+
entrypoint: string,
|
|
163
|
+
files: MemoryFiles,
|
|
164
|
+
format: "esm" | "iife" = "esm",
|
|
165
|
+
): Promise<string> => (await bundleProjectChisel(entrypoint, files, format, false)).code
|
|
166
|
+
|
|
167
|
+
/** Like `bundleProject`, but also returns chisel's source map. The map's `sources` are the original
|
|
168
|
+
* module paths and its generated positions are relative to `code` (line 1 = first line of `code`). */
|
|
169
|
+
export const bundleProjectWithMap = async (
|
|
170
|
+
entrypoint: string,
|
|
171
|
+
files: MemoryFiles,
|
|
172
|
+
format: "esm" | "iife" = "esm",
|
|
173
|
+
): Promise<{ code: string, map: string | null }> => bundleProjectChisel(entrypoint, files, format, true)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Turns a project's files into the esbuild input map + entrypoint + metadata header, then bundles.
|
|
2
|
+
// The one canonical "user project → deployable bundle" path: the backend and CLI both call this
|
|
3
|
+
// (the backend resolves asset URLs from the DB, the CLI from local metadata — same function, the
|
|
4
|
+
// URLs are passed in as `publicUrl` + per-entry `fileSrc`/`shaders`).
|
|
5
|
+
|
|
6
|
+
import { bundleProjectWithMap } from "./bundler"
|
|
7
|
+
import { buildHeader } from "./header"
|
|
8
|
+
import { inlineSourceMapComment, offsetSourceMap } from "./sourcemap"
|
|
9
|
+
|
|
10
|
+
export type CompileFileType = "text" | "resource" | "shader"
|
|
11
|
+
|
|
12
|
+
export type CompileEntry = {
|
|
13
|
+
path: string
|
|
14
|
+
type: CompileFileType
|
|
15
|
+
/** Source text for code / .svg files. */
|
|
16
|
+
text?: string
|
|
17
|
+
/** Server src for a binary resource (e.g. "/uploads/x.png"); prefixed with publicUrl. */
|
|
18
|
+
fileSrc?: string
|
|
19
|
+
/** Compiled-shader artifacts (platform + src); the first drives the per-backend URL. */
|
|
20
|
+
shaders?: { platform: string, src: string }[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type CompileOptions = {
|
|
24
|
+
entries: CompileEntry[]
|
|
25
|
+
name: string
|
|
26
|
+
publicUrl: string
|
|
27
|
+
entryOverride?: string
|
|
28
|
+
/** Prepend the metadata header (default true). */
|
|
29
|
+
header?: boolean
|
|
30
|
+
/** Generated-icon URL for the header (backend only). */
|
|
31
|
+
icon?: string
|
|
32
|
+
/** "esm" (default, shipped) or "iife" (self-running, for the local renderer). */
|
|
33
|
+
format?: "esm" | "iife"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The default entrypoint: the largest top-level .ts/.js file with no `export` (a script entry). */
|
|
37
|
+
const detectEntry = (entries: CompileEntry[]): string => {
|
|
38
|
+
let entry = ""
|
|
39
|
+
let max = -1
|
|
40
|
+
for (const e of entries) {
|
|
41
|
+
const ext = e.path.split(".").at(-1)?.toLowerCase()
|
|
42
|
+
if (e.type === "text" && e.text !== undefined && !e.text.includes("export") && (ext === "ts" || ext === "js")) {
|
|
43
|
+
if (e.text.length > max) { entry = e.path; max = e.text.length }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return entry
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const compileProject = async (opts: CompileOptions): Promise<string> => {
|
|
50
|
+
const entry = opts.entryOverride ?? detectEntry(opts.entries)
|
|
51
|
+
if (!entry) {
|
|
52
|
+
throw new Error("Couldn't find an entrypoint (a top-level .ts/.js file with no `export`). Pass entryOverride.")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const files: Record<string, string> = {}
|
|
56
|
+
for (const e of opts.entries) {
|
|
57
|
+
if (e.type === "resource") {
|
|
58
|
+
files[e.path] = `export default "${opts.publicUrl}${e.fileSrc ?? ""}"`
|
|
59
|
+
} else if (e.type === "shader") {
|
|
60
|
+
const shaders = e.shaders ?? []
|
|
61
|
+
if (shaders.length > 2) {
|
|
62
|
+
files[e.path] = `export default \`${opts.publicUrl}${shaders[0].src.replace("_opengl", "_${_creator.subBackend ?? _creator.backend}")}\``
|
|
63
|
+
} else if (shaders.length > 0) {
|
|
64
|
+
files[e.path] = `export default \`${opts.publicUrl}${shaders[0].src.replace("_opengl", "_${_creator.backend}")}\``
|
|
65
|
+
} else {
|
|
66
|
+
files[e.path] = `export default ""` // local-only shader with no compiled artifact yet
|
|
67
|
+
}
|
|
68
|
+
} else if (e.path.toLowerCase().endsWith(".svg")) {
|
|
69
|
+
files[e.path] = `export default SvgSource(\`${e.text ?? ""}\`)`
|
|
70
|
+
} else {
|
|
71
|
+
files[e.path] = e.text ?? ""
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const { code, map } = await bundleProjectWithMap(entry, files, opts.format ?? "esm")
|
|
76
|
+
if (opts.header === false) {
|
|
77
|
+
return map ? `${code}\n${inlineSourceMapComment(map)}` : code
|
|
78
|
+
}
|
|
79
|
+
const header = buildHeader(opts.name, code, { icon: opts.icon })
|
|
80
|
+
if (!map) return header + code
|
|
81
|
+
// Shift the map down past the header (prepended before the code); the inline comment lands last.
|
|
82
|
+
const headerLines = (header.match(/\n/g) ?? []).length
|
|
83
|
+
return `${header}${code}\n${inlineSourceMapComment(offsetSourceMap(map, headerLines))}`
|
|
84
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The metadata header the launcher reads from the compiled bundle: name / icon / preload / gl / 2d /
|
|
2
|
+
// ar. Single source of truth — the backend and CLI both call this (today they mirror it by hand).
|
|
3
|
+
//
|
|
4
|
+
// Detection keys on the *bundled* `_creator*` calls, which the SDK's thin wrappers still emit
|
|
5
|
+
// (new Scene() → _creator.createScene, new Scene2D() → _creator2d.createScene), so the re-skin keeps
|
|
6
|
+
// the exact same signals the old worker bundles produced.
|
|
7
|
+
|
|
8
|
+
export type HeaderOptions = {
|
|
9
|
+
/** Generated-icon URL (backend supplies it; the CLI omits it). */
|
|
10
|
+
icon?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const buildHeader = (name: string, code: string, opts: HeaderOptions = {}): string => {
|
|
14
|
+
let header = `// name: ${name.slice(0, 20)}\n`
|
|
15
|
+
if (opts.icon) header += `// icon: ${opts.icon}\n`
|
|
16
|
+
header += `// last-updated: ${new Date().toISOString()}\n`
|
|
17
|
+
|
|
18
|
+
// Local assets to preload: explicit fetchLocal() calls + implicit engine archives.
|
|
19
|
+
const localAssets = new Set<string>()
|
|
20
|
+
for (const m of code.matchAll(/_creatorUtils\.fetchLocal\((.+?)\)/g)) {
|
|
21
|
+
localAssets.add(m[1].replaceAll(/['"]/g, "").trim())
|
|
22
|
+
}
|
|
23
|
+
if (/_creator\.createGlb/.test(code)) localAssets.add("uberarchive")
|
|
24
|
+
if (/_creator\.captureImage/.test(code)) localAssets.add("capture")
|
|
25
|
+
if (localAssets.size > 0) header += `// preload: ${[ ...localAssets ].join(";")}\n`
|
|
26
|
+
|
|
27
|
+
// GL: disabled unless the project touches the 3D engine; deferred if it lazily creates the engine.
|
|
28
|
+
if (!code.includes("_creator.createScene") && !code.includes("_creator.createEntity")) header += `// gl: disable\n`
|
|
29
|
+
else if (code.includes("_creator.createEngine")) header += `// gl: defer\n`
|
|
30
|
+
|
|
31
|
+
// 2D (creator-2d) — non-exclusive: emitted alongside `gl: disable` so hosts skip Filament and a
|
|
32
|
+
// 2D-aware host brings up the lightweight sokol engine instead.
|
|
33
|
+
if (code.includes("_creator2d.")) header += `// 2d: enable\n`
|
|
34
|
+
|
|
35
|
+
if (code.includes("_creator.createARController")) header += "// ar: enable"
|
|
36
|
+
header += "\n"
|
|
37
|
+
return header
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Same engine signals the header uses, for callers that need the project kind (e.g. icon type). */
|
|
41
|
+
export const detectProjectKind = (code: string): "ar" | "ui" | "2d" | "3d" => {
|
|
42
|
+
if (code.includes("_creator.createARController")) return "ar"
|
|
43
|
+
if (code.includes("_creator2d.")) return "2d"
|
|
44
|
+
if (!code.includes("_creator.")) return "ui"
|
|
45
|
+
return "3d"
|
|
46
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The public entry of the SDK's bundle build system. The backend, CLI, and local renderer import
|
|
2
|
+
// from here instead of re-forking a bundler. (Node + Bun safe — no top-level host globals.)
|
|
3
|
+
|
|
4
|
+
export { bundleProject, bundleProjectWithMap, type MemoryFiles } from "./bundler"
|
|
5
|
+
export { offsetSourceMap, inlineSourceMapComment } from "./sourcemap"
|
|
6
|
+
export { buildHeader, detectProjectKind, type HeaderOptions } from "./header"
|
|
7
|
+
export {
|
|
8
|
+
compileProject,
|
|
9
|
+
type CompileEntry,
|
|
10
|
+
type CompileOptions,
|
|
11
|
+
type CompileFileType,
|
|
12
|
+
} from "./compileProject"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Helpers for serving the bundler's source map alongside the bundle. The map's generated positions
|
|
2
|
+
// are relative to the bundle code; when the deploy/preview prepends a comment header, the map must be
|
|
3
|
+
// shifted down by that many lines, then inlined into the bundle as a data URL (the preview executes
|
|
4
|
+
// the bundle via `new Function(string)`, which has no URL identity, so an external `.map` can't be
|
|
5
|
+
// fetched — it must be inline).
|
|
6
|
+
|
|
7
|
+
/** Shift every generated position in `mapJson` down by `lines` (each VLQ line is `;`-separated). */
|
|
8
|
+
export const offsetSourceMap = (mapJson: string, lines: number): string => {
|
|
9
|
+
if (lines <= 0) return mapJson
|
|
10
|
+
const map = JSON.parse(mapJson) as { mappings: string }
|
|
11
|
+
map.mappings = ";".repeat(lines) + map.mappings
|
|
12
|
+
return JSON.stringify(map)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Inline the map into a bundle string under a **non-standard** directive (`lecodesMap`, not
|
|
17
|
+
* `sourceMappingURL`), so the browser does NOT auto-apply it. The bundle runs via `new Function`, and
|
|
18
|
+
* the browser maps the raw stack position *including* the `new Function` wrapper (~2-3 lines) which the
|
|
19
|
+
* map can't subtract — so its native source location would be off by those lines. Our preview mapper
|
|
20
|
+
* reads `lecodesMap` and subtracts the (calibrated) wrapper itself, so its locations are correct.
|
|
21
|
+
*/
|
|
22
|
+
export const inlineSourceMapComment = (mapJson: string): string => {
|
|
23
|
+
const b64 = Buffer.from(mapJson, "utf8").toString("base64")
|
|
24
|
+
return `//# lecodesMap=data:application/json;charset=utf-8;base64,${b64}`
|
|
25
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
// const hero = new Sprite({ texture })
|
|
5
5
|
// .aspect(SpriteAnimation, { fps: 8, clips: { walk: [1, 2, 3] } }) // attach + configure
|
|
6
|
-
// .aspect(
|
|
6
|
+
// .aspect(Shape2D, { box: [16, 8] }) // chains: returns the node
|
|
7
7
|
// hero.anim.play('walk') // access by name (the chain result is typed as having it)
|
|
8
8
|
// if (hero.has(Physics)) hero.physics.velocity = [0, 0] // has() is a type guard
|
|
9
9
|
//
|
|
@@ -73,7 +73,7 @@ const policyOf = (a: Updatable): { early: boolean; order: number } =>
|
|
|
73
73
|
// Stable insert by ascending order: place after the last element whose order is <= this one's.
|
|
74
74
|
const addSorted = (list: Updatable[], a: Updatable, order: number): void => {
|
|
75
75
|
let i = list.length
|
|
76
|
-
while (i > 0 &&
|
|
76
|
+
while (i > 0 && list[i - 1].order > order) i--
|
|
77
77
|
list.splice(i, 0, a)
|
|
78
78
|
}
|
|
79
79
|
|