@run0/jiki 0.1.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/dist/browser-bundle.d.ts +40 -0
- package/dist/builtins.d.ts +22 -0
- package/dist/code-transform.d.ts +7 -0
- package/dist/config/cdn.d.ts +13 -0
- package/dist/container.d.ts +101 -0
- package/dist/dev-server.d.ts +69 -0
- package/dist/errors.d.ts +19 -0
- package/dist/frameworks/code-transforms.d.ts +32 -0
- package/dist/frameworks/next-api-handler.d.ts +72 -0
- package/dist/frameworks/next-dev-server.d.ts +141 -0
- package/dist/frameworks/next-html-generator.d.ts +36 -0
- package/dist/frameworks/next-route-resolver.d.ts +19 -0
- package/dist/frameworks/next-shims.d.ts +78 -0
- package/dist/frameworks/remix-dev-server.d.ts +47 -0
- package/dist/frameworks/sveltekit-dev-server.d.ts +43 -0
- package/dist/frameworks/vite-dev-server.d.ts +50 -0
- package/dist/fs-errors.d.ts +36 -0
- package/dist/index.cjs +14916 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.mjs +14898 -0
- package/dist/index.mjs.map +1 -0
- package/dist/kernel.d.ts +48 -0
- package/dist/memfs.d.ts +144 -0
- package/dist/metrics.d.ts +78 -0
- package/dist/module-resolver.d.ts +60 -0
- package/dist/network-interceptor.d.ts +71 -0
- package/dist/npm/cache.d.ts +76 -0
- package/dist/npm/index.d.ts +60 -0
- package/dist/npm/lockfile-reader.d.ts +32 -0
- package/dist/npm/pnpm.d.ts +18 -0
- package/dist/npm/registry.d.ts +45 -0
- package/dist/npm/resolver.d.ts +39 -0
- package/dist/npm/sync-installer.d.ts +18 -0
- package/dist/npm/tarball.d.ts +4 -0
- package/dist/npm/workspaces.d.ts +46 -0
- package/dist/persistence.d.ts +94 -0
- package/dist/plugin.d.ts +156 -0
- package/dist/polyfills/assert.d.ts +30 -0
- package/dist/polyfills/child_process.d.ts +116 -0
- package/dist/polyfills/chokidar.d.ts +18 -0
- package/dist/polyfills/crypto.d.ts +49 -0
- package/dist/polyfills/events.d.ts +28 -0
- package/dist/polyfills/fs.d.ts +82 -0
- package/dist/polyfills/http.d.ts +147 -0
- package/dist/polyfills/module.d.ts +29 -0
- package/dist/polyfills/net.d.ts +53 -0
- package/dist/polyfills/os.d.ts +91 -0
- package/dist/polyfills/path.d.ts +96 -0
- package/dist/polyfills/perf_hooks.d.ts +21 -0
- package/dist/polyfills/process.d.ts +99 -0
- package/dist/polyfills/querystring.d.ts +15 -0
- package/dist/polyfills/readdirp.d.ts +18 -0
- package/dist/polyfills/readline.d.ts +32 -0
- package/dist/polyfills/stream.d.ts +106 -0
- package/dist/polyfills/stubs.d.ts +737 -0
- package/dist/polyfills/tty.d.ts +25 -0
- package/dist/polyfills/url.d.ts +41 -0
- package/dist/polyfills/util.d.ts +61 -0
- package/dist/polyfills/v8.d.ts +43 -0
- package/dist/polyfills/vm.d.ts +76 -0
- package/dist/polyfills/worker-threads.d.ts +77 -0
- package/dist/polyfills/ws.d.ts +32 -0
- package/dist/polyfills/zlib.d.ts +87 -0
- package/dist/runtime-helpers.d.ts +4 -0
- package/dist/runtime-interface.d.ts +39 -0
- package/dist/sandbox.d.ts +69 -0
- package/dist/server-bridge.d.ts +55 -0
- package/dist/shell-commands.d.ts +2 -0
- package/dist/shell.d.ts +101 -0
- package/dist/transpiler.d.ts +47 -0
- package/dist/type-checker.d.ts +57 -0
- package/dist/types/package-json.d.ts +17 -0
- package/dist/utils/binary-encoding.d.ts +4 -0
- package/dist/utils/hash.d.ts +6 -0
- package/dist/utils/safe-path.d.ts +6 -0
- package/dist/worker-runtime.d.ts +34 -0
- package/package.json +59 -0
- package/src/browser-bundle.ts +498 -0
- package/src/builtins.ts +222 -0
- package/src/code-transform.ts +183 -0
- package/src/config/cdn.ts +17 -0
- package/src/container.ts +343 -0
- package/src/dev-server.ts +322 -0
- package/src/errors.ts +604 -0
- package/src/frameworks/code-transforms.ts +667 -0
- package/src/frameworks/next-api-handler.ts +366 -0
- package/src/frameworks/next-dev-server.ts +1252 -0
- package/src/frameworks/next-html-generator.ts +585 -0
- package/src/frameworks/next-route-resolver.ts +521 -0
- package/src/frameworks/next-shims.ts +1084 -0
- package/src/frameworks/remix-dev-server.ts +163 -0
- package/src/frameworks/sveltekit-dev-server.ts +197 -0
- package/src/frameworks/vite-dev-server.ts +370 -0
- package/src/fs-errors.ts +118 -0
- package/src/index.ts +188 -0
- package/src/kernel.ts +381 -0
- package/src/memfs.ts +1006 -0
- package/src/metrics.ts +140 -0
- package/src/module-resolver.ts +511 -0
- package/src/network-interceptor.ts +143 -0
- package/src/npm/cache.ts +172 -0
- package/src/npm/index.ts +377 -0
- package/src/npm/lockfile-reader.ts +105 -0
- package/src/npm/pnpm.ts +108 -0
- package/src/npm/registry.ts +120 -0
- package/src/npm/resolver.ts +339 -0
- package/src/npm/sync-installer.ts +217 -0
- package/src/npm/tarball.ts +136 -0
- package/src/npm/workspaces.ts +255 -0
- package/src/persistence.ts +235 -0
- package/src/plugin.ts +293 -0
- package/src/polyfills/assert.ts +164 -0
- package/src/polyfills/child_process.ts +535 -0
- package/src/polyfills/chokidar.ts +52 -0
- package/src/polyfills/crypto.ts +433 -0
- package/src/polyfills/events.ts +178 -0
- package/src/polyfills/fs.ts +297 -0
- package/src/polyfills/http.ts +478 -0
- package/src/polyfills/module.ts +97 -0
- package/src/polyfills/net.ts +123 -0
- package/src/polyfills/os.ts +108 -0
- package/src/polyfills/path.ts +169 -0
- package/src/polyfills/perf_hooks.ts +30 -0
- package/src/polyfills/process.ts +349 -0
- package/src/polyfills/querystring.ts +66 -0
- package/src/polyfills/readdirp.ts +72 -0
- package/src/polyfills/readline.ts +80 -0
- package/src/polyfills/stream.ts +610 -0
- package/src/polyfills/stubs.ts +600 -0
- package/src/polyfills/tty.ts +43 -0
- package/src/polyfills/url.ts +97 -0
- package/src/polyfills/util.ts +173 -0
- package/src/polyfills/v8.ts +62 -0
- package/src/polyfills/vm.ts +111 -0
- package/src/polyfills/worker-threads.ts +189 -0
- package/src/polyfills/ws.ts +73 -0
- package/src/polyfills/zlib.ts +244 -0
- package/src/runtime-helpers.ts +83 -0
- package/src/runtime-interface.ts +46 -0
- package/src/sandbox.ts +178 -0
- package/src/server-bridge.ts +473 -0
- package/src/service-worker.ts +153 -0
- package/src/shell-commands.ts +708 -0
- package/src/shell.ts +795 -0
- package/src/transpiler.ts +282 -0
- package/src/type-checker.ts +241 -0
- package/src/types/package-json.ts +17 -0
- package/src/utils/binary-encoding.ts +38 -0
- package/src/utils/hash.ts +24 -0
- package/src/utils/safe-path.ts +38 -0
- package/src/worker-runtime.ts +42 -0
package/src/builtins.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// Core polyfills loaded eagerly (used by almost every container).
|
|
2
|
+
import * as _pathMod from "./polyfills/path";
|
|
3
|
+
import _eventsMod from "./polyfills/events";
|
|
4
|
+
import _streamMod, { bufferModule as _bufferMod } from "./polyfills/stream";
|
|
5
|
+
|
|
6
|
+
export { _bufferMod as bufferPolyfill };
|
|
7
|
+
|
|
8
|
+
// Secondary polyfills — statically imported but only evaluated via the
|
|
9
|
+
// registry factory on first require(). The `loaded` cache in resolveBuiltin
|
|
10
|
+
// ensures each factory runs at most once. While the modules themselves are
|
|
11
|
+
// loaded at bundle time, no per-module initialization runs until first use.
|
|
12
|
+
import * as _httpMod from "./polyfills/http";
|
|
13
|
+
import * as _netMod from "./polyfills/net";
|
|
14
|
+
import * as _urlMod from "./polyfills/url";
|
|
15
|
+
import * as _qsMod from "./polyfills/querystring";
|
|
16
|
+
import * as _utilMod from "./polyfills/util";
|
|
17
|
+
import * as _ttyMod from "./polyfills/tty";
|
|
18
|
+
import * as _osMod from "./polyfills/os";
|
|
19
|
+
import * as _cryptoMod from "./polyfills/crypto";
|
|
20
|
+
import * as _zlibMod from "./polyfills/zlib";
|
|
21
|
+
import * as _cpMod from "./polyfills/child_process";
|
|
22
|
+
import _assertMod from "./polyfills/assert";
|
|
23
|
+
import * as _readlineMod from "./polyfills/readline";
|
|
24
|
+
import * as _v8Mod from "./polyfills/v8";
|
|
25
|
+
import * as _vmMod from "./polyfills/vm";
|
|
26
|
+
import * as _moduleMod from "./polyfills/module";
|
|
27
|
+
import * as _perfHooksMod from "./polyfills/perf_hooks";
|
|
28
|
+
import * as _chokidarMod from "./polyfills/chokidar";
|
|
29
|
+
import * as _wsMod from "./polyfills/ws";
|
|
30
|
+
import * as _readdirpMod from "./polyfills/readdirp";
|
|
31
|
+
|
|
32
|
+
// Stubs are imported but each stub is only constructed via its IIFE when
|
|
33
|
+
// first accessed through the registry factory.
|
|
34
|
+
import * as _stubsMod from "./polyfills/stubs";
|
|
35
|
+
import _workerThreadsMod from "./polyfills/worker-threads";
|
|
36
|
+
|
|
37
|
+
function cloneNamespace(ns: Record<string, unknown>): Record<string, unknown> {
|
|
38
|
+
const copy: Record<string, unknown> = {};
|
|
39
|
+
for (const k of Object.keys(ns)) copy[k] = ns[k];
|
|
40
|
+
return copy;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class StringDecoder {
|
|
44
|
+
private enc: string;
|
|
45
|
+
constructor(encoding?: string) {
|
|
46
|
+
this.enc = encoding || "utf8";
|
|
47
|
+
}
|
|
48
|
+
write(buf: Uint8Array): string {
|
|
49
|
+
return new TextDecoder(this.enc).decode(buf);
|
|
50
|
+
}
|
|
51
|
+
end(buf?: Uint8Array): string {
|
|
52
|
+
return buf ? this.write(buf) : "";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const FS_CONSTANTS = {
|
|
57
|
+
O_RDONLY: 0,
|
|
58
|
+
O_WRONLY: 1,
|
|
59
|
+
O_RDWR: 2,
|
|
60
|
+
O_CREAT: 64,
|
|
61
|
+
O_EXCL: 128,
|
|
62
|
+
O_TRUNC: 512,
|
|
63
|
+
O_APPEND: 1024,
|
|
64
|
+
O_DIRECTORY: 65536,
|
|
65
|
+
O_NOFOLLOW: 131072,
|
|
66
|
+
O_SYNC: 1052672,
|
|
67
|
+
F_OK: 0,
|
|
68
|
+
R_OK: 4,
|
|
69
|
+
W_OK: 2,
|
|
70
|
+
X_OK: 1,
|
|
71
|
+
S_IFMT: 61440,
|
|
72
|
+
S_IFREG: 32768,
|
|
73
|
+
S_IFDIR: 16384,
|
|
74
|
+
S_IFLNK: 40960,
|
|
75
|
+
S_IRWXU: 448,
|
|
76
|
+
S_IRUSR: 256,
|
|
77
|
+
S_IWUSR: 128,
|
|
78
|
+
S_IXUSR: 64,
|
|
79
|
+
UV_FS_COPYFILE_EXCL: 1,
|
|
80
|
+
COPYFILE_EXCL: 1,
|
|
81
|
+
UV_FS_COPYFILE_FICLONE: 2,
|
|
82
|
+
} as const;
|
|
83
|
+
|
|
84
|
+
type Loader = () => unknown;
|
|
85
|
+
|
|
86
|
+
const registry: Record<string, Loader> = {
|
|
87
|
+
path: () => _pathMod,
|
|
88
|
+
events: () => _eventsMod,
|
|
89
|
+
stream: () => _streamMod,
|
|
90
|
+
buffer: () => _bufferMod,
|
|
91
|
+
http: () => cloneNamespace(_httpMod as unknown as Record<string, unknown>),
|
|
92
|
+
https: () => (_httpMod as any).httpsModule,
|
|
93
|
+
net: () => _netMod,
|
|
94
|
+
url: () => _urlMod,
|
|
95
|
+
querystring: () => _qsMod,
|
|
96
|
+
util: () => _utilMod,
|
|
97
|
+
tty: () => _ttyMod,
|
|
98
|
+
os: () => _osMod,
|
|
99
|
+
crypto: () => _cryptoMod,
|
|
100
|
+
zlib: () => _zlibMod,
|
|
101
|
+
child_process: () => _cpMod,
|
|
102
|
+
assert: () => _assertMod,
|
|
103
|
+
readline: () => _readlineMod,
|
|
104
|
+
v8: () => _v8Mod,
|
|
105
|
+
vm: () => _vmMod,
|
|
106
|
+
module: () => _moduleMod,
|
|
107
|
+
perf_hooks: () => _perfHooksMod,
|
|
108
|
+
chokidar: () => _chokidarMod,
|
|
109
|
+
ws: () => _wsMod,
|
|
110
|
+
readdirp: () => _readdirpMod,
|
|
111
|
+
|
|
112
|
+
async_hooks: () => _stubsMod.async_hooks,
|
|
113
|
+
cluster: () => _stubsMod.cluster,
|
|
114
|
+
dgram: () => _stubsMod.dgram,
|
|
115
|
+
diagnostics_channel: () => _stubsMod.diagnostics_channel,
|
|
116
|
+
dns: () => _stubsMod.dns,
|
|
117
|
+
domain: () => _stubsMod.domain,
|
|
118
|
+
esbuild: () => _stubsMod.esbuild,
|
|
119
|
+
fsevents: () => _stubsMod.fsevents,
|
|
120
|
+
http2: () => _stubsMod.http2,
|
|
121
|
+
inspector: () => _stubsMod.inspector,
|
|
122
|
+
"inspector/promises": () => _stubsMod.inspector,
|
|
123
|
+
rollup: () => _stubsMod.rollup_stub,
|
|
124
|
+
tls: () => _stubsMod.tls,
|
|
125
|
+
worker_threads: () => _workerThreadsMod,
|
|
126
|
+
|
|
127
|
+
constants: () => FS_CONSTANTS,
|
|
128
|
+
string_decoder: () => ({ StringDecoder }),
|
|
129
|
+
timers: () => ({
|
|
130
|
+
setTimeout: globalThis.setTimeout.bind(globalThis),
|
|
131
|
+
setInterval: globalThis.setInterval.bind(globalThis),
|
|
132
|
+
setImmediate: (fn: () => void) => setTimeout(fn, 0),
|
|
133
|
+
clearTimeout: globalThis.clearTimeout.bind(globalThis),
|
|
134
|
+
clearInterval: globalThis.clearInterval.bind(globalThis),
|
|
135
|
+
clearImmediate: globalThis.clearTimeout.bind(globalThis),
|
|
136
|
+
}),
|
|
137
|
+
_http_common: () => ({}),
|
|
138
|
+
_http_incoming: () => ({}),
|
|
139
|
+
_http_outgoing: () => ({}),
|
|
140
|
+
console: () => ({
|
|
141
|
+
...console,
|
|
142
|
+
Console: class Console {
|
|
143
|
+
log(...a: unknown[]) {
|
|
144
|
+
console.log(...a);
|
|
145
|
+
}
|
|
146
|
+
error(...a: unknown[]) {
|
|
147
|
+
console.error(...a);
|
|
148
|
+
}
|
|
149
|
+
warn(...a: unknown[]) {
|
|
150
|
+
console.warn(...a);
|
|
151
|
+
}
|
|
152
|
+
info(...a: unknown[]) {
|
|
153
|
+
console.info(...a);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
"util/types": () => _utilMod.types,
|
|
158
|
+
"path/posix": () => _pathMod,
|
|
159
|
+
"path/win32": () => _pathMod.win32,
|
|
160
|
+
"timers/promises": () => ({
|
|
161
|
+
setTimeout: (ms: number) => new Promise(r => setTimeout(r, ms)),
|
|
162
|
+
setInterval: globalThis.setInterval,
|
|
163
|
+
setImmediate: (v?: unknown) => new Promise(r => setTimeout(() => r(v), 0)),
|
|
164
|
+
scheduler: { wait: (ms: number) => new Promise(r => setTimeout(r, ms)) },
|
|
165
|
+
}),
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const loaded: Record<string, unknown> = {};
|
|
169
|
+
|
|
170
|
+
export function resolveBuiltin(name: string): unknown | undefined {
|
|
171
|
+
if (loaded[name] !== undefined) return loaded[name];
|
|
172
|
+
const loader = registry[name];
|
|
173
|
+
if (!loader) return undefined;
|
|
174
|
+
const result = loader();
|
|
175
|
+
loaded[name] = result;
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function isBuiltinModule(name: string): boolean {
|
|
180
|
+
return (
|
|
181
|
+
name in registry ||
|
|
182
|
+
name === "fs" ||
|
|
183
|
+
name === "fs/promises" ||
|
|
184
|
+
name === "process"
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Register a custom polyfill for a built-in module name.
|
|
190
|
+
* Replaces any existing polyfill for the same name.
|
|
191
|
+
* The factory is called lazily on first `require()`.
|
|
192
|
+
*/
|
|
193
|
+
export function registerBuiltin(name: string, factory: () => unknown): void {
|
|
194
|
+
registry[name] = factory;
|
|
195
|
+
// Clear loaded cache so the new factory takes effect on next require().
|
|
196
|
+
delete loaded[name];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Register multiple custom polyfills at once.
|
|
201
|
+
*/
|
|
202
|
+
export function registerBuiltins(map: Record<string, () => unknown>): void {
|
|
203
|
+
for (const [name, factory] of Object.entries(map)) {
|
|
204
|
+
registerBuiltin(name, factory);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Unregister a custom polyfill, reverting to the default (if any was
|
|
210
|
+
* overridden) or removing the module entirely.
|
|
211
|
+
*/
|
|
212
|
+
export function unregisterBuiltin(name: string): void {
|
|
213
|
+
delete registry[name];
|
|
214
|
+
delete loaded[name];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Return a list of all registered builtin module names. */
|
|
218
|
+
export function listBuiltins(): string[] {
|
|
219
|
+
return Object.keys(registry);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export const builtinModules = loaded;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import * as acorn from "acorn";
|
|
2
|
+
import acornJsx from "acorn-jsx";
|
|
3
|
+
import * as pathShim from "./polyfills/path";
|
|
4
|
+
import { transformEsmToCjsSimple } from "./frameworks/code-transforms";
|
|
5
|
+
import { simpleHash } from "./utils/hash";
|
|
6
|
+
|
|
7
|
+
const jsxParser = acorn.Parser.extend(acornJsx());
|
|
8
|
+
import {
|
|
9
|
+
needsTranspilation,
|
|
10
|
+
transpileSync,
|
|
11
|
+
transpile,
|
|
12
|
+
isInitialized,
|
|
13
|
+
hasSyncSupport,
|
|
14
|
+
} from "./transpiler";
|
|
15
|
+
|
|
16
|
+
const tsCache = new Map<string, string>();
|
|
17
|
+
|
|
18
|
+
export { tsCache as transpileCache };
|
|
19
|
+
|
|
20
|
+
export function removeShebang(source: string): string {
|
|
21
|
+
return source.charCodeAt(0) === 0x23 && source.charCodeAt(1) === 0x21
|
|
22
|
+
? source.slice(source.indexOf("\n") + 1)
|
|
23
|
+
: source;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function rewriteDynamicImports(source: string): string {
|
|
27
|
+
return source.replace(/(?<![.$\w])import\s*\(/g, "__dynamicImport(");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function visitAstNodes(root: any, fn: (n: any) => void): void {
|
|
31
|
+
if (!root || typeof root !== "object") return;
|
|
32
|
+
if (typeof root.type === "string") fn(root);
|
|
33
|
+
const skip = new Set(["type", "start", "end", "loc", "range"]);
|
|
34
|
+
for (const prop of Object.keys(root)) {
|
|
35
|
+
if (skip.has(prop)) continue;
|
|
36
|
+
const val = root[prop];
|
|
37
|
+
if (!val || typeof val !== "object") continue;
|
|
38
|
+
if (Array.isArray(val)) {
|
|
39
|
+
for (const el of val) {
|
|
40
|
+
if (el && typeof el === "object" && typeof el.type === "string") {
|
|
41
|
+
visitAstNodes(el, fn);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
} else if (typeof val.type === "string") {
|
|
45
|
+
visitAstNodes(val, fn);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function esmToCjsViaAst(source: string, _file: string): string {
|
|
51
|
+
const tree = jsxParser.parse(source, {
|
|
52
|
+
ecmaVersion: "latest",
|
|
53
|
+
sourceType: "module",
|
|
54
|
+
}) as any;
|
|
55
|
+
|
|
56
|
+
const patches: { from: number; to: number; text: string }[] = [];
|
|
57
|
+
|
|
58
|
+
visitAstNodes(tree, (node: any) => {
|
|
59
|
+
if (
|
|
60
|
+
node.type === "MetaProperty" &&
|
|
61
|
+
node.meta?.name === "import" &&
|
|
62
|
+
node.property?.name === "meta"
|
|
63
|
+
) {
|
|
64
|
+
patches.push({ from: node.start, to: node.end, text: "import_meta" });
|
|
65
|
+
}
|
|
66
|
+
if (node.type === "ImportExpression") {
|
|
67
|
+
patches.push({
|
|
68
|
+
from: node.start,
|
|
69
|
+
to: node.start + 6,
|
|
70
|
+
text: "__dynamicImport",
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const usesImportDecl = tree.body.some(
|
|
76
|
+
(n: any) => n.type === "ImportDeclaration",
|
|
77
|
+
);
|
|
78
|
+
const usesExportDecl = tree.body.some((n: any) =>
|
|
79
|
+
n.type?.startsWith("Export"),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
let result = source;
|
|
83
|
+
patches.sort((a, b) => b.from - a.from);
|
|
84
|
+
for (const { from, to, text } of patches) {
|
|
85
|
+
result = result.slice(0, from) + text + result.slice(to);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (usesImportDecl || usesExportDecl) {
|
|
89
|
+
result = transformEsmToCjsSimple(result);
|
|
90
|
+
if (usesExportDecl) {
|
|
91
|
+
result =
|
|
92
|
+
'Object.defineProperty(exports, "__esModule", { value: true });\n' +
|
|
93
|
+
result;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function esmToCjsViaRegex(source: string, file: string): string {
|
|
100
|
+
let out = source;
|
|
101
|
+
const dir = pathShim.dirname(file);
|
|
102
|
+
out = out.replace(/\bimport\.meta\.url\b/g, `import_meta.url`);
|
|
103
|
+
out = out.replace(/\bimport\.meta\.dirname\b/g, `import_meta.dirname`);
|
|
104
|
+
out = out.replace(/\bimport\.meta\.filename\b/g, `import_meta.filename`);
|
|
105
|
+
out = out.replace(/\bimport\.meta\b/g, `import_meta`);
|
|
106
|
+
out = rewriteDynamicImports(out);
|
|
107
|
+
const hasImport = /\bimport\s+[\w{*'"]/m.test(source);
|
|
108
|
+
const hasExport =
|
|
109
|
+
/\bexport\s+(?:default|const|let|var|function|class|{|\*)/m.test(source);
|
|
110
|
+
if (hasImport || hasExport) {
|
|
111
|
+
out = transformEsmToCjsSimple(out);
|
|
112
|
+
if (hasExport) {
|
|
113
|
+
out =
|
|
114
|
+
'Object.defineProperty(exports, "__esModule", { value: true });\n' +
|
|
115
|
+
out;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function transformEsmToCjs(source: string, file: string): string {
|
|
122
|
+
if (!/\bimport\b|\bexport\b/.test(source)) return source;
|
|
123
|
+
try {
|
|
124
|
+
return esmToCjsViaAst(source, file);
|
|
125
|
+
} catch {
|
|
126
|
+
return esmToCjsViaRegex(source, file);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function processSource(
|
|
131
|
+
source: string,
|
|
132
|
+
file: string,
|
|
133
|
+
sourceMaps?: boolean,
|
|
134
|
+
): string {
|
|
135
|
+
if (needsTranspilation(file)) {
|
|
136
|
+
const key = `${file}:${simpleHash(source)}`;
|
|
137
|
+
const hit = tsCache.get(key);
|
|
138
|
+
if (hit !== undefined) return hit;
|
|
139
|
+
if (!isInitialized()) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Transpiler not initialized. Call kernel.init() before loading TypeScript files (${file}).`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (hasSyncSupport()) {
|
|
145
|
+
const compiled = transpileSync(source, file, { sourcemap: sourceMaps });
|
|
146
|
+
const result = transformEsmToCjs(compiled, file);
|
|
147
|
+
tsCache.set(key, result);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
throw new Error(
|
|
151
|
+
`File "${file}" has not been pre-transpiled. Call kernel.prepareFile("${file}") before running TypeScript in the browser.`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return transformEsmToCjs(source, file);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function getProcessedSource(
|
|
158
|
+
rawSource: string,
|
|
159
|
+
filepath: string,
|
|
160
|
+
cache: Map<string, string>,
|
|
161
|
+
sourceMaps?: boolean,
|
|
162
|
+
): string {
|
|
163
|
+
let source = removeShebang(rawSource);
|
|
164
|
+
const hash = simpleHash(source);
|
|
165
|
+
const key = `${filepath}:${hash}`;
|
|
166
|
+
const cached = cache.get(key);
|
|
167
|
+
if (cached !== undefined) return cached;
|
|
168
|
+
const result = processSource(source, filepath, sourceMaps);
|
|
169
|
+
cache.set(key, result);
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function ensureTranspiled(
|
|
174
|
+
file: string,
|
|
175
|
+
source: string,
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
if (!needsTranspilation(file)) return;
|
|
178
|
+
const key = `${file}:${simpleHash(source)}`;
|
|
179
|
+
if (tsCache.has(key)) return;
|
|
180
|
+
const compiled = await transpile(source, file);
|
|
181
|
+
const result = transformEsmToCjs(compiled, file);
|
|
182
|
+
tsCache.set(key, result);
|
|
183
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized CDN version pins and URLs.
|
|
3
|
+
* Change versions here to update them across the entire platform.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const REACT_VERSION = "18.2.0";
|
|
7
|
+
export const ESBUILD_WASM_VERSION = "0.20.0";
|
|
8
|
+
export const REACT_REFRESH_VERSION = "0.14.0";
|
|
9
|
+
|
|
10
|
+
export const REACT_CDN = `https://esm.sh/react@${REACT_VERSION}`;
|
|
11
|
+
export const REACT_DOM_CDN = `https://esm.sh/react-dom@${REACT_VERSION}`;
|
|
12
|
+
export const REACT_REFRESH_CDN = `https://esm.sh/react-refresh@${REACT_REFRESH_VERSION}/runtime`;
|
|
13
|
+
|
|
14
|
+
export const ESBUILD_WASM_ESM_CDN = `https://esm.sh/esbuild-wasm@${ESBUILD_WASM_VERSION}`;
|
|
15
|
+
export const ESBUILD_WASM_BINARY_CDN = `https://unpkg.com/esbuild-wasm@${ESBUILD_WASM_VERSION}/esbuild.wasm`;
|
|
16
|
+
|
|
17
|
+
export const TAILWIND_CDN_URL = "https://cdn.tailwindcss.com";
|