Skip to content

Commit b8dc20f

Browse files
Playground directly into the website (microsoft#2638)
Stop using an iframe and load the latest version(not next)
1 parent 8697f61 commit b8dc20f

33 files changed

+480
-157
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"editor.detectIndentation": false,
2828
"editor.defaultFormatter": "esbenp.prettier-vscode"
2929
},
30-
"[json][[jsonc][yaml][typespec][markdown]": {
30+
"[json][[jsonc][yaml][typespec][markdown][css]": {
3131
"editor.formatOnSave": true,
3232
"editor.formatOnPaste": true,
3333
"editor.insertSpaces": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@typespec/playground",
5+
"comment": "Enable changing the theme",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@typespec/playground"
10+
}

common/config/rush/pnpm-lock.yaml

+32-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/playground-website/package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
"typespec"
1919
],
2020
"type": "module",
21-
"main": "dist/src/lib.js",
21+
"exports": {
22+
"./samples": {
23+
"types": "./samples/dist/samples.d.ts",
24+
"default": "./samples/dist/samples.js"
25+
}
26+
},
2227
"engines": {
2328
"node": ">=18.0.0"
2429
},
2530
"scripts": {
26-
"clean": "rimraf ./dist ./dist-dev ./temp ./typespecContents.json",
27-
"build": "tsc -p . && vite build 2>&1",
31+
"clean": "rimraf ./dist ./dist-dev ./temp ./samples/dist",
32+
"build-samples": "node ./samples/build.js",
33+
"build": "npm run build-samples && tsc -p . && vite build 2>&1",
2834
"preview": "npm run build && vite preview",
2935
"start": "vite",
3036
"watch": "vite",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @ts-check
2+
import { buildSamples_experimental } from "@typespec/playground/tooling";
3+
import { dirname, resolve } from "path";
4+
import { fileURLToPath } from "url";
5+
6+
const __dirname = dirname(fileURLToPath(import.meta.url));
7+
const packageRoot = resolve(__dirname, "..");
8+
9+
await buildSamples_experimental(packageRoot, resolve(__dirname, "dist/samples.js"), {
10+
"API versioning": {
11+
filename: "samples/versioning.tsp",
12+
preferredEmitter: "@typespec/openapi3",
13+
},
14+
"Discriminated unions": {
15+
filename: "samples/unions.tsp",
16+
preferredEmitter: "@typespec/openapi3",
17+
},
18+
"HTTP service": {
19+
filename: "samples/http.tsp",
20+
preferredEmitter: "@typespec/openapi3",
21+
compilerOptions: { linterRuleSet: { extends: ["@typespec/http/all"] } },
22+
},
23+
"REST framework": {
24+
filename: "samples/rest.tsp",
25+
preferredEmitter: "@typespec/openapi3",
26+
compilerOptions: { linterRuleSet: { extends: ["@typespec/http/all"] } },
27+
},
28+
"Protobuf Kiosk": {
29+
filename: "samples/kiosk.tsp",
30+
preferredEmitter: "@typespec/protobuf",
31+
},
32+
"Json Schema": {
33+
filename: "samples/json-schema.tsp",
34+
preferredEmitter: "@typespec/json-schema",
35+
},
36+
});

packages/playground-website/src/main.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import PlaygroundManifest from "@typespec/playground/manifest";
22
import { renderReactPlayground } from "@typespec/playground/react";
33
import { SwaggerUIViewer } from "@typespec/playground/react/viewers";
4+
import samples from "../samples/dist/samples.js";
5+
46
import "@typespec/playground/style.css";
57
import "./style.css";
68

79
await renderReactPlayground({
810
...PlaygroundManifest,
11+
samples,
912
emitterViewers: {
1013
"@typespec/openapi3": [SwaggerUIViewer],
1114
},

packages/playground-website/tsconfig.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@
1212
"skipLibCheck": true,
1313
"tsBuildInfoFile": "temp/tsconfig.tsbuildinfo",
1414
"jsx": "react-jsx",
15+
"checkJs": true,
16+
"allowJs": true,
1517
"jsxImportSource": "@emotion/react",
1618
"lib": ["DOM", "ES2022"],
1719
"types": ["@emotion/react"]
1820
},
19-
"include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "e2e/**/*.ts", "vite.config.ts"]
21+
"include": [
22+
"src/**/*.ts",
23+
"src/**/*.tsx",
24+
"test/**/*.ts",
25+
"e2e/**/*.ts",
26+
"vite.config.ts",
27+
"samples/**/*.js",
28+
"samples/**/*.d.ts"
29+
]
2030
}

packages/playground-website/vite.config.ts

-28
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,6 @@ export default defineConfig(({ mode }) => {
1717
"@typespec/json-schema",
1818
"@typespec/protobuf",
1919
],
20-
samples: {
21-
"API versioning": {
22-
filename: "samples/versioning.tsp",
23-
preferredEmitter: "@typespec/openapi3",
24-
},
25-
"Discriminated unions": {
26-
filename: "samples/unions.tsp",
27-
preferredEmitter: "@typespec/openapi3",
28-
},
29-
"HTTP service": {
30-
filename: "samples/http.tsp",
31-
preferredEmitter: "@typespec/openapi3",
32-
compilerOptions: { linterRuleSet: { extends: ["@typespec/http/all"] } },
33-
},
34-
"REST framework": {
35-
filename: "samples/rest.tsp",
36-
preferredEmitter: "@typespec/openapi3",
37-
compilerOptions: { linterRuleSet: { extends: ["@typespec/http/all"] } },
38-
},
39-
"Protobuf Kiosk": {
40-
filename: "samples/kiosk.tsp",
41-
preferredEmitter: "@typespec/protobuf",
42-
},
43-
"Json Schema": {
44-
filename: "samples/json-schema.tsp",
45-
preferredEmitter: "@typespec/json-schema",
46-
},
47-
},
4820
enableSwaggerUI: true,
4921
links: {
5022
githubIssueUrl: `https://github.com/microsoft/typespec/issues/new`,

packages/playground/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"types": "./dist/src/vite/index.d.ts",
2828
"default": "./dist/vite/index.js"
2929
},
30+
"./tooling": {
31+
"types": "./dist/src/tooling/index.d.ts",
32+
"default": "./dist/tooling/index.js"
33+
},
3034
"./manifest": {
3135
"types": "./dist/src/manifest.d.ts",
3236
"default": "./dist/manifest.js"

packages/playground/rollup.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ const external = [
1818
"react-dom/client",
1919
"vite",
2020
"@vitejs/plugin-react",
21+
"fs/promises",
2122
];
2223
export default defineConfig([
2324
{
2425
input: {
2526
index: "src/index.ts",
2627
"react/index": "src/react/index.ts",
2728
"react/viewers": "src/react/viewers.tsx",
29+
"tooling/index": "src/tooling/index.ts",
2830
"vite/index": "src/vite/index.ts",
2931
},
3032
treeshake: false,

packages/playground/src/core.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export async function importTypeSpecCompiler(
2020
* @returns Promise with the loaded module.
2121
*/
2222
export async function importLibrary(name: string, config: LibraryImportOptions): Promise<unknown> {
23-
return config.useShim ? importShim(name) : import(/* @vite-ignore */ name);
23+
return config.useShim
24+
? importShim(name)
25+
: import(/* @vite-ignore */ /* webpackIgnore: true */ name);
2426
}
2527

2628
/**

packages/playground/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { createBrowserHost } from "./browser-host.js";
22
export { registerMonacoDefaultWorkers } from "./monaco-worker.js";
33
export { registerMonacoLanguage } from "./services.js";
4-
export { createUrlStateStorage } from "./state-storage.js";
5-
export type { PlaygroundSample } from "./types.js";
4+
export { StateStorage, createUrlStateStorage } from "./state-storage.js";
5+
export type { BrowserHost, PlaygroundSample } from "./types.js";

packages/playground/src/react/diagnostic-list.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css } from "@emotion/react";
2+
import { tokens } from "@fluentui/react-components";
23
import type { Diagnostic } from "@typespec/compiler";
34
import { FunctionComponent } from "react";
45

@@ -31,11 +32,11 @@ export const DiagnosticItem: FunctionComponent<DiagnosticItemProps> = ({ diagnos
3132
>
3233
{diagnostic.severity}
3334
</div>
34-
<div css={{ padding: "0 5px", color: "#333333" }}>{diagnostic.code}</div>
35+
<div css={{ padding: "0 5px", color: tokens.colorNeutralForeground2 }}>{diagnostic.code}</div>
3536
<div css={{ padding: "0 5px" }}>{diagnostic.message}</div>
3637
</div>
3738
);
3839
};
3940

40-
const errorColor = css({ color: "#cc2222" });
41-
const warningColor = css({ color: "orange" });
41+
const errorColor = css({ color: tokens.colorStatusDangerForeground1 });
42+
const warningColor = css({ color: tokens.colorStatusWarningForeground1 });

packages/playground/src/react/editor-command-bar.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Toolbar,
88
ToolbarButton,
99
Tooltip,
10+
tokens,
1011
} from "@fluentui/react-components";
1112
import {
1213
Broom16Filled,
@@ -61,7 +62,7 @@ export const EditorCommandBar: FunctionComponent<EditorCommandBarProps> = ({
6162
const bugButton = newIssue ? <FileBugButton onClick={newIssue} /> : undefined;
6263

6364
return (
64-
<div css={{ borderBottom: "1px solid #f5f5f5" }}>
65+
<div css={{ borderBottom: `1px solid ${tokens.colorNeutralStroke1}` }}>
6566
<Toolbar>
6667
<Tooltip content="Save" relationship="description" withArrow>
6768
<ToolbarButton aria-label="Save" icon={<Save16Regular />} onClick={saveCode as any} />

packages/playground/src/react/editor.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const Editor: FunctionComponent<EditorProps> = ({ model, options, actions
3030
onMount?.({ editor: editorRef.current });
3131
}, []);
3232

33+
useEffect(() => {
34+
editor.setTheme(options.theme ?? "vs");
35+
}, [options.theme]);
36+
3337
useEffect(() => {
3438
const disposables: IDisposable[] = [];
3539
for (const command of actions ?? []) {

packages/playground/src/react/error-tab.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css } from "@emotion/react";
2+
import { tokens } from "@fluentui/react-components";
23
import type { Diagnostic } from "@typespec/compiler";
34
import { FunctionComponent } from "react";
45
import { DiagnosticList } from "./diagnostic-list.js";
@@ -28,7 +29,7 @@ export const InternalCompilerError: FunctionComponent<InternalCompilerErrorProps
2829
<div css={{ CenterStyles }}>
2930
<div
3031
css={{
31-
border: "1px solid #cc2222",
32+
border: `1px solid ${tokens.colorStatusDangerBorder1}`,
3233
padding: "10px",
3334
margin: "20px",
3435
}}

packages/playground/src/react/footer.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { css } from "@emotion/react";
2-
import { MANIFEST } from "@typespec/compiler";
32
import { FunctionComponent, PropsWithChildren } from "react";
3+
import { BrowserHost } from "../types.js";
44

5-
export const Footer: FunctionComponent = () => {
5+
export interface FooterProps {
6+
host: BrowserHost;
7+
}
8+
export const Footer: FunctionComponent<FooterProps> = ({ host }) => {
9+
const { MANIFEST } = host.compiler;
610
const prItem = MANIFEST.pr ? (
711
<FooterItem link={`https://github.com/microsoft/typespec/pull/${MANIFEST.pr}`}>
812
<span>PR </span>
+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export { Playground } from "./playground.js";
2-
export type { PlaygroundProps } from "./playground.js";
3-
export { createReactPlayground, renderReactPlayground } from "./standalone.js";
2+
export type { PlaygroundProps, PlaygroundSaveData } from "./playground.js";
3+
export {
4+
StandalonePlayground,
5+
createReactPlayground,
6+
renderReactPlayground,
7+
} from "./standalone.js";
48
export * from "./types.js";

0 commit comments

Comments
 (0)