(
userSource: string,
opts: BuildPreviewDocumentOptions = {},
)
| 593 | } |
| 594 | |
| 595 | export function buildPreviewDocument( |
| 596 | userSource: string, |
| 597 | opts: BuildPreviewDocumentOptions = {}, |
| 598 | ): string { |
| 599 | const stripped = removeCspMetaTags(userSource); |
| 600 | // Already-wrapped srcdoc (round-trip safe). When the workspace preview path |
| 601 | // supplies a base URL, inject it once so relative assets still resolve. |
| 602 | if (stripped.includes(JSX_TEMPLATE_BEGIN)) { |
| 603 | return injectBaseHrefIntoHtmlDocument(stripped, opts.baseHref); |
| 604 | } |
| 605 | |
| 606 | const classified = classifyRenderableSource(stripped, opts.path); |
| 607 | const kind = classified === 'unknown' && opts.path === undefined ? 'jsx' : classified; |
| 608 | if (kind === 'unknown') { |
| 609 | throw new Error(`Unsupported preview file type: ${opts.path ?? 'unknown'}`); |
| 610 | } |
| 611 | |
| 612 | if (kind === 'html') { |
| 613 | const withRuntime = needsJsxRuntimeInHtml(stripped) |
| 614 | ? injectJsxRuntimeIntoHtml(stripped) |
| 615 | : stripped; |
| 616 | return injectOverlayIntoHtmlDocument( |
| 617 | injectPreviewViewportSupportIntoHtmlDocument( |
| 618 | injectBaseHrefIntoHtmlDocument(withRuntime, opts.baseHref), |
| 619 | ), |
| 620 | ); |
| 621 | } |
| 622 | |
| 623 | return wrapJsxAsSrcdoc(stripped, { kind, baseHref: opts.baseHref }); |
| 624 | } |
| 625 | |
| 626 | function ensureStandaloneShell(html: string): string { |
| 627 | const trimmed = html.trim(); |
no test coverage detected