Skip to content

Commit

Permalink
Fix playground usage from external package (#2540)
Browse files Browse the repository at this point in the history
Playground usage was completely broken when used as a package. Local
reference seems to make it work but hwne the package is in node_modules
vite doesn't allow certain things
  • Loading branch information
timotheeguerin authored Oct 5, 2023
1 parent e02b74a commit 99d80f8
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"**/dist-dev/**": true,
"**/.docusaurus/**": true,
"**/CHANGELOG.json": true,
"**/CHANGELOG.md": true,
"docs/spec.html": true,
"common/config/rush/pnpm-lock.yaml": true,
"**/node_modules/**": true,
"packages/website/versioned_docs/**": true,
"packages/samples/scratch/**": false // Those files are in gitignore but we still want to search for them
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/bundler",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/bundler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/playground",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/playground"
}
5 changes: 4 additions & 1 deletion common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/bundler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/bundler",
"version": "0.1.0-alpha.0",
"version": "0.1.0-alpha.1",
"author": "Microsoft Corporation",
"description": "Package to bundle a typespec library.",
"homepage": "https://microsoft.github.io/typespec",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground-website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"!dist/test/**"
],
"dependencies": {
"@typespec/playground": "workspace:~0.1.0-alpha.0",
"@typespec/playground": "workspace:~0.1.0-alpha.1",
"@typespec/versioning": "workspace:~0.48.0",
"@typespec/compiler": "workspace:~0.48.1",
"@typespec/http": "workspace:~0.48.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground-website/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlaygroundManifest } from "@typespec/playground/manifest";
import PlaygroundManifest from "@typespec/playground/manifest";
import { renderReactPlayground } from "@typespec/playground/react";
import { SwaggerUIViewer } from "@typespec/playground/react/viewers";
import "./style.css";
Expand Down
17 changes: 17 additions & 0 deletions packages/playground/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@typespec/openapi",
"entries": [
{
"version": "0.1.0-alpha.1",
"tag": "@typespec/playground_v0.1.0-alpha.1",
"date": "Wed, 03 Oct 2023 18:00:18 GMT",
"comments": {
"none": [
{
"comment": "Fix: Usage of the package in vite dev mode was broken. Some errors due to the loading of the manifest as well as monaco editor workers.\n**BREAKING CHANGE:** Changed `import { PlaygroundManifest } from \"@typespec/playground/manifest\";` to`import PlaygroundManifest from \"@typespec/playground/manifest\";`"
}
]
}
}
]
}
12 changes: 12 additions & 0 deletions packages/playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Change Log - @typespec/playground

This log was last generated on Wed, 04 Oct 2023 18:19:25 GMT and should not be manually modified.

## 0.1.0-alpha.1
Wed, 03 Oct 2023 18:00:18 GMT

### Updates

- Fix: Usage of the package in vite dev mode was broken. Some errors due to the loading of the manifest as well as monaco editor workers.
**BREAKING CHANGE:** Changed `import { PlaygroundManifest } from "@typespec/playground/manifest";` to`import PlaygroundManifest from "@typespec/playground/manifest";`

2 changes: 1 addition & 1 deletion packages/playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default config;
In `src/main.tsx`:

