-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.js
More file actions
103 lines (96 loc) · 3.07 KB
/
Copy pathvite.config.js
File metadata and controls
103 lines (96 loc) · 3.07 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { mkdir, writeFile } from 'fs/promises';
import crossOriginIsolation from 'vite-plugin-cross-origin-isolation';
function benchmarkResultsSaver() {
return {
name: 'benchmark-results-saver',
configureServer(server) {
server.middlewares.use('/__benchmark/save-results', async (req, res, next) => {
if (req.method !== 'POST') {
next();
return;
}
try {
const chunks = [];
for await (const chunk of req) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
const rawBody = Buffer.concat(chunks).toString('utf8');
const body = rawBody ? JSON.parse(rawBody) : {};
const { filename, payload } = body;
const validName =
typeof filename === 'string' &&
(/^\d{8}_\d{6}_benchmark_[a-z0-9-]+_(parallel|serial)\.json$/i.test(filename) ||
/^benchmark_[a-z0-9-]+_(parallel|serial)_\d{8}_\d{6}\.json$/i.test(filename));
if (!validName || payload == null) {
res.statusCode = 400;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ ok: false, error: 'invalid_payload' }));
return;
}
const outDir = resolve(__dirname, 'benchmark/results');
await mkdir(outDir, { recursive: true });
const outputPath = resolve(outDir, filename);
await writeFile(outputPath, JSON.stringify(payload, null, 2), 'utf8');
res.statusCode = 200;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ ok: true, path: `benchmark/results/${filename}` }));
} catch (error) {
res.statusCode = 500;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ ok: false, error: error?.message ?? 'save_failed' }));
}
});
},
};
}
export default defineConfig({
plugins: [crossOriginIsolation(), benchmarkResultsSaver()],
server: {
watch: null,
},
define: {
'import.meta.env.PROD': 'true',
'import.meta.env.DEV': 'false',
'process.env.NODE_ENV': JSON.stringify('production'),
'import.meta.url': '""',
},
build: {
target: 'es2020',
lib: {
entry: resolve(__dirname, 'src/index.js'),
name: 'OpenMapTiles Router',
fileName: 'omt-router',
formats: ['es', 'cjs'],
},
sourcemap: false,
rollupOptions: {
external: ['perf_hooks', 'crypto', 'fs'],
output: {
globals: {
fs: '{}',
crypto: '{}',
perf_hooks: '{}',
},
codeSplitting: false,
},
treeshake: {
annotations: true,
moduleSideEffects: false,
propertyReadSideEffects: false,
},
},
},
worker: {
format: 'iife',
rollupOptions: {
external: ['perf_hooks', 'crypto', 'fs'],
treeshake: {
annotations: true,
moduleSideEffects: false,
propertyReadSideEffects: false,
},
},
},
});