@innovatorved/code-lab 0.1.9 → 0.1.11
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 +1 -1
- package/README.md +1 -1
- package/dist/code-lab.css +87 -15
- package/dist/code-lab.d.ts +1 -1
- package/dist/code-lab.esm.js +218 -22
- package/dist/code-lab.js +217 -21
- package/dist/index.d.ts +1 -1
- package/dist/preview-sw.js +2 -2
- package/dist/runtime.esm.js +29 -4
- package/dist/runtime.js +29 -4
- package/dist/schemas.d.ts +1 -1
- package/dist/shell.esm.js +151 -9
- package/dist/shell.js +151 -9
- package/dist/terminal.esm.js +1 -1
- package/dist/terminal.js +1 -1
- package/package.json +1 -1
- package/src/code-lab.css +87 -15
- package/src/csp.ts +6 -1
- package/src/index.ts +1 -1
- package/src/runtime.ts +28 -3
- package/src/schemas.ts +1 -1
- package/src/shell.ts +150 -8
- package/src/workbench.ts +37 -8
package/dist/shell.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ============================================================
|
|
3
|
-
* @innovatorved/code-lab v0.1.
|
|
3
|
+
* @innovatorved/code-lab v0.1.11 — Standalone Browser Code IDE & Virtual Runtime
|
|
4
4
|
* ============================================================
|
|
5
5
|
* Features:
|
|
6
6
|
* - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
|
|
@@ -3138,11 +3138,48 @@ var CodeLabLogger = (() => {
|
|
|
3138
3138
|
if (!pkgName) continue;
|
|
3139
3139
|
const subpath = spec.slice(pkgName.length).replace(/^\/+/, '');
|
|
3140
3140
|
const version = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3141
|
+
if (subpath && /\.css$/i.test(subpath)) {
|
|
3142
|
+
const cssHref = `${CDN_ESM_BASE}/${pkgName}@${version}/${subpath.replace(/^\/+/, '')}`;
|
|
3143
|
+
const loader = `if(typeof document!=='undefined'){const l=document.createElement('link');l.rel='stylesheet';l.href=${JSON.stringify(cssHref)};document.head.appendChild(l);}export default ${JSON.stringify(cssHref)};`;
|
|
3144
|
+
imports[spec] = `data:text/javascript;charset=utf-8,${encodeURIComponent(loader)}`;
|
|
3145
|
+
continue;
|
|
3146
|
+
}
|
|
3141
3147
|
imports[spec] = cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3142
3148
|
}
|
|
3143
3149
|
return imports;
|
|
3144
3150
|
}
|
|
3145
3151
|
|
|
3152
|
+
// Some templates pin a CDN URL in the source instead of importing a bare
|
|
3153
|
+
// specifier, e.g. `import React from "https://cdn.jsdelivr.net/npm/react@19.3.1/+esm"`.
|
|
3154
|
+
// Those URL specifiers are resolved by the browser directly, so they bypass the
|
|
3155
|
+
// generated import map (and with it the project's declared version and the
|
|
3156
|
+
// multi-CDN fallback). Import maps can remap URL specifiers, so alias each
|
|
3157
|
+
// literal URL found in the project to the resolved bare-specifier URL.
|
|
3158
|
+
// The pinned version is kept when the project has no dependency to resolve.
|
|
3159
|
+
const PINNED_CDN_URL_RE = /https?:\/\/(?:cdn\.jsdelivr\.net\/npm|esm\.sh)\/((?:@[^\/'"`@\s]+\/)?[^\/'"`@\s]+)@([^\s'"`),;]+)/g;
|
|
3160
|
+
|
|
3161
|
+
function aliasPinnedCdnUrls(vfs, rootDir, text, importMap) {
|
|
3162
|
+
if (!text || typeof text !== 'string') return;
|
|
3163
|
+
const re = new RegExp(PINNED_CDN_URL_RE.source, 'g');
|
|
3164
|
+
let match;
|
|
3165
|
+
while ((match = re.exec(text)) !== null) {
|
|
3166
|
+
const url = match[0];
|
|
3167
|
+
if (url.includes('${')) continue;
|
|
3168
|
+
const pkgName = decodeURIComponent(match[1]);
|
|
3169
|
+
const rest = match[2];
|
|
3170
|
+
const pinnedVersion = rest.split(/[\/?]/)[0];
|
|
3171
|
+
const tail = rest.slice(pinnedVersion.length);
|
|
3172
|
+
const rawSubpath = tail.startsWith('/') && tail !== '/+esm' ? tail.slice(1).split(/[?#]/)[0] : '';
|
|
3173
|
+
const subpath = rawSubpath.replace(/^\/+|\/+$/g, '');
|
|
3174
|
+
const bare = subpath ? `${pkgName}/${subpath}` : pkgName;
|
|
3175
|
+
|
|
3176
|
+
const declared = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3177
|
+
const version = declared && declared !== 'latest' ? declared : decodeURIComponent(pinnedVersion);
|
|
3178
|
+
const target = importMap[bare] || cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3179
|
+
if (target && target !== url) importMap[url] = target;
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
|
|
3146
3183
|
function collectPreviewScriptFiles(vfs, rootDir, maxFiles = 200) {
|
|
3147
3184
|
const files = [];
|
|
3148
3185
|
const walk = (dir, depth) => {
|
|
@@ -3172,6 +3209,33 @@ var CodeLabLogger = (() => {
|
|
|
3172
3209
|
return files;
|
|
3173
3210
|
}
|
|
3174
3211
|
|
|
3212
|
+
function collectPreviewAssetFiles(vfs, rootDir, maxFiles = 200) {
|
|
3213
|
+
const files = [];
|
|
3214
|
+
const walk = (dir, depth) => {
|
|
3215
|
+
if (depth > 8 || files.length >= maxFiles) return;
|
|
3216
|
+
let entries = [];
|
|
3217
|
+
try { entries = vfs.readdirSync(dir); } catch (_) { return; }
|
|
3218
|
+
for (const name of entries) {
|
|
3219
|
+
if (name === 'node_modules' || name.startsWith('.')) continue;
|
|
3220
|
+
const full = PosixPath.join(dir, name);
|
|
3221
|
+
let st;
|
|
3222
|
+
try { st = vfs.statSync(full); } catch (_) { continue; }
|
|
3223
|
+
const dirFlag = st && (typeof st.isDirectory === 'function' ? st.isDirectory() : st.isDirectory);
|
|
3224
|
+
if (dirFlag) {
|
|
3225
|
+
walk(full, depth + 1);
|
|
3226
|
+
} else if (/\.(?:css|json|svg)$/i.test(name) && files.length < maxFiles) {
|
|
3227
|
+
try {
|
|
3228
|
+
const content = vfs.readFileSync(full, 'utf8');
|
|
3229
|
+
const rel = full.startsWith(rootDir + '/') ? full.slice(rootDir.length + 1) : name;
|
|
3230
|
+
files.push({ full, rel, content });
|
|
3231
|
+
} catch (_) {}
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
};
|
|
3235
|
+
walk(rootDir, 0);
|
|
3236
|
+
return files;
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3175
3239
|
function collectPreviewScripts(vfs, rootDir, maxFiles = 200) {
|
|
3176
3240
|
return collectPreviewScriptFiles(vfs, rootDir, maxFiles).map((f) => f.content);
|
|
3177
3241
|
}
|
|
@@ -3254,11 +3318,31 @@ var CodeLabLogger = (() => {
|
|
|
3254
3318
|
return out;
|
|
3255
3319
|
};
|
|
3256
3320
|
|
|
3321
|
+
const inlineLocalCssImports = (cssText, cssDir) => {
|
|
3322
|
+
if (!cssText) return '';
|
|
3323
|
+
return cssText.replace(/@import\s+(?:url\(\s*['"]?([^'")]+)['"]?\s*\)|['"]([^'"]+)['"])\s*;?/gi, (full, u1, u2) => {
|
|
3324
|
+
const spec = (u1 || u2 || '').trim();
|
|
3325
|
+
if (!spec || /^(?:https?:|data:|\/\/)/i.test(spec)) return full;
|
|
3326
|
+
if (spec === 'tailwindcss') return '';
|
|
3327
|
+
const subTarget = resolveLocalTarget(cssDir, spec);
|
|
3328
|
+
if (subTarget && /\.css$/i.test(subTarget)) {
|
|
3329
|
+
const subCss = readText(subTarget);
|
|
3330
|
+
if (subCss !== null) return subCss;
|
|
3331
|
+
}
|
|
3332
|
+
return '';
|
|
3333
|
+
});
|
|
3334
|
+
};
|
|
3335
|
+
|
|
3257
3336
|
const buildHtmlPreviewResponse = (htmlBody, htmlTargetPath) => {
|
|
3258
3337
|
const scriptFiles = collectPreviewScriptFiles(ctx.vfs, rootDir);
|
|
3338
|
+
const assetFiles = collectPreviewAssetFiles(ctx.vfs, rootDir);
|
|
3259
3339
|
const specifiers = new Set();
|
|
3340
|
+
const compiledScripts = [];
|
|
3260
3341
|
for (const f of scriptFiles) {
|
|
3261
3342
|
for (const s of extractBareSpecifiers(f.content)) specifiers.add(s);
|
|
3343
|
+
const compiled = transpileAndRewriteLocalImports(f.content, f.full);
|
|
3344
|
+
for (const s of extractBareSpecifiers(compiled)) specifiers.add(s);
|
|
3345
|
+
compiledScripts.push({ file: f, compiled });
|
|
3262
3346
|
}
|
|
3263
3347
|
// Also scan inline module scripts in HTML for bare specifiers
|
|
3264
3348
|
const inlineScriptRe = /<script\b[^>]*>([\s\S]*?)<\/script>/gi;
|
|
@@ -3270,14 +3354,55 @@ var CodeLabLogger = (() => {
|
|
|
3270
3354
|
}
|
|
3271
3355
|
|
|
3272
3356
|
const importMap = buildPreviewImportMap(ctx.vfs, rootDir, specifiers);
|
|
3273
|
-
for (const f of scriptFiles)
|
|
3274
|
-
|
|
3275
|
-
|
|
3357
|
+
for (const f of scriptFiles) aliasPinnedCdnUrls(ctx.vfs, rootDir, f.content, importMap);
|
|
3358
|
+
aliasPinnedCdnUrls(ctx.vfs, rootDir, htmlBody, importMap);
|
|
3359
|
+
for (const { file, compiled } of compiledScripts) {
|
|
3360
|
+
const key = toLocalImportKey(file.full);
|
|
3276
3361
|
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(compiled)}`;
|
|
3277
3362
|
}
|
|
3278
3363
|
|
|
3364
|
+
let needsTailwindBrowser = false;
|
|
3365
|
+
for (const af of assetFiles) {
|
|
3366
|
+
const key = toLocalImportKey(af.full);
|
|
3367
|
+
if (/\.css$/i.test(af.full)) {
|
|
3368
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(af.content)) {
|
|
3369
|
+
needsTailwindBrowser = true;
|
|
3370
|
+
}
|
|
3371
|
+
const resolvedCss = inlineLocalCssImports(af.content, PosixPath.dirname(af.full));
|
|
3372
|
+
const cssModuleJs = `const __css=${JSON.stringify(resolvedCss)};if(typeof document!=='undefined'){const __s=document.createElement('style');__s.setAttribute('data-codelab-css',${JSON.stringify(af.rel)});__s.textContent=__css;document.head.appendChild(__s);}export default __css;`;
|
|
3373
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(cssModuleJs)}`;
|
|
3374
|
+
} else if (/\.json$/i.test(af.full)) {
|
|
3375
|
+
const jsonModuleJs = `export default ${af.content.trim() || 'null'};`;
|
|
3376
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(jsonModuleJs)}`;
|
|
3377
|
+
} else if (/\.svg$/i.test(af.full)) {
|
|
3378
|
+
const svgDataUrl = `data:image/svg+xml;utf8,${encodeURIComponent(af.content)}`;
|
|
3379
|
+
const svgModuleJs = `export default ${JSON.stringify(svgDataUrl)};`;
|
|
3380
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(svgModuleJs)}`;
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3383
|
+
|
|
3279
3384
|
const htmlDir = PosixPath.dirname(htmlTargetPath);
|
|
3280
|
-
let transformedHtml = htmlBody;
|
|
3385
|
+
let transformedHtml = htmlBody.replace(/%PUBLIC_URL%\//g, '/').replace(/%PUBLIC_URL%/g, '');
|
|
3386
|
+
|
|
3387
|
+
// Auto-inject entry script for Create React App / bundler templates where index.html omits <script src="...">
|
|
3388
|
+
const hasLocalScriptTag = /<script\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/i.test(transformedHtml);
|
|
3389
|
+
if (!hasLocalScriptTag) {
|
|
3390
|
+
const defaultEntries = [
|
|
3391
|
+
'/src/main.tsx', '/src/main.jsx', '/src/main.ts', '/src/main.js',
|
|
3392
|
+
'/src/index.tsx', '/src/index.jsx', '/src/index.ts', '/src/index.js'
|
|
3393
|
+
];
|
|
3394
|
+
for (const cand of defaultEntries) {
|
|
3395
|
+
if (isFile(PosixPath.join(rootDir, cand.slice(1)))) {
|
|
3396
|
+
const entryTag = `<script type="module" src="${cand}"></script>`;
|
|
3397
|
+
if (/<\/body>/i.test(transformedHtml)) {
|
|
3398
|
+
transformedHtml = transformedHtml.replace(/<\/body>/i, ` ${entryTag}\n</body>`);
|
|
3399
|
+
} else {
|
|
3400
|
+
transformedHtml += `\n${entryTag}`;
|
|
3401
|
+
}
|
|
3402
|
+
break;
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3281
3406
|
|
|
3282
3407
|
// Inline local <link rel="stylesheet" href="..."> so styles work in opaque-origin / srcdoc frames
|
|
3283
3408
|
transformedHtml = transformedHtml.replace(/<link\b([^>]*?)>/gi, (fullTag, attrs) => {
|
|
@@ -3288,7 +3413,11 @@ var CodeLabLogger = (() => {
|
|
|
3288
3413
|
if (!cssTarget || !/\.css$/i.test(cssTarget)) return fullTag;
|
|
3289
3414
|
const cssContent = readText(cssTarget);
|
|
3290
3415
|
if (cssContent === null) return fullTag;
|
|
3291
|
-
|
|
3416
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(cssContent)) {
|
|
3417
|
+
needsTailwindBrowser = true;
|
|
3418
|
+
}
|
|
3419
|
+
const resolvedCss = inlineLocalCssImports(cssContent, PosixPath.dirname(cssTarget));
|
|
3420
|
+
return `<style data-codelab-href="${hrefMatch[1].replace(/"/g, '"')}">\n${resolvedCss}\n</style>`;
|
|
3292
3421
|
});
|
|
3293
3422
|
|
|
3294
3423
|
// Inline local <script ... src="..."></script> so entry modules & scripts execute reliably in opaque-origin / srcdoc frames
|
|
@@ -3300,11 +3429,20 @@ var CodeLabLogger = (() => {
|
|
|
3300
3429
|
const rawScript = readText(scriptTarget);
|
|
3301
3430
|
if (rawScript === null) return fullTag;
|
|
3302
3431
|
const compiled = transpileAndRewriteLocalImports(rawScript, scriptTarget);
|
|
3303
|
-
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget);
|
|
3432
|
+
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget) || /^\s*(?:import|export)\b/m.test(rawScript);
|
|
3304
3433
|
const typeAttr = isMod ? ' type="module"' : '';
|
|
3305
3434
|
return `<script${typeAttr} data-codelab-src="${srcMatch[1].replace(/"/g, '"')}">\n${compiled}\n</script>`;
|
|
3306
3435
|
});
|
|
3307
3436
|
|
|
3437
|
+
if (needsTailwindBrowser && !transformedHtml.includes('@tailwindcss/browser')) {
|
|
3438
|
+
const twTag = '<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>';
|
|
3439
|
+
if (/<head[^>]*>/i.test(transformedHtml)) {
|
|
3440
|
+
transformedHtml = transformedHtml.replace(/<head[^>]*>/i, (m) => `${m}\n ${twTag}`);
|
|
3441
|
+
} else {
|
|
3442
|
+
transformedHtml = twTag + '\n' + transformedHtml;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
|
|
3308
3446
|
return injectPreviewExtras(transformedHtml, importMap);
|
|
3309
3447
|
};
|
|
3310
3448
|
|
|
@@ -3316,7 +3454,9 @@ var CodeLabLogger = (() => {
|
|
|
3316
3454
|
const hasExt = /\.[A-Za-z0-9]+$/.test(rel);
|
|
3317
3455
|
|
|
3318
3456
|
if (!rel || pathname.endsWith('/') || isDir(target)) {
|
|
3319
|
-
|
|
3457
|
+
const rootIdx = PosixPath.join(target, 'index.html');
|
|
3458
|
+
const pubIdx = PosixPath.join(target, 'public', 'index.html');
|
|
3459
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx;
|
|
3320
3460
|
}
|
|
3321
3461
|
if (!exists(target) && !hasExt) {
|
|
3322
3462
|
// Resolve extensionless module specifiers (e.g. `./components/Counter`)
|
|
@@ -3330,7 +3470,9 @@ var CodeLabLogger = (() => {
|
|
|
3330
3470
|
for (const c of cands) { if (exists(c)) { target = c; break; } }
|
|
3331
3471
|
}
|
|
3332
3472
|
if (!exists(target) && !hasExt) {
|
|
3333
|
-
|
|
3473
|
+
const rootIdx = PosixPath.join(rootDir, 'index.html');
|
|
3474
|
+
const pubIdx = PosixPath.join(rootDir, 'public', 'index.html');
|
|
3475
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx; // SPA fallback
|
|
3334
3476
|
}
|
|
3335
3477
|
if (!exists(target)) {
|
|
3336
3478
|
return new Response(
|
package/dist/shell.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ============================================================
|
|
3
|
-
* @innovatorved/code-lab v0.1.
|
|
3
|
+
* @innovatorved/code-lab v0.1.11 — Standalone Browser Code IDE & Virtual Runtime
|
|
4
4
|
* ============================================================
|
|
5
5
|
* Features:
|
|
6
6
|
* - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
|
|
@@ -3139,11 +3139,48 @@ var CodeLabLogger = (() => {
|
|
|
3139
3139
|
if (!pkgName) continue;
|
|
3140
3140
|
const subpath = spec.slice(pkgName.length).replace(/^\/+/, '');
|
|
3141
3141
|
const version = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3142
|
+
if (subpath && /\.css$/i.test(subpath)) {
|
|
3143
|
+
const cssHref = `${CDN_ESM_BASE}/${pkgName}@${version}/${subpath.replace(/^\/+/, '')}`;
|
|
3144
|
+
const loader = `if(typeof document!=='undefined'){const l=document.createElement('link');l.rel='stylesheet';l.href=${JSON.stringify(cssHref)};document.head.appendChild(l);}export default ${JSON.stringify(cssHref)};`;
|
|
3145
|
+
imports[spec] = `data:text/javascript;charset=utf-8,${encodeURIComponent(loader)}`;
|
|
3146
|
+
continue;
|
|
3147
|
+
}
|
|
3142
3148
|
imports[spec] = cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3143
3149
|
}
|
|
3144
3150
|
return imports;
|
|
3145
3151
|
}
|
|
3146
3152
|
|
|
3153
|
+
// Some templates pin a CDN URL in the source instead of importing a bare
|
|
3154
|
+
// specifier, e.g. `import React from "https://cdn.jsdelivr.net/npm/react@19.3.1/+esm"`.
|
|
3155
|
+
// Those URL specifiers are resolved by the browser directly, so they bypass the
|
|
3156
|
+
// generated import map (and with it the project's declared version and the
|
|
3157
|
+
// multi-CDN fallback). Import maps can remap URL specifiers, so alias each
|
|
3158
|
+
// literal URL found in the project to the resolved bare-specifier URL.
|
|
3159
|
+
// The pinned version is kept when the project has no dependency to resolve.
|
|
3160
|
+
const PINNED_CDN_URL_RE = /https?:\/\/(?:cdn\.jsdelivr\.net\/npm|esm\.sh)\/((?:@[^\/'"`@\s]+\/)?[^\/'"`@\s]+)@([^\s'"`),;]+)/g;
|
|
3161
|
+
|
|
3162
|
+
function aliasPinnedCdnUrls(vfs, rootDir, text, importMap) {
|
|
3163
|
+
if (!text || typeof text !== 'string') return;
|
|
3164
|
+
const re = new RegExp(PINNED_CDN_URL_RE.source, 'g');
|
|
3165
|
+
let match;
|
|
3166
|
+
while ((match = re.exec(text)) !== null) {
|
|
3167
|
+
const url = match[0];
|
|
3168
|
+
if (url.includes('${')) continue;
|
|
3169
|
+
const pkgName = decodeURIComponent(match[1]);
|
|
3170
|
+
const rest = match[2];
|
|
3171
|
+
const pinnedVersion = rest.split(/[\/?]/)[0];
|
|
3172
|
+
const tail = rest.slice(pinnedVersion.length);
|
|
3173
|
+
const rawSubpath = tail.startsWith('/') && tail !== '/+esm' ? tail.slice(1).split(/[?#]/)[0] : '';
|
|
3174
|
+
const subpath = rawSubpath.replace(/^\/+|\/+$/g, '');
|
|
3175
|
+
const bare = subpath ? `${pkgName}/${subpath}` : pkgName;
|
|
3176
|
+
|
|
3177
|
+
const declared = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3178
|
+
const version = declared && declared !== 'latest' ? declared : decodeURIComponent(pinnedVersion);
|
|
3179
|
+
const target = importMap[bare] || cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3180
|
+
if (target && target !== url) importMap[url] = target;
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
|
|
3147
3184
|
function collectPreviewScriptFiles(vfs, rootDir, maxFiles = 200) {
|
|
3148
3185
|
const files = [];
|
|
3149
3186
|
const walk = (dir, depth) => {
|
|
@@ -3173,6 +3210,33 @@ var CodeLabLogger = (() => {
|
|
|
3173
3210
|
return files;
|
|
3174
3211
|
}
|
|
3175
3212
|
|
|
3213
|
+
function collectPreviewAssetFiles(vfs, rootDir, maxFiles = 200) {
|
|
3214
|
+
const files = [];
|
|
3215
|
+
const walk = (dir, depth) => {
|
|
3216
|
+
if (depth > 8 || files.length >= maxFiles) return;
|
|
3217
|
+
let entries = [];
|
|
3218
|
+
try { entries = vfs.readdirSync(dir); } catch (_) { return; }
|
|
3219
|
+
for (const name of entries) {
|
|
3220
|
+
if (name === 'node_modules' || name.startsWith('.')) continue;
|
|
3221
|
+
const full = PosixPath.join(dir, name);
|
|
3222
|
+
let st;
|
|
3223
|
+
try { st = vfs.statSync(full); } catch (_) { continue; }
|
|
3224
|
+
const dirFlag = st && (typeof st.isDirectory === 'function' ? st.isDirectory() : st.isDirectory);
|
|
3225
|
+
if (dirFlag) {
|
|
3226
|
+
walk(full, depth + 1);
|
|
3227
|
+
} else if (/\.(?:css|json|svg)$/i.test(name) && files.length < maxFiles) {
|
|
3228
|
+
try {
|
|
3229
|
+
const content = vfs.readFileSync(full, 'utf8');
|
|
3230
|
+
const rel = full.startsWith(rootDir + '/') ? full.slice(rootDir.length + 1) : name;
|
|
3231
|
+
files.push({ full, rel, content });
|
|
3232
|
+
} catch (_) {}
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
};
|
|
3236
|
+
walk(rootDir, 0);
|
|
3237
|
+
return files;
|
|
3238
|
+
}
|
|
3239
|
+
|
|
3176
3240
|
function collectPreviewScripts(vfs, rootDir, maxFiles = 200) {
|
|
3177
3241
|
return collectPreviewScriptFiles(vfs, rootDir, maxFiles).map((f) => f.content);
|
|
3178
3242
|
}
|
|
@@ -3255,11 +3319,31 @@ var CodeLabLogger = (() => {
|
|
|
3255
3319
|
return out;
|
|
3256
3320
|
};
|
|
3257
3321
|
|
|
3322
|
+
const inlineLocalCssImports = (cssText, cssDir) => {
|
|
3323
|
+
if (!cssText) return '';
|
|
3324
|
+
return cssText.replace(/@import\s+(?:url\(\s*['"]?([^'")]+)['"]?\s*\)|['"]([^'"]+)['"])\s*;?/gi, (full, u1, u2) => {
|
|
3325
|
+
const spec = (u1 || u2 || '').trim();
|
|
3326
|
+
if (!spec || /^(?:https?:|data:|\/\/)/i.test(spec)) return full;
|
|
3327
|
+
if (spec === 'tailwindcss') return '';
|
|
3328
|
+
const subTarget = resolveLocalTarget(cssDir, spec);
|
|
3329
|
+
if (subTarget && /\.css$/i.test(subTarget)) {
|
|
3330
|
+
const subCss = readText(subTarget);
|
|
3331
|
+
if (subCss !== null) return subCss;
|
|
3332
|
+
}
|
|
3333
|
+
return '';
|
|
3334
|
+
});
|
|
3335
|
+
};
|
|
3336
|
+
|
|
3258
3337
|
const buildHtmlPreviewResponse = (htmlBody, htmlTargetPath) => {
|
|
3259
3338
|
const scriptFiles = collectPreviewScriptFiles(ctx.vfs, rootDir);
|
|
3339
|
+
const assetFiles = collectPreviewAssetFiles(ctx.vfs, rootDir);
|
|
3260
3340
|
const specifiers = new Set();
|
|
3341
|
+
const compiledScripts = [];
|
|
3261
3342
|
for (const f of scriptFiles) {
|
|
3262
3343
|
for (const s of extractBareSpecifiers(f.content)) specifiers.add(s);
|
|
3344
|
+
const compiled = transpileAndRewriteLocalImports(f.content, f.full);
|
|
3345
|
+
for (const s of extractBareSpecifiers(compiled)) specifiers.add(s);
|
|
3346
|
+
compiledScripts.push({ file: f, compiled });
|
|
3263
3347
|
}
|
|
3264
3348
|
// Also scan inline module scripts in HTML for bare specifiers
|
|
3265
3349
|
const inlineScriptRe = /<script\b[^>]*>([\s\S]*?)<\/script>/gi;
|
|
@@ -3271,14 +3355,55 @@ var CodeLabLogger = (() => {
|
|
|
3271
3355
|
}
|
|
3272
3356
|
|
|
3273
3357
|
const importMap = buildPreviewImportMap(ctx.vfs, rootDir, specifiers);
|
|
3274
|
-
for (const f of scriptFiles)
|
|
3275
|
-
|
|
3276
|
-
|
|
3358
|
+
for (const f of scriptFiles) aliasPinnedCdnUrls(ctx.vfs, rootDir, f.content, importMap);
|
|
3359
|
+
aliasPinnedCdnUrls(ctx.vfs, rootDir, htmlBody, importMap);
|
|
3360
|
+
for (const { file, compiled } of compiledScripts) {
|
|
3361
|
+
const key = toLocalImportKey(file.full);
|
|
3277
3362
|
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(compiled)}`;
|
|
3278
3363
|
}
|
|
3279
3364
|
|
|
3365
|
+
let needsTailwindBrowser = false;
|
|
3366
|
+
for (const af of assetFiles) {
|
|
3367
|
+
const key = toLocalImportKey(af.full);
|
|
3368
|
+
if (/\.css$/i.test(af.full)) {
|
|
3369
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(af.content)) {
|
|
3370
|
+
needsTailwindBrowser = true;
|
|
3371
|
+
}
|
|
3372
|
+
const resolvedCss = inlineLocalCssImports(af.content, PosixPath.dirname(af.full));
|
|
3373
|
+
const cssModuleJs = `const __css=${JSON.stringify(resolvedCss)};if(typeof document!=='undefined'){const __s=document.createElement('style');__s.setAttribute('data-codelab-css',${JSON.stringify(af.rel)});__s.textContent=__css;document.head.appendChild(__s);}export default __css;`;
|
|
3374
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(cssModuleJs)}`;
|
|
3375
|
+
} else if (/\.json$/i.test(af.full)) {
|
|
3376
|
+
const jsonModuleJs = `export default ${af.content.trim() || 'null'};`;
|
|
3377
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(jsonModuleJs)}`;
|
|
3378
|
+
} else if (/\.svg$/i.test(af.full)) {
|
|
3379
|
+
const svgDataUrl = `data:image/svg+xml;utf8,${encodeURIComponent(af.content)}`;
|
|
3380
|
+
const svgModuleJs = `export default ${JSON.stringify(svgDataUrl)};`;
|
|
3381
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(svgModuleJs)}`;
|
|
3382
|
+
}
|
|
3383
|
+
}
|
|
3384
|
+
|
|
3280
3385
|
const htmlDir = PosixPath.dirname(htmlTargetPath);
|
|
3281
|
-
let transformedHtml = htmlBody;
|
|
3386
|
+
let transformedHtml = htmlBody.replace(/%PUBLIC_URL%\//g, '/').replace(/%PUBLIC_URL%/g, '');
|
|
3387
|
+
|
|
3388
|
+
// Auto-inject entry script for Create React App / bundler templates where index.html omits <script src="...">
|
|
3389
|
+
const hasLocalScriptTag = /<script\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/i.test(transformedHtml);
|
|
3390
|
+
if (!hasLocalScriptTag) {
|
|
3391
|
+
const defaultEntries = [
|
|
3392
|
+
'/src/main.tsx', '/src/main.jsx', '/src/main.ts', '/src/main.js',
|
|
3393
|
+
'/src/index.tsx', '/src/index.jsx', '/src/index.ts', '/src/index.js'
|
|
3394
|
+
];
|
|
3395
|
+
for (const cand of defaultEntries) {
|
|
3396
|
+
if (isFile(PosixPath.join(rootDir, cand.slice(1)))) {
|
|
3397
|
+
const entryTag = `<script type="module" src="${cand}"></script>`;
|
|
3398
|
+
if (/<\/body>/i.test(transformedHtml)) {
|
|
3399
|
+
transformedHtml = transformedHtml.replace(/<\/body>/i, ` ${entryTag}\n</body>`);
|
|
3400
|
+
} else {
|
|
3401
|
+
transformedHtml += `\n${entryTag}`;
|
|
3402
|
+
}
|
|
3403
|
+
break;
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3282
3407
|
|
|
3283
3408
|
// Inline local <link rel="stylesheet" href="..."> so styles work in opaque-origin / srcdoc frames
|
|
3284
3409
|
transformedHtml = transformedHtml.replace(/<link\b([^>]*?)>/gi, (fullTag, attrs) => {
|
|
@@ -3289,7 +3414,11 @@ var CodeLabLogger = (() => {
|
|
|
3289
3414
|
if (!cssTarget || !/\.css$/i.test(cssTarget)) return fullTag;
|
|
3290
3415
|
const cssContent = readText(cssTarget);
|
|
3291
3416
|
if (cssContent === null) return fullTag;
|
|
3292
|
-
|
|
3417
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(cssContent)) {
|
|
3418
|
+
needsTailwindBrowser = true;
|
|
3419
|
+
}
|
|
3420
|
+
const resolvedCss = inlineLocalCssImports(cssContent, PosixPath.dirname(cssTarget));
|
|
3421
|
+
return `<style data-codelab-href="${hrefMatch[1].replace(/"/g, '"')}">\n${resolvedCss}\n</style>`;
|
|
3293
3422
|
});
|
|
3294
3423
|
|
|
3295
3424
|
// Inline local <script ... src="..."></script> so entry modules & scripts execute reliably in opaque-origin / srcdoc frames
|
|
@@ -3301,11 +3430,20 @@ var CodeLabLogger = (() => {
|
|
|
3301
3430
|
const rawScript = readText(scriptTarget);
|
|
3302
3431
|
if (rawScript === null) return fullTag;
|
|
3303
3432
|
const compiled = transpileAndRewriteLocalImports(rawScript, scriptTarget);
|
|
3304
|
-
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget);
|
|
3433
|
+
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget) || /^\s*(?:import|export)\b/m.test(rawScript);
|
|
3305
3434
|
const typeAttr = isMod ? ' type="module"' : '';
|
|
3306
3435
|
return `<script${typeAttr} data-codelab-src="${srcMatch[1].replace(/"/g, '"')}">\n${compiled}\n</script>`;
|
|
3307
3436
|
});
|
|
3308
3437
|
|
|
3438
|
+
if (needsTailwindBrowser && !transformedHtml.includes('@tailwindcss/browser')) {
|
|
3439
|
+
const twTag = '<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>';
|
|
3440
|
+
if (/<head[^>]*>/i.test(transformedHtml)) {
|
|
3441
|
+
transformedHtml = transformedHtml.replace(/<head[^>]*>/i, (m) => `${m}\n ${twTag}`);
|
|
3442
|
+
} else {
|
|
3443
|
+
transformedHtml = twTag + '\n' + transformedHtml;
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
|
|
3309
3447
|
return injectPreviewExtras(transformedHtml, importMap);
|
|
3310
3448
|
};
|
|
3311
3449
|
|
|
@@ -3317,7 +3455,9 @@ var CodeLabLogger = (() => {
|
|
|
3317
3455
|
const hasExt = /\.[A-Za-z0-9]+$/.test(rel);
|
|
3318
3456
|
|
|
3319
3457
|
if (!rel || pathname.endsWith('/') || isDir(target)) {
|
|
3320
|
-
|
|
3458
|
+
const rootIdx = PosixPath.join(target, 'index.html');
|
|
3459
|
+
const pubIdx = PosixPath.join(target, 'public', 'index.html');
|
|
3460
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx;
|
|
3321
3461
|
}
|
|
3322
3462
|
if (!exists(target) && !hasExt) {
|
|
3323
3463
|
// Resolve extensionless module specifiers (e.g. `./components/Counter`)
|
|
@@ -3331,7 +3471,9 @@ var CodeLabLogger = (() => {
|
|
|
3331
3471
|
for (const c of cands) { if (exists(c)) { target = c; break; } }
|
|
3332
3472
|
}
|
|
3333
3473
|
if (!exists(target) && !hasExt) {
|
|
3334
|
-
|
|
3474
|
+
const rootIdx = PosixPath.join(rootDir, 'index.html');
|
|
3475
|
+
const pubIdx = PosixPath.join(rootDir, 'public', 'index.html');
|
|
3476
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx; // SPA fallback
|
|
3335
3477
|
}
|
|
3336
3478
|
if (!exists(target)) {
|
|
3337
3479
|
return new Response(
|
package/dist/terminal.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ============================================================
|
|
3
|
-
* @innovatorved/code-lab v0.1.
|
|
3
|
+
* @innovatorved/code-lab v0.1.11 — Standalone Browser Code IDE & Virtual Runtime
|
|
4
4
|
* ============================================================
|
|
5
5
|
* Features:
|
|
6
6
|
* - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
|
package/dist/terminal.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ============================================================
|
|
3
|
-
* @innovatorved/code-lab v0.1.
|
|
3
|
+
* @innovatorved/code-lab v0.1.11 — Standalone Browser Code IDE & Virtual Runtime
|
|
4
4
|
* ============================================================
|
|
5
5
|
* Features:
|
|
6
6
|
* - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@innovatorved/code-lab",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
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",
|