sandlot 0.1.3 → 0.2.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 (115) hide show
  1. package/dist/browser/bundler.d.ts +68 -0
  2. package/dist/browser/bundler.d.ts.map +1 -0
  3. package/dist/browser/executor.d.ts +46 -0
  4. package/dist/browser/executor.d.ts.map +1 -0
  5. package/dist/browser/index.d.ts +9 -0
  6. package/dist/browser/index.d.ts.map +1 -0
  7. package/dist/browser/index.js +2692 -0
  8. package/dist/browser/preset.d.ts +63 -0
  9. package/dist/browser/preset.d.ts.map +1 -0
  10. package/dist/commands/index.d.ts +20 -11
  11. package/dist/commands/index.d.ts.map +1 -1
  12. package/dist/commands/types.d.ts +31 -132
  13. package/dist/commands/types.d.ts.map +1 -1
  14. package/dist/core/bundler-utils.d.ts +142 -0
  15. package/dist/core/bundler-utils.d.ts.map +1 -0
  16. package/dist/core/esm-types-resolver.d.ts +125 -0
  17. package/dist/core/esm-types-resolver.d.ts.map +1 -0
  18. package/dist/core/executor.d.ts +35 -0
  19. package/dist/core/executor.d.ts.map +1 -0
  20. package/dist/{fs.d.ts → core/fs.d.ts} +27 -29
  21. package/dist/core/fs.d.ts.map +1 -0
  22. package/dist/core/sandbox.d.ts +30 -0
  23. package/dist/core/sandbox.d.ts.map +1 -0
  24. package/dist/core/sandlot.d.ts +30 -0
  25. package/dist/core/sandlot.d.ts.map +1 -0
  26. package/dist/core/shared-module-registry.d.ts +46 -0
  27. package/dist/core/shared-module-registry.d.ts.map +1 -0
  28. package/dist/core/typechecker.d.ts +60 -0
  29. package/dist/core/typechecker.d.ts.map +1 -0
  30. package/dist/index.d.ts +11 -16
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1405 -2049
  33. package/dist/node/bundler.d.ts +48 -0
  34. package/dist/node/bundler.d.ts.map +1 -0
  35. package/dist/node/executor.d.ts +48 -0
  36. package/dist/node/executor.d.ts.map +1 -0
  37. package/dist/node/index.d.ts +9 -0
  38. package/dist/node/index.d.ts.map +1 -0
  39. package/dist/node/index.js +2646 -0
  40. package/dist/node/preset.d.ts +62 -0
  41. package/dist/node/preset.d.ts.map +1 -0
  42. package/dist/types.d.ts +525 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/package.json +27 -8
  45. package/src/browser/bundler.ts +294 -0
  46. package/src/browser/executor.ts +71 -0
  47. package/src/browser/index.ts +57 -0
  48. package/src/browser/preset.ts +179 -0
  49. package/src/commands/index.ts +526 -43
  50. package/src/commands/types.ts +82 -146
  51. package/src/core/bundler-utils.ts +630 -0
  52. package/src/core/esm-types-resolver.ts +432 -0
  53. package/src/core/executor.ts +161 -0
  54. package/src/{fs.ts → core/fs.ts} +59 -37
  55. package/src/core/sandbox.ts +621 -0
  56. package/src/core/sandlot.ts +77 -0
  57. package/src/core/shared-module-registry.ts +138 -0
  58. package/src/core/typechecker.ts +607 -0
  59. package/src/index.ts +104 -139
  60. package/src/node/bundler.ts +194 -0
  61. package/src/node/executor.ts +87 -0
  62. package/src/node/index.ts +39 -0
  63. package/src/node/preset.ts +178 -0
  64. package/src/types.ts +668 -0
  65. package/README.md +0 -243
  66. package/dist/build-emitter.d.ts +0 -47
  67. package/dist/build-emitter.d.ts.map +0 -1
  68. package/dist/builder.d.ts +0 -370
  69. package/dist/builder.d.ts.map +0 -1
  70. package/dist/bundler.d.ts +0 -148
  71. package/dist/bundler.d.ts.map +0 -1
  72. package/dist/commands/compile.d.ts +0 -13
  73. package/dist/commands/compile.d.ts.map +0 -1
  74. package/dist/commands/packages.d.ts +0 -17
  75. package/dist/commands/packages.d.ts.map +0 -1
  76. package/dist/commands/run.d.ts +0 -40
  77. package/dist/commands/run.d.ts.map +0 -1
  78. package/dist/commands.d.ts +0 -179
  79. package/dist/commands.d.ts.map +0 -1
  80. package/dist/fs.d.ts.map +0 -1
  81. package/dist/internal.d.ts +0 -79
  82. package/dist/internal.d.ts.map +0 -1
  83. package/dist/internal.js +0 -1976
  84. package/dist/loader.d.ts +0 -164
  85. package/dist/loader.d.ts.map +0 -1
  86. package/dist/packages.d.ts +0 -199
  87. package/dist/packages.d.ts.map +0 -1
  88. package/dist/runner.d.ts +0 -314
  89. package/dist/runner.d.ts.map +0 -1
  90. package/dist/sandbox-manager.d.ts +0 -261
  91. package/dist/sandbox-manager.d.ts.map +0 -1
  92. package/dist/sandbox.d.ts +0 -267
  93. package/dist/sandbox.d.ts.map +0 -1
  94. package/dist/shared-modules.d.ts +0 -148
  95. package/dist/shared-modules.d.ts.map +0 -1
  96. package/dist/shared-resources.d.ts +0 -102
  97. package/dist/shared-resources.d.ts.map +0 -1
  98. package/dist/ts-libs.d.ts +0 -98
  99. package/dist/ts-libs.d.ts.map +0 -1
  100. package/dist/typechecker.d.ts +0 -127
  101. package/dist/typechecker.d.ts.map +0 -1
  102. package/src/build-emitter.ts +0 -64
  103. package/src/builder.ts +0 -498
  104. package/src/bundler.ts +0 -542
  105. package/src/commands/compile.ts +0 -236
  106. package/src/commands/packages.ts +0 -154
  107. package/src/commands/run.ts +0 -245
  108. package/src/internal.ts +0 -119
  109. package/src/loader.ts +0 -229
  110. package/src/packages.ts +0 -936
  111. package/src/sandbox.ts +0 -396
  112. package/src/shared-modules.ts +0 -280
  113. package/src/shared-resources.ts +0 -166
  114. package/src/ts-libs.ts +0 -320
  115. package/src/typechecker.ts +0 -635
