@innovatorved/code-lab 0.1.3 → 0.1.5
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/ARCHITECTURE.md +2 -1
- package/README.md +4 -2
- package/dist/code-lab.css +49 -0
- package/dist/code-lab.d.ts +2 -1
- package/dist/code-lab.esm.js +1227 -67
- package/dist/code-lab.js +1227 -67
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.esm.js +377 -21
- package/dist/runtime.js +377 -21
- package/dist/shell.esm.js +766 -39
- package/dist/shell.js +766 -39
- package/package.json +6 -2
- package/src/code-lab.css +49 -0
- package/src/runtime.js +377 -21
- package/src/runtime.ts +382 -22
- package/src/shell.js +766 -39
- package/src/shell.ts +766 -39
- package/src/workbench.js +84 -7
- package/src/workbench.ts +84 -7
package/dist/runtime.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export declare const createVirtualFs: (options?: any) => any;
|
|
|
39
39
|
export declare const createVirtualHttp: (portTable?: any, vfs?: any) => any;
|
|
40
40
|
export declare const createVirtualNet: (portTable?: any) => any;
|
|
41
41
|
export declare const transpileTS: (code: string, filename?: string) => string;
|
|
42
|
-
export declare const preloadNpmPackages: (
|
|
42
|
+
export declare const preloadNpmPackages: (files?: Record<string, any>, onProgressOrCache?: ((pkg: string) => void) | Map<string, string>, onProgress?: (pkg: string) => void) => Promise<Record<string, string>>;
|
|
43
43
|
export declare const createVirtualPortTable: () => any;
|
|
44
44
|
export declare const executeInVirtualRuntime: (options?: any) => Promise<RuntimeExecutionResult>;
|
|
45
45
|
export declare const createWorkerRuntimeSource: () => string;
|
package/dist/runtime.esm.js
CHANGED
|
@@ -3972,14 +3972,316 @@ function __initVirtualRuntime(rootScope) {
|
|
|
3972
3972
|
};
|
|
3973
3973
|
}
|
|
3974
3974
|
|
|
3975
|
+
// =========================================================================
|
|
3976
|
+
// 7b. JSX / TSX SUPPORT
|
|
3977
|
+
// -------------------------------------------------------------------------
|
|
3978
|
+
// Rewrites JSX syntax into classic `createElement(...)` calls. This runs
|
|
3979
|
+
// BEFORE the string-masking pass so that JSX text (which routinely contains
|
|
3980
|
+
// quotes/apostrophes, e.g. <p>Don't</p>) is captured verbatim, while genuine
|
|
3981
|
+
// JS strings, comments and template literals are skipped untouched.
|
|
3982
|
+
// =========================================================================
|
|
3983
|
+
function detectJsxSyntax(source, filename) {
|
|
3984
|
+
if (/\.(jsx|tsx)$/i.test(filename || '')) return true;
|
|
3985
|
+
if (!source || source.indexOf('<') === -1) return false;
|
|
3986
|
+
if (/@jsx\b/.test(source)) return true;
|
|
3987
|
+
if (source.indexOf('<>') !== -1) return true;
|
|
3988
|
+
if (/<\/[A-Za-z]/.test(source)) return true;
|
|
3989
|
+
return false;
|
|
3990
|
+
}
|
|
3991
|
+
|
|
3992
|
+
function readJsxPragma(source) {
|
|
3993
|
+
let pragma = 'React.createElement';
|
|
3994
|
+
let frag = null;
|
|
3995
|
+
const m = /@jsx\s+([A-Za-z_$][\w$.]*)/.exec(source);
|
|
3996
|
+
if (m) pragma = m[1];
|
|
3997
|
+
const fm = /@jsxFrag\s+([A-Za-z_$][\w$.]*)/.exec(source);
|
|
3998
|
+
if (fm) frag = fm[1];
|
|
3999
|
+
if (!frag) frag = pragma === 'React.createElement' ? 'React.Fragment' : 'Fragment';
|
|
4000
|
+
return { pragma, frag };
|
|
4001
|
+
}
|
|
4002
|
+
|
|
4003
|
+
const JSX_WS_RE = /\s/;
|
|
4004
|
+
const JSX_NAME_START_RE = /[A-Za-z_$]/;
|
|
4005
|
+
const JSX_NAME_CHAR_RE = /[A-Za-z0-9_$:.\-]/;
|
|
4006
|
+
|
|
4007
|
+
function jsxThrow(message) {
|
|
4008
|
+
const err = new Error(message);
|
|
4009
|
+
err.jsxParse = true;
|
|
4010
|
+
throw err;
|
|
4011
|
+
}
|
|
4012
|
+
|
|
4013
|
+
// Returns the exclusive end index if a JS string / template / comment starts
|
|
4014
|
+
// at `i`, otherwise -1. Regexes are deliberately ignored.
|
|
4015
|
+
function jsxSkipJsLiteral(source, i) {
|
|
4016
|
+
const ch = source[i];
|
|
4017
|
+
const next = source[i + 1];
|
|
4018
|
+
const len = source.length;
|
|
4019
|
+
if (ch === '/' && next === '/') {
|
|
4020
|
+
const e = source.indexOf('\n', i);
|
|
4021
|
+
return e === -1 ? len : e;
|
|
4022
|
+
}
|
|
4023
|
+
if (ch === '/' && next === '*') {
|
|
4024
|
+
const e = source.indexOf('*/', i + 2);
|
|
4025
|
+
return e === -1 ? len : e + 2;
|
|
4026
|
+
}
|
|
4027
|
+
if (ch === '"' || ch === "'") {
|
|
4028
|
+
let j = i + 1;
|
|
4029
|
+
while (j < len) {
|
|
4030
|
+
const c = source[j];
|
|
4031
|
+
if (c === '\\') { j += 2; continue; }
|
|
4032
|
+
if (c === ch) { j++; break; }
|
|
4033
|
+
if (c === '\n') break;
|
|
4034
|
+
j++;
|
|
4035
|
+
}
|
|
4036
|
+
return j;
|
|
4037
|
+
}
|
|
4038
|
+
if (ch === '`') {
|
|
4039
|
+
let j = i + 1;
|
|
4040
|
+
let depth = 0;
|
|
4041
|
+
while (j < len) {
|
|
4042
|
+
const c = source[j];
|
|
4043
|
+
if (c === '\\') { j += 2; continue; }
|
|
4044
|
+
if (c === '$' && source[j + 1] === '{') { depth++; j += 2; continue; }
|
|
4045
|
+
if (c === '}' && depth > 0) { depth--; j++; continue; }
|
|
4046
|
+
if (c === '`' && depth === 0) { j++; break; }
|
|
4047
|
+
j++;
|
|
4048
|
+
}
|
|
4049
|
+
return j;
|
|
4050
|
+
}
|
|
4051
|
+
return -1;
|
|
4052
|
+
}
|
|
4053
|
+
|
|
4054
|
+
function jsxReadName(source, i) {
|
|
4055
|
+
if (!JSX_NAME_START_RE.test(source[i] || '')) jsxThrow('jsx: expected a tag name');
|
|
4056
|
+
let j = i;
|
|
4057
|
+
while (j < source.length && JSX_NAME_CHAR_RE.test(source[j])) j++;
|
|
4058
|
+
return { name: source.slice(i, j), next: j };
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
function jsxSkipWs(source, i) {
|
|
4062
|
+
while (i < source.length && JSX_WS_RE.test(source[i])) i++;
|
|
4063
|
+
return i;
|
|
4064
|
+
}
|
|
4065
|
+
|
|
4066
|
+
function normalizeJsxText(raw) {
|
|
4067
|
+
if (!raw) return '';
|
|
4068
|
+
let t = raw.replace(/\r/g, '');
|
|
4069
|
+
if (t.indexOf('\n') !== -1) {
|
|
4070
|
+
t = t.split('\n').map((s) => s.replace(/[ \t]+/g, ' ').trim()).filter(Boolean).join(' ');
|
|
4071
|
+
} else if (!/\S/.test(t)) {
|
|
4072
|
+
return '';
|
|
4073
|
+
} else {
|
|
4074
|
+
t = t.replace(/[ \t]+/g, ' ');
|
|
4075
|
+
}
|
|
4076
|
+
return t ? JSON.stringify(t) : '';
|
|
4077
|
+
}
|
|
4078
|
+
|
|
4079
|
+
// Reads a balanced `{ ... }` expression container, skipping nested JSX and
|
|
4080
|
+
// JS literals. Returns the raw inner source (later re-transformed) plus the
|
|
4081
|
+
// index just past the closing brace.
|
|
4082
|
+
function jsxReadBraced(source, i, opts) {
|
|
4083
|
+
let depth = 0;
|
|
4084
|
+
let j = i;
|
|
4085
|
+
while (j < source.length) {
|
|
4086
|
+
const lit = jsxSkipJsLiteral(source, j);
|
|
4087
|
+
if (lit !== -1) { j = lit; continue; }
|
|
4088
|
+
const c = source[j];
|
|
4089
|
+
if (c === '{') { depth++; j++; continue; }
|
|
4090
|
+
if (c === '}') {
|
|
4091
|
+
depth--;
|
|
4092
|
+
j++;
|
|
4093
|
+
if (depth === 0) return { inner: source.slice(i + 1, j - 1), next: j };
|
|
4094
|
+
continue;
|
|
4095
|
+
}
|
|
4096
|
+
if (c === '<' && (JSX_NAME_START_RE.test(source[j + 1] || '') || source[j + 1] === '>')) {
|
|
4097
|
+
try {
|
|
4098
|
+
const el = jsxParseElement(source, j, opts);
|
|
4099
|
+
j = el.next;
|
|
4100
|
+
continue;
|
|
4101
|
+
} catch (e) {
|
|
4102
|
+
if (!e || !e.jsxParse) throw e;
|
|
4103
|
+
}
|
|
4104
|
+
}
|
|
4105
|
+
j++;
|
|
4106
|
+
}
|
|
4107
|
+
jsxThrow('jsx: unterminated expression container');
|
|
4108
|
+
}
|
|
4109
|
+
|
|
4110
|
+
function jsxParseElement(source, i, opts) {
|
|
4111
|
+
const o = opts || readJsxPragma(source);
|
|
4112
|
+
let j = i + 1;
|
|
4113
|
+
let isFragment = false;
|
|
4114
|
+
let typeExpr;
|
|
4115
|
+
if (source[j] === '>') {
|
|
4116
|
+
isFragment = true;
|
|
4117
|
+
typeExpr = o.frag;
|
|
4118
|
+
j++;
|
|
4119
|
+
} else {
|
|
4120
|
+
const nm = jsxReadName(source, j);
|
|
4121
|
+
j = nm.next;
|
|
4122
|
+
const raw = nm.name;
|
|
4123
|
+
const isReference = raw.indexOf('.') !== -1 || raw.indexOf(':') !== -1 || /^[A-Z_$]/.test(raw);
|
|
4124
|
+
if (isReference) {
|
|
4125
|
+
typeExpr = raw.replace(/:/g, '.');
|
|
4126
|
+
} else {
|
|
4127
|
+
typeExpr = JSON.stringify(raw);
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4130
|
+
|
|
4131
|
+
const props = [];
|
|
4132
|
+
let selfClosing = false;
|
|
4133
|
+
if (!isFragment) {
|
|
4134
|
+
for (;;) {
|
|
4135
|
+
j = jsxSkipWs(source, j);
|
|
4136
|
+
if (j >= source.length) jsxThrow('jsx: unexpected end of input inside a tag');
|
|
4137
|
+
const c = source[j];
|
|
4138
|
+
if (c === '/') {
|
|
4139
|
+
if (source[j + 1] !== '>') jsxThrow('jsx: malformed self-closing tag');
|
|
4140
|
+
j += 2;
|
|
4141
|
+
selfClosing = true;
|
|
4142
|
+
break;
|
|
4143
|
+
}
|
|
4144
|
+
if (c === '>') { j++; break; }
|
|
4145
|
+
if (c === '{') {
|
|
4146
|
+
const br = jsxReadBraced(source, j, o);
|
|
4147
|
+
const inner = br.inner.trim();
|
|
4148
|
+
if (inner.slice(0, 3) !== '...') jsxThrow('jsx: only a spread {...} may appear where an attribute is expected');
|
|
4149
|
+
props.push({ spread: jsxTransform(inner.slice(3), o) });
|
|
4150
|
+
j = br.next;
|
|
4151
|
+
continue;
|
|
4152
|
+
}
|
|
4153
|
+
if (!JSX_NAME_START_RE.test(c)) jsxThrow('jsx: malformed attribute');
|
|
4154
|
+
const am = jsxReadName(source, j);
|
|
4155
|
+
const attrName = am.name;
|
|
4156
|
+
j = jsxSkipWs(source, am.next);
|
|
4157
|
+
let valueExpr = 'true';
|
|
4158
|
+
if (source[j] === '=') {
|
|
4159
|
+
j = jsxSkipWs(source, j + 1);
|
|
4160
|
+
const vc = source[j];
|
|
4161
|
+
if (vc === '"' || vc === "'") {
|
|
4162
|
+
let k = j + 1;
|
|
4163
|
+
while (k < source.length && source[k] !== vc) {
|
|
4164
|
+
if (source[k] === '\\') k++;
|
|
4165
|
+
k++;
|
|
4166
|
+
}
|
|
4167
|
+
if (k >= source.length) jsxThrow('jsx: unterminated attribute string');
|
|
4168
|
+
valueExpr = JSON.stringify(source.slice(j + 1, k));
|
|
4169
|
+
j = k + 1;
|
|
4170
|
+
} else if (vc === '{') {
|
|
4171
|
+
const br = jsxReadBraced(source, j, o);
|
|
4172
|
+
valueExpr = jsxTransform(br.inner, o) || 'undefined';
|
|
4173
|
+
j = br.next;
|
|
4174
|
+
} else if (vc === '<') {
|
|
4175
|
+
const el = jsxParseElement(source, j, o);
|
|
4176
|
+
valueExpr = el.code;
|
|
4177
|
+
j = el.next;
|
|
4178
|
+
} else {
|
|
4179
|
+
jsxThrow('jsx: unsupported attribute value');
|
|
4180
|
+
}
|
|
4181
|
+
}
|
|
4182
|
+
const propKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName) ? attrName : JSON.stringify(attrName);
|
|
4183
|
+
props.push({ key: propKey, value: valueExpr });
|
|
4184
|
+
}
|
|
4185
|
+
}
|
|
4186
|
+
|
|
4187
|
+
const children = [];
|
|
4188
|
+
if (!selfClosing) {
|
|
4189
|
+
for (;;) {
|
|
4190
|
+
if (j >= source.length) jsxThrow('jsx: missing closing tag');
|
|
4191
|
+
const c = source[j];
|
|
4192
|
+
if (c === '<' && source[j + 1] === '/') {
|
|
4193
|
+
j = jsxSkipWs(source, j + 2);
|
|
4194
|
+
if (source[j] === '>') { j++; break; }
|
|
4195
|
+
const cm = jsxReadName(source, j);
|
|
4196
|
+
j = jsxSkipWs(source, cm.next);
|
|
4197
|
+
if (source[j] !== '>') jsxThrow('jsx: malformed closing tag');
|
|
4198
|
+
j++;
|
|
4199
|
+
break;
|
|
4200
|
+
}
|
|
4201
|
+
if (c === '<') {
|
|
4202
|
+
const el = jsxParseElement(source, j, o);
|
|
4203
|
+
children.push(el.code);
|
|
4204
|
+
j = el.next;
|
|
4205
|
+
continue;
|
|
4206
|
+
}
|
|
4207
|
+
if (c === '{') {
|
|
4208
|
+
const br = jsxReadBraced(source, j, o);
|
|
4209
|
+
const inner = jsxTransform(br.inner, o);
|
|
4210
|
+
if (inner && inner.trim()) children.push(inner);
|
|
4211
|
+
j = br.next;
|
|
4212
|
+
continue;
|
|
4213
|
+
}
|
|
4214
|
+
let k = j;
|
|
4215
|
+
while (k < source.length && source[k] !== '<' && source[k] !== '{') k++;
|
|
4216
|
+
const text = normalizeJsxText(source.slice(j, k));
|
|
4217
|
+
if (text) children.push(text);
|
|
4218
|
+
j = k;
|
|
4219
|
+
}
|
|
4220
|
+
}
|
|
4221
|
+
|
|
4222
|
+
let propsCode = 'null';
|
|
4223
|
+
if (props.length) {
|
|
4224
|
+
const parts = props.map((p) => (p.spread !== undefined ? '...(' + p.spread + ')' : p.key + ': ' + p.value));
|
|
4225
|
+
propsCode = '{ ' + parts.join(', ') + ' }';
|
|
4226
|
+
}
|
|
4227
|
+
|
|
4228
|
+
const args = [typeExpr, propsCode].concat(children);
|
|
4229
|
+
return { code: o.pragma + '(' + args.join(', ') + ')', next: j };
|
|
4230
|
+
}
|
|
4231
|
+
|
|
4232
|
+
function jsxTransform(source, opts) {
|
|
4233
|
+
const o = opts || readJsxPragma(source);
|
|
4234
|
+
let i = 0;
|
|
4235
|
+
let out = '';
|
|
4236
|
+
while (i < source.length) {
|
|
4237
|
+
const lit = jsxSkipJsLiteral(source, i);
|
|
4238
|
+
if (lit !== -1) { out += source.slice(i, lit); i = lit; continue; }
|
|
4239
|
+
const ch = source[i];
|
|
4240
|
+
if (ch === '<' && (JSX_NAME_START_RE.test(source[i + 1] || '') || source[i + 1] === '>')) {
|
|
4241
|
+
try {
|
|
4242
|
+
const el = jsxParseElement(source, i, o);
|
|
4243
|
+
out += el.code;
|
|
4244
|
+
i = el.next;
|
|
4245
|
+
continue;
|
|
4246
|
+
} catch (e) {
|
|
4247
|
+
if (!e || !e.jsxParse) throw e;
|
|
4248
|
+
}
|
|
4249
|
+
}
|
|
4250
|
+
out += ch;
|
|
4251
|
+
i++;
|
|
4252
|
+
}
|
|
4253
|
+
return out;
|
|
4254
|
+
}
|
|
4255
|
+
|
|
3975
4256
|
// =========================================================================
|
|
3976
4257
|
// 8. FAST TYPESCRIPT & ESM-TO-CJS TRANSPILER
|
|
3977
4258
|
// =========================================================================
|
|
3978
|
-
function transpileTS(source, filename = 'module.ts') {
|
|
4259
|
+
function transpileTS(source, filename = 'module.ts', opts = {}) {
|
|
4260
|
+
// `keepEsm` leaves `import`/`export` statements intact so the output can run
|
|
4261
|
+
// natively in a browser as an ES module (used by the built-in preview server).
|
|
4262
|
+
const keepEsm = Boolean(opts && opts.keepEsm);
|
|
3979
4263
|
const isTS = /\.(ts|tsx|mts|cts)$/i.test(filename)
|
|
3980
|
-
||
|
|
3981
|
-
const hasESM = /^\s*(import|export)\
|
|
3982
|
-
|
|
4264
|
+
|| /^[ \t]*(?:export\s+)?(?:abstract\s+class\s+[A-Za-z_$]|declare\s+(?:const|let|var|function|class|namespace|module|global)\b|interface\s+[A-Za-z_$][A-Za-z0-9_$]*|type\s+[A-Za-z_$][A-Za-z0-9_$]*\s*(?:<[^>]*>)?\s*=|enum\s+[A-Za-z_$][A-Za-z0-9_$]*\s*\{|namespace\s+[A-Za-z_$][A-Za-z0-9_$]*\s*\{)/m.test(source);
|
|
4265
|
+
const hasESM = /^\s*(?:import|export)\b/m.test(source)
|
|
4266
|
+
|| /\bexport\s*\{/.test(source)
|
|
4267
|
+
|| /\bimport\s*\{/.test(source)
|
|
4268
|
+
|| /\bexport\s+default\b/.test(source);
|
|
4269
|
+
const hasJsx = detectJsxSyntax(source, filename);
|
|
4270
|
+
if (!isTS && !hasESM && !hasJsx) return source;
|
|
4271
|
+
|
|
4272
|
+
// JSX/TSX must be rewritten before masking so JSX text is kept verbatim.
|
|
4273
|
+
if (hasJsx) {
|
|
4274
|
+
const { pragma, frag } = readJsxPragma(source);
|
|
4275
|
+
const needsReact = pragma === 'React.createElement' && !/\bReact\b/.test(source);
|
|
4276
|
+
const transformed = jsxTransform(source, { pragma, frag });
|
|
4277
|
+
if (transformed !== source) {
|
|
4278
|
+
source = transformed;
|
|
4279
|
+
// The classic runtime needs a React binding available to the JSX output.
|
|
4280
|
+
if (needsReact) {
|
|
4281
|
+
source = (keepEsm ? 'import React from "react";\n' : 'const React = require("react");\n') + source;
|
|
4282
|
+
}
|
|
4283
|
+
}
|
|
4284
|
+
}
|
|
3983
4285
|
|
|
3984
4286
|
// Step 1: Mask strings, template literals, regex literals, and comments so transforms never corrupt literals
|
|
3985
4287
|
const placeholders = [];
|
|
@@ -4542,10 +4844,10 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4542
4844
|
} while (code !== prevCode);
|
|
4543
4845
|
}
|
|
4544
4846
|
|
|
4545
|
-
if (hasESM || isTS) {
|
|
4847
|
+
if ((hasESM || isTS) && !keepEsm) {
|
|
4546
4848
|
// Convert ES module imports (including multi-line `{ ... }`)
|
|
4547
|
-
code = code.replace(
|
|
4548
|
-
code = code.replace(
|
|
4849
|
+
code = code.replace(/\bimport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, 'const $1 = require($2);');
|
|
4850
|
+
code = code.replace(/\bimport\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*,\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, defId, specifiers, mod) => {
|
|
4549
4851
|
const cleaned = specifiers
|
|
4550
4852
|
.split(',')
|
|
4551
4853
|
.map((s) => s.trim())
|
|
@@ -4556,7 +4858,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4556
4858
|
return `const ${defId} = (function(m){ return m && m.__esModule ? m.default : m; })(require(${mTrim}));` +
|
|
4557
4859
|
(cleaned ? ` const { ${cleaned} } = require(${mTrim});` : '');
|
|
4558
4860
|
});
|
|
4559
|
-
code = code.replace(
|
|
4861
|
+
code = code.replace(/\bimport\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, specifiers, mod) => {
|
|
4560
4862
|
const cleaned = specifiers
|
|
4561
4863
|
.split(',')
|
|
4562
4864
|
.map((s) => s.trim())
|
|
@@ -4565,42 +4867,42 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4565
4867
|
.join(', ');
|
|
4566
4868
|
return cleaned ? `const { ${cleaned} } = require(${mod.trim()});` : '';
|
|
4567
4869
|
});
|
|
4568
|
-
code = code.replace(
|
|
4569
|
-
code = code.replace(
|
|
4870
|
+
code = code.replace(/\bimport\s+([A-Za-z_$][A-Za-z0-9_$]*)\s+from\s+([^\n;]+);?/gm, 'const $1 = (function(m){ return m && m.__esModule ? m.default : m; })(require($2));');
|
|
4871
|
+
code = code.replace(/\bimport\s+(\x00__LIT_\d+__\x00)\s*;?/gm, 'require($1);');
|
|
4570
4872
|
|
|
4571
4873
|
// Convert ES module exports
|
|
4572
4874
|
const exportedNames = [];
|
|
4573
4875
|
let defaultExportName = null;
|
|
4574
4876
|
let hasEsmExport = false;
|
|
4575
4877
|
|
|
4576
|
-
code = code.replace(
|
|
4878
|
+
code = code.replace(/\bexport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, (_, ns, mod) => {
|
|
4577
4879
|
hasEsmExport = true;
|
|
4578
4880
|
return `exports.${ns} = require(${mod.trim()});`;
|
|
4579
4881
|
});
|
|
4580
|
-
code = code.replace(
|
|
4882
|
+
code = code.replace(/\bexport\s*\*\s*from\s*([^\n;]+);?/gm, (_, mod) => {
|
|
4581
4883
|
hasEsmExport = true;
|
|
4582
4884
|
return `Object.assign(exports, require(${mod.trim()}));`;
|
|
4583
4885
|
});
|
|
4584
|
-
code = code.replace(
|
|
4886
|
+
code = code.replace(/\bexport\s+default\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4585
4887
|
hasEsmExport = true;
|
|
4586
4888
|
defaultExportName = name;
|
|
4587
4889
|
return `${kw} ${name}`;
|
|
4588
4890
|
});
|
|
4589
|
-
code = code.replace(
|
|
4891
|
+
code = code.replace(/\bexport\s+default\s+/gm, () => {
|
|
4590
4892
|
hasEsmExport = true;
|
|
4591
4893
|
return 'exports.default = ';
|
|
4592
4894
|
});
|
|
4593
|
-
code = code.replace(
|
|
4895
|
+
code = code.replace(/\bexport\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4594
4896
|
hasEsmExport = true;
|
|
4595
4897
|
exportedNames.push(name);
|
|
4596
4898
|
return `${kw} ${name}`;
|
|
4597
4899
|
});
|
|
4598
|
-
code = code.replace(
|
|
4900
|
+
code = code.replace(/\bexport\s+(const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
|
|
4599
4901
|
hasEsmExport = true;
|
|
4600
4902
|
exportedNames.push(name);
|
|
4601
4903
|
return `${kw} ${name}`;
|
|
4602
4904
|
});
|
|
4603
|
-
code = code.replace(
|
|
4905
|
+
code = code.replace(/\bexport\s*\{([^}]+)\}\s*from\s*([^\n;]+);?/gm, (_, list, mod) => {
|
|
4604
4906
|
hasEsmExport = true;
|
|
4605
4907
|
const mTrim = mod.trim();
|
|
4606
4908
|
return list.split(',').map((item) => {
|
|
@@ -4612,7 +4914,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4612
4914
|
return `exports.${exported} = require(${mTrim}).${local};`;
|
|
4613
4915
|
}).join(' ');
|
|
4614
4916
|
});
|
|
4615
|
-
code = code.replace(
|
|
4917
|
+
code = code.replace(/\bexport\s*\{([^}]+)\}\s*;?/gm, (_, list) => {
|
|
4616
4918
|
hasEsmExport = true;
|
|
4617
4919
|
return list.split(',').map((item) => {
|
|
4618
4920
|
const trimmed = item.trim();
|
|
@@ -4679,8 +4981,22 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4679
4981
|
}
|
|
4680
4982
|
|
|
4681
4983
|
const _npmPackageCache = new Map();
|
|
4984
|
+
const _npmMetadataCache = new Map();
|
|
4985
|
+
|
|
4986
|
+
async function preloadNpmPackages(files = {}, secondArg = null, thirdArg = null) {
|
|
4987
|
+
// The 2nd/3rd arguments are overloaded for backwards compatibility:
|
|
4988
|
+
// preloadNpmPackages(files)
|
|
4989
|
+
// preloadNpmPackages(files, onProgress) // progress callback (used by the Workbench)
|
|
4990
|
+
// preloadNpmPackages(files, existingCache) // package-text cache (Map)
|
|
4991
|
+
// preloadNpmPackages(files, existingCache, onProgress)
|
|
4992
|
+
const isCacheLike = (v) => !!v && typeof v !== 'function' && typeof v.has === 'function' && typeof v.get === 'function';
|
|
4993
|
+
let onProgress = null;
|
|
4994
|
+
let existingCache = _npmPackageCache;
|
|
4995
|
+
if (typeof secondArg === 'function') onProgress = secondArg;
|
|
4996
|
+
else if (isCacheLike(secondArg)) existingCache = secondArg;
|
|
4997
|
+
if (typeof thirdArg === 'function') onProgress = onProgress || thirdArg;
|
|
4998
|
+
else if (isCacheLike(thirdArg)) existingCache = thirdArg;
|
|
4682
4999
|
|
|
4683
|
-
async function preloadNpmPackages(files = {}, existingCache = _npmPackageCache) {
|
|
4684
5000
|
const specs = new Set();
|
|
4685
5001
|
const builtinSet = new Set([
|
|
4686
5002
|
'fs', 'fs/promises', 'path', 'os', 'crypto', 'zlib', 'http', 'https', 'net',
|
|
@@ -4716,20 +5032,51 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4716
5032
|
while ((m = impRe.exec(content)) !== null) addSpec(m[1]);
|
|
4717
5033
|
}
|
|
4718
5034
|
|
|
5035
|
+
// Follow declared dependencies transitively (best effort, bounded). Metadata
|
|
5036
|
+
// is memoised across calls so repeated preloads stay cheap.
|
|
5037
|
+
if (typeof fetch === 'function') {
|
|
5038
|
+
let pass = 0;
|
|
5039
|
+
while (pass < 6) {
|
|
5040
|
+
const pending = [];
|
|
5041
|
+
for (const pkg of specs) {
|
|
5042
|
+
if (!_npmMetadataCache.has(pkg)) pending.push(pkg);
|
|
5043
|
+
}
|
|
5044
|
+
if (pending.length === 0) break;
|
|
5045
|
+
pass++;
|
|
5046
|
+
await Promise.all(pending.map(async (pkg) => {
|
|
5047
|
+
let deps = [];
|
|
5048
|
+
try {
|
|
5049
|
+
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}/package.json`);
|
|
5050
|
+
if (res && res.ok) {
|
|
5051
|
+
const meta = await res.json();
|
|
5052
|
+
deps = Object.keys((meta && meta.dependencies) || {});
|
|
5053
|
+
}
|
|
5054
|
+
} catch (_) {}
|
|
5055
|
+
_npmMetadataCache.set(pkg, deps);
|
|
5056
|
+
}));
|
|
5057
|
+
for (const pkg of pending) {
|
|
5058
|
+
for (const dep of (_npmMetadataCache.get(pkg) || [])) addSpec(dep);
|
|
5059
|
+
}
|
|
5060
|
+
}
|
|
5061
|
+
}
|
|
5062
|
+
|
|
4719
5063
|
const loaded = {};
|
|
4720
5064
|
for (const pkg of specs) {
|
|
4721
5065
|
const nmKey = `node_modules/${pkg}/index.js`;
|
|
4722
5066
|
if (files[nmKey] || files[`/workspace/${nmKey}`]) continue;
|
|
4723
|
-
if (existingCache
|
|
5067
|
+
if (existingCache.has(pkg)) {
|
|
4724
5068
|
loaded[pkg] = existingCache.get(pkg);
|
|
4725
5069
|
continue;
|
|
4726
5070
|
}
|
|
5071
|
+
if (typeof onProgress === 'function') {
|
|
5072
|
+
try { onProgress(pkg); } catch (_) {}
|
|
5073
|
+
}
|
|
4727
5074
|
if (typeof fetch === 'function') {
|
|
4728
5075
|
try {
|
|
4729
5076
|
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}`);
|
|
4730
5077
|
if (res && res.ok) {
|
|
4731
5078
|
const text = await res.text();
|
|
4732
|
-
if (existingCache) existingCache.set(pkg, text);
|
|
5079
|
+
if (typeof existingCache.set === 'function') existingCache.set(pkg, text);
|
|
4733
5080
|
loaded[pkg] = text;
|
|
4734
5081
|
}
|
|
4735
5082
|
} catch (_) {}
|
|
@@ -7192,6 +7539,14 @@ function __initVirtualRuntime(rootScope) {
|
|
|
7192
7539
|
};
|
|
7193
7540
|
}
|
|
7194
7541
|
|
|
7542
|
+
// Transpile a source file so it can run natively in a browser as an ES module:
|
|
7543
|
+
// JSX is compiled to the classic runtime (auto-importing React when needed) and
|
|
7544
|
+
// TypeScript annotations are stripped, but `import`/`export` are preserved so an
|
|
7545
|
+
// import map can resolve bare specifiers to CDN ESM builds.
|
|
7546
|
+
function transpileForBrowser(source, filename = 'module.js') {
|
|
7547
|
+
return transpileTS(source, filename, { keepEsm: true });
|
|
7548
|
+
}
|
|
7549
|
+
|
|
7195
7550
|
return {
|
|
7196
7551
|
VirtualBuffer,
|
|
7197
7552
|
VirtualCrypto,
|
|
@@ -7211,6 +7566,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
7211
7566
|
createVirtualHttp,
|
|
7212
7567
|
createVirtualNet,
|
|
7213
7568
|
transpileTS,
|
|
7569
|
+
transpileForBrowser,
|
|
7214
7570
|
preloadNpmPackages,
|
|
7215
7571
|
createVirtualPortTable,
|
|
7216
7572
|
executeInVirtualRuntime
|