sandlot 0.1.4 → 0.2.1
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/dist/browser/bundler.d.ts +68 -0
- package/dist/browser/bundler.d.ts.map +1 -0
- package/dist/browser/executor.d.ts +46 -0
- package/dist/browser/executor.d.ts.map +1 -0
- package/dist/browser/index.d.ts +9 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +2690 -0
- package/dist/browser/preset.d.ts +63 -0
- package/dist/browser/preset.d.ts.map +1 -0
- package/dist/commands/index.d.ts +20 -11
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/types.d.ts +37 -130
- package/dist/commands/types.d.ts.map +1 -1
- package/dist/core/bundler-utils.d.ts +142 -0
- package/dist/core/bundler-utils.d.ts.map +1 -0
- package/dist/core/esm-types-resolver.d.ts +125 -0
- package/dist/core/esm-types-resolver.d.ts.map +1 -0
- package/dist/core/executor.d.ts +35 -0
- package/dist/core/executor.d.ts.map +1 -0
- package/dist/{fs.d.ts → core/fs.d.ts} +27 -29
- package/dist/core/fs.d.ts.map +1 -0
- package/dist/core/sandbox.d.ts +30 -0
- package/dist/core/sandbox.d.ts.map +1 -0
- package/dist/core/sandlot.d.ts +30 -0
- package/dist/core/sandlot.d.ts.map +1 -0
- package/dist/core/shared-module-registry.d.ts +46 -0
- package/dist/core/shared-module-registry.d.ts.map +1 -0
- package/dist/core/typechecker.d.ts +60 -0
- package/dist/core/typechecker.d.ts.map +1 -0
- package/dist/index.d.ts +11 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1398 -2010
- package/dist/node/bundler.d.ts +48 -0
- package/dist/node/bundler.d.ts.map +1 -0
- package/dist/node/executor.d.ts +48 -0
- package/dist/node/executor.d.ts.map +1 -0
- package/dist/node/index.d.ts +9 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +2644 -0
- package/dist/node/preset.d.ts +62 -0
- package/dist/node/preset.d.ts.map +1 -0
- package/dist/types.d.ts +528 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +16 -6
- package/src/browser/bundler.ts +294 -0
- package/src/browser/executor.ts +71 -0
- package/src/browser/index.ts +57 -0
- package/src/browser/preset.ts +179 -0
- package/src/commands/index.ts +498 -37
- package/src/commands/types.ts +117 -145
- package/src/core/bundler-utils.ts +630 -0
- package/src/core/esm-types-resolver.ts +432 -0
- package/src/core/executor.ts +161 -0
- package/src/{fs.ts → core/fs.ts} +59 -37
- package/src/core/sandbox.ts +624 -0
- package/src/core/sandlot.ts +77 -0
- package/src/core/shared-module-registry.ts +138 -0
- package/src/core/typechecker.ts +609 -0
- package/src/index.ts +106 -139
- package/src/node/bundler.ts +194 -0
- package/src/node/executor.ts +87 -0
- package/src/node/index.ts +39 -0
- package/src/node/preset.ts +178 -0
- package/src/types.ts +672 -0
- package/README.md +0 -243
- package/dist/build-emitter.d.ts +0 -47
- package/dist/build-emitter.d.ts.map +0 -1
- package/dist/builder.d.ts +0 -370
- package/dist/builder.d.ts.map +0 -1
- package/dist/bundler.d.ts +0 -152
- package/dist/bundler.d.ts.map +0 -1
- package/dist/commands/compile.d.ts +0 -13
- package/dist/commands/compile.d.ts.map +0 -1
- package/dist/commands/packages.d.ts +0 -17
- package/dist/commands/packages.d.ts.map +0 -1
- package/dist/commands/run.d.ts +0 -40
- package/dist/commands/run.d.ts.map +0 -1
- package/dist/commands.d.ts +0 -179
- package/dist/commands.d.ts.map +0 -1
- package/dist/fs.d.ts.map +0 -1
- package/dist/internal.d.ts +0 -79
- package/dist/internal.d.ts.map +0 -1
- package/dist/internal.js +0 -1942
- package/dist/loader.d.ts +0 -164
- package/dist/loader.d.ts.map +0 -1
- package/dist/packages.d.ts +0 -199
- package/dist/packages.d.ts.map +0 -1
- package/dist/runner.d.ts +0 -314
- package/dist/runner.d.ts.map +0 -1
- package/dist/sandbox-manager.d.ts +0 -261
- package/dist/sandbox-manager.d.ts.map +0 -1
- package/dist/sandbox.d.ts +0 -267
- package/dist/sandbox.d.ts.map +0 -1
- package/dist/shared-modules.d.ts +0 -148
- package/dist/shared-modules.d.ts.map +0 -1
- package/dist/shared-resources.d.ts +0 -102
- package/dist/shared-resources.d.ts.map +0 -1
- package/dist/ts-libs.d.ts +0 -85
- package/dist/ts-libs.d.ts.map +0 -1
- package/dist/typechecker.d.ts +0 -127
- package/dist/typechecker.d.ts.map +0 -1
- package/src/build-emitter.ts +0 -64
- package/src/builder.ts +0 -498
- package/src/bundler.ts +0 -575
- package/src/commands/compile.ts +0 -236
- package/src/commands/packages.ts +0 -154
- package/src/commands/run.ts +0 -245
- package/src/internal.ts +0 -119
- package/src/loader.ts +0 -229
- package/src/packages.ts +0 -936
- package/src/sandbox.ts +0 -398
- package/src/shared-modules.ts +0 -280
- package/src/shared-resources.ts +0 -166
- package/src/ts-libs.ts +0 -218
- package/src/typechecker.ts +0 -635
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ISharedModuleRegistry } from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a unique instance ID for a registry.
|
|
5
|
+
* Uses crypto.randomUUID if available, otherwise falls back to a simple counter + timestamp.
|
|
6
|
+
*/
|
|
7
|
+
let instanceCounter = 0;
|
|
8
|
+
function generateInstanceId(): string {
|
|
9
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
10
|
+
return crypto.randomUUID().slice(0, 8);
|
|
11
|
+
}
|
|
12
|
+
// Fallback for environments without crypto.randomUUID
|
|
13
|
+
return `${Date.now().toString(36)}_${(++instanceCounter).toString(36)}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Simple shared module registry implementation.
|
|
18
|
+
* Created internally by Sandlot from the sharedModules option.
|
|
19
|
+
*
|
|
20
|
+
* Each registry instance has a unique key (e.g., `__sandlot_abc123__`) that
|
|
21
|
+
* isolates it from other Sandlot instances. This allows multiple Sandlot
|
|
22
|
+
* instances to run concurrently with separate shared module configurations.
|
|
23
|
+
*
|
|
24
|
+
* The registry must be exposed globally for bundled code to access shared modules
|
|
25
|
+
* at runtime. Call `exposeGlobally()` after creation, or use `createSharedModuleRegistry()`
|
|
26
|
+
* which does this automatically.
|
|
27
|
+
*/
|
|
28
|
+
export class SharedModuleRegistry implements ISharedModuleRegistry {
|
|
29
|
+
private modules: Map<string, unknown>;
|
|
30
|
+
private exportNamesMap: Map<string, string[]>;
|
|
31
|
+
private _registryKey: string;
|
|
32
|
+
|
|
33
|
+
constructor(modules: Record<string, unknown>) {
|
|
34
|
+
this._registryKey = `__sandlot_${generateInstanceId()}__`;
|
|
35
|
+
this.modules = new Map(Object.entries(modules));
|
|
36
|
+
this.exportNamesMap = new Map();
|
|
37
|
+
|
|
38
|
+
// Introspect exports for each module
|
|
39
|
+
for (const [id, mod] of this.modules) {
|
|
40
|
+
this.exportNamesMap.set(id, this.introspectExports(mod));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The unique global key where this registry is exposed.
|
|
46
|
+
* Bundled code accesses the registry via `globalThis[registryKey]`.
|
|
47
|
+
*/
|
|
48
|
+
get registryKey(): string {
|
|
49
|
+
return this._registryKey;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Expose this registry globally so bundled code can access shared modules.
|
|
54
|
+
* This sets globalThis[this.registryKey] = this.
|
|
55
|
+
*/
|
|
56
|
+
exposeGlobally(): this {
|
|
57
|
+
(globalThis as Record<string, unknown>)[this._registryKey] = this;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Remove this registry from global scope.
|
|
63
|
+
*/
|
|
64
|
+
removeFromGlobal(): void {
|
|
65
|
+
if ((globalThis as Record<string, unknown>)[this._registryKey] === this) {
|
|
66
|
+
delete (globalThis as Record<string, unknown>)[this._registryKey];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get(moduleId: string): unknown {
|
|
71
|
+
const mod = this.modules.get(moduleId);
|
|
72
|
+
if (mod === undefined && !this.modules.has(moduleId)) {
|
|
73
|
+
throw new Error(`Shared module "${moduleId}" not registered.`);
|
|
74
|
+
}
|
|
75
|
+
return mod;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
has(moduleId: string): boolean {
|
|
79
|
+
return this.modules.has(moduleId);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getExportNames(moduleId: string): string[] {
|
|
83
|
+
return this.exportNamesMap.get(moduleId) ?? [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
list(): string[] {
|
|
87
|
+
return [...this.modules.keys()];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private introspectExports(module: unknown): string[] {
|
|
91
|
+
if (module === null || module === undefined) {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
if (typeof module !== "object" && typeof module !== "function") {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const exports: string[] = [];
|
|
99
|
+
for (const key of Object.keys(module as object)) {
|
|
100
|
+
if (this.isValidIdentifier(key)) {
|
|
101
|
+
exports.push(key);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return exports;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private isValidIdentifier(name: string): boolean {
|
|
108
|
+
if (name.length === 0) return false;
|
|
109
|
+
if (!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)) return false;
|
|
110
|
+
const reserved = [
|
|
111
|
+
"default",
|
|
112
|
+
"class",
|
|
113
|
+
"function",
|
|
114
|
+
"var",
|
|
115
|
+
"let",
|
|
116
|
+
"const",
|
|
117
|
+
"import",
|
|
118
|
+
"export",
|
|
119
|
+
];
|
|
120
|
+
return !reserved.includes(name);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Create a shared module registry from a modules object.
|
|
126
|
+
* Automatically exposes the registry globally for bundled code to access.
|
|
127
|
+
* Returns null if no modules are provided.
|
|
128
|
+
*/
|
|
129
|
+
export function createSharedModuleRegistry(
|
|
130
|
+
modules?: Record<string, unknown>
|
|
131
|
+
): SharedModuleRegistry | null {
|
|
132
|
+
if (!modules || Object.keys(modules).length === 0) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const registry = new SharedModuleRegistry(modules);
|
|
136
|
+
registry.exposeGlobally();
|
|
137
|
+
return registry;
|
|
138
|
+
}
|