package/src/internal.ts DELETED
@@ -1,119 +0,0 @@
1
- /**
2
- * Sandlot Internal APIs
3
- *
4
- * These exports are for advanced use cases and may change without notice.
5
- * Import from 'sandlot/internal' only when the main API doesn't meet your needs.
6
- *
7
- * @module sandlot/internal
8
- */
9
-
10
- // =============================================================================
11
- // Module URL Management
12
- // =============================================================================
13
-
14
- /**
15
- * Create a blob URL for a bundle. Prefer `loadModule` from 'sandlot' instead.
16
- * Remember to call `revokeModuleUrl()` when done to free memory.
17
- */
18
- export { createModuleUrl, revokeModuleUrl } from "./loader";
19
-
20
- // =============================================================================
21
- // Package Resolution Internals
22
- // =============================================================================
23
-
24
- /**
25
- * Parse a package specifier like "lodash@4.17.21" into name and version.
26
- */
27
- export { parsePackageSpec } from "./packages";
28
-
29
- /**
30
- * Resolve an import path to an esm.sh URL using installed package versions.
31
- */
32
- export { resolveToEsmUrl } from "./packages";
33
-
34
- /**
35
- * Parse an import path into package name and subpath.
36
- */
37
- export { parseImportPath } from "./packages";
38
-
39
- /**
40
- * In-memory cache for package types (DTS files from esm.sh).
41
- */
42
- export { InMemoryTypesCache } from "./packages";
43
-
44
- // =============================================================================
45
- // TypeScript Library Internals
46
- // =============================================================================
47
-
48
- /**
49
- * Parse `/// <reference lib="..." />` directives from TypeScript source.
50
- */
51
- export { parseLibReferences } from "./ts-libs";
52
-
53
- /**
54
- * Convert a lib name (e.g., "ES2020") to its filename (e.g., "lib.es2020.d.ts").
55
- */
56
- export { libNameToFileName } from "./ts-libs";
57
-
58
- /**
59
- * Extract the lib name from a filename (e.g., "lib.es2020.d.ts" -> "ES2020").
60
- */
61
- export { extractLibName } from "./ts-libs";
62
-
63
- /**
64
- * Fetch a single TypeScript lib file from CDN.
65
- */
66
- export { fetchLibFile } from "./ts-libs";
67
-
68
- /**
69
- * Fetch multiple TypeScript lib files, resolving dependencies.
70
- */
71
- export { fetchAllLibs } from "./ts-libs";
72
-
73
- /**
74
- * Cache for TypeScript lib files.
75
- */
76
- export { LibCache } from "./ts-libs";
77
-
78
- // =============================================================================
79
- // Shared Module Registry Internals
80
- // =============================================================================
81
-
82
- /**
83
- * The SharedModuleRegistry class. Prefer `registerSharedModules` from 'sandlot'.
84
- */
85
- export { SharedModuleRegistry } from "./shared-modules";
86
-
87
- /**
88
- * Get the global shared module registry instance.
89
- */
90
- export { getSharedModuleRegistry } from "./shared-modules";
91
-
92
- /**
93
- * Check if the global registry exists.
94
- */
95
- export { hasSharedModuleRegistry } from "./shared-modules";
96
-
97
- /**
98
- * Get the runtime code for accessing a shared module.
99
- */
100
- export { getSharedModuleRuntimeCode } from "./shared-modules";
101
-
102
- // =============================================================================
103
- // Command Internals
104
- // =============================================================================
105
-
106
- /**
107
- * Format esbuild messages for display.
108
- */
109
- export { formatEsbuildMessages } from "./commands";
110
-
111
- // =============================================================================
112
- // BuildEmitter (for custom build event handling)
113
- // =============================================================================
114
-
115
- /**
116
- * Build event emitter for sandbox environments.
117
- * Use this for custom build event handling in advanced use cases.
118
- */
119
- export { BuildEmitter } from "./build-emitter";
package/src/loader.ts DELETED
@@ -1,229 +0,0 @@
1
- /**
2
- * Module loader utilities for Sandlot bundles.
3
- *
4
- * Takes a BundleResult and turns it into usable JavaScript exports.
5
- * No external dependencies - just the basics for loading compiled code.
6
- *
7
- * @example
8
- * ```ts
9
- * // Capture the bundle via onBuild callback
10
- * let buildResult: BundleResult | null = null;
11
- * const unsubscribe = sandbox.onBuild((result) => {
12
- * buildResult = result;
13
- * });
14
- *
15
- * const cmd = await sandbox.bash.exec("build /src/index.ts");
16
- * if (cmd.exitCode === 0 && buildResult) {
17
- * // Load all exports
18
- * const module = await loadModule<{ add: (a: number, b: number) => number }>(buildResult);
19
- * console.log(module.add(1, 2)); // 3
20
- *
21
- * // Load a specific export
22
- * const add = await loadExport<(a: number, b: number) => number>(buildResult, "add");
23
- * console.log(add(1, 2)); // 3
24
- * }
25
- * unsubscribe();
26
- * ```
27
- */
28
-
29
- import type { BundleResult } from "./bundler";
30
-
31
- /**
32
- * Error thrown when loading a module fails
33
- */
34
- export class ModuleLoadError extends Error {
35
- constructor(message: string, cause?: unknown) {
36
- super(message, { cause });
37
- this.name = "ModuleLoadError";
38
- }
39
- }
40
-
41
- /**
42
- * Error thrown when an expected export is not found
43
- */
44
- export class ExportNotFoundError extends Error {
45
- constructor(
46
- public readonly exportName: string,
47
- public readonly availableExports: string[]
48
- ) {
49
- super(
50
- `Export "${exportName}" not found. Available exports: ${availableExports.length > 0 ? availableExports.join(", ") : "(none)"
51
- }`
52
- );
53
- this.name = "ExportNotFoundError";
54
- }
55
- }
56
-
57
- /**
58
- * Create a blob URL for a bundle that can be dynamically imported.
59
- * Remember to call `revokeModuleUrl()` when done to free memory.
60
- *
61
- * @param result - The bundle result from a successful build
62
- * @returns A blob URL that can be passed to `import()`
63
- *
64
- * @example
65
- * ```ts
66
- * const url = createModuleUrl(buildResult);
67
- * try {
68
- * const module = await import(url);
69
- * console.log(module.default);
70
- * } finally {
71
- * revokeModuleUrl(url);
72
- * }
73
- * ```
74
- */
75
- export function createModuleUrl(result: BundleResult): string {
76
- const blob = new Blob([result.code], { type: "application/javascript" });
77
- return URL.createObjectURL(blob);
78
- }
79
-
80
- /**
81
- * Revoke a blob URL created by `createModuleUrl()`.
82
- * This frees the memory associated with the blob.
83
- *
84
- * @param url - The blob URL to revoke
85
- */
86
- export function revokeModuleUrl(url: string): void {
87
- URL.revokeObjectURL(url);
88
- }
89
-
90
- /**
91
- * Load all exports from a bundle result.
92
- *
93
- * @typeParam T - The expected shape of the module's exports
94
- * @param result - The bundle result from a successful build
95
- * @returns A promise that resolves to the module's exports
96
- * @throws {ModuleLoadError} If the module fails to load
97
- *
98
- * @example
99
- * ```ts
100
- * interface MyModule {
101
- * add: (a: number, b: number) => number;
102
- * multiply: (a: number, b: number) => number;
103
- * }
104
- *
105
- * const module = await loadModule<MyModule>(buildResult);
106
- * console.log(module.add(2, 3)); // 5
107
- * console.log(module.multiply(2, 3)); // 6
108
- * ```
109
- */
110
- export async function loadModule<T = Record<string, unknown>>(
111
- result: BundleResult
112
- ): Promise<T> {
113
- const url = createModuleUrl(result);
114
- try {
115
- // @vite-ignore comment prevents Vite from trying to analyze this dynamic import
116
- const module = await import(/* @vite-ignore */ url);
117
- return module as T;
118
- } catch (err) {
119
- throw new ModuleLoadError(
120
- `Failed to load module: ${err instanceof Error ? err.message : String(err)}`,
121
- err
122
- );
123
- } finally {
124
- revokeModuleUrl(url);
125
- }
126
- }
127
-
128
- /**
129
- * Load a specific named export from a bundle result.
130
- *
131
- * @typeParam T - The expected type of the export
132
- * @param result - The bundle result from a successful build
133
- * @param exportName - The name of the export to retrieve (use "default" for default export)
134
- * @returns A promise that resolves to the export's value
135
- * @throws {ModuleLoadError} If the module fails to load
136
- * @throws {ExportNotFoundError} If the export doesn't exist
137
- *
138
- * @example
139
- * ```ts
140
- * // Load a named export
141
- * const add = await loadExport<(a: number, b: number) => number>(
142
- * buildResult,
143
- * "add"
144
- * );
145
- * console.log(add(2, 3)); // 5
146
- *
147
- * // Load the default export
148
- * const Calculator = await loadExport<typeof Calculator>(
149
- * buildResult,
150
- * "default"
151
- * );
152
- * ```
153
- */
154
- export async function loadExport<T = unknown>(
155
- result: BundleResult,
156
- exportName: string = "default"
157
- ): Promise<T> {
158
- const module = await loadModule<Record<string, unknown>>(result);
159
-
160
- if (!(exportName in module)) {
161
- const availableExports = Object.keys(module).filter(
162
- (key) => !key.startsWith("__")
163
- );
164
- throw new ExportNotFoundError(exportName, availableExports);
165
- }
166
-
167
- return module[exportName] as T;
168
- }
169
-
170
- /**
171
- * Load the default export from a bundle result.
172
- * Convenience wrapper around `loadExport(result, "default")`.
173
- *
174
- * @typeParam T - The expected type of the default export
175
- * @param result - The bundle result from a successful build
176
- * @returns A promise that resolves to the default export
177
- * @throws {ModuleLoadError} If the module fails to load
178
- * @throws {ExportNotFoundError} If there is no default export
179
- *
180
- * @example
181
- * ```ts
182
- * // For a module that does: export default function add(a, b) { return a + b; }
183
- * const add = await loadDefault<(a: number, b: number) => number>(buildResult);
184
- * console.log(add(2, 3)); // 5
185
- * ```
186
- */
187
- export async function loadDefault<T = unknown>(result: BundleResult): Promise<T> {
188
- return loadExport<T>(result, "default");
189
- }
190
-
191
- /**
192
- * Get a list of export names from a bundle result.
193
- * Useful for introspection or debugging.
194
- *
195
- * @param result - The bundle result from a successful build
196
- * @returns A promise that resolves to an array of export names
197
- *
198
- * @example
199
- * ```ts
200
- * const exports = await getExportNames(buildResult);
201
- * console.log(exports); // ["add", "multiply", "default"]
202
- * ```
203
- */
204
- export async function getExportNames(result: BundleResult): Promise<string[]> {
205
- const module = await loadModule<Record<string, unknown>>(result);
206
- return Object.keys(module).filter((key) => !key.startsWith("__"));
207
- }
208
-
209
- /**
210
- * Check if a bundle has a specific export.
211
- *
212
- * @param result - The bundle result from a successful build
213
- * @param exportName - The name of the export to check for
214
- * @returns A promise that resolves to true if the export exists
215
- *
216
- * @example
217
- * ```ts
218
- * if (await hasExport(buildResult, "add")) {
219
- * const add = await loadExport(buildResult, "add");
220
- * }
221
- * ```
222
- */
223
- export async function hasExport(
224
- result: BundleResult,
225
- exportName: string
226
- ): Promise<boolean> {
227
- const module = await loadModule<Record<string, unknown>>(result);
228
- return exportName in module;
229
- }