-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-plugin.js
More file actions
44 lines (40 loc) · 1.53 KB
/
Copy pathdev-plugin.js
File metadata and controls
44 lines (40 loc) · 1.53 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
// Vite dev-server plugin: serves veditor.web/dist/ at a local URL prefix
// so that consumer apps (notehub, metabrowse) can load veditor from the
// same origin during development, avoiding CORS issues.
//
// Usage in a consumer's vite.config.ts:
// import veditorDev from '../veditor.web/dev-plugin.js';
// export default defineConfig({ plugins: [veditorDev()] });
//
// Then set VITE_VEDITOR_BASE=/veditor when running the dev server.
import { readFileSync, existsSync } from 'fs';
import { join, extname } from 'path';
import { fileURLToPath } from 'url';
const distDir = join(fileURLToPath(new URL('.', import.meta.url)), 'dist');
const mimeTypes = {
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.map': 'application/json',
};
export default function veditorDev(prefix = '/veditor') {
return {
name: 'veditor-dev',
configureServer(server) {
if (!existsSync(distDir)) {
console.warn(`[veditor-dev] ${distDir} not found — run "make build-veditor" first`);
return;
}
server.middlewares.use((req, res, next) => {
if (!req.url?.startsWith(prefix + '/')) return next();
const urlPath = req.url.split('?')[0];
const relPath = urlPath.slice(prefix.length);
const filePath = join(distDir, relPath);
if (!existsSync(filePath)) return next();
const ext = extname(filePath);
res.setHeader('Content-Type', mimeTypes[ext] || 'application/octet-stream');
res.end(readFileSync(filePath));
});
},
};
}