blob: b2127925127eadad6010cac02ef822e2de3c55bf [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({
9 plugins: [
10 hmrPlugin({
11 include: ["./src/**/*.ts"],
12 presets: [presets.lit],
13 }),
Philip Zeyliger272a90e2025-05-16 14:49:51 -070014 // Custom plugin for handling the root path redirect and Monaco workers
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000015 {
16 name: "configure-server",
17 configureServer(server) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070018 // Handle root redirects and Monaco worker proxying
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000019 server.middlewares.use((req, res, next) => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070020 // Root redirect
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000021 if (req.url === "/") {
22 res.writeHead(302, {
23 Location: "/src/web-components/demo/index.html",
24 });
25 res.end();
26 return;
27 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -070028
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 Zeyliger16fa8b42025-05-02 04:28:16 +000096 next();
97 });
98 },
99 },
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100100 ],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000101 server: {
102 // Define a middleware to handle the root path redirects
103 middlewareMode: false,
104 fs: {
105 // Allow serving files from these directories
Pokey Rulec575fd72025-05-06 15:46:38 +0100106 allow: ["."],
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000107 },
108 },
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700109 // 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 Rulee2a8c2f2025-04-23 15:09:25 +0100145});