-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvite.dev.config.js
More file actions
74 lines (71 loc) · 3 KB
/
vite.dev.config.js
File metadata and controls
74 lines (71 loc) · 3 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
71
72
73
74
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { readFileSync } from 'fs';
import { patchDevtoolsBundle } from './scripts/patch-devtools.js';
// Read the installed Chrome DevTools frontend version so the dev server can route
// /devtools-<version>/ at the same versioned path that production uses. Matching
// the URL shape in both modes keeps the viewer code path identical.
const devtoolsPkg = JSON.parse(readFileSync(resolve(__dirname, 'node_modules/@chrome-devtools/index/package.json'), 'utf-8'));
const devtoolsVersion = devtoolsPkg.version;
const devtoolsDirName = `devtools-${devtoolsVersion}`;
const devtoolsRoot = resolve(__dirname, 'node_modules/@chrome-devtools/index');
const devtoolsPrefix = `/${devtoolsDirName}/`;
const devtoolsServePlugin = {
name: 'waterfall-tools-devtools-serve',
transformIndexHtml(html) {
return html.replace(
/<meta name="waterfall-devtools-path"[^>]*>/,
`<meta name="waterfall-devtools-path" content="./${devtoolsDirName}/">`
);
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!req.url || !req.url.startsWith(devtoolsPrefix)) return next();
const rel = req.url.slice(devtoolsPrefix.length).split('?')[0].split('#')[0];
// Normalize and keep inside devtoolsRoot
const safe = rel.replace(/\\/g, '/').split('/').filter(p => p && p !== '..').join('/');
const filePath = resolve(devtoolsRoot, safe || 'index.html');
if (!filePath.startsWith(devtoolsRoot)) return next();
try {
let buf = readFileSync(filePath);
const ext = filePath.split('.').pop().toLowerCase();
if (ext === 'js' || ext === 'mjs') {
const patched = patchDevtoolsBundle(buf.toString('utf-8'));
buf = Buffer.from(patched, 'utf-8');
}
const ctype = {
html: 'text/html; charset=utf-8',
js: 'text/javascript; charset=utf-8',
mjs: 'text/javascript; charset=utf-8',
css: 'text/css; charset=utf-8',
json: 'application/json; charset=utf-8',
svg: 'image/svg+xml',
png: 'image/png',
jpg: 'image/jpeg',
avif: 'image/avif',
wasm: 'application/wasm',
ico: 'image/x-icon',
woff: 'font/woff',
woff2: 'font/woff2'
}[ext] || 'application/octet-stream';
res.setHeader('Content-Type', ctype);
res.end(buf);
} catch {
next();
}
});
}
};
export default defineConfig({
root: 'src/viewer',
plugins: [devtoolsServePlugin],
resolve: {
alias: {
// During UI development, alias the bare specifier directly back to local source code
// so Hot Module Replacement (HMR) seamlessly updates API side changes in real-time.
'waterfall-tools': resolve(__dirname, 'src/core/waterfall-tools.js'),
'platform-canvas-impl': resolve(__dirname, 'src/platforms/browser/canvas-browser.js'),
'platform-storage-impl': resolve(__dirname, 'src/platforms/browser/storage-browser.js')
}
}
});