-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
92 lines (77 loc) · 2.73 KB
/
vite.config.ts
File metadata and controls
92 lines (77 loc) · 2.73 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
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, type Plugin } from 'vite';
/**
* Vite plugin to inline croner WASM binary into the bundle.
* Replicates the esbuild wasmInlinePlugin from worker.compile.ts:
* reads the .wasm binary, base64-encodes it, and emits JS that
* compiles + instantiates the module at runtime.
*/
function cronerWasmInlinePlugin(): Plugin {
return {
name: 'croner-wasm-inline',
enforce: 'pre',
resolveId(id) {
if (id === '@openworkers/croner-wasm') {
return '\0croner-wasm-inline';
}
},
load(id) {
if (id !== '\0croner-wasm-inline') return;
const wasmPath = resolve('./node_modules/@openworkers/croner-wasm/dist/bundler/croner_wasm_bg.wasm');
const bgJsPath = resolve('./node_modules/@openworkers/croner-wasm/dist/bundler/croner_wasm_bg.js');
const wasmBytes = readFileSync(wasmPath);
const base64 = wasmBytes.toString('base64');
// Discover WASM imports at build time
const wasmModule = new WebAssembly.Module(wasmBytes);
const imports = WebAssembly.Module.imports(wasmModule);
const importNames = imports.filter((i) => i.module === './croner_wasm_bg.js').map((i) => i.name);
return `
import {
${importNames.join(',\n ')},
__wbg_set_wasm,
WasmCron,
parseAndDescribe,
} from ${JSON.stringify(bgJsPath)};
const _base64 = "${base64}";
const _bytes = Uint8Array.from(atob(_base64), c => c.charCodeAt(0));
const _module = new WebAssembly.Module(_bytes);
const _instance = new WebAssembly.Instance(_module, {
"./croner_wasm_bg.js": { ${importNames.join(', ')} }
});
__wbg_set_wasm(_instance.exports);
_instance.exports.__wbindgen_start();
export { WasmCron, parseAndDescribe };
`;
}
};
}
/**
* Override zod locales to only include English (saves ~50KB per worker).
*/
function zodLocalesPlugin(): Plugin {
return {
name: 'zod-locales-en-only',
enforce: 'pre',
resolveId(id) {
if (id === 'zod/v4/locales' || id.endsWith('/zod/v4/locales/index.js')) {
return '\0zod-locales-en';
}
},
load(id) {
if (id !== '\0zod-locales-en') return;
return `export { default as en } from "zod/v4/locales/en.js";`;
}
};
}
const envPort = process.env.PORT;
if (!/^\d{1,5}$/.test(envPort || '')) {
console.warn(`Warning: Invalid PORT environment variable "${envPort}", defaulting to 5173`);
}
export default defineConfig({
plugins: [cronerWasmInlinePlugin(), zodLocalesPlugin(), sveltekit()],
server: {
port: parseInt(process.env.PORT || '5173', 10)
}
});