-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
70 lines (66 loc) · 1.87 KB
/
Copy pathvite.config.js
File metadata and controls
70 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { defineConfig } from 'vite';
import path from 'path';
import fs from 'fs';
const repoRoot = path.resolve(__dirname, '../..');
/** Base for built HTML asset URLs — CLI `--base` overrides this at dev/preview time. */
const basePath = process.env.FORMSPEC_BASE_PATH || '/';
/** Serves `/examples`, `/packages`, `/registries` from the monorepo root (dev + preview). */
function attachRepoRootStatic(server) {
const MIME = { '.css': 'text/css', '.js': 'text/javascript', '.json': 'application/json' };
server.middlewares.use((req, res, next) => {
let pathname = req.url?.split('?')[0] || '';
try {
pathname = decodeURIComponent(pathname);
} catch {
/* ignore */
}
const viteBase = (server.config.base || '/').replace(/\/$/, '');
if (viteBase && pathname.startsWith(viteBase)) {
pathname = pathname.slice(viteBase.length) || '/';
}
if (
!pathname.startsWith('/packages/') &&
!pathname.startsWith('/examples/') &&
!pathname.startsWith('/registries/')
) {
return next();
}
const fsPath = path.join(repoRoot, pathname);
const mime = MIME[path.extname(fsPath).toLowerCase()];
if (!mime) return next();
fs.readFile(fsPath, (err, data) => {
if (err) return next();
res.setHeader('Content-Type', mime);
res.end(data);
});
});
}
export default defineConfig({
base: basePath,
build: {
target: 'es2022',
rollupOptions: {
input: {
main: path.resolve(__dirname, 'index.html'),
tools: path.resolve(__dirname, 'tools.html'),
},
},
},
server: {
allowedHosts: true,
fs: {
allow: [repoRoot],
},
},
plugins: [
{
name: 'repo-root-static',
configureServer(server) {
attachRepoRootStatic(server);
},
configurePreviewServer(server) {
attachRepoRootStatic(server);
},
},
],
});