egaki 0.8.0 → 0.10.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/cli/cli.d.ts.map +1 -1
  3. package/dist/cli/cli.js +39 -0
  4. package/dist/cli/dev-server.d.ts +30 -0
  5. package/dist/cli/dev-server.d.ts.map +1 -0
  6. package/dist/cli/dev-server.js +266 -0
  7. package/dist/cli/dev-server.test.d.ts +2 -0
  8. package/dist/cli/dev-server.test.d.ts.map +1 -0
  9. package/dist/cli/dev-server.test.js +95 -0
  10. package/dist/vite/angled-screen-shader.d.ts +50 -0
  11. package/dist/vite/angled-screen-shader.d.ts.map +1 -0
  12. package/dist/vite/angled-screen-shader.js +616 -0
  13. package/dist/vite/angled-screen.d.ts +12 -3
  14. package/dist/vite/angled-screen.d.ts.map +1 -1
  15. package/dist/vite/angled-screen.js +12 -3
  16. package/dist/vite/mdx-video.d.ts +4 -2
  17. package/dist/vite/mdx-video.d.ts.map +1 -1
  18. package/dist/vite/mdx-video.js +4 -2
  19. package/dist/vite/mdx-video.test.js +1 -0
  20. package/dist/vite/render-client.d.ts +27 -0
  21. package/dist/vite/render-client.d.ts.map +1 -1
  22. package/dist/vite/render-client.js +47 -51
  23. package/dist/vite/sdk.d.ts.map +1 -1
  24. package/dist/vite/sdk.js +4 -3
  25. package/dist/vite/shader-renderer.d.ts +4 -0
  26. package/dist/vite/shader-renderer.d.ts.map +1 -1
  27. package/dist/vite/shader-renderer.js +3 -3
  28. package/dist/vite/tweakpane-hook.d.ts +6 -0
  29. package/dist/vite/tweakpane-hook.d.ts.map +1 -1
  30. package/dist/vite/tweakpane-hook.js +101 -4
  31. package/dist/vite/vite-plugin.d.ts.map +1 -1
  32. package/dist/vite/vite-plugin.js +108 -9
  33. package/package.json +7 -11
  34. package/src/cli/cli.ts +50 -0
  35. package/src/cli/dev-server.test.ts +109 -0
  36. package/src/cli/dev-server.ts +303 -0
  37. package/src/vite/angled-screen-shader.tsx +753 -0
  38. package/src/vite/angled-screen.tsx +13 -4
  39. package/src/vite/mdx-video.test.tsx +1 -0
  40. package/src/vite/mdx-video.tsx +4 -1
  41. package/src/vite/render-client.ts +52 -51
  42. package/src/vite/sdk.ts +4 -3
  43. package/src/vite/shader-renderer.tsx +3 -3
  44. package/src/vite/tweakpane-hook.tsx +102 -4
  45. package/src/vite/vite-plugin.ts +117 -9
