blob: 73455148987130f2271b632851333e41d1ffdf2d [file] [log] [blame]
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01001import { dirname, resolve } from "node:path";
2import { fileURLToPath } from "node:url";
3import { hmrPlugin, presets } from "vite-plugin-web-components-hmr";
4import { defineConfig } from "vite";
5
6const __dirname = dirname(fileURLToPath(import.meta.url));
7
8export default defineConfig({
Philip Zeyliger3cde2822025-06-21 09:32:38 -07009 // 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 Rulee2a8c2f2025-04-23 15:09:25 +010014 plugins: [
15 hmrPlugin({
16 include: ["./src/**/*.ts"],
17 presets: [presets.lit],
18 }),
Philip Zeyliger272a90e2025-05-16 14:49:51 -070019 // Custom plugin for handling the root path redirect and Monaco workers
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000020 {
21 name: "configure-server",
22 configureServer(server) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070023 // Handle root redirects and Monaco worker proxying
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000024 server.middlewares.use((req, res, next) => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070025 // Root redirect
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000026 if (req.url === "/") {
27 res.writeHead(302, {
28 Location: "/src/web-components/demo/index.html",
29 });
30 res.end();
31 return;
32 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -070033
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 Zeyliger16fa8b42025-05-02 04:28:16 +0000101 next();
102 });
103 },
104 },
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100105 ],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000106 server: {
107 // Define a middleware to handle the root path redirects
108 middlewareMode: false,
109 fs: {
110 // Allow serving files from these directories
Pokey Rulec575fd72025-05-06 15:46:38 +0100111 allow: ["."],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000112 },
113 },
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700114 // Configure Monaco Editor
115 resolve: {
philip.zeyliger7351cd92025-06-14 12:25:31 -0700116 // No alias needed, use default Monaco import
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700117 },
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 Rulee2a8c2f2025-04-23 15:09:25 +0100148});