-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvite.config.ts
94 lines (83 loc) · 3.23 KB
/
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
import react from '@vitejs/plugin-react';
import { join } from 'node:path';
import { Alias, defineConfig, loadEnv } from 'vite';
import { getWorkspacePackageInfo } from '@votingworks/monorepo-utils';
export default defineConfig(async (env) => {
const workspaceRootPath = join(__dirname, '../../..');
const workspacePackages = await getWorkspacePackageInfo(workspaceRootPath);
const envPrefix = 'REACT_APP_';
const rootDotenvValues = loadEnv(env.mode, workspaceRootPath, envPrefix);
const coreDotenvValues = loadEnv(env.mode, __dirname, envPrefix);
const processEnvDefines = [
...Object.entries(rootDotenvValues),
...Object.entries(coreDotenvValues),
].reduce<Record<string, string>>(
(acc, [key, value]) => ({
...acc,
[`process.env.${key}`]: JSON.stringify(value),
}),
{}
);
return {
build: {
// Write build files to `build` directory.
outDir: 'build',
// Do not minify build files. We don't need the space savings and this is
// a minor transparency improvement.
minify: false,
},
// Replace some code in Node modules, `#define`-style, to avoid referencing
// Node-only globals like `process`.
define: {
'process.env.NODE_DEBUG': 'undefined',
'process.platform': JSON.stringify('browser'),
'process.version': JSON.stringify(process.version),
// TODO: Replace these with the appropriate `import.meta.env` values.
...processEnvDefines,
},
resolve: {
alias: [
// Replace NodeJS built-in modules with polyfills.
//
// The trailing slash is important, otherwise it will be resolved as a
// built-in NodeJS module.
{ find: 'buffer', replacement: require.resolve('buffer/') },
{ find: 'node:buffer', replacement: require.resolve('buffer/') },
{ find: 'fs', replacement: join(__dirname, './src/stubs/fs.ts') },
{ find: 'node:fs', replacement: join(__dirname, './src/stubs/fs.ts') },
{ find: 'path', replacement: require.resolve('path/') },
{ find: 'node:path', replacement: require.resolve('path/') },
{ find: 'util', replacement: require.resolve('util/') },
{ find: 'node:util', replacement: require.resolve('util/') },
// Create aliases for all workspace packages, i.e.
//
// {
// '@votingworks/types': '…/libs/types/src/index.ts',
// '@votingworks/utils': '…/libs/utils/src/index.ts',
// …
// }
//
// This allows re-mapping imports for workspace packages to their
// TypeScript source code rather than the built JavaScript.
...Array.from(workspacePackages.values()).reduce<Alias[]>(
(aliases, { path, name, source }) =>
!source
? aliases
: [...aliases, { find: name, replacement: join(path, source) }],
[]
),
],
},
plugins: [react()],
// Configure the Vite dev server to proxy API requests to the dev backend server
server: {
proxy: {
'/api': 'http://localhost:3002',
'/files': 'http://localhost:3002',
},
port: 3000,
},
// Pass some environment variables to the client in `import.meta.env`.
envPrefix,
};
});