blob: 9fc8e3a1d578f33b9162742534f315a782594ff3 [file] [log] [blame]
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01001import { dirname, resolve } from "node:path";
2import { fileURLToPath } from "node:url";
Sean McCullough618bfb22025-06-25 20:52:30 +00003import { spawn } from "node:child_process";
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01004import { hmrPlugin, presets } from "vite-plugin-web-components-hmr";
5import { defineConfig } from "vite";
Sean McCullough618bfb22025-06-25 20:52:30 +00006import type { Plugin } from "vite";
7import * as fs from "node:fs";
8import tailwindcss from "@tailwindcss/vite";
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01009
10const __dirname = dirname(fileURLToPath(import.meta.url));
11
12export default defineConfig({
Philip Zeyliger3cde2822025-06-21 09:32:38 -070013 // 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 Rulee2a8c2f2025-04-23 15:09:25 +010018 plugins: [
Sean McCullough618bfb22025-06-25 20:52:30 +000019 tailwindcss(),
Pokey Rulee2a8c2f2025-04-23 15:09:25 +010020 hmrPlugin({
21 include: ["./src/**/*.ts"],
22 presets: [presets.lit],
23 }),
Philip Zeyliger272a90e2025-05-16 14:49:51 -070024 // Custom plugin for handling the root path redirect and Monaco workers
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000025 {
26 name: "configure-server",
27 configureServer(server) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070028 // Handle root redirects and Monaco worker proxying
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000029 server.middlewares.use((req, res, next) => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070030 // Root redirect
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000031 if (req.url === "/") {
32 res.writeHead(302, {
33 Location: "/src/web-components/demo/index.html",
34 });
35 res.end();
36 return;
37 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -070038
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 Zeyliger16fa8b42025-05-02 04:28:16 +0000106 next();
107 });
108 },
109 },
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100110 ],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000111 server: {
112 // Define a middleware to handle the root path redirects
113 middlewareMode: false,
114 fs: {
115 // Allow serving files from these directories
Pokey Rulec575fd72025-05-06 15:46:38 +0100116 allow: ["."],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000117 },
118 },
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700119 // Configure Monaco Editor
120 resolve: {
philip.zeyliger7351cd92025-06-14 12:25:31 -0700121 // No alias needed, use default Monaco import
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700122 },
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 Rulee2a8c2f2025-04-23 15:09:25 +0100153});