sandlot 0.1.1 → 0.1.3

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 (59) hide show
  1. package/README.md +145 -518
  2. package/dist/build-emitter.d.ts +47 -0
  3. package/dist/build-emitter.d.ts.map +1 -0
  4. package/dist/builder.d.ts +370 -0
  5. package/dist/builder.d.ts.map +1 -0
  6. package/dist/bundler.d.ts +3 -3
  7. package/dist/bundler.d.ts.map +1 -1
  8. package/dist/commands/compile.d.ts +13 -0
  9. package/dist/commands/compile.d.ts.map +1 -0
  10. package/dist/commands/index.d.ts +17 -0
  11. package/dist/commands/index.d.ts.map +1 -0
  12. package/dist/commands/packages.d.ts +17 -0
  13. package/dist/commands/packages.d.ts.map +1 -0
  14. package/dist/commands/run.d.ts +40 -0
  15. package/dist/commands/run.d.ts.map +1 -0
  16. package/dist/commands/types.d.ts +141 -0
  17. package/dist/commands/types.d.ts.map +1 -0
  18. package/dist/fs.d.ts +60 -42
  19. package/dist/fs.d.ts.map +1 -1
  20. package/dist/index.d.ts +5 -4
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +304 -491
  23. package/dist/internal.d.ts +5 -0
  24. package/dist/internal.d.ts.map +1 -1
  25. package/dist/internal.js +174 -95
  26. package/dist/runner.d.ts +314 -0
  27. package/dist/runner.d.ts.map +1 -0
  28. package/dist/sandbox-manager.d.ts +45 -33
  29. package/dist/sandbox-manager.d.ts.map +1 -1
  30. package/dist/sandbox.d.ts +144 -70
  31. package/dist/sandbox.d.ts.map +1 -1
  32. package/dist/shared-modules.d.ts +22 -3
  33. package/dist/shared-modules.d.ts.map +1 -1
  34. package/dist/shared-resources.d.ts +0 -3
  35. package/dist/shared-resources.d.ts.map +1 -1
  36. package/dist/typechecker.d.ts +1 -1
  37. package/package.json +3 -17
  38. package/src/build-emitter.ts +64 -0
  39. package/src/builder.ts +498 -0
  40. package/src/bundler.ts +86 -57
  41. package/src/commands/compile.ts +236 -0
  42. package/src/commands/index.ts +51 -0
  43. package/src/commands/packages.ts +154 -0
  44. package/src/commands/run.ts +245 -0
  45. package/src/commands/types.ts +172 -0
  46. package/src/fs.ts +90 -216
  47. package/src/index.ts +34 -12
  48. package/src/internal.ts +5 -2
  49. package/src/sandbox.ts +214 -220
  50. package/src/shared-modules.ts +74 -4
  51. package/src/shared-resources.ts +0 -3
  52. package/src/ts-libs.ts +1 -1
  53. package/src/typechecker.ts +1 -1
  54. package/dist/react.d.ts +0 -159
  55. package/dist/react.d.ts.map +0 -1
  56. package/dist/react.js +0 -149
  57. package/src/commands.ts +0 -733
  58. package/src/react.tsx +0 -331
  59. package/src/sandbox-manager.ts +0 -490
package/dist/sandbox.d.ts CHANGED
@@ -1,23 +1,34 @@
1
- import { Bash, defineCommand } from "just-bash/browser";
2
- import { IndexedDbFs, type IndexedDbFsOptions } from "./fs";
3
- import { type BundleResult } from "./bundler";
1
+ import { Bash, defineCommand, type BashOptions } from "just-bash/browser";
2
+ import { Filesystem } from "./fs";
3
+ import { type BuildOutput, type ValidateFn } from "./commands";
4
4
  import { type SharedResources } from "./shared-resources";
