-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
130 lines (118 loc) · 3.67 KB
/
vite.config.ts
File metadata and controls
130 lines (118 loc) · 3.67 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import path from 'path';
import fs from 'fs';
import react from '@vitejs/plugin-react';
import laravel from 'laravel-vite-plugin';
import dotenv from 'dotenv';
import { defineConfig, loadEnv } from 'vite';
import tailwindcss from '@tailwindcss/vite';
import { NodePackageImporter } from 'sass-embedded';
import manifestSRI from 'vite-plugin-manifest-sri';
interface ImportMetaEnv {
readonly VITE_DEV_URL: string;
readonly VITE_APP_PORT: string;
readonly VITE_DEV_PORT: string;
readonly APP_DEVELOPMENT_ENV: string;
readonly VITE_NOCAPTCHA_SITEKEY: string;
}
/**
* Check if the additional environment file should be loaded.
*/
function shouldLoadAdditionalEnv(mainEnv): boolean {
const isLocalEnv = mainEnv.APP_ENV === 'local';
return (
(mainEnv.APP_DEVELOPMENT_ENV && mainEnv.APP_DEVELOPMENT_ENV.length > 1 && isLocalEnv) ||
false
);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default defineConfig(({ mode, command }) => {
// Load the main .env file (Vite's built-in function)
const mainEnv = loadEnv(mode, process.cwd(), '');
// Determine which additional .env file to load
const developmentEnv = mainEnv.APP_DEVELOPMENT_ENV ?? 'native'; // Default to .env.native
let additionalEnv = {};
if (shouldLoadAdditionalEnv(mainEnv)) {
const additionalEnvFile = path.resolve(process.cwd(), `.env.${developmentEnv}`);
if (fs.existsSync(additionalEnvFile)) {
console.log(`Loading additional environment variables from ${additionalEnvFile}`);
additionalEnv = dotenv.parse(fs.readFileSync(additionalEnvFile));
// Manually inject additionalEnv into process.env
for (const [key, value] of Object.entries(additionalEnv)) {
process.env[key] = value as string;
}
} else {
console.warn(`Additional environment file does not exist: ${additionalEnvFile}`);
}
}
// Merge environment variables (additionalEnv **overrides** mainEnv)
const env: ImportMetaEnv = { ...mainEnv, ...additionalEnv } as ImportMetaEnv;
const DEV_URL = env.VITE_DEV_URL ?? 'http://localhost';
const APP_PORT = env.VITE_APP_PORT ?? '8000';
const DEV_PORT = env.VITE_DEV_PORT ?? '5179';
const origins = [
`${DEV_URL}`, // Laravel app URL
`${DEV_URL}:${APP_PORT}`, // Laravel served over HTTPS (DDEV)
`${DEV_URL}:${DEV_PORT}`, // Vite dev server
];
if (DEV_URL.includes('localhost')) {
// Add 127.0.0.1 when DEV_URL is localhost
origins.push('http://127.0.0.1:' + APP_PORT);
}
// Determine WebSocket protocol based on DEV_URL
// Ensures Hot Module Reloading (HMR) works with HTTPS
const wsProtocol = DEV_URL.startsWith('https') ? 'wss' : 'ws';
return {
plugins: [
laravel({
input: 'resources/js/app.tsx',
ssr: 'resources/js/ssr.tsx',
refresh: true,
}),
react(),
tailwindcss(),
manifestSRI(),
],
server: {
host: '0.0.0.0',
port: Number(DEV_PORT),
strictPort: true,
origin: `${DEV_URL}:${DEV_PORT}`,
cors: {
origin: origins,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
},
hmr: {
protocol: wsProtocol,
host: new URL(DEV_URL).hostname, // Extracts the hostname
port: Number(DEV_PORT),
},
},
css: {
postcss: './postcss.config.js',
preprocessorOptions: {
scss: {
api: 'modern-compiler',
silenceDeprecations: [
'mixed-decls',
'color-functions',
'import',
'global-builtin',
],
importers: [new NodePackageImporter()],
},
},
devSourcemap: true,
},
resolve: {
alias: {
'@fontsource-variable': path.resolve(
__dirname,
'node_modules/@fontsource-variable'
),
'ziggy-js': path.resolve(__dirname, 'vendor/tightenco/ziggy'),
},
},
};
});