-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Expand file tree
/
Copy pathvitest.workers.config.ts
More file actions
75 lines (67 loc) · 2.35 KB
/
Copy pathvitest.workers.config.ts
File metadata and controls
75 lines (67 loc) · 2.35 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
import fs from 'node:fs';
import path from 'node:path';
import { cloudflareTest } from '@cloudflare/vitest-pool-workers';
import type { Plugin } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';
// Resolve .worker.ts files instead of .ts files, same as tsdown-worker.config.ts
function workerAliasPlugin(): Plugin {
return {
name: 'worker-alias',
enforce: 'pre',
resolveId(source, importer) {
// Skip if no importer (entry point) or already a .worker file
if (!importer || source.includes('.worker')) {
return null;
}
// Handle relative imports
if (source.startsWith('.')) {
const importerDir = path.dirname(importer);
const resolved = path.resolve(importerDir, source);
for (const ext of ['.worker.ts', '.worker.tsx']) {
const workerPath = resolved + ext;
if (fs.existsSync(workerPath)) {
return workerPath;
}
const withoutExt = resolved.replace(/\.(ts|tsx)$/, '');
const workerPathAlt = withoutExt + ext;
if (fs.existsSync(workerPathAlt)) {
return workerPathAlt;
}
}
}
// Handle @/ alias imports
if (source.startsWith('@/')) {
const libPath = path.resolve('./lib', source.slice(2));
for (const ext of ['.worker.ts', '.worker.tsx']) {
const workerPath = libPath + ext;
if (fs.existsSync(workerPath)) {
return workerPath;
}
}
}
return null;
},
};
}
export default defineConfig({
plugins: [
cloudflareTest({
miniflare: {
compatibilityDate: '2025-06-17',
compatibilityFlags: ['nodejs_compat'],
kvNamespaces: ['CACHE'],
},
}),
workerAliasPlugin(),
tsconfigPaths({ root: '.' }),
],
resolve: {
alias: {
'dotenv/config': path.resolve('./lib/shims/dotenv-config.ts'),
},
},
test: {
include: ['lib/**/*.worker.test.ts'],
},
});