5
- export type { BundleResult } from "./bundler";
6
- export type { TypecheckResult } from "./typechecker";
7
- export type { SharedResources, TypesCache } from "./shared-resources";
8
- export type { PackageManifest, InstallResult } from "./packages";
9
- export type { RunContext, RunOptions, RunResult } from "./commands";
10
- export { installPackage, uninstallPackage, listPackages, getPackageManifest } from "./packages";
11
- export { InMemoryTypesCache } from "./shared-resources";
12
- export { loadModule, loadExport, loadDefault, getExportNames, hasExport, createModuleUrl, revokeModuleUrl, ModuleLoadError, ExportNotFoundError, } from "./loader";
5
+ /**
6
+ * Options that can be passed through to the just-bash Bash constructor.
7
+ * Excludes options that sandlot controls internally (fs, customCommands, files).
8
+ */
9
+ export type SandboxBashOptions = Omit<BashOptions, 'fs' | 'customCommands' | 'files'>;
13
10
  /**
14
11
  * Options for creating a sandbox environment
15
12
  */
16
13
  export interface SandboxOptions {
17
14
  /**
18
- * Options for the IndexedDB filesystem
15
+ * Initial files to populate the filesystem with.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const sandbox = await createSandbox({
20
+ * initialFiles: {
21
+ * '/src/index.ts': 'export const x = 1;',
22
+ * '/tsconfig.json': JSON.stringify({ compilerOptions: { strict: true } }),
23
+ * },
24
+ * });
25
+ * ```
26
+ */
27
+ initialFiles?: Record<string, string>;
28
+ /**
29
+ * Maximum filesystem size in bytes (default: 50MB)
19
30
  */
20
- fsOptions?: IndexedDbFsOptions;
31
+ maxFilesystemSize?: number;
21
32
  /**
22
33
  * Path to tsconfig.json in the virtual filesystem.
23
34
  * Default: "/tsconfig.json"
@@ -33,10 +44,12 @@ export interface SandboxOptions {
33
44
  resources?: SharedResources;
34
45
  /**
35
46
  * Callback invoked when a build succeeds.
36
- * Receives the bundle result with the compiled code.
37
- * Use this to dynamically import the bundle or halt the agent.
47
+ * Receives the build output with the bundle and loaded module.
48
+ *
49
+ * For agent workflows, prefer using `createBuilder()` which handles
50
+ * build capture automatically.
38
51
  */
39
- onBuild?: (result: BundleResult) => void | Promise<void>;
52
+ onBuild?: (result: BuildOutput) => void | Promise<void>;
40
53
  /**
41
54
  * Additional custom commands to add to the bash environment
42
55
  */
@@ -49,6 +62,9 @@ export interface SandboxOptions {
49
62
  * This solves the "multiple React instances" problem by allowing dynamic
50
63
  * components to share the same React instance as the host application.
51
64
  *
65
+ * Type definitions are automatically installed for these modules so that
66
+ * TypeScript can typecheck code that imports them.
67
+ *
52
68
  * @example
53
69
  * ```ts
54
70
  * // Host setup
@@ -61,73 +77,141 @@ export interface SandboxOptions {
61
77
  * 'react-dom/client': ReactDOM,
62
78
  * });
63
79
  *
64
- * // Create sandbox with shared modules
65
- * const sandbox = await createInMemorySandbox({
80
+ * // Create sandbox with shared modules (types auto-installed)
81
+ * const sandbox = await createSandbox({
66
82
  * sharedModules: ['react', 'react-dom/client'],
67
83
  * });
68
84
  * ```
69
85
  */
70
86
  sharedModules?: string[];
87
+ /**
88
+ * Options passed through to the just-bash Bash constructor.
89
+ * Use this to configure environment variables, execution limits,
90
+ * network access, logging, and other bash-level settings.
91
+ *
92
+ * Note: `fs`, `customCommands`, and `files` are controlled by sandlot
93
+ * and cannot be overridden here.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const sandbox = await createSandbox({
98
+ * bashOptions: {
99
+ * cwd: '/src',
100
+ * env: { NODE_ENV: 'development' },
101
+ * executionLimits: { maxCommandCount: 1000 },
102
+ * },
103
+ * });
104
+ * ```
105
+ */
106
+ bashOptions?: SandboxBashOptions;
107
+ }
108
+ /**
109
+ * Sandbox state that can be serialized for persistence.
110
+ */
111
+ export interface SandboxState {
112
+ /**
113
+ * All files in the filesystem as path -> content mapping.
114
+ * Can be passed as `initialFiles` when creating a new sandbox.
115
+ */
116
+ files: Record<string, string>;
71
117
  }
72
118
  /**
73
119
  * The sandbox environment containing the filesystem and bash shell
74
120
  */
75
121
  export interface Sandbox {
76
122
  /**
77
- * The virtual filesystem (IndexedDB-backed)
123
+ * The virtual filesystem
78
124
  */
79
- fs: IndexedDbFs;
125
+ fs: Filesystem;
80
126
  /**
81
127
  * The just-bash shell environment
82
128
  */
83
129
  bash: Bash;
84
130
  /**
85
- * Check if there are unsaved changes in the filesystem.
131
+ * The last successful build output, or null if no build has succeeded yet.
132
+ *
133
+ * This is updated automatically whenever a `build` command succeeds.
134
+ * Contains both the bundle and the loaded (and validated, if applicable) module.
86
135
  *
87
136
  * @example
88
137
  * ```ts
89
- * await sandbox.bash.exec('echo "hello" > /test.txt');
90
- * console.log(sandbox.isDirty()); // true
91
- * await sandbox.save();
92
- * console.log(sandbox.isDirty()); // false
138
+ * // Agent loop pattern
139
+ * while (!sandbox.lastBuild) {
140
+ * const response = await agent.step();
141
+ * await sandbox.bash.exec(response.command);
142
+ * }
143
+ * // Build succeeded, sandbox.lastBuild contains bundle + module
144
+ * const App = sandbox.lastBuild.module.App;
93
145
  * ```
94
146
  */
95
- isDirty(): boolean;
147
+ lastBuild: BuildOutput | null;
96
148
  /**
97
- * Save the filesystem to IndexedDB.
149
+ * Get the current sandbox state for persistence.
98
150
  *
99
- * @returns true if saved, false if nothing to save or in-memory only
100
- */
101
- save(): Promise<boolean>;
102
- /**
103
- * Close all resources (filesystem, etc.)
151
+ * Returns a serializable object containing all files that can be
152
+ * JSON-serialized and used to restore the sandbox later.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * // Save sandbox state
157
+ * const state = sandbox.getState();
158
+ * localStorage.setItem('my-project', JSON.stringify(state));
159
+ *
160
+ * // Later, restore the sandbox
161
+ * const saved = JSON.parse(localStorage.getItem('my-project'));
162
+ * const sandbox2 = await createSandbox({ initialFiles: saved.files });
163
+ * ```
104
164
  */
105
- close(): void;
165
+ getState(): SandboxState;
106
166
  /**
107
167
  * Subscribe to build events. Called whenever a build succeeds.
108
168
  * Returns an unsubscribe function.
109
169
  *
110
- * Use this to capture the bundle result when build succeeds.
111
- * The callback receives the BundleResult with the compiled code.
170
+ * For agent workflows, prefer using `createBuilder()` which handles
171
+ * build capture automatically. Use `onBuild` directly when you need
172
+ * more control over the subscription lifecycle.
112
173
  *
113
174
  * @example
114
175
  * ```ts
115
- * let bundle: BundleResult | null = null;
116
- *
117
- * const sandbox = await createInMemorySandbox({
118
- * onBuild: (result) => {
119
- * bundle = result;
120
- * console.log('Built:', result.code.length, 'bytes');
121
- * },
176
+ * let lastBuild: BuildOutput | null = null;
177
+ * const unsubscribe = sandbox.onBuild((result) => {
178
+ * lastBuild = result;
122
179
  * });
123
180
  *
124
- * // ... agent writes files and calls build ...
181
+ * await sandbox.bash.exec('build /src/index.ts');
182
+ * unsubscribe();
183
+ *
184
+ * if (lastBuild) {
185
+ * const App = lastBuild.module.App as React.ComponentType;
186
+ * }
187
+ * ```
188
+ */
189
+ onBuild(callback: (result: BuildOutput) => void | Promise<void>): () => void;
190
+ /**
191
+ * Set a validation function for the build command.
192
+ *
193
+ * When set, the build command will run this function after loading
194
+ * the module. If validation throws, the build fails and the agent
195
+ * sees the error. If validation passes, the validated module is
196
+ * available in the build output.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * sandbox.setValidation((mod) => {
201
+ * if (!mod.App) throw new Error("Must export App component");
202
+ * return { App: mod.App as React.ComponentType };
203
+ * });
125
204
  *
126
- * // After successful build, bundle is available
127
- * console.log(bundle?.code);
205
+ * // Now build will fail if App is missing
206
+ * await sandbox.bash.exec('build /src/index.ts');
128
207
  * ```
129
208
  */
130
- onBuild(callback: (result: BundleResult) => void | Promise<void>): () => void;
209
+ setValidation(fn: ValidateFn): void;
210
+ /**
211
+ * Clear the validation function.
212
+ * After calling this, builds will not perform validation.
213
+ */
214
+ clearValidation(): void;
131
215
  }
132
216
  /**
133
217
  * Create an in-browser agent sandbox with a virtual filesystem, TypeScript
@@ -136,9 +220,12 @@ export interface Sandbox {
136
220
  * The sandbox provides a just-bash shell with custom commands:
137
221
  * - `tsc [entry]` - Type check the project
138
222
  * - `build [entry] [options]` - Build the project (runs typecheck first)
223
+ * - `install <pkg>` - Install npm packages
224
+ * - `uninstall <pkg>` - Remove packages
225
+ * - `list` - List installed packages
226
+ * - `run <entry>` - Run a script
139
227
  *
140
228
  * Build options:
141
- * - `--output, -o <path>` - Output path (default: /dist/bundle.js)
142
229
  * - `--format, -f <esm|iife|cjs>` - Output format (default: esm)
143
230
  * - `--minify, -m` - Enable minification
144
231
  * - `--skip-typecheck, -s` - Skip type checking
@@ -148,18 +235,14 @@ export interface Sandbox {
148
235
  * let bundleResult: BundleResult | null = null;
149
236
  *
150
237
  * const sandbox = await createSandbox({
151
- * fsOptions: {
152
- * dbName: "my-project",
153
- * initialFiles: {
154
- * "/src/index.ts": "export const hello = 'world';",
155
- * "/tsconfig.json": JSON.stringify({
156
- * compilerOptions: { target: "ES2020", strict: true }
157
- * }),
158
- * },
238
+ * initialFiles: {
239
+ * '/src/index.ts': 'export const hello = "world";',
240
+ * '/tsconfig.json': JSON.stringify({
241
+ * compilerOptions: { target: 'ES2020', strict: true }
242
+ * }),
159
243
  * },
160
244
  * onBuild: (result) => {
161
245
  * bundleResult = result;
162
- * // Could also: dynamically import, halt agent, etc.
163
246
  * },
164
247
  * });
165
248
  *
@@ -167,27 +250,18 @@ export interface Sandbox {
167
250
  * await sandbox.bash.exec('echo "console.log(1);" > /src/index.ts');
168
251
  *
169
252
  * // Type check
170
- * const tscResult = await sandbox.bash.exec("tsc");
253
+ * const tscResult = await sandbox.bash.exec('tsc /src/index.ts');
171
254
  * console.log(tscResult.stdout);
172
255
  *
173
256
  * // Build (includes typecheck, triggers onBuild callback)
174
- * const buildResult = await sandbox.bash.exec("build");
257
+ * const buildResult = await sandbox.bash.exec('build /src/index.ts');
175
258
  * console.log(buildResult.stdout);
176
259
  * console.log(bundleResult?.code); // The compiled bundle
177
260
  *
178
- * // Save to IndexedDB
179
- * await sandbox.save();
180
- *
181
- * // Clean up
182
- * sandbox.close();
261
+ * // Save state for later
262
+ * const state = sandbox.getState();
263
+ * localStorage.setItem('my-project', JSON.stringify(state));
183
264
  * ```
184
265
  */
185
266
  export declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
186
- /**
187
- * Create an in-memory sandbox (no IndexedDB persistence).
188
- * Useful for testing or temporary workspaces.
189
- */
190
- export declare function createInMemorySandbox(options?: Omit<SandboxOptions, "fsOptions"> & {
191
- initialFiles?: Record<string, string>;
192
- }): Promise<Sandbox>;
193
267
  //# sourceMappingURL=sandbox.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,MAAM,CAAC;AAC5D,OAAO,EAAe,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAE3D,OAAO,EAAuB,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAG/E,YA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,aAAa,CAAC,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,OAAO,CAAC,CA4GlF"}
@@ -21,7 +21,7 @@
21
21
  * });