```tsx
import { PlaygroundManifest } from "@typespec/playground/manifest";
import PlaygroundManifest from "@typespec/playground/manifest";
import { renderReactPlayground } from "@typespec/playground/react";
import { SwaggerUIViewer } from "@typespec/playground/react/viewers";
import "./style.css";
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/playground",
"version": "0.1.0-alpha.0",
"version": "0.1.0-alpha.1",
"author": "Microsoft Corporation",
"description": "TypeSpec playground UI components.",
"homepage": "https://microsoft.github.io/typespec",
Expand Down
11 changes: 6 additions & 5 deletions packages/playground/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import manifest from "playground-manifest.js";
import { PlaygroundConfig } from "./vite/types.js";
/**
* THIS FILE IS MEANT TO BE STUBBED using the manifest plugin.
*/
import { PlaygroundConfig } from "../src/vite/types.js";

export const PlaygroundManifest: PlaygroundConfig = manifest;
const config: PlaygroundConfig = {} as any;
export default config;
39 changes: 27 additions & 12 deletions packages/playground/src/monaco-worker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";

export function registerMonacoDefaultWorkers() {
self.MonacoEnvironment = {
createTrustedTypesPolicy: () => undefined,
getWorker(_: any, label: string) {
if (label === "json") {
return new jsonWorker();
getWorker: function (workerId, label) {
const getWorkerModule = (moduleUrl: string, label: string) => {
return new Worker((self.MonacoEnvironment as any).getWorkerUrl(moduleUrl), {
name: label,
type: "module",
});
};

switch (label) {
case "json":
return getWorkerModule("/monaco-editor/esm/vs/language/json/json.worker?worker", label);
case "css":
case "scss":
case "less":
return getWorkerModule("/monaco-editor/esm/vs/language/css/css.worker?worker", label);
case "html":
case "handlebars":
case "razor":
return getWorkerModule("/monaco-editor/esm/vs/language/html/html.worker?worker", label);
case "typescript":
case "javascript":
return getWorkerModule(
"/monaco-editor/esm/vs/language/typescript/ts.worker?worker",
label
);
default:
return getWorkerModule("/monaco-editor/esm/vs/editor/editor.worker?worker", label);
}
return new editorWorker();
},
};
}
5 changes: 3 additions & 2 deletions packages/playground/src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ function playgroundManifestPlugin(config: PlaygroundUserConfig): Plugin {

return {
name: "playground-manifest",
enforce: "pre", // Need to run before resolving library imports to stub `@typespec/playground/manifest`
async configResolved(c) {
viteConfig = c;
},
resolveId(id: string) {
if (id === "playground-manifest.js") {
if (id === "@typespec/playground/manifest") {
return id;
}
return null;
},
load(id: string) {
if (id === `playground-manifest.js`) {
if (id === `@typespec/playground/manifest`) {
const sampleImport = Object.values(samples)
.map(
(sampleValue, index) =>
Expand Down
9 changes: 8 additions & 1 deletion packages/playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@
"lib": ["DOM"],
"types": ["@emotion/react"]
},
"include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "e2e/**/*.ts", "vite.config.ts"]
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"test/**/*.ts",
"e2e/**/*.ts",
"vite.config.ts",
"definitions/*.d.ts"
]
}
13 changes: 7 additions & 6 deletions packages/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,25 @@
"!dist/test/**"
],
"dependencies": {
"@typespec/versioning": "workspace:~0.48.0",
"@typespec/best-practices": "workspace:~0.45.0",
"@typespec/compiler": "workspace:~0.48.1",
"@typespec/html-program-viewer": "workspace:~0.48.0",
"@typespec/http": "workspace:~0.48.0",
"@typespec/rest": "workspace:~0.48.0",
"@typespec/json-schema": "workspace:~0.48.0",
"@typespec/openapi": "workspace:~0.48.0",
"@typespec/openapi3": "workspace:~0.48.1",
"@typespec/html-program-viewer": "workspace:~0.48.0"
"@typespec/rest": "workspace:~0.48.0",
"@typespec/versioning": "workspace:~0.48.0"
},
"devDependencies": {
"@typespec/internal-build-utils": "workspace:~0.48.0",
"@typespec/eslint-config-typespec": "workspace:~0.48.0",
"@types/mocha": "~10.0.1",
"@types/node": "~18.11.9",
"@typespec/eslint-config-typespec": "workspace:~0.48.0",
"@typespec/internal-build-utils": "workspace:~0.48.0",
"autorest": "~3.3.2",
"cross-env": "~7.0.3",
"eslint": "^8.49.0",
"mocha": "~10.2.0",
"autorest": "~3.3.2",
"rimraf": "~5.0.1",
"typescript": "~5.2.2"
}
Expand Down

0 comments on commit 99d80f8

Please sign in to comment.