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