@innovatorved/code-lab 0.1.4 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innovatorved/code-lab",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Standalone browser-native IDE Workbench, POSIX Shell & Coreutils, PTY Terminal, and Web Worker Node.js/Bun Virtual Runtime.",
5
5
  "main": "./dist/code-lab.js",
6
6
  "module": "./dist/code-lab.esm.js",
@@ -72,7 +72,7 @@
72
72
  "build": "bun run tools/bundle.js",
73
73
  "typecheck": "bun x tsc --noEmit",
74
74
  "test": "bun run tests/test-all-builds.js",
75
- "test:all": "bun run tools/bundle.js && bun run tests/test-all-builds.js && bun run tests/test-runtime-advanced.js && bun run tests/test-option1-shell-bun-http.js && bun run tests/test-coreutils-adversarial.js && bun run tests/test-stress-shell.js && bun run tests/test-workbench-ui.js && bun run tests/test-terminal-streaming.js && bun run tests/test-git-clone-streaming.js && bun run tests/test-m1-adversarial-challenger.js && bun run tests/test-m1-interruption-lifecycle-stress.js && bun run tests/test-gfm-markdown.js && bun run tests/test-m2-adversarial-syntax.js && bun run tests/test-m2-adversarial-interactive.js && bun run tests/test-web-preview.js && bun run tests/test-preview-sw.js",
75
+ "test:all": "bun run tools/bundle.js && bun run tests/test-all-builds.js && bun run tests/test-runtime-advanced.js && bun run tests/test-option1-shell-bun-http.js && bun run tests/test-frameworks.js && bun run tests/test-coreutils-adversarial.js && bun run tests/test-stress-shell.js && bun run tests/test-workbench-ui.js && bun run tests/test-terminal-streaming.js && bun run tests/test-git-clone-streaming.js && bun run tests/test-m1-adversarial-challenger.js && bun run tests/test-m1-interruption-lifecycle-stress.js && bun run tests/test-gfm-markdown.js && bun run tests/test-m2-adversarial-syntax.js && bun run tests/test-m2-adversarial-interactive.js && bun run tests/test-web-preview.js && bun run tests/test-preview-sw.js",
76
76
  "pack:check": "bun pm pack --dry-run"
77
77
  },
78
78
  "dependencies": {
@@ -83,6 +83,10 @@
83
83
  "esbuild": "^0.24.0",
84
84
  "typescript": "^5.6.3"
85
85
  },
86
+ "publishConfig": {
87
+ "access": "public",
88
+ "registry": "https://registry.npmjs.org/"
89
+ },
86
90
  "keywords": [
87
91
  "ide",
88
92
  "code-playground",
package/src/code-lab.css CHANGED
@@ -4353,3 +4353,52 @@ code-lab,
4353
4353
  font-size: 11px;
4354
4354
  color: var(--cl-fg-faint);
4355
4355
  }
