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