|
1 | | -import fs from "node:fs"; |
2 | | -import path from "node:path"; |
3 | | -import { fileURLToPath } from "node:url"; |
4 | | - |
5 | 1 | import { introspectRouter } from "./introspect"; |
| 2 | +import { UI_CSS, UI_JS } from "./ui-assets"; |
6 | 3 |
|
7 | 4 | import type { JsonSchema } from "@trpc-studio/core"; |
8 | 5 |
|
@@ -30,51 +27,6 @@ function safeInlineJson(json: string): string { |
30 | 27 | return json.replace(/<\//g, "<\\/"); |
31 | 28 | } |
32 | 29 |
|
33 | | -// Load UI assets — try multiple resolution strategies |
34 | | -let cachedJs: string | null = null; |
35 | | -let cachedCss: string | null = null; |
36 | | -let assetsLoaded = false; |
37 | | - |
38 | | -function loadAssets(): void { |
39 | | - if (assetsLoaded) return; |
40 | | - assetsLoaded = true; |
41 | | - |
42 | | - const tryPaths: string[] = []; |
43 | | - |
44 | | - // Strategy 1: __dirname (CJS, standard Node.js) |
45 | | - if (typeof __dirname !== "undefined") { |
46 | | - tryPaths.push(path.join(__dirname, "ui", "assets")); |
47 | | - } |
48 | | - |
49 | | - // Strategy 2: import.meta.url (ESM) |
50 | | - try { |
51 | | - const esmDir = path.dirname(fileURLToPath(import.meta.url)); |
52 | | - tryPaths.push(path.join(esmDir, "ui", "assets")); |
53 | | - } catch { |
54 | | - // import.meta.url not available |
55 | | - } |
56 | | - |
57 | | - // Strategy 3: resolve from the package itself |
58 | | - try { |
59 | | - const pkgDir = path.dirname(require.resolve("@srawad/trpc-studio/package.json")); |
60 | | - tryPaths.push(path.join(pkgDir, "dist", "ui", "assets")); |
61 | | - } catch { |
62 | | - // package not resolvable this way |
63 | | - } |
64 | | - |
65 | | - for (const dir of tryPaths) { |
66 | | - try { |
67 | | - const js = fs.readFileSync(path.join(dir, "index.js"), "utf-8"); |
68 | | - const css = fs.readFileSync(path.join(dir, "index.css"), "utf-8"); |
69 | | - cachedJs = js; |
70 | | - cachedCss = css; |
71 | | - return; |
72 | | - } catch { |
73 | | - // try next path |
74 | | - } |
75 | | - } |
76 | | -} |
77 | | - |
78 | 30 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
79 | 31 | export function renderTrpcStudio(router: any, options: RenderOptions): string { |
80 | 32 | const manifest = introspectRouter(router); |
@@ -108,27 +60,24 @@ export function renderTrpcStudio(router: any, options: RenderOptions): string { |
108 | 60 | const safeOptions = safeInlineJson(optionsJson); |
109 | 61 | const scriptTag = `<script>window.__TRPC_STUDIO_MANIFEST__=${safeManifest};window.__TRPC_STUDIO_OPTIONS__=${safeOptions};</script>`; |
110 | 62 |
|
111 | | - // Load and cache UI assets |
112 | | - loadAssets(); |
113 | | - |
114 | | - if (cachedJs) { |
| 63 | + if (UI_JS) { |
115 | 64 | return `<!DOCTYPE html> |
116 | 65 | <html lang="en"> |
117 | 66 | <head> |
118 | 67 | <meta charset="UTF-8" /> |
119 | 68 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
120 | 69 | <title>${title}</title> |
121 | | - ${cachedCss ? `<style>${cachedCss}</style>` : ""} |
| 70 | + ${UI_CSS ? `<style>${UI_CSS}</style>` : ""} |
122 | 71 | ${scriptTag} |
123 | 72 | </head> |
124 | 73 | <body> |
125 | 74 | <div id="root"></div> |
126 | | - <script type="module">${cachedJs}</script> |
| 75 | + <script type="module">${UI_JS}</script> |
127 | 76 | </body> |
128 | 77 | </html>`; |
129 | 78 | } |
130 | 79 |
|
131 | | - // Fallback if UI assets not found |
| 80 | + // Fallback if UI assets were not embedded |
132 | 81 | return `<!DOCTYPE html> |
133 | 82 | <html lang="en"> |
134 | 83 | <head> |
|
0 commit comments