22
22
  *
23
23
  * // Now create sandbox with sharedModules option
24
- * const sandbox = await createInMemorySandbox({
24
+ * const sandbox = await createSandbox({
25
25
  * sharedModules: ['react', 'react-dom/client'],
26
26
  * });
27
27
  * ```
@@ -33,9 +33,11 @@
33
33
  */
34
34
  export declare class SharedModuleRegistry {
35
35
  private modules;
36
+ private exportNames;
36
37
  constructor();
37
38
  /**
38
- * Register a module to be shared with dynamic bundles
39
+ * Register a module to be shared with dynamic bundles.
40
+ * Automatically introspects the module to discover its exports.
39
41
  *
40
42
  * @param moduleId - The import specifier (e.g., 'react', 'react-dom/client')
41
43
  * @param module - The module's exports object
@@ -43,7 +45,8 @@ export declare class SharedModuleRegistry {
43
45
  */
44
46
  register(moduleId: string, module: unknown): this;
45
47
  /**
46
- * Register multiple modules at once
48
+ * Register multiple modules at once.
49
+ * Automatically introspects each module to discover its exports.
47
50
  *
48
51
  * @param modules - Object mapping module IDs to their exports
49
52
  * @returns this for chaining
@@ -74,6 +77,14 @@ export declare class SharedModuleRegistry {
74
77
  * Get list of all registered module IDs
75
78
  */
76
79
  list(): string[];
80
+ /**
81
+ * Get the export names for a registered module.
82
+ * These were discovered by introspecting the module at registration time.
83
+ *
84
+ * @param moduleId - The import specifier
85
+ * @returns Array of export names, or empty array if not registered
86
+ */
87
+ getExportNames(moduleId: string): string[];
77
88
  /**
78
89
  * Clear all registrations
79
90
  */
@@ -121,6 +132,14 @@ export declare function unregisterSharedModule(moduleId: string): boolean;
121
132
  * Clear all shared modules from the default registry.
122
133
  */
123
134
  export declare function clearSharedModules(): void;
135
+ /**
136
+ * Get the export names for a registered shared module.
137
+ * Used by the bundler to generate proper re-export statements.
138
+ *
139
+ * @param moduleId - The import specifier
140
+ * @returns Array of export names, or empty array if not registered
141
+ */
142
+ export declare function getSharedModuleExports(moduleId: string): string[];
124
143
  /**
125
144
  * Generate the runtime code that dynamic bundles use to access shared modules.
126
145
  * This is injected into bundles when they import from shared modules.
@@ -1 +1 @@
1
- {"version":3,"file":"shared-modules.d.ts","sourceRoot":"","sources":["../src/shared-modules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAOH;;;;GAIG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,OAAO,CAA8B;;IAO7C;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAKjD;;;;;OAKG;IACH,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAOnD;;;;;OAKG;IACH,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIrC;;;;;;OAMG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAa9B;;;;OAIG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAI9B;;OAEG;IACH,IAAI,IAAI,MAAM,EAAE;IAIhB;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF;AAKD;;;GAGG;AACH,wBAAgB,uBAAuB,IAAI,oBAAoB,CAK9D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAEjD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAE5E;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAEhE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,M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,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAEjE;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAanE"}
@@ -6,9 +6,6 @@
6
6
  * - esbuild WASM (~10MB) - singleton bundler initialization
7
7
  * - Types cache - avoids redundant network fetches when multiple sandboxes
8
8
  * install the same packages
9
- *
10
- * This module consolidates what was previously scattered across
11
- * sandbox.ts and sandbox-manager.ts into a single source of truth.
12
9
  */
13
10
  import { type TypesCache } from "./packages";
14
11
  export type { TypesCache } from "./packages";
@@ -1 +1 @@
1
- {"version":3,"file":"shared-resources.d.ts","sourceRoot":"","sources":["../src/shared-resources.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,EAAsB,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAGjE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE9B;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAEhB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,eAAe,CAAC,CAqB1B;AAWD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,eAAe,CAAC,CAapE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAG5C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C"}
1
+ {"version":3,"file":"shared-resources.d.ts","sourceRoot":"","sources":["../src/shared-resources.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAAsB,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAGjE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE9B;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAEhB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,eAAe,CAAC,CAqB1B;AAWD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,eAAe,CAAC,CAapE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAG5C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C"}
@@ -76,7 +76,7 @@ export interface TypecheckResult {
76
76
  *
77
77
  * @example
78
78
  * ```ts
79
- * const fs = IndexedDbFs.createInMemory({
79
+ * const fs = Filesystem.create({
80
80
  * initialFiles: {
81
81
  * "/tsconfig.json": JSON.stringify({
82
82
  * compilerOptions: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sandlot",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Browser-based TypeScript sandbox with esbuild bundling and type checking",
5
5
  "author": "blindmansion",
6
6
  "license": "MIT",
@@ -22,11 +22,6 @@
22
22
  "import": "./dist/index.js",
23
23
  "default": "./dist/index.js"
24
24
  },
25
- "./react": {
26
- "types": "./dist/react.d.ts",
27
- "import": "./dist/react.js",
28
- "default": "./dist/react.js"
29
- },
30
25
  "./internal": {
31
26
  "types": "./dist/internal.d.ts",
32
27
  "import": "./dist/internal.js",
@@ -39,7 +34,7 @@
39
34
  ],
40
35
  "scripts": {
41
36
  "build": "bun run build:js && bun run build:types",
42
- "build:js": "bun build ./src/index.ts ./src/react.tsx ./src/internal.ts --outdir ./dist --format esm --packages external",
37
+ "build:js": "bun build ./src/index.ts ./src/internal.ts --outdir ./dist --format esm --packages external",
43
38
  "build:types": "tsc --emitDeclarationOnly",
44
39
  "prepublishOnly": "bun run build"
45
40
  },
@@ -48,17 +43,8 @@
48
43
  "just-bash": "^2.6.0",
49
44
  "typescript": "^5.9.3"
50
45
  },
51
- "peerDependencies": {
52
- "react": ">=17.0.0"
53
- },
54
- "peerDependenciesMeta": {
55
- "react": {
56
- "optional": true
57
- }
58
- },
59
46
  "devDependencies": {
60
47
  "@types/bun": "latest",
61
- "@types/react": "^19",
62
- "react": "^19.2.3"
48
+ "@types/react": "^19"
63
49
  }
64
50
  }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Build event emitter for sandbox environments.
3
+ *
4
+ * Simple typed event emitter for successful build results.
5
+ * Used internally by the sandbox to notify listeners when a build succeeds.
6
+ */
7
+
8
+ import type { BuildOutput } from "./commands/types";
9
+
10
+ /**
11
+ * Simple typed event emitter for build results.
12
+ *
13
+ * Only emits on successful builds—build failures are communicated via
14
+ * the bash command's exit code and stderr.
15
+ *
16
+ * For most use cases, prefer using `createBuilder()` which handles
17
+ * build result capture automatically.
18
+ */
19
+ export class BuildEmitter {
20
+ private listeners = new Set<(result: BuildOutput) => void | Promise<void>>();
21
+
22
+ /**
23
+ * Emit a build result to all listeners.
24
+ * Called internally when a build command succeeds.
25
+ */
26
+ emit = async (result: BuildOutput): Promise<void> => {
27
+ const promises: Promise<void>[] = [];
28
+ for (const listener of this.listeners) {
29
+ const ret = listener(result);
30
+ if (ret instanceof Promise) {
31
+ promises.push(ret);
32
+ }
33
+ }
34
+ await Promise.all(promises);
35
+ };
36
+
37
+ /**
38
+ * Subscribe to build events. Returns an unsubscribe function.
39
+ *
40
+ * The callback is invoked each time a build succeeds, receiving
41
+ * the BuildOutput with the bundle and loaded module.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * let lastBuild: BuildOutput | null = null;
46
+ * const unsubscribe = sandbox.onBuild((result) => {
47
+ * lastBuild = result;
48
+ * });
49
+ *
50
+ * await sandbox.bash.exec('build /src/index.ts');
51
+ * unsubscribe();
52
+ *
53
+ * if (lastBuild) {
54
+ * const App = lastBuild.module.App as React.ComponentType;
55
+ * }
56
+ * ```
57
+ */
58
+ on(callback: (result: BuildOutput) => void | Promise<void>): () => void {
59
+ this.listeners.add(callback);
60
+ return () => {
61
+ this.listeners.delete(callback);
62
+ };
63
+ }
64
+ }