blob: e9b52d3ba6f0543310e0f99ca52b215bd1d59652 [file] [log] [blame]
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01001import { hmrPlugin, presets } from "vite-plugin-web-components-hmr";
2import { defineConfig } from "vite";
Sean McCullough618bfb22025-06-25 20:52:30 +00003import tailwindcss from "@tailwindcss/vite";
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01004
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01005export default defineConfig({
Philip Zeyliger3cde2822025-06-21 09:32:38 -07006 // 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 Rulee2a8c2f2025-04-23 15:09:25 +010011 plugins: [
Sean McCullough618bfb22025-06-25 20:52:30 +000012 tailwindcss(),
Pokey Rulee2a8c2f2025-04-23 15:09:25 +010013 hmrPlugin({
14 include: ["./src/**/*.ts"],
15 presets: [presets.lit],
16 }),
Philip Zeyliger272a90e2025-05-16 14:49:51 -070017 // Custom plugin for handling the root path redirect and Monaco workers
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000018 {
19 name: "configure-server",
20 configureServer(server) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070021 // Handle root redirects and Monaco worker proxying
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000022 server.middlewares.use((req, res, next) => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070023 // Root redirect
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000024 if (req.url === "/") {
25 res.writeHead(302, {
26 Location: "/src/web-components/demo/index.html",
27 });
28 res.end();
29 return;
30 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -070031
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 Zeyliger16fa8b42025-05-02 04:28:16 +000099 next();
100 });
101 },
102 },
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100103 ],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000104 server: {
105 // Define a middleware to handle the root path redirects
106 middlewareMode: false,
107 fs: {
108 // Allow serving files from these directories
Pokey Rulec575fd72025-05-06 15:46:38 +0100109 allow: ["."],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000110 },
111 },
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700112 // Configure Monaco Editor
113 resolve: {
philip.zeyliger7351cd92025-06-14 12:25:31 -0700114 // No alias needed, use default Monaco import
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700115 },
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 Rulee2a8c2f2025-04-23 15:09:25 +0100146});