@@ -0,0 +1,303 @@
1
+ // Zero-config dev server for egaki MDX videos.
2
+ //
3
+ // `egaki dev video.mdx` boots a Vite dev server with inline config
4
+ // (configFile: false) — no package.json, vite.config.ts, or npm install
5
+ // needed in the user's folder.
6
+ //
7
+ // Dependency resolution: when the folder has no usable node_modules, a
8
+ // shim node_modules is created containing symlinks to egaki itself and
9
+ // every egaki dependency (react, remotion, vite, ...) resolved from the
10
+ // CLI's own installation. A plain fallback resolveId plugin is NOT enough
11
+ // because Vite's dep optimizer resolves optimizeDeps.include entries with
12
+ // an internal resolver (alias + vite:resolve only) that skips user
13
+ // plugins — without real node_modules those includes fail and CJS deps
14
+ // like safe-mdx break in the browser. The shim makes standard node
15
+ // resolution work everywhere (vite:resolve, optimizer, tailwind scans).
16
+ // The shim dir contains a .egaki-shim marker so it can be refreshed
17
+ // idempotently and recognized as CLI-owned.
18
+ //
19
+ // Vite's cache dir is placed under ~/.cache/egaki/vite/<hash> so repeated
20
+ // runs reuse the prebundle without polluting the user's folder further.
21
+
22
+ import fs from 'node:fs'
23
+ import net from 'node:net'
24
+ import os from 'node:os'
25
+ import path from 'node:path'
26
+ import crypto from 'node:crypto'
27
+ import { createRequire } from 'node:module'
28
+ import { fileURLToPath } from 'node:url'
29
+ import type { ViteDevServer } from 'vite'
30
+ import { video } from '../vite/vite-plugin.ts'
31
+
32
+ // Package root: this file runs from src/cli/ (dev) or dist/cli/ (published).
33
+ // Going up 2 levels reaches the egaki package root either way.
34
+ const __pkgRoot = path.resolve(fileURLToPath(new URL('.', import.meta.url)), '../..')
35
+ const egakiRequire = createRequire(path.join(__pkgRoot, 'package.json'))
36
+
37
+ const SHIM_MARKER = '.egaki-shim'
38
+
39
+ export interface StartDevServerOptions {
40
+ /** Path to an .mdx entry file, or a directory containing .mdx files.
41
+ * Defaults to the current working directory (auto-discovers video.mdx
42
+ * > index.mdx > first .mdx alphabetically). */
43
+ entry?: string
44
+ /** Fixed port. When omitted, a random free port is chosen. */
45
+ port?: number
46
+ /** Host to bind. Defaults to localhost. */
47
+ host?: string
48
+ }
49
+
50
+ export interface RunningDevServer {
51
+ url: string
52
+ port: number
53
+ root: string
54
+ server: ViteDevServer
55
+ close: () => Promise<void>
56
+ }
57
+
58
+ /** Find a random free TCP port by binding to port 0. */
59
+ export async function findFreePort(host = '127.0.0.1'): Promise<number> {
60
+ return new Promise((resolve, reject) => {
61
+ const probe = net.createServer()
62
+ probe.once('error', reject)
63
+ probe.listen(0, host, () => {
64
+ const address = probe.address() as net.AddressInfo
65
+ probe.close(() => resolve(address.port))
66
+ })
67
+ })
68
+ }
69
+
70
+ /** Locate the real directory of a package installed in egaki's own tree.
71
+ * Tries pkg/package.json first (works for most packages), then falls
72
+ * back to resolving the main entry and walking up to the package root
73
+ * (for packages whose exports map hides ./package.json). */
74
+ function packageDirFromEgaki(pkg: string): string | undefined {
75
+ try {
76
+ return path.dirname(fs.realpathSync(egakiRequire.resolve(`${pkg}/package.json`)))
77
+ } catch {
78
+ // Fall through to entry-based lookup
79
+ }
80
+ try {
81
+ let dir = path.dirname(fs.realpathSync(egakiRequire.resolve(pkg)))
82
+ while (true) {
83
+ const pkgJsonPath = path.join(dir, 'package.json')
84
+ if (fs.existsSync(pkgJsonPath)) {
85
+ const name = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')).name
86
+ if (name === pkg) return dir
87
+ }
88
+ const parent = path.dirname(dir)
89
+ if (parent === dir) return undefined
90
+ dir = parent
91
+ }
92
+ } catch {
93
+ return undefined
94
+ }
95
+ }
96
+
97
+ /** All packages the shim should link: egaki itself plus every dependency
98
+ * and (present) optional/peer dependency of the egaki package. */
99
+ function shimPackageDirs(): Map<string, string> {
100
+ const pkgJson = JSON.parse(
101
+ fs.readFileSync(path.join(__pkgRoot, 'package.json'), 'utf-8'),
102
+ )
103
+ const names = new Set<string>([
104
+ ...Object.keys(pkgJson.dependencies ?? {}),
105
+ ...Object.keys(pkgJson.optionalDependencies ?? {}),
106
+ ...Object.keys(pkgJson.peerDependencies ?? {}),
107
+ ])
108
+ const dirs = new Map<string, string>()
109
+ dirs.set('egaki', fs.realpathSync(__pkgRoot))
110
+ for (const name of names) {
111
+ const dir = packageDirFromEgaki(name)
112
+ if (dir) dirs.set(name, dir)
113
+ }
114
+ return dirs
115
+ }
116
+
117
+ /** Ensure bare imports resolve from the project root.
118
+ *
119
+ * - egaki already resolvable from root → real project, do nothing
120
+ * - no node_modules (or a CLI-owned shim) → (re)create the shim
121
+ * - foreign node_modules without egaki → error with guidance
122
+ */
123
+ export function ensureDependencyShim(root: string): Error | 'normal' | 'shim' {
124
+ const nodeModulesDir = path.join(root, 'node_modules')
125
+ const isShim = fs.existsSync(path.join(nodeModulesDir, SHIM_MARKER))
126
+
127
+ if (!isShim) {
128
+ // Walk up from root looking for node_modules/egaki — a plain fs walk
129
+ // instead of createRequire().resolve() because test runners (tsx,
130
+ // vitest) patch Node's resolver and make it resolve 'egaki' from the
131
+ // wrong context, silently skipping shim creation.
132
+ let dir = root
133
+ while (true) {
134
+ if (fs.existsSync(path.join(dir, 'node_modules', 'egaki', 'package.json'))) {
135
+ return 'normal'
136
+ }
137
+ const parent = path.dirname(dir)
138
+ if (parent === dir) break
139
+ dir = parent
140
+ }
141
+ if (fs.existsSync(nodeModulesDir)) {
142
+ return new Error(
143
+ `${nodeModulesDir} exists but 'egaki' is not installed there.\n` +
144
+ `Either run 'npm install egaki' in ${root}, or remove node_modules ` +
145
+ `to let 'egaki dev' manage dependencies automatically.`,
146
+ )
147
+ }
148
+ }
149
+
150
+ fs.mkdirSync(nodeModulesDir, { recursive: true })
151
+ fs.writeFileSync(
152
+ path.join(nodeModulesDir, SHIM_MARKER),
153
+ 'This node_modules was generated by `egaki dev`. Safe to delete.\n',
154
+ )
155
+ for (const [name, targetDir] of shimPackageDirs()) {
156
+ const linkPath = path.join(nodeModulesDir, name)
157
+ fs.mkdirSync(path.dirname(linkPath), { recursive: true })
158
+ const existing = fs.lstatSync(linkPath, { throwIfNoEntry: false })
159
+ if (existing) {
160
+ if (!existing.isSymbolicLink()) continue
161
+ if (fs.readlinkSync(linkPath) === targetDir) continue
162
+ fs.unlinkSync(linkPath)
163
+ }
164
+ fs.symlinkSync(targetDir, linkPath, 'dir')
165
+ }
166
+ return 'shim'
167
+ }
168
+
169
+ /** Walk up from a real path and collect the parent of every ancestor
170
+ * directory named node_modules. These parents are safe fs.allow roots
171
+ * that cover hoisted/pnpm-store dependency locations. */
172
+ function nodeModulesAncestorRoots(realPath: string): string[] {
173
+ const roots: string[] = []
174
+ let dir = realPath
175
+ while (true) {
176
+ const parent = path.dirname(dir)
177
+ if (parent === dir) break
178
+ if (path.basename(dir) === 'node_modules') roots.push(parent)
179
+ dir = parent
180
+ }
181
+ return roots
182
+ }
183
+
184
+ /** Compute fs.allow roots so Vite can serve framework + dependency files
185
+ * that live outside the user's project root (in the CLI's install tree). */
186
+ function computeFsAllow(root: string): string[] {
187
+ const allow = new Set<string>([root])
188
+ const probes = [path.join(__pkgRoot, 'package.json')]
189
+ for (const id of ['react', 'react-dom', 'remotion', 'vite', 'spiceflow']) {
190
+ try {
191
+ probes.push(egakiRequire.resolve(id))
192
+ } catch {
193
+ // Probe missing: skip
194
+ }
195
+ }
196
+ for (const probe of probes) {
197
+ try {
198
+ const real = fs.realpathSync(probe)
199
+ allow.add(path.dirname(real))
200
+ for (const ancestorRoot of nodeModulesAncestorRoots(real)) {
201
+ allow.add(ancestorRoot)
202
+ }
203
+ } catch {
204
+ // Probe missing: skip
205
+ }
206
+ }
207
+ allow.add(fs.realpathSync(__pkgRoot))
208
+ return [...allow]
209
+ }
210
+
211
+ /** Resolve the entry option to { root, entryFile }.
212
+ * Accepts a file path, a directory, or nothing (cwd). */
213
+ function resolveEntry(entry: string | undefined): Error | { root: string; entryFile?: string } {
214
+ const target = path.resolve(process.cwd(), entry ?? '.')
215
+ if (!fs.existsSync(target)) {
216
+ return new Error(`entry not found: ${target}`)
217
+ }
218
+ const stat = fs.statSync(target)
219
+ if (stat.isDirectory()) {
220
+ const hasMdx = fs
221
+ .readdirSync(target)
222
+ .some((name) => name.endsWith('.mdx'))
223
+ if (!hasMdx) {
224
+ return new Error(
225
+ `no .mdx files found in ${target}\nCreate a video.mdx file or pass one explicitly: egaki dev path/to/video.mdx`,
226
+ )
227
+ }
228
+ return { root: target }
229
+ }
230
+ if (!/\.mdx$/.test(target)) {
231
+ return new Error(`entry must be an .mdx file or a directory, got: ${target}`)
232
+ }
233
+ return { root: path.dirname(target), entryFile: target }
234
+ }
235
+
236
+ /** Warm up the server: the very first request can 500 transiently while
237
+ * the dep optimizer discovers new deps and reloads the ssr program (a
238
+ * browser recovers via auto-reload, but agents/tests hit the URL once).
239
+ * Retry until the page serves 200 so the returned URL is ready to use. */
240
+ async function warmUp(url: string, timeoutMs: number): Promise<void> {
241
+ const deadline = Date.now() + timeoutMs
242
+ while (Date.now() < deadline) {
243
+ try {
244
+ const response = await fetch(url)
245
+ if (response.ok) return
246
+ } catch {
247
+ // Server not accepting connections yet
248
+ }
249
+ await new Promise((resolve) => setTimeout(resolve, 300))
250
+ }
251
+ console.warn(`[egaki] dev server warmup did not reach 200 within ${timeoutMs}ms`)
252
+ }
253
+
254
+ /** Start the zero-config dev server. Returns Error | RunningDevServer. */
255
+ export async function startDevServer(
256
+ options: StartDevServerOptions = {},
257
+ ): Promise<Error | RunningDevServer> {
258
+ const entryResult = resolveEntry(options.entry)
259
+ if (entryResult instanceof Error) return entryResult
260
+ const { root, entryFile } = entryResult
261
+
262
+ const shimResult = ensureDependencyShim(root)
263
+ if (shimResult instanceof Error) return shimResult
264
+
265
+ const host = options.host ?? 'localhost'
266
+ const port = options.port ?? (await findFreePort())
267
+
268
+ const rootHash = crypto.createHash('sha256').update(root).digest('hex').slice(0, 12)
269
+ const cacheDir = path.join(os.homedir(), '.cache', 'egaki', 'vite', rootHash)
270
+
271
+ const { createServer } = await import('vite')
272
+
273
+ const server = await createServer({
274
+ configFile: false,
275
+ root,
276
+ cacheDir,
277
+ plugins: [video(entryFile ? { entry: entryFile } : undefined)],
278
+ resolve: {
279
+ dedupe: ['react', 'react-dom', 'remotion', '@remotion/player', '@remotion/media'],
280
+ },
281
+ server: {
282
+ port,
283
+ strictPort: true,
284
+ host,
285
+ fs: {
286
+ allow: computeFsAllow(root),
287
+ },
288
+ },
289
+ })
290
+
291
+ await server.listen()
292
+
293
+ const url = `http://${host}:${port}/`
294
+ await warmUp(url, 60_000)
295
+
296
+ return {
297
+ url,
298
+ port,
299
+ root,
300
+ server,
301
+ close: () => server.close(),
302
+ }
303
+ }