-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.config.ts
More file actions
103 lines (93 loc) · 3.23 KB
/
Copy pathvitest.config.ts
File metadata and controls
103 lines (93 loc) · 3.23 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 "vitest/config";
import { readFileSync } from "fs";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Plugin that resolves .js imports to .ts files within test/ and src/.
*/
function resolveTsFromJs() {
return {
name: "resolve-ts-from-js",
resolveId(source: string, importer: string | undefined) {
if (!source.endsWith(".js") || !importer) return null;
const tsPath = source.replace(/\.js$/, ".ts");
const resolved = resolve(dirname(importer), tsPath);
try {
readFileSync(resolved);
return resolved;
} catch {
return null;
}
},
};
}
/**
* Plugin that inlines .proto files as strings (matches rollup-plugin-string).
*/
function stringPlugin() {
return {
name: "string",
transform(code: string, id: string) {
if (id.endsWith(".proto")) {
return { code: `export default ${JSON.stringify(code)};`, map: null };
}
},
};
}
/**
* Plugin that bundles the Emscripten curve25519 module and inlines the WASM
* binary so it doesn't need to be loaded from disk at runtime.
*/
function curveWasmInlinePlugin() {
const wasmPath = resolve(__dirname, "build", "curve25519_compiled.wasm");
const wasmJsPath = resolve(__dirname, "build", "curve25519_compiled.js");
const wasmBase64 = readFileSync(wasmPath).toString("base64");
return {
name: "curve-wasm-inline",
resolveId(id: string) {
if (id.includes("curve25519_compiled") && !id.endsWith(".wasm")) {
return wasmJsPath;
}
return null;
},
load(id: string) {
if (id === wasmJsPath) {
let js = readFileSync(wasmJsPath, "utf8");
// Replace the Emscripten WASM loading with an inlined base64 version.
const injection = `var wasmBinary = Uint8Array.from(atob("${wasmBase64}"), c => c.charCodeAt(0));`;
// Insert after the "var wasmBinary;" declaration line
js = js.replace(/^var wasmBinary;$/m, injection);
// Fix __dirname not being available in ESM: replace with import.meta.url based path
js = js.replace(
"scriptDirectory = __dirname + '/';",
`scriptDirectory = new URL('.', import.meta.url).pathname + '/';`
);
return js;
}
return null;
},
};
}
export default defineConfig({
plugins: [resolveTsFromJs(), stringPlugin(), curveWasmInlinePlugin()],
test: {
globals: true,
include: ["test/**/*.ts"],
exclude: [
"test/support/**",
"test/utils.ts",
"test/testvectors.ts",
"test/omemo2-vector.ts",
"test/identity-key-store.ts",
"test/prekey-store.ts",
"test/session-store.ts",
"test/signed-prekey-store.ts",
],
setupFiles: ["test/support/vitest-setup.ts"],
testTimeout: 20000,
},
esbuild: {
target: "es2020",
},
});