4356
+
4357
+ .codelab-preview-empty {
4358
+ display: flex;
4359
+ flex-direction: column;
4360
+ align-items: center;
4361
+ gap: 8px;
4362
+ padding: 28px 20px;
4363
+ text-align: center;
4364
+ border-top: 1px solid var(--cl-border);
4365
+ background: var(--cl-bg-subtle, transparent);
4366
+ }
4367
+
4368
+ .codelab-preview-empty__icon {
4369
+ display: inline-flex;
4370
+ align-items: center;
4371
+ justify-content: center;
4372
+ width: 38px;
4373
+ height: 38px;
4374
+ border-radius: 50%;
4375
+ color: var(--cl-accent, #4f9cf9);
4376
+ background: color-mix(in srgb, var(--cl-accent, #4f9cf9) 14%, transparent);
4377
+ }
4378
+
4379
+ .codelab-preview-empty__title {
4380
+ font-size: 13px;
4381
+ font-weight: 600;
4382
+ color: var(--cl-fg);
4383
+ }
4384
+
4385
+ .codelab-preview-empty__text {
4386
+ max-width: 440px;
4387
+ font-size: 12px;
4388
+ line-height: 1.5;
4389
+ color: var(--cl-fg-muted);
4390
+ }
4391
+
4392
+ .codelab-preview-empty__text code,
4393
+ .codelab-preview-empty__hint code {
4394
+ font-family: var(--cl-font-mono);
4395
+ font-size: 11px;
4396
+ padding: 1px 5px;
4397
+ border-radius: 4px;
4398
+ background: color-mix(in srgb, var(--cl-fg, #fff) 8%, transparent);
4399
+ }
4400
+
4401
+ .codelab-preview-empty__hint {
4402
+ font-size: 11px;
4403
+ color: var(--cl-fg-faint);
4404
+ }
package/src/runtime.js CHANGED
@@ -3954,14 +3954,316 @@ function __initVirtualRuntime(rootScope) {
3954
3954
  };
3955
3955
  }
3956
3956
 
3957
+ // =========================================================================
3958
+ // 7b. JSX / TSX SUPPORT
3959
+ // -------------------------------------------------------------------------
3960
+ // Rewrites JSX syntax into classic `createElement(...)` calls. This runs
3961
+ // BEFORE the string-masking pass so that JSX text (which routinely contains
3962
+ // quotes/apostrophes, e.g. <p>Don't</p>) is captured verbatim, while genuine
3963
+ // JS strings, comments and template literals are skipped untouched.
3964
+ // =========================================================================
3965
+ function detectJsxSyntax(source, filename) {
3966
+ if (/\.(jsx|tsx)$/i.test(filename || '')) return true;
3967
+ if (!source || source.indexOf('<') === -1) return false;
3968
+ if (/@jsx\b/.test(source)) return true;
3969
+ if (source.indexOf('<>') !== -1) return true;
3970
+ if (/<\/[A-Za-z]/.test(source)) return true;
3971
+ return false;
3972
+ }
3973
+
3974
+ function readJsxPragma(source) {
3975
+ let pragma = 'React.createElement';
3976
+ let frag = null;
3977
+ const m = /@jsx\s+([A-Za-z_$][\w$.]*)/.exec(source);
3978
+ if (m) pragma = m[1];
3979
+ const fm = /@jsxFrag\s+([A-Za-z_$][\w$.]*)/.exec(source);
3980
+ if (fm) frag = fm[1];
3981
+ if (!frag) frag = pragma === 'React.createElement' ? 'React.Fragment' : 'Fragment';
3982
+ return { pragma, frag };
3983
+ }
3984
+
3985
+ const JSX_WS_RE = /\s/;
3986
+ const JSX_NAME_START_RE = /[A-Za-z_$]/;
3987
+ const JSX_NAME_CHAR_RE = /[A-Za-z0-9_$:.\-]/;
3988
+
3989
+ function jsxThrow(message) {
3990
+ const err = new Error(message);
3991
+ err.jsxParse = true;
3992
+ throw err;
3993
+ }
3994
+
3995
+ // Returns the exclusive end index if a JS string / template / comment starts
3996
+ // at `i`, otherwise -1. Regexes are deliberately ignored.
3997
+ function jsxSkipJsLiteral(source, i) {
3998
+ const ch = source[i];
3999
+ const next = source[i + 1];
4000
+ const len = source.length;
4001
+ if (ch === '/' && next === '/') {
4002
+ const e = source.indexOf('\n', i);
4003
+ return e === -1 ? len : e;
4004
+ }
4005
+ if (ch === '/' && next === '*') {
4006
+ const e = source.indexOf('*/', i + 2);
4007
+ return e === -1 ? len : e + 2;
4008
+ }
4009
+ if (ch === '"' || ch === "'") {
4010
+ let j = i + 1;
4011
+ while (j < len) {
4012
+ const c = source[j];
4013
+ if (c === '\\') { j += 2; continue; }
4014
+ if (c === ch) { j++; break; }
4015
+ if (c === '\n') break;
4016
+ j++;
4017
+ }
4018
+ return j;
4019
+ }
4020
+ if (ch === '`') {
4021
+ let j = i + 1;
4022
+ let depth = 0;
4023
+ while (j < len) {
4024
+ const c = source[j];
4025
+ if (c === '\\') { j += 2; continue; }
4026
+ if (c === '$' && source[j + 1] === '{') { depth++; j += 2; continue; }
4027
+ if (c === '}' && depth > 0) { depth--; j++; continue; }
4028
+ if (c === '`' && depth === 0) { j++; break; }
4029
+ j++;
4030
+ }
4031
+ return j;
4032
+ }
4033
+ return -1;
4034
+ }
4035
+
4036
+ function jsxReadName(source, i) {
4037
+ if (!JSX_NAME_START_RE.test(source[i] || '')) jsxThrow('jsx: expected a tag name');
4038
+ let j = i;
4039
+ while (j < source.length && JSX_NAME_CHAR_RE.test(source[j])) j++;
4040
+ return { name: source.slice(i, j), next: j };
4041
+ }
4042
+
4043
+ function jsxSkipWs(source, i) {
4044
+ while (i < source.length && JSX_WS_RE.test(source[i])) i++;
4045
+ return i;
4046
+ }
4047
+
4048
+ function normalizeJsxText(raw) {
4049
+ if (!raw) return '';
4050
+ let t = raw.replace(/\r/g, '');
4051
+ if (t.indexOf('\n') !== -1) {
4052
+ t = t.split('\n').map((s) => s.replace(/[ \t]+/g, ' ').trim()).filter(Boolean).join(' ');
4053
+ } else if (!/\S/.test(t)) {
4054
+ return '';
4055
+ } else {
4056
+ t = t.replace(/[ \t]+/g, ' ');
4057
+ }
4058
+ return t ? JSON.stringify(t) : '';
4059
+ }
4060
+
4061
+ // Reads a balanced `{ ... }` expression container, skipping nested JSX and
4062
+ // JS literals. Returns the raw inner source (later re-transformed) plus the
4063
+ // index just past the closing brace.
4064
+ function jsxReadBraced(source, i, opts) {
4065
+ let depth = 0;
4066
+ let j = i;
4067
+ while (j < source.length) {
4068
+ const lit = jsxSkipJsLiteral(source, j);
4069
+ if (lit !== -1) { j = lit; continue; }
4070
+ const c = source[j];
4071
+ if (c === '{') { depth++; j++; continue; }
4072
+ if (c === '}') {
4073
+ depth--;
4074
+ j++;
4075
+ if (depth === 0) return { inner: source.slice(i + 1, j - 1), next: j };
4076
+ continue;
4077
+ }
4078
+ if (c === '<' && (JSX_NAME_START_RE.test(source[j + 1] || '') || source[j + 1] === '>')) {
4079
+ try {
4080
+ const el = jsxParseElement(source, j, opts);
4081
+ j = el.next;
4082
+ continue;
4083
+ } catch (e) {
4084
+ if (!e || !e.jsxParse) throw e;
4085
+ }
4086
+ }
4087
+ j++;
4088
+ }
4089
+ jsxThrow('jsx: unterminated expression container');
4090
+ }
4091
+
4092
+ function jsxParseElement(source, i, opts) {
4093
+ const o = opts || readJsxPragma(source);
4094
+ let j = i + 1;
4095
+ let isFragment = false;
4096
+ let typeExpr;
4097
+ if (source[j] === '>') {
4098
+ isFragment = true;
4099
+ typeExpr = o.frag;
4100
+ j++;
4101
+ } else {
4102
+ const nm = jsxReadName(source, j);
4103
+ j = nm.next;
4104
+ const raw = nm.name;
4105
+ const isReference = raw.indexOf('.') !== -1 || raw.indexOf(':') !== -1 || /^[A-Z_$]/.test(raw);
4106
+ if (isReference) {
4107
+ typeExpr = raw.replace(/:/g, '.');
4108
+ } else {
4109
+ typeExpr = JSON.stringify(raw);
4110
+ }
4111
+ }
4112
+
4113
+ const props = [];
4114
+ let selfClosing = false;
4115
+ if (!isFragment) {
4116
+ for (;;) {
4117
+ j = jsxSkipWs(source, j);
4118
+ if (j >= source.length) jsxThrow('jsx: unexpected end of input inside a tag');
4119
+ const c = source[j];
4120
+ if (c === '/') {
4121
+ if (source[j + 1] !== '>') jsxThrow('jsx: malformed self-closing tag');
4122
+ j += 2;
4123
+ selfClosing = true;
4124
+ break;
4125
+ }
4126
+ if (c === '>') { j++; break; }
4127
+ if (c === '{') {
4128
+ const br = jsxReadBraced(source, j, o);
4129
+ const inner = br.inner.trim();
4130
+ if (inner.slice(0, 3) !== '...') jsxThrow('jsx: only a spread {...} may appear where an attribute is expected');
4131
+ props.push({ spread: jsxTransform(inner.slice(3), o) });
4132
+ j = br.next;
4133
+ continue;
4134
+ }
4135
+ if (!JSX_NAME_START_RE.test(c)) jsxThrow('jsx: malformed attribute');
4136
+ const am = jsxReadName(source, j);
4137
+ const attrName = am.name;
4138
+ j = jsxSkipWs(source, am.next);
4139
+ let valueExpr = 'true';
4140
+ if (source[j] === '=') {
4141
+ j = jsxSkipWs(source, j + 1);
4142
+ const vc = source[j];
4143
+ if (vc === '"' || vc === "'") {
4144
+ let k = j + 1;
4145
+ while (k < source.length && source[k] !== vc) {
4146
+ if (source[k] === '\\') k++;
4147
+ k++;
4148
+ }
4149
+ if (k >= source.length) jsxThrow('jsx: unterminated attribute string');
4150
+ valueExpr = JSON.stringify(source.slice(j + 1, k));
4151
+ j = k + 1;
4152
+ } else if (vc === '{') {
4153
+ const br = jsxReadBraced(source, j, o);
4154
+ valueExpr = jsxTransform(br.inner, o) || 'undefined';
4155
+ j = br.next;
4156
+ } else if (vc === '<') {
4157
+ const el = jsxParseElement(source, j, o);
4158
+ valueExpr = el.code;
4159
+ j = el.next;
4160
+ } else {
4161
+ jsxThrow('jsx: unsupported attribute value');
4162
+ }
4163
+ }
4164
+ const propKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName) ? attrName : JSON.stringify(attrName);
4165
+ props.push({ key: propKey, value: valueExpr });
4166
+ }
4167
+ }
4168
+
4169
+ const children = [];
4170
+ if (!selfClosing) {
4171
+ for (;;) {
4172
+ if (j >= source.length) jsxThrow('jsx: missing closing tag');
4173
+ const c = source[j];
4174
+ if (c === '<' && source[j + 1] === '/') {
4175
+ j = jsxSkipWs(source, j + 2);
4176
+ if (source[j] === '>') { j++; break; }
4177
+ const cm = jsxReadName(source, j);
4178
+ j = jsxSkipWs(source, cm.next);
4179
+ if (source[j] !== '>') jsxThrow('jsx: malformed closing tag');
4180
+ j++;
4181
+ break;
4182
+ }
4183
+ if (c === '<') {
4184
+ const el = jsxParseElement(source, j, o);
4185
+ children.push(el.code);
4186
+ j = el.next;
4187
+ continue;
4188
+ }
4189
+ if (c === '{') {
4190
+ const br = jsxReadBraced(source, j, o);
4191
+ const inner = jsxTransform(br.inner, o);
4192
+ if (inner && inner.trim()) children.push(inner);
4193
+ j = br.next;
4194
+ continue;
4195
+ }
4196
+ let k = j;
4197
+ while (k < source.length && source[k] !== '<' && source[k] !== '{') k++;
4198
+ const text = normalizeJsxText(source.slice(j, k));
4199
+ if (text) children.push(text);
4200
+ j = k;
4201
+ }
4202
+ }
4203
+
4204
+ let propsCode = 'null';
4205
+ if (props.length) {
4206
+ const parts = props.map((p) => (p.spread !== undefined ? '...(' + p.spread + ')' : p.key + ': ' + p.value));
4207
+ propsCode = '{ ' + parts.join(', ') + ' }';
4208
+ }
4209
+
4210
+ const args = [typeExpr, propsCode].concat(children);
4211
+ return { code: o.pragma + '(' + args.join(', ') + ')', next: j };
4212
+ }
4213
+
4214
+ function jsxTransform(source, opts) {
4215
+ const o = opts || readJsxPragma(source);
4216
+ let i = 0;
4217
+ let out = '';
4218
+ while (i < source.length) {
4219
+ const lit = jsxSkipJsLiteral(source, i);
4220
+ if (lit !== -1) { out += source.slice(i, lit); i = lit; continue; }
4221
+ const ch = source[i];
4222
+ if (ch === '<' && (JSX_NAME_START_RE.test(source[i + 1] || '') || source[i + 1] === '>')) {
4223
+ try {
4224
+ const el = jsxParseElement(source, i, o);
4225
+ out += el.code;
4226
+ i = el.next;
4227
+ continue;
4228
+ } catch (e) {
4229
+ if (!e || !e.jsxParse) throw e;
4230
+ }
4231
+ }
4232
+ out += ch;
4233
+ i++;
4234
+ }
4235
+ return out;
4236
+ }
4237
+
3957
4238
  // =========================================================================
3958
4239
  // 8. FAST TYPESCRIPT & ESM-TO-CJS TRANSPILER
3959
4240
  // =========================================================================
3960
- function transpileTS(source, filename = 'module.ts') {
4241
+ function transpileTS(source, filename = 'module.ts', opts = {}) {
4242
+ // `keepEsm` leaves `import`/`export` statements intact so the output can run
4243
+ // natively in a browser as an ES module (used by the built-in preview server).
4244
+ const keepEsm = Boolean(opts && opts.keepEsm);
3961
4245
  const isTS = /\.(ts|tsx|mts|cts)$/i.test(filename)
3962
- || /\b(interface\s+[A-Za-z_$]|type\s+[A-Za-z_$][A-Za-z0-9_$]*\s*(?:<[^>]*>)?\s*=|enum\s+[A-Za-z_$]|abstract\s+class\s+|declare\s+(?:const|let|var|function|class|type|interface)\b|public\s+|private\s+|protected\s+|readonly\s+|satisfies\s+[A-Za-z_${]|\bas\s+(?:const|unknown|any|[A-Za-z_${]))/m.test(source);
3963
- const hasESM = /^\s*(import|export)\s+/m.test(source);
3964
- if (!isTS && !hasESM) return source;
4246
+ || /^[ \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);
4247
+ const hasESM = /^\s*(?:import|export)\b/m.test(source)
4248
+ || /\bexport\s*\{/.test(source)
4249
+ || /\bimport\s*\{/.test(source)
4250
+ || /\bexport\s+default\b/.test(source);
4251
+ const hasJsx = detectJsxSyntax(source, filename);
4252
+ if (!isTS && !hasESM && !hasJsx) return source;
4253
+
4254
+ // JSX/TSX must be rewritten before masking so JSX text is kept verbatim.
4255
+ if (hasJsx) {
4256
+ const { pragma, frag } = readJsxPragma(source);
4257
+ const needsReact = pragma === 'React.createElement' && !/\bReact\b/.test(source);
4258
+ const transformed = jsxTransform(source, { pragma, frag });
4259
+ if (transformed !== source) {
4260
+ source = transformed;
4261
+ // The classic runtime needs a React binding available to the JSX output.
4262
+ if (needsReact) {
4263
+ source = (keepEsm ? 'import React from "react";\n' : 'const React = require("react");\n') + source;
4264
+ }
4265
+ }
4266
+ }
3965
4267
 
3966
4268
  // Step 1: Mask strings, template literals, regex literals, and comments so transforms never corrupt literals
3967
4269
  const placeholders = [];
@@ -4524,10 +4826,10 @@ function __initVirtualRuntime(rootScope) {
4524
4826
  } while (code !== prevCode);
4525
4827
  }
4526
4828
 
4527
- if (hasESM || isTS) {
4829
+ if ((hasESM || isTS) && !keepEsm) {
4528
4830
  // Convert ES module imports (including multi-line `{ ... }`)
4529
- code = code.replace(/^\s*import\s+\*\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s+from\s+([^\n;]+);?/gm, 'const $1 = require($2);');
4530
- code = code.replace(/^\s*import\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*,\s*\{([\s\S]*?)\}\s+from\s+([^\n;]+);?/gm, (_, defId, specifiers, mod) => {
4831
+ code = code.replace(/\bimport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, 'const $1 = require($2);');
4832
+ code = code.replace(/\bimport\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*,\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, defId, specifiers, mod) => {
4531
4833
  const cleaned = specifiers
4532
4834
  .split(',')
4533
4835
  .map((s) => s.trim())
@@ -4538,7 +4840,7 @@ function __initVirtualRuntime(rootScope) {
4538
4840
  return `const ${defId} = (function(m){ return m && m.__esModule ? m.default : m; })(require(${mTrim}));` +
4539
4841
  (cleaned ? ` const { ${cleaned} } = require(${mTrim});` : '');
4540
4842
  });
4541
- code = code.replace(/^\s*import\s+\{([\s\S]*?)\}\s+from\s+([^\n;]+);?/gm, (_, specifiers, mod) => {
4843
+ code = code.replace(/\bimport\s*\{([\s\S]*?)\}\s*from\s*([^\n;]+);?/gm, (_, specifiers, mod) => {
4542
4844
  const cleaned = specifiers
4543
4845
  .split(',')
4544
4846
  .map((s) => s.trim())
@@ -4547,42 +4849,42 @@ function __initVirtualRuntime(rootScope) {
4547
4849
  .join(', ');
4548
4850
  return cleaned ? `const { ${cleaned} } = require(${mod.trim()});` : '';
4549
4851
  });
4550
- code = code.replace(/^\s*import\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));');
4551
- code = code.replace(/^\s*import\s+(\x00__LIT_\d+__\x00)\s*;?/gm, 'require($1);');
4852
+ 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));');
4853
+ code = code.replace(/\bimport\s+(\x00__LIT_\d+__\x00)\s*;?/gm, 'require($1);');
4552
4854
 
4553
4855
  // Convert ES module exports
4554
4856
  const exportedNames = [];
4555
4857
  let defaultExportName = null;
4556
4858
  let hasEsmExport = false;
4557
4859
 
4558
- code = code.replace(/^\s*export\s+\*\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s+from\s+([^\n;]+);?/gm, (_, ns, mod) => {
4860
+ code = code.replace(/\bexport\s*\*\s*as\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*from\s*([^\n;]+);?/gm, (_, ns, mod) => {
4559
4861
  hasEsmExport = true;
4560
4862
  return `exports.${ns} = require(${mod.trim()});`;
4561
4863
  });
4562
- code = code.replace(/^\s*export\s+\*\s+from\s+([^\n;]+);?/gm, (_, mod) => {
4864
+ code = code.replace(/\bexport\s*\*\s*from\s*([^\n;]+);?/gm, (_, mod) => {
4563
4865
  hasEsmExport = true;
4564
4866
  return `Object.assign(exports, require(${mod.trim()}));`;
4565
4867
  });
4566
- code = code.replace(/^\s*export\s+default\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4868
+ code = code.replace(/\bexport\s+default\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4567
4869
  hasEsmExport = true;
4568
4870
  defaultExportName = name;
4569
4871
  return `${kw} ${name}`;
4570
4872
  });
4571
- code = code.replace(/^\s*export\s+default\s+/gm, () => {
4873
+ code = code.replace(/\bexport\s+default\s+/gm, () => {
4572
4874
  hasEsmExport = true;
4573
4875
  return 'exports.default = ';
4574
4876
  });
4575
- code = code.replace(/^\s*export\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4877
+ code = code.replace(/\bexport\s+(async\s+function|function|class)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4576
4878
  hasEsmExport = true;
4577
4879
  exportedNames.push(name);
4578
4880
  return `${kw} ${name}`;
4579
4881
  });
4580
- code = code.replace(/^\s*export\s+(const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4882
+ code = code.replace(/\bexport\s+(const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, (_, kw, name) => {
4581
4883
  hasEsmExport = true;
4582
4884
  exportedNames.push(name);
4583
4885
  return `${kw} ${name}`;
4584
4886
  });
4585
- code = code.replace(/^\s*export\s+\{([^}]+)\}\s+from\s+([^\n;]+);?/gm, (_, list, mod) => {
4887
+ code = code.replace(/\bexport\s*\{([^}]+)\}\s*from\s*([^\n;]+);?/gm, (_, list, mod) => {
4586
4888
  hasEsmExport = true;
4587
4889
  const mTrim = mod.trim();
4588
4890
  return list.split(',').map((item) => {
@@ -4594,7 +4896,7 @@ function __initVirtualRuntime(rootScope) {
4594
4896
  return `exports.${exported} = require(${mTrim}).${local};`;
4595
4897
  }).join(' ');
4596
4898
  });
4597
- code = code.replace(/^\s*export\s+\{([^}]+)\}\s*;?/gm, (_, list) => {
4899
+ code = code.replace(/\bexport\s*\{([^}]+)\}\s*;?/gm, (_, list) => {
4598
4900
  hasEsmExport = true;
4599
4901
  return list.split(',').map((item) => {
4600
4902
  const trimmed = item.trim();
@@ -7219,6 +7521,14 @@ function __initVirtualRuntime(rootScope) {
7219
7521
  };
7220
7522
  }
7221
7523
 
7524
+ // Transpile a source file so it can run natively in a browser as an ES module:
7525
+ // JSX is compiled to the classic runtime (auto-importing React when needed) and
7526
+ // TypeScript annotations are stripped, but `import`/`export` are preserved so an
7527
+ // import map can resolve bare specifiers to CDN ESM builds.
7528
+ function transpileForBrowser(source, filename = 'module.js') {
7529
+ return transpileTS(source, filename, { keepEsm: true });
7530
+ }
7531
+
7222
7532
  return {
7223
7533
  VirtualBuffer,
7224
7534
  VirtualCrypto,
@@ -7238,6 +7548,7 @@ function __initVirtualRuntime(rootScope) {
7238
7548
  createVirtualHttp,
7239
7549
  createVirtualNet,
7240
7550
  transpileTS,
7551
+ transpileForBrowser,
7241
7552
  preloadNpmPackages,
7242
7553
  createVirtualPortTable,
7243
7554
  executeInVirtualRuntime