-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-vite-config.ts
95 lines (89 loc) · 2.49 KB
/
node-vite-config.ts
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
import { deepkitType } from '@deepkit/vite';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import { join } from 'node:path';
import { workspaceRoot } from '@nx/devkit';
import { defineConfig, PluginOption } from 'vite';
export interface NodeViteConfigOptions {
readonly root: string;
readonly plugins?: readonly PluginOption[];
readonly debug?: boolean;
}
export function defineNodeConfig({
root,
plugins,
debug,
}: NodeViteConfigOptions) {
return defineConfig(({ mode }) => {
const tsConfig =
mode === 'test'
? join(root, 'tsconfig.spec.json')
: join(root, 'tsconfig.app.json');
const projectPathFromWorkspaceRoot = root.replace(`${workspaceRoot}/`, '');
const buildLibsFromSource = process.env.NX_TASK_TARGET_TARGET === 'serve';
const envVars = Object.entries(process.env).reduce(
(defs, [key, value]) => ({
...defs,
[`import.meta.env.${key}`]: JSON.stringify(value),
}),
{},
);
return {
root,
cacheDir: `${workspaceRoot}/node_modules/.vite/${projectPathFromWorkspaceRoot}`,
build: {
minify: mode === 'production',
outDir: `${workspaceRoot}/dist/${projectPathFromWorkspaceRoot}`,
emptyOutDir: true,
rollupOptions: {
preserveEntrySignatures: 'strict',
output: {
esModule: true,
entryFileNames: `[name].mjs`,
},
},
commonjsOptions: {
transformMixedEsModules: true,
},
},
resolve: {
mainFields: ['module'],
},
plugins: [
deepkitType({
tsConfig,
compilerOptions: {
sourceMap: true,
},
}),
nxViteTsPaths({
debug,
buildLibsFromSource,
}),
...(plugins || []),
],
test: {
passWithNoTests: true,
environment: 'node',
include: ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: join(
workspaceRoot,
'coverage',
projectPathFromWorkspaceRoot,
),
provider: 'v8',
},
isolate: false,
cache: {
dir: join(workspaceRoot, 'node_modules/.cache/vitest'),
},
},
define: {
'import.meta.vitest': mode === 'test',
'import.meta.env.NX_WORKSPACE_ROOT': JSON.stringify(workspaceRoot),
...envVars,
},
};
});
}