Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Pin @babel/standalone so generated React actually renders

Abi Rajacd1e71c59fcc
diff --git a/backend/babel_cdn.py b/backend/babel_cdn.py
new file mode 100644
index 00000000..e1482305
--- /dev/null
+++ b/backend/babel_cdn.py
@@ -0,0 +1,27 @@
+"""Normalize the @babel/standalone CDN URL in generated HTML.
+
+Generated React pages load @babel/standalone unversioned, which now resolves to
+Babel 8 — whose preset-react defaults to the automatic JSX runtime and injects
+`import { jsx } from "react/jsx-runtime"`. That import is invalid in a plain
+browser <script> with React loaded as a UMD global, so JSX never compiles and
+React never mounts (blank preview / screenshot / export).
+
+Pinning to a Babel 7 release keeps the classic runtime (React.createElement),
+which works. We rewrite any @babel/standalone CDN reference to a pinned version
+wherever generated code is rendered or exported, so already-generated projects
+(which baked in the unversioned URL) keep working too.
+"""
+
+import re
+
+PINNED_BABEL_STANDALONE_URL = "https://unpkg.com/@babel/[email protected]/babel.min.js"
+
+_BABEL_CDN_RE = re.compile(
+ r"https?://(?:unpkg\.com|cdn\.jsdelivr\.net/npm)/@babel/standalone"
+ r"(?:@[0-9.]+)?/babel(?:\.min)?\.js"
+)
+
+
+def normalize_babel_cdn(html: str) -> str:
+ """Rewrite any @babel/standalone CDN URL to the pinned, working version."""
+ return _BABEL_CDN_RE.sub(PINNED_BABEL_STANDALONE_URL, html)
diff --git a/backend/preview_screenshot/registry.py b/backend/preview_screenshot/registry.py
index 09d08325..10471706 100644
--- a/backend/preview_screenshot/registry.py
+++ b/backend/preview_screenshot/registry.py
@@ -1,5 +1,6 @@
from typing import Optional
+from babel_cdn import normalize_babel_cdn
from preview_screenshot.base import ScreenshotBackend
from preview_screenshot.playwright_backend import PlaywrightBackend
@@ -44,6 +45,7 @@ async def capture_preview_screenshot(
"""Render HTML to PNG via the active backend.
The public entry point the screenshot_preview tool calls; the backend choice
- is invisible to callers.
+ is invisible to callers. Normalizes the Babel CDN first so generated React
+ pages (old and new) actually mount before we capture.
"""
- return await _backend.capture(html, device, full_page)
+ return await _backend.capture(normalize_babel_cdn(html), device, full_page)
diff --git a/backend/prompts/system_prompt.py b/backend/prompts/system_prompt.py
index 17b48405..29627a64 100644
--- a/backend/prompts/system_prompt.py
+++ b/backend/prompts/system_prompt.py
@@ -45,8 +45,8 @@ You are a coding agent that's an expert at building front-ends.
- Use these script to include React so that it can run on a standalone page:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react.development.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.development.js"></script>
- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
-- For babel, make sure to use https://unpkg.com/@babel/standalone/babel.min.js. DO NOT USE https://cdn.babeljs.io/babel.min.js as it is not the correct version and will cause errors.
+ <script src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script>
+- For babel, make sure to use https://unpkg.com/@babel/[email protected]/babel.min.js (pin this exact version — the unversioned URL now resolves to Babel 8, whose automatic JSX runtime injects an `import` that breaks in-browser transforms). DO NOT USE https://cdn.babeljs.io/babel.min.js as it is not the correct version and will cause errors.
- Use this script to include Tailwind: <script src="https://cdn.tailwindcss.com"></script>
## Ionic
diff --git a/backend/routes/export.py b/backend/routes/export.py
index 8399d221..442ec99c 100644
--- a/backend/routes/export.py
+++ b/backend/routes/export.py
@@ -16,6 +16,8 @@ from fastapi import APIRouter
from fastapi.responses import Response
from pydantic import BaseModel
+from babel_cdn import normalize_babel_cdn
+
router = APIRouter()
IMAGE_ATTRIBUTE_SELECTORS = [
@@ -449,7 +451,7 @@ def create_project_zip(index_html: str, assets: Iterable[ExportedAsset]) -> byte
@router.post("/api/export")
async def export_code(request: ExportRequest) -> Response:
- soup = BeautifulSoup(request.code, "html.parser")
+ soup = BeautifulSoup(normalize_babel_cdn(request.code), "html.parser")
candidates = collect_asset_candidates(soup)
async with httpx.AsyncClient(
diff --git a/backend/tests/test_babel_cdn.py b/backend/tests/test_babel_cdn.py
new file mode 100644
index 00000000..66f540eb
--- /dev/null
+++ b/backend/tests/test_babel_cdn.py
@@ -0,0 +1,31 @@
+from babel_cdn import PINNED_BABEL_STANDALONE_URL, normalize_babel_cdn
+
+
+def test_rewrites_unversioned_unpkg() -> None:
+ html = '<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>'
+ out = normalize_babel_cdn(html)
+ assert PINNED_BABEL_STANDALONE_URL in out
+ assert "@babel/standalone/babel.min.js" not in out # unversioned form gone
+
+
+def test_rewrites_versioned_and_jsdelivr_and_nonmin() -> None:
+ for url in [
+ "https://unpkg.com/@babel/[email protected]/babel.min.js",
+ "https://unpkg.com/@babel/[email protected]/babel.js",
+ "https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js",
+ ]:
+ out = normalize_babel_cdn(f'<script src="{url}"></script>')
+ assert out == f'<script src="{PINNED_BABEL_STANDALONE_URL}"></script>'
+
+
+def test_leaves_other_cdns_untouched() -> None:
+ html = (
+ '<script src="https://cdn.tailwindcss.com"></script>'
+ '<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react.development.js"></script>'
+ )
+ assert normalize_babel_cdn(html) == html
+
+
+def test_idempotent_on_pinned_url() -> None:
+ html = f'<script src="{PINNED_BABEL_STANDALONE_URL}"></script>'
+ assert normalize_babel_cdn(html) == html
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsx
index 04eb5b75..158ca2d1 100644
--- a/frontend/src/components/preview/PreviewComponent.tsx
+++ b/frontend/src/components/preview/PreviewComponent.tsx
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
import classNames from "classnames";
import useThrottle from "../../hooks/useThrottle";
import { useAppStore } from "../../store/app-store";
+import { normalizeBabelCdn } from "../../lib/babelCdn";
import {
applySelectModeCursor,
hideHoverOverlay,
@@ -270,8 +271,10 @@ function PreviewComponent({
useEffect(() => {
const iframe = iframeRef.current;
if (!iframe) return;
- if (iframe.srcdoc !== throttledCode) {
- iframe.srcdoc = throttledCode;
+ // Normalize the Babel CDN so generated React pages (old and new) mount.
+ const html = normalizeBabelCdn(throttledCode);
+ if (iframe.srcdoc !== html) {
+ iframe.srcdoc = html;
}
}, [throttledCode]);
diff --git a/frontend/src/components/preview/download.ts b/frontend/src/components/preview/download.ts
index 3d87edd9..3b9235eb 100644
--- a/frontend/src/components/preview/download.ts
+++ b/frontend/src/components/preview/download.ts
@@ -1,4 +1,5 @@
import { HTTP_BACKEND_URL } from "../../config";
+import { normalizeBabelCdn } from "../../lib/babelCdn";
function downloadBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
@@ -40,6 +41,9 @@ export const downloadCode = async (code: string) => {
);
} catch (error) {
console.warn("Falling back to downloading index.html", error);
- downloadBlob(new Blob([code], { type: "text/html" }), "index.html");
+ downloadBlob(
+ new Blob([normalizeBabelCdn(code)], { type: "text/html" }),
+ "index.html"
+ );
}
};
diff --git a/frontend/src/lib/babelCdn.ts b/frontend/src/lib/babelCdn.ts
new file mode 100644
index 00000000..f800070b
--- /dev/null
+++ b/frontend/src/lib/babelCdn.ts
@@ -0,0 +1,16 @@
+// See backend/babel_cdn.py for the full rationale. Generated React pages load
+// @babel/standalone unversioned, which now resolves to Babel 8 — whose
+// automatic JSX runtime injects an `import` that breaks in-browser transforms,
+// so React never mounts. Pinning to a Babel 7 release keeps the classic runtime
+// (React.createElement). We rewrite the CDN URL wherever generated code is
+// rendered or exported, so already-generated projects keep working too.
+
+const PINNED_BABEL_STANDALONE_URL =
+ "https://unpkg.com/@babel/[email protected]/babel.min.js";
+
+const BABEL_CDN_RE =
+ /https?:\/\/(?:unpkg\.com|cdn\.jsdelivr\.net\/npm)\/@babel\/standalone(?:@[0-9.]+)?\/babel(?:\.min)?\.js/g;
+
+export function normalizeBabelCdn(html: string): string {
+ return html.replace(BABEL_CDN_RE, PINNED_BABEL_STANDALONE_URL);
+}