Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions website/src/components/playground-component/import-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,41 @@ export interface ImportMap {
imports: Record<string, string>;
}

const pkgsBaseUrl = "https://typespec.blob.core.windows.net/pkgs";

async function fetchAdditionalPackageImports(
packageNames: readonly string[],
): Promise<Record<string, string>> {
const imports: Record<string, string> = {};

const results = await Promise.allSettled(
packageNames.map(async (name) => {
const url = `${pkgsBaseUrl}/${name}/latest.json`;
const response = await fetch(url);
if (!response.ok) {
// eslint-disable-next-line no-console
console.warn(`Failed to load latest index for ${name}: ${response.status}`);
return undefined;
}
return JSON.parse(await response.text()) as ImportMap;
}),
);

for (const result of results) {
if (result.status === "fulfilled" && result.value) {
Object.assign(imports, result.value.imports);
}
}

return imports;
}

export async function loadImportMap({
latestVersion,
additionalPackages = [],
}: {
latestVersion: string;
additionalPackages?: readonly string[];
}): Promise<VersionData> {
const optionsScript = document.querySelector("script[type=playground-options]");
if (optionsScript === undefined) {
Expand All @@ -24,15 +55,19 @@ export async function loadImportMap({
const parsed = new URLSearchParams(window.location.search);

const requestedVersion = parsed.get("version");
const importMapUrl = `https://typespec.blob.core.windows.net/pkgs/indexes/azure/${
requestedVersion ?? latestVersion
}.json`;
const importMapUrl = `${pkgsBaseUrl}/indexes/azure/${requestedVersion ?? latestVersion}.json`;

const response = await fetch(importMapUrl);
const content = await response.text();
const [mainResponse, additionalImports] = await Promise.all([
fetch(importMapUrl),
fetchAdditionalPackageImports(additionalPackages),
]);
const content = await mainResponse.text();

const importMap = JSON.parse(content);

// Merge additional imports into the main import map
Object.assign(importMap.imports, additionalImports);

(window as any).importShim.addImportMap(importMap);

return {
Expand Down
7 changes: 6 additions & 1 deletion website/src/components/react-pages/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "@typespec/playground/styles.css";
import { useEffect, useState, type ReactNode } from "react";
import { loadImportMap, type VersionData } from "../playground-component/import-map";

const additionalPlaygroundPackages = ["@typespec/http-client-python"];

export const AsyncPlayground = ({
latestVersion,
fallback,
Expand All @@ -15,7 +17,10 @@ export const AsyncPlayground = ({
WebsitePlayground: typeof import("../playground-component/playground").WebsitePlayground;
}>(undefined as any);
useEffect(() => {
Promise.all([loadImportMap({ latestVersion }), import("../playground-component/playground")])
Promise.all([
loadImportMap({ latestVersion, additionalPackages: additionalPlaygroundPackages }),
import("../playground-component/playground"),
])
.then((x) => setMod({ versionData: x[0] as any, WebsitePlayground: x[1].WebsitePlayground }))
.catch((e) => {
throw e;
Expand Down
Loading