| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 1 | import { hmrPlugin, presets } from "vite-plugin-web-components-hmr"; |
| 2 | import { defineConfig } from "vite"; |
| Sean McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 3 | import tailwindcss from "@tailwindcss/vite"; |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 4 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 5 | export default defineConfig({ |
| Philip Zeyliger | 3cde282 | 2025-06-21 09:32:38 -0700 | [diff] [blame] | 6 | // Define build-time constants for compatibility with production build |
| 7 | define: { |
| 8 | __MONACO_HASH__: JSON.stringify("dev"), // Use 'dev' as hash in development |
| 9 | __MERMAID_HASH__: JSON.stringify("dev"), // Use 'dev' as hash in development |
| 10 | }, |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 11 | plugins: [ |
| Sean McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 12 | tailwindcss(), |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 13 | hmrPlugin({ |
| 14 | include: ["./src/**/*.ts"], |
| 15 | presets: [presets.lit], |
| 16 | }), |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 17 | // Custom plugin for handling the root path redirect and Monaco workers |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 18 | { |
| 19 | name: "configure-server", |
| 20 | configureServer(server) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 21 | // Handle root redirects and Monaco worker proxying |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 22 | server.middlewares.use((req, res, next) => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 23 | // Root redirect |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 24 | if (req.url === "/") { |
| 25 | res.writeHead(302, { |
| 26 | Location: "/src/web-components/demo/index.html", |
| 27 | }); |
| 28 | res.end(); |
| 29 | return; |
| 30 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 31 | |
| 32 | // Monaco worker and asset mapping |
| 33 | // This ensures the development paths match the production paths |
| 34 | |
| 35 | // Handle worker URLs |
| 36 | if (req.url?.startsWith("/static/editor.worker.js")) { |
| 37 | const workerPath = |
| 38 | "/node_modules/monaco-editor/esm/vs/editor/editor.worker.js"; |
| 39 | res.writeHead(302, { Location: workerPath }); |
| 40 | res.end(); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (req.url?.startsWith("/static/json.worker.js")) { |
| 45 | const workerPath = |
| 46 | "/node_modules/monaco-editor/esm/vs/language/json/json.worker.js"; |
| 47 | res.writeHead(302, { Location: workerPath }); |
| 48 | res.end(); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (req.url?.startsWith("/static/css.worker.js")) { |
| 53 | const workerPath = |
| 54 | "/node_modules/monaco-editor/esm/vs/language/css/css.worker.js"; |
| 55 | res.writeHead(302, { Location: workerPath }); |
| 56 | res.end(); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if (req.url?.startsWith("/static/html.worker.js")) { |
| 61 | const workerPath = |
| 62 | "/node_modules/monaco-editor/esm/vs/language/html/html.worker.js"; |
| 63 | res.writeHead(302, { Location: workerPath }); |
| 64 | res.end(); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (req.url?.startsWith("/static/ts.worker.js")) { |
| 69 | const workerPath = |
| 70 | "/node_modules/monaco-editor/esm/vs/language/typescript/ts.worker.js"; |
| 71 | res.writeHead(302, { Location: workerPath }); |
| 72 | res.end(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Handle CSS and font files |
| 77 | if ( |
| 78 | req.url?.startsWith("/static/monaco/min/vs/editor/editor.main.css") |
| 79 | ) { |
| 80 | const cssPath = |
| 81 | "/node_modules/monaco-editor/min/vs/editor/editor.main.css"; |
| 82 | res.writeHead(302, { Location: cssPath }); |
| 83 | res.end(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if ( |
| 88 | req.url?.startsWith( |
| 89 | "/static/monaco/min/vs/base/browser/ui/codicons/codicon/codicon.ttf", |
| 90 | ) |
| 91 | ) { |
| 92 | const fontPath = |
| 93 | "/node_modules/monaco-editor/min/vs/base/browser/ui/codicons/codicon/codicon.ttf"; |
| 94 | res.writeHead(302, { Location: fontPath }); |
| 95 | res.end(); |
| 96 | return; |
| 97 | } |
| 98 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 99 | next(); |
| 100 | }); |
| 101 | }, |
| 102 | }, |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 103 | ], |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 104 | server: { |
| 105 | // Define a middleware to handle the root path redirects |
| 106 | middlewareMode: false, |
| 107 | fs: { |
| 108 | // Allow serving files from these directories |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 109 | allow: ["."], |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 110 | }, |
| 111 | }, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 112 | // Configure Monaco Editor |
| 113 | resolve: { |
| philip.zeyliger | 7351cd9 | 2025-06-14 12:25:31 -0700 | [diff] [blame] | 114 | // No alias needed, use default Monaco import |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 115 | }, |
| 116 | optimizeDeps: { |
| 117 | include: ["monaco-editor"], |
| 118 | }, |
| 119 | // Allow importing CSS as string |
| 120 | css: { |
| 121 | preprocessorOptions: { |
| 122 | additionalData: '@import "monaco-editor/min/vs/editor/editor.main.css";', |
| 123 | }, |
| 124 | }, |
| 125 | build: { |
| 126 | rollupOptions: { |
| 127 | output: { |
| 128 | manualChunks: { |
| 129 | "monaco-editor": ["monaco-editor"], |
| 130 | // Add separate chunks for Monaco editor workers |
| 131 | "monaco-editor-worker": ["monaco-editor/esm/vs/editor/editor.worker"], |
| 132 | "monaco-json-worker": [ |
| 133 | "monaco-editor/esm/vs/language/json/json.worker", |
| 134 | ], |
| 135 | "monaco-css-worker": ["monaco-editor/esm/vs/language/css/css.worker"], |
| 136 | "monaco-html-worker": [ |
| 137 | "monaco-editor/esm/vs/language/html/html.worker", |
| 138 | ], |
| 139 | "monaco-ts-worker": [ |
| 140 | "monaco-editor/esm/vs/language/typescript/ts.worker", |
| 141 | ], |
| 142 | }, |
| 143 | }, |
| 144 | }, |
| 145 | }, |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 146 | }); |