@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/src/code-lab.css
CHANGED
|
@@ -4259,19 +4259,20 @@ code-lab,
|
|
|
4259
4259
|
display: flex;
|
|
4260
4260
|
flex-direction: column;
|
|
4261
4261
|
height: 100%;
|
|
4262
|
-
min-height:
|
|
4262
|
+
min-height: 0;
|
|
4263
4263
|
gap: 8px;
|
|
4264
4264
|
}
|
|
4265
4265
|
|
|
4266
4266
|
.codelab-preview-chrome {
|
|
4267
4267
|
display: flex;
|
|
4268
4268
|
align-items: center;
|
|
4269
|
-
gap:
|
|
4269
|
+
gap: 6px;
|
|
4270
4270
|
flex-wrap: nowrap;
|
|
4271
4271
|
padding: 6px 8px;
|
|
4272
4272
|
background: var(--cl-bg-panel);
|
|
4273
4273
|
border: 1px solid var(--cl-border);
|
|
4274
4274
|
border-radius: var(--cl-radius-md);
|
|
4275
|
+
flex-shrink: 0;
|
|
4275
4276
|
}
|
|
4276
4277
|
|
|
4277
4278
|
.codelab-preview-nav { display: flex; gap: 2px; flex: 0 0 auto; }
|
|
@@ -4294,7 +4295,7 @@ code-lab,
|
|
|
4294
4295
|
.codelab-preview-btn:disabled { opacity: 0.35; cursor: default; }
|
|
4295
4296
|
|
|
4296
4297
|
.codelab-preview-port-select {
|
|
4297
|
-
flex: 0
|
|
4298
|
+
flex: 0 1 auto;
|
|
4298
4299
|
height: 28px;
|
|
4299
4300
|
background: var(--cl-bg-elevated);
|
|
4300
4301
|
color: var(--cl-fg);
|
|
@@ -4302,12 +4303,16 @@ code-lab,
|
|
|
4302
4303
|
border-radius: var(--cl-radius-sm);
|
|
4303
4304
|
font-size: 12px;
|
|
4304
4305
|
padding: 0 6px;
|
|
4305
|
-
max-width:
|
|
4306
|
+
max-width: 170px;
|
|
4307
|
+
}
|
|
4308
|
+
|
|
4309
|
+
.codelab-preview-split .codelab-preview-port-select {
|
|
4310
|
+
max-width: 118px;
|
|
4306
4311
|
}
|
|
4307
4312
|
|
|
4308
4313
|
.codelab-preview-url-input {
|
|
4309
|
-
flex: 1 1
|
|
4310
|
-
min-width:
|
|
4314
|
+
flex: 1 1 160px;
|
|
4315
|
+
min-width: 120px;
|
|
4311
4316
|
height: 28px;
|
|
4312
4317
|
background: var(--cl-bg);
|
|
4313
4318
|
color: var(--cl-fg);
|
|
@@ -4324,8 +4329,8 @@ code-lab,
|
|
|
4324
4329
|
|
|
4325
4330
|
/* CSP badge: which remote hosts the current preview may reach. */
|
|
4326
4331
|
.codelab-preview-csp {
|
|
4327
|
-
flex: 0
|
|
4328
|
-
max-width:
|
|
4332
|
+
flex: 0 1 auto;
|
|
4333
|
+
max-width: 180px;
|
|
4329
4334
|
overflow: hidden;
|
|
4330
4335
|
text-overflow: ellipsis;
|
|
4331
4336
|
white-space: nowrap;
|
|
@@ -4338,6 +4343,31 @@ code-lab,
|
|
|
4338
4343
|
background: var(--cl-bg-elevated);
|
|
4339
4344
|
}
|
|
4340
4345
|
|
|
4346
|
+
.codelab-preview-split .codelab-preview-csp {
|
|
4347
|
+
max-width: 110px;
|
|
4348
|
+
}
|
|
4349
|
+
|
|
4350
|
+
/* The split pane is narrower than the drawer, so the single-row chrome would
|
|
4351
|
+
clip its right-hand controls. Wrap instead, and give the URL its own row. */
|
|
4352
|
+
.codelab-preview-split .codelab-preview-chrome {
|
|
4353
|
+
flex-wrap: wrap;
|
|
4354
|
+
row-gap: 4px;
|
|
4355
|
+
}
|
|
4356
|
+
|
|
4357
|
+
.codelab-preview-split .codelab-preview-url-input {
|
|
4358
|
+
order: 10;
|
|
4359
|
+
flex: 1 1 100%;
|
|
4360
|
+
}
|
|
4361
|
+
|
|
4362
|
+
/* Icon-only pop-out keeps the split chrome to two rows. */
|
|
4363
|
+
.codelab-preview-split [data-cl-action="preview-popout"] {
|
|
4364
|
+
padding: 0 6px;
|
|
4365
|
+
}
|
|
4366
|
+
|
|
4367
|
+
.codelab-preview-split [data-cl-action="preview-popout"] > span {
|
|
4368
|
+
display: none;
|
|
4369
|
+
}
|
|
4370
|
+
|
|
4341
4371
|
.codelab-preview-csp--blocked {
|
|
4342
4372
|
color: var(--cl-red);
|
|
4343
4373
|
border-color: var(--cl-red);
|
|
@@ -4345,7 +4375,7 @@ code-lab,
|
|
|
4345
4375
|
|
|
4346
4376
|
.codelab-preview-viewport {
|
|
4347
4377
|
flex: 1 1 auto;
|
|
4348
|
-
min-height:
|
|
4378
|
+
min-height: 120px;
|
|
4349
4379
|
display: flex;
|
|
4350
4380
|
justify-content: center;
|
|
4351
4381
|
background: var(--cl-bg);
|
|
@@ -4430,12 +4460,20 @@ code-lab,
|
|
|
4430
4460
|
}
|
|
4431
4461
|
|
|
4432
4462
|
/* ── Split-Pane Web Preview, Maximize Mode & Inline Console ─────────────── */
|
|
4463
|
+
.codelab-workspace.has-preview-split {
|
|
4464
|
+
grid-template-columns: var(--cl-explorer-width, 196px) 4px minmax(260px, 1fr) 5px minmax(240px, var(--cl-preview-split-width, 44%));
|
|
4465
|
+
}
|
|
4466
|
+
|
|
4467
|
+
.codelab.files-collapsed .codelab-workspace.has-preview-split {
|
|
4468
|
+
grid-template-columns: 0px 0px minmax(260px, 1fr) 5px minmax(240px, var(--cl-preview-split-width, 44%));
|
|
4469
|
+
}
|
|
4470
|
+
|
|
4433
4471
|
.codelab-preview-split {
|
|
4434
4472
|
display: none;
|
|
4435
4473
|
flex-direction: column;
|
|
4436
|
-
width:
|
|
4437
|
-
|
|
4438
|
-
|
|
4474
|
+
width: 100%;
|
|
4475
|
+
/* Width comes from the grid track so the pane can shrink with the viewport. */
|
|
4476
|
+
min-width: 0;
|
|
4439
4477
|
height: 100%;
|
|
4440
4478
|
background: var(--cl-bg-panel);
|
|
4441
4479
|
border-left: 1px solid var(--cl-border);
|
|
@@ -4560,7 +4598,7 @@ code-lab,
|
|
|
4560
4598
|
/* ── Ports, Mini HTTP Client & Network Inspector Drawer UI ──────────────── */
|
|
4561
4599
|
.codelab-ports-panel {
|
|
4562
4600
|
display: grid;
|
|
4563
|
-
grid-template-columns: minmax(
|
|
4601
|
+
grid-template-columns: minmax(360px, 1.25fr) minmax(320px, 1fr);
|
|
4564
4602
|
gap: 12px;
|
|
4565
4603
|
padding: 10px 12px;
|
|
4566
4604
|
height: 100%;
|
|
@@ -4678,7 +4716,7 @@ code-lab,
|
|
|
4678
4716
|
text-transform: uppercase;
|
|
4679
4717
|
letter-spacing: 0.03em;
|
|
4680
4718
|
color: var(--cl-fg-muted);
|
|
4681
|
-
padding: 6px
|
|
4719
|
+
padding: 6px 6px;
|
|
4682
4720
|
border-bottom: 1px solid var(--cl-border);
|
|
4683
4721
|
background: var(--cl-bg-elevated);
|
|
4684
4722
|
white-space: nowrap;
|
|
@@ -4686,7 +4724,7 @@ code-lab,
|
|
|
4686
4724
|
|
|
4687
4725
|
.codelab-ports-table td,
|
|
4688
4726
|
.codelab-net-table td {
|
|
4689
|
-
padding: 6px
|
|
4727
|
+
padding: 6px 6px;
|
|
4690
4728
|
border-bottom: 1px solid var(--cl-border);
|
|
4691
4729
|
color: var(--cl-fg);
|
|
4692
4730
|
vertical-align: middle;
|
|
@@ -5085,3 +5123,37 @@ code-lab,
|
|
|
5085
5123
|
white-space: pre-wrap;
|
|
5086
5124
|
word-break: break-word;
|
|
5087
5125
|
}
|
|
5126
|
+
|
|
5127
|
+
/* ── Split-Pane Web Preview: responsive behaviour ────────────────────────────
|
|
5128
|
+
These must come after the base `.codelab-workspace.has-preview-split` rule
|
|
5129
|
+
above: equal specificity, later wins. */
|
|
5130
|
+
@media (max-width: 900px) {
|
|
5131
|
+
/* Device emulation and the allowlist badge are not useful in a narrow pane.
|
|
5132
|
+
The violation chip stays visible so blocked requests are still surfaced. */
|
|
5133
|
+
.codelab-preview-split .codelab-preview-modes,
|
|
5134
|
+
.codelab-preview-split #cl-preview-csp-badge {
|
|
5135
|
+
display: none;
|
|
5136
|
+
}
|
|
5137
|
+
|
|
5138
|
+
/* Keep the split, but give the editor a floor and cap the preview share. */
|
|
5139
|
+
.codelab-workspace.has-preview-split {
|
|
5140
|
+
grid-template-columns: 155px 4px minmax(180px, 1fr) 5px minmax(200px, min(42%, var(--cl-preview-split-width, 44%)));
|
|
5141
|
+
}
|
|
5142
|
+
.codelab.files-collapsed .codelab-workspace.has-preview-split {
|
|
5143
|
+
grid-template-columns: 0px 0px minmax(180px, 1fr) 5px minmax(200px, min(42%, var(--cl-preview-split-width, 44%)));
|
|
5144
|
+
}
|
|
5145
|
+
}
|
|
5146
|
+
|
|
5147
|
+
@media (max-width: 680px) {
|
|
5148
|
+
/* A five-column split cannot fit a phone: stack the preview under the editor
|
|
5149
|
+
instead of letting both shrink to nothing. The explorer and its splitter are
|
|
5150
|
+
hidden at this width, so only editor, splitter and preview are placed. */
|
|
5151
|
+
.codelab-workspace.has-preview-split {
|
|
5152
|
+
grid-template-columns: 1fr;
|
|
5153
|
+
grid-template-rows: minmax(140px, 1fr) 5px 45%;
|
|
5154
|
+
}
|
|
5155
|
+
.codelab-preview-split {
|
|
5156
|
+
border-left: 0;
|
|
5157
|
+
border-top: 1px solid var(--cl-border);
|
|
5158
|
+
}
|
|
5159
|
+
}
|
package/src/csp.ts
CHANGED
|
@@ -195,7 +195,12 @@ export function buildPreviewPolicy(options: PreviewCspOptions = {}): string {
|
|
|
195
195
|
|
|
196
196
|
const directive = (name: string, parts: string[]): string => `${name} ${unique(parts).join(' ')}`;
|
|
197
197
|
|
|
198
|
-
|
|
198
|
+
// `data:` is required for scripts: the preview import map serves every local
|
|
199
|
+
// module as `data:text/javascript;...` so it works in a sandboxed frame (see
|
|
200
|
+
// buildHtmlPreviewResponse in src/shell.ts). Without it, any project with
|
|
201
|
+
// local ES modules fails to boot. `'unsafe-inline'` already allows inline
|
|
202
|
+
// script, so this does not widen the practical attack surface.
|
|
203
|
+
const script = (): string[] => [self, 'data:', 'blob:', "'unsafe-inline'", "'unsafe-eval'"].concat(sourcesFor(mode, allow, 'scriptSrc'));
|
|
199
204
|
const style = (): string[] => [self, "'unsafe-inline'"].concat(sourcesFor(mode, allow, 'styleSrc'));
|
|
200
205
|
const withData = (key: keyof PreviewCspSources): string[] => [self, 'data:', 'blob:'].concat(sourcesFor(mode, allow, key));
|
|
201
206
|
|
package/src/index.ts
CHANGED
package/src/runtime.ts
CHANGED
|
@@ -4048,6 +4048,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4048
4048
|
if (/@jsx\b/.test(source)) return true;
|
|
4049
4049
|
if (source.indexOf('<>') !== -1) return true;
|
|
4050
4050
|
if (/<\/[A-Za-z]/.test(source)) return true;
|
|
4051
|
+
if (/<[A-Za-z_$][A-Za-z0-9_$:.\-]*\b[^>\n]*\/>/.test(source)) return true;
|
|
4051
4052
|
return false;
|
|
4052
4053
|
}
|
|
4053
4054
|
|
|
@@ -4125,6 +4126,23 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4125
4126
|
return i;
|
|
4126
4127
|
}
|
|
4127
4128
|
|
|
4129
|
+
function isJsxCommentOnly(str) {
|
|
4130
|
+
if (!str) return true;
|
|
4131
|
+
let k = 0;
|
|
4132
|
+
while (k < str.length) {
|
|
4133
|
+
k = jsxSkipWs(str, k);
|
|
4134
|
+
if (k >= str.length) break;
|
|
4135
|
+
if (str[k] === '/' && (str[k + 1] === '/' || str[k + 1] === '*')) {
|
|
4136
|
+
const next = jsxSkipJsLiteral(str, k);
|
|
4137
|
+
if (next === -1) return false;
|
|
4138
|
+
k = next;
|
|
4139
|
+
} else {
|
|
4140
|
+
return false;
|
|
4141
|
+
}
|
|
4142
|
+
}
|
|
4143
|
+
return true;
|
|
4144
|
+
}
|
|
4145
|
+
|
|
4128
4146
|
function normalizeJsxText(raw) {
|
|
4129
4147
|
if (!raw) return '';
|
|
4130
4148
|
let t = raw.replace(/\r/g, '');
|
|
@@ -4268,8 +4286,12 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4268
4286
|
}
|
|
4269
4287
|
if (c === '{') {
|
|
4270
4288
|
const br = jsxReadBraced(source, j, o);
|
|
4271
|
-
|
|
4272
|
-
|
|
4289
|
+
if (!isJsxCommentOnly(br.inner)) {
|
|
4290
|
+
const inner = jsxTransform(br.inner, o);
|
|
4291
|
+
if (inner && inner.trim()) {
|
|
4292
|
+
children.push(inner.indexOf('//') !== -1 && !inner.endsWith('\n') ? inner + '\n' : inner);
|
|
4293
|
+
}
|
|
4294
|
+
}
|
|
4273
4295
|
j = br.next;
|
|
4274
4296
|
continue;
|
|
4275
4297
|
}
|
|
@@ -4334,7 +4356,8 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4334
4356
|
// JSX/TSX must be rewritten before masking so JSX text is kept verbatim.
|
|
4335
4357
|
if (hasJsx) {
|
|
4336
4358
|
const { pragma, frag } = readJsxPragma(source);
|
|
4337
|
-
const
|
|
4359
|
+
const hasReactBinding = /\bimport\s+(?!type\b)(?:\*\s+as\s+React\b|React\b)|(?:\bconst|\blet|\bvar|\bfunction|\bclass)\s+React\b|\bReact\s*=/.test(source);
|
|
4360
|
+
const needsReact = pragma === 'React.createElement' && (keepEsm ? !hasReactBinding : !/\bReact\b/.test(source));
|
|
4338
4361
|
const transformed = jsxTransform(source, { pragma, frag });
|
|
4339
4362
|
if (transformed !== source) {
|
|
4340
4363
|
source = transformed;
|
|
@@ -4342,6 +4365,8 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4342
4365
|
if (needsReact) {
|
|
4343
4366
|
source = (keepEsm ? 'import React from "react";\n' : 'const React = require("react");\n') + source;
|
|
4344
4367
|
}
|
|
4368
|
+
} else if (!isTS && !hasESM) {
|
|
4369
|
+
return source;
|
|
4345
4370
|
}
|
|
4346
4371
|
}
|
|
4347
4372
|
|
package/src/schemas.ts
CHANGED
package/src/shell.ts
CHANGED
|
@@ -3019,11 +3019,48 @@
|
|
|
3019
3019
|
if (!pkgName) continue;
|
|
3020
3020
|
const subpath = spec.slice(pkgName.length).replace(/^\/+/, '');
|
|
3021
3021
|
const version = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3022
|
+
if (subpath && /\.css$/i.test(subpath)) {
|
|
3023
|
+
const cssHref = `${CDN_ESM_BASE}/${pkgName}@${version}/${subpath.replace(/^\/+/, '')}`;
|
|
3024
|
+
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)};`;
|
|
3025
|
+
imports[spec] = `data:text/javascript;charset=utf-8,${encodeURIComponent(loader)}`;
|
|
3026
|
+
continue;
|
|
3027
|
+
}
|
|
3022
3028
|
imports[spec] = cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3023
3029
|
}
|
|
3024
3030
|
return imports;
|
|
3025
3031
|
}
|
|
3026
3032
|
|
|
3033
|
+
// Some templates pin a CDN URL in the source instead of importing a bare
|
|
3034
|
+
// specifier, e.g. `import React from "https://cdn.jsdelivr.net/npm/react@19.3.1/+esm"`.
|
|
3035
|
+
// Those URL specifiers are resolved by the browser directly, so they bypass the
|
|
3036
|
+
// generated import map (and with it the project's declared version and the
|
|
3037
|
+
// multi-CDN fallback). Import maps can remap URL specifiers, so alias each
|
|
3038
|
+
// literal URL found in the project to the resolved bare-specifier URL.
|
|
3039
|
+
// The pinned version is kept when the project has no dependency to resolve.
|
|
3040
|
+
const PINNED_CDN_URL_RE = /https?:\/\/(?:cdn\.jsdelivr\.net\/npm|esm\.sh)\/((?:@[^\/'"`@\s]+\/)?[^\/'"`@\s]+)@([^\s'"`),;]+)/g;
|
|
3041
|
+
|
|
3042
|
+
function aliasPinnedCdnUrls(vfs, rootDir, text, importMap) {
|
|
3043
|
+
if (!text || typeof text !== 'string') return;
|
|
3044
|
+
const re = new RegExp(PINNED_CDN_URL_RE.source, 'g');
|
|
3045
|
+
let match;
|
|
3046
|
+
while ((match = re.exec(text)) !== null) {
|
|
3047
|
+
const url = match[0];
|
|
3048
|
+
if (url.includes('${')) continue;
|
|
3049
|
+
const pkgName = decodeURIComponent(match[1]);
|
|
3050
|
+
const rest = match[2];
|
|
3051
|
+
const pinnedVersion = rest.split(/[\/?]/)[0];
|
|
3052
|
+
const tail = rest.slice(pinnedVersion.length);
|
|
3053
|
+
const rawSubpath = tail.startsWith('/') && tail !== '/+esm' ? tail.slice(1).split(/[?#]/)[0] : '';
|
|
3054
|
+
const subpath = rawSubpath.replace(/^\/+|\/+$/g, '');
|
|
3055
|
+
const bare = subpath ? `${pkgName}/${subpath}` : pkgName;
|
|
3056
|
+
|
|
3057
|
+
const declared = resolveBareVersion(vfs, rootDir, pkgName);
|
|
3058
|
+
const version = declared && declared !== 'latest' ? declared : decodeURIComponent(pinnedVersion);
|
|
3059
|
+
const target = importMap[bare] || cdnEsmUrl(pkgName, version, subpath || undefined);
|
|
3060
|
+
if (target && target !== url) importMap[url] = target;
|
|
3061
|
+
}
|
|
3062
|
+
}
|
|
3063
|
+
|
|
3027
3064
|
function collectPreviewScriptFiles(vfs, rootDir, maxFiles = 200) {
|
|
3028
3065
|
const files = [];
|
|
3029
3066
|
const walk = (dir, depth) => {
|
|
@@ -3053,6 +3090,33 @@
|
|
|
3053
3090
|
return files;
|
|
3054
3091
|
}
|
|
3055
3092
|
|
|
3093
|
+
function collectPreviewAssetFiles(vfs, rootDir, maxFiles = 200) {
|
|
3094
|
+
const files = [];
|
|
3095
|
+
const walk = (dir, depth) => {
|
|
3096
|
+
if (depth > 8 || files.length >= maxFiles) return;
|
|
3097
|
+
let entries = [];
|
|
3098
|
+
try { entries = vfs.readdirSync(dir); } catch (_) { return; }
|
|
3099
|
+
for (const name of entries) {
|
|
3100
|
+
if (name === 'node_modules' || name.startsWith('.')) continue;
|
|
3101
|
+
const full = PosixPath.join(dir, name);
|
|
3102
|
+
let st;
|
|
3103
|
+
try { st = vfs.statSync(full); } catch (_) { continue; }
|
|
3104
|
+
const dirFlag = st && (typeof st.isDirectory === 'function' ? st.isDirectory() : st.isDirectory);
|
|
3105
|
+
if (dirFlag) {
|
|
3106
|
+
walk(full, depth + 1);
|
|
3107
|
+
} else if (/\.(?:css|json|svg)$/i.test(name) && files.length < maxFiles) {
|
|
3108
|
+
try {
|
|
3109
|
+
const content = vfs.readFileSync(full, 'utf8');
|
|
3110
|
+
const rel = full.startsWith(rootDir + '/') ? full.slice(rootDir.length + 1) : name;
|
|
3111
|
+
files.push({ full, rel, content });
|
|
3112
|
+
} catch (_) {}
|
|
3113
|
+
}
|
|
3114
|
+
}
|
|
3115
|
+
};
|
|
3116
|
+
walk(rootDir, 0);
|
|
3117
|
+
return files;
|
|
3118
|
+
}
|
|
3119
|
+
|
|
3056
3120
|
function collectPreviewScripts(vfs, rootDir, maxFiles = 200) {
|
|
3057
3121
|
return collectPreviewScriptFiles(vfs, rootDir, maxFiles).map((f) => f.content);
|
|
3058
3122
|
}
|
|
@@ -3135,11 +3199,31 @@
|
|
|
3135
3199
|
return out;
|
|
3136
3200
|
};
|
|
3137
3201
|
|
|
3202
|
+
const inlineLocalCssImports = (cssText, cssDir) => {
|
|
3203
|
+
if (!cssText) return '';
|
|
3204
|
+
return cssText.replace(/@import\s+(?:url\(\s*['"]?([^'")]+)['"]?\s*\)|['"]([^'"]+)['"])\s*;?/gi, (full, u1, u2) => {
|
|
3205
|
+
const spec = (u1 || u2 || '').trim();
|
|
3206
|
+
if (!spec || /^(?:https?:|data:|\/\/)/i.test(spec)) return full;
|
|
3207
|
+
if (spec === 'tailwindcss') return '';
|
|
3208
|
+
const subTarget = resolveLocalTarget(cssDir, spec);
|
|
3209
|
+
if (subTarget && /\.css$/i.test(subTarget)) {
|
|
3210
|
+
const subCss = readText(subTarget);
|
|
3211
|
+
if (subCss !== null) return subCss;
|
|
3212
|
+
}
|
|
3213
|
+
return '';
|
|
3214
|
+
});
|
|
3215
|
+
};
|
|
3216
|
+
|
|
3138
3217
|
const buildHtmlPreviewResponse = (htmlBody, htmlTargetPath) => {
|
|
3139
3218
|
const scriptFiles = collectPreviewScriptFiles(ctx.vfs, rootDir);
|
|
3219
|
+
const assetFiles = collectPreviewAssetFiles(ctx.vfs, rootDir);
|
|
3140
3220
|
const specifiers = new Set();
|
|
3221
|
+
const compiledScripts = [];
|
|
3141
3222
|
for (const f of scriptFiles) {
|
|
3142
3223
|
for (const s of extractBareSpecifiers(f.content)) specifiers.add(s);
|
|
3224
|
+
const compiled = transpileAndRewriteLocalImports(f.content, f.full);
|
|
3225
|
+
for (const s of extractBareSpecifiers(compiled)) specifiers.add(s);
|
|
3226
|
+
compiledScripts.push({ file: f, compiled });
|
|
3143
3227
|
}
|
|
3144
3228
|
// Also scan inline module scripts in HTML for bare specifiers
|
|
3145
3229
|
const inlineScriptRe = /<script\b[^>]*>([\s\S]*?)<\/script>/gi;
|
|
@@ -3151,14 +3235,55 @@
|
|
|
3151
3235
|
}
|
|
3152
3236
|
|
|
3153
3237
|
const importMap = buildPreviewImportMap(ctx.vfs, rootDir, specifiers);
|
|
3154
|
-
for (const f of scriptFiles)
|
|
3155
|
-
|
|
3156
|
-
|
|
3238
|
+
for (const f of scriptFiles) aliasPinnedCdnUrls(ctx.vfs, rootDir, f.content, importMap);
|
|
3239
|
+
aliasPinnedCdnUrls(ctx.vfs, rootDir, htmlBody, importMap);
|
|
3240
|
+
for (const { file, compiled } of compiledScripts) {
|
|
3241
|
+
const key = toLocalImportKey(file.full);
|
|
3157
3242
|
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(compiled)}`;
|
|
3158
3243
|
}
|
|
3159
3244
|
|
|
3245
|
+
let needsTailwindBrowser = false;
|
|
3246
|
+
for (const af of assetFiles) {
|
|
3247
|
+
const key = toLocalImportKey(af.full);
|
|
3248
|
+
if (/\.css$/i.test(af.full)) {
|
|
3249
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(af.content)) {
|
|
3250
|
+
needsTailwindBrowser = true;
|
|
3251
|
+
}
|
|
3252
|
+
const resolvedCss = inlineLocalCssImports(af.content, PosixPath.dirname(af.full));
|
|
3253
|
+
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;`;
|
|
3254
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(cssModuleJs)}`;
|
|
3255
|
+
} else if (/\.json$/i.test(af.full)) {
|
|
3256
|
+
const jsonModuleJs = `export default ${af.content.trim() || 'null'};`;
|
|
3257
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(jsonModuleJs)}`;
|
|
3258
|
+
} else if (/\.svg$/i.test(af.full)) {
|
|
3259
|
+
const svgDataUrl = `data:image/svg+xml;utf8,${encodeURIComponent(af.content)}`;
|
|
3260
|
+
const svgModuleJs = `export default ${JSON.stringify(svgDataUrl)};`;
|
|
3261
|
+
importMap[key] = `data:text/javascript;charset=utf-8,${encodeURIComponent(svgModuleJs)}`;
|
|
3262
|
+
}
|
|
3263
|
+
}
|
|
3264
|
+
|
|
3160
3265
|
const htmlDir = PosixPath.dirname(htmlTargetPath);
|
|
3161
|
-
let transformedHtml = htmlBody;
|
|
3266
|
+
let transformedHtml = htmlBody.replace(/%PUBLIC_URL%\//g, '/').replace(/%PUBLIC_URL%/g, '');
|
|
3267
|
+
|
|
3268
|
+
// Auto-inject entry script for Create React App / bundler templates where index.html omits <script src="...">
|
|
3269
|
+
const hasLocalScriptTag = /<script\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/i.test(transformedHtml);
|
|
3270
|
+
if (!hasLocalScriptTag) {
|
|
3271
|
+
const defaultEntries = [
|
|
3272
|
+
'/src/main.tsx', '/src/main.jsx', '/src/main.ts', '/src/main.js',
|
|
3273
|
+
'/src/index.tsx', '/src/index.jsx', '/src/index.ts', '/src/index.js'
|
|
3274
|
+
];
|
|
3275
|
+
for (const cand of defaultEntries) {
|
|
3276
|
+
if (isFile(PosixPath.join(rootDir, cand.slice(1)))) {
|
|
3277
|
+
const entryTag = `<script type="module" src="${cand}"></script>`;
|
|
3278
|
+
if (/<\/body>/i.test(transformedHtml)) {
|
|
3279
|
+
transformedHtml = transformedHtml.replace(/<\/body>/i, ` ${entryTag}\n</body>`);
|
|
3280
|
+
} else {
|
|
3281
|
+
transformedHtml += `\n${entryTag}`;
|
|
3282
|
+
}
|
|
3283
|
+
break;
|
|
3284
|
+
}
|
|
3285
|
+
}
|
|
3286
|
+
}
|
|
3162
3287
|
|
|
3163
3288
|
// Inline local <link rel="stylesheet" href="..."> so styles work in opaque-origin / srcdoc frames
|
|
3164
3289
|
transformedHtml = transformedHtml.replace(/<link\b([^>]*?)>/gi, (fullTag, attrs) => {
|
|
@@ -3169,7 +3294,11 @@
|
|
|
3169
3294
|
if (!cssTarget || !/\.css$/i.test(cssTarget)) return fullTag;
|
|
3170
3295
|
const cssContent = readText(cssTarget);
|
|
3171
3296
|
if (cssContent === null) return fullTag;
|
|
3172
|
-
|
|
3297
|
+
if (/@import\s+['"]tailwindcss['"]/i.test(cssContent)) {
|
|
3298
|
+
needsTailwindBrowser = true;
|
|
3299
|
+
}
|
|
3300
|
+
const resolvedCss = inlineLocalCssImports(cssContent, PosixPath.dirname(cssTarget));
|
|
3301
|
+
return `<style data-codelab-href="${hrefMatch[1].replace(/"/g, '"')}">\n${resolvedCss}\n</style>`;
|
|
3173
3302
|
});
|
|
3174
3303
|
|
|
3175
3304
|
// Inline local <script ... src="..."></script> so entry modules & scripts execute reliably in opaque-origin / srcdoc frames
|
|
@@ -3181,11 +3310,20 @@
|
|
|
3181
3310
|
const rawScript = readText(scriptTarget);
|
|
3182
3311
|
if (rawScript === null) return fullTag;
|
|
3183
3312
|
const compiled = transpileAndRewriteLocalImports(rawScript, scriptTarget);
|
|
3184
|
-
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget);
|
|
3313
|
+
const isMod = /\btype\s*=\s*["']module["']/i.test(attrs) || /\.(?:tsx|ts|jsx|mjs)$/i.test(scriptTarget) || /^\s*(?:import|export)\b/m.test(rawScript);
|
|
3185
3314
|
const typeAttr = isMod ? ' type="module"' : '';
|
|
3186
3315
|
return `<script${typeAttr} data-codelab-src="${srcMatch[1].replace(/"/g, '"')}">\n${compiled}\n</script>`;
|
|
3187
3316
|
});
|
|
3188
3317
|
|
|
3318
|
+
if (needsTailwindBrowser && !transformedHtml.includes('@tailwindcss/browser')) {
|
|
3319
|
+
const twTag = '<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>';
|
|
3320
|
+
if (/<head[^>]*>/i.test(transformedHtml)) {
|
|
3321
|
+
transformedHtml = transformedHtml.replace(/<head[^>]*>/i, (m) => `${m}\n ${twTag}`);
|
|
3322
|
+
} else {
|
|
3323
|
+
transformedHtml = twTag + '\n' + transformedHtml;
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
|
|
3189
3327
|
return injectPreviewExtras(transformedHtml, importMap);
|
|
3190
3328
|
};
|
|
3191
3329
|
|
|
@@ -3197,7 +3335,9 @@
|
|
|
3197
3335
|
const hasExt = /\.[A-Za-z0-9]+$/.test(rel);
|
|
3198
3336
|
|
|
3199
3337
|
if (!rel || pathname.endsWith('/') || isDir(target)) {
|
|
3200
|
-
|
|
3338
|
+
const rootIdx = PosixPath.join(target, 'index.html');
|
|
3339
|
+
const pubIdx = PosixPath.join(target, 'public', 'index.html');
|
|
3340
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx;
|
|
3201
3341
|
}
|
|
3202
3342
|
if (!exists(target) && !hasExt) {
|
|
3203
3343
|
// Resolve extensionless module specifiers (e.g. `./components/Counter`)
|
|
@@ -3211,7 +3351,9 @@
|
|
|
3211
3351
|
for (const c of cands) { if (exists(c)) { target = c; break; } }
|
|
3212
3352
|
}
|
|
3213
3353
|
if (!exists(target) && !hasExt) {
|
|
3214
|
-
|
|
3354
|
+
const rootIdx = PosixPath.join(rootDir, 'index.html');
|
|
3355
|
+
const pubIdx = PosixPath.join(rootDir, 'public', 'index.html');
|
|
3356
|
+
target = (!exists(rootIdx) && exists(pubIdx)) ? pubIdx : rootIdx; // SPA fallback
|
|
3215
3357
|
}
|
|
3216
3358
|
if (!exists(target)) {
|
|
3217
3359
|
return new Response(
|