-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvite.config.ts
More file actions
89 lines (84 loc) · 2.54 KB
/
vite.config.ts
File metadata and controls
89 lines (84 loc) · 2.54 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
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import tailwindcss from '@tailwindcss/vite';
import tanstackRouter from '@tanstack/router-plugin/vite';
import react from '@vitejs/plugin-react-swc';
import * as v from 'valibot';
import { defineConfig } from 'vite';
/**
* Fixes issue with "__dirname is not defined in ES module scope"
* https://flaviocopes.com/fix-dirname-not-defined-es-module-scope
*
* This is only necessary when using vite with `--configLoader runner`.
* We use this option to allow for importing TS files from monorepos.
* https://vite.dev/config/#configuring-vite
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envSchema = v.object({
/**
* Since vite is only used during development, we can assume the structure
* will resemble a URL such as: http://localhost:3035.
* This will then be used to set the vite dev server's host and port.
*/
PUBLIC_WEB_URL: v.pipe(
v.optional(v.string(), 'http://localhost:3035'),
v.url(),
),
/**
* Set this if you want to run or deploy your app at a base URL. This is
* usually required for deploying a repository to Github/Gitlab pages.
*/
PUBLIC_BASE_PATH: v.pipe(v.optional(v.string(), '/'), v.startsWith('/')),
});
const env = v.parse(envSchema, process.env);
const webUrl = new URL(env.PUBLIC_WEB_URL);
const host = webUrl.hostname;
const port = parseInt(webUrl.port, 10);
export default defineConfig({
plugins: [
tanstackRouter({
routeToken: 'layout',
autoCodeSplitting: true,
}),
tailwindcss(),
react(),
],
base: env.PUBLIC_BASE_PATH,
envPrefix: 'PUBLIC_',
server: {
host,
port,
strictPort: true,
},
build: {
rollupOptions: {
output: {
/**
* Modified from:
* https://github.com/vitejs/vite/discussions/9440#discussioncomment-11430454
*/
manualChunks(id) {
if (id.includes('node_modules')) {
const modulePath = id.split('node_modules/')[1];
const topLevelFolder = modulePath?.split('/')[0];
if (topLevelFolder !== '.pnpm') {
return topLevelFolder;
}
const scopedPackageName = modulePath?.split('/')[1];
const chunkName =
scopedPackageName?.split('@')[
scopedPackageName.startsWith('@') ? 1 : 0
];
return chunkName;
}
},
},
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
});