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