-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvite.config.js
More file actions
55 lines (51 loc) · 1.86 KB
/
Copy pathvite.config.js
File metadata and controls
55 lines (51 loc) · 1.86 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
import { defineConfig } from 'vite';
import dotenv from 'dotenv';
import { resolve } from 'path';
dotenv.config({ path: resolve(import.meta.dirname, '.env') });
export default defineConfig(() => {
const projectRepoRoot = resolve(import.meta.dirname);
const workspaceDir = process.cwd();
const isSample =
workspaceDir !== projectRepoRoot && workspaceDir.includes('/samples/');
const apiKey = process.env.GOOGLE_MAPS_API_KEY || '';
return {
root: isSample ? workspaceDir : projectRepoRoot,
plugins: [
{
name: 'html-transform',
enforce: 'pre',
transformIndexHtml(html) {
console.log(
'transformIndexHtml running! API Key length:',
apiKey.length
);
return html.replace(/GOOGLE_MAPS_API_KEY/g, apiKey);
},
},
{
name: 'code-transform',
enforce: 'pre',
transform(code) {
if (code.includes('GOOGLE_MAPS_API_KEY')) {
return {
code: code.replace(/GOOGLE_MAPS_API_KEY/g, apiKey),
map: null,
};
}
},
},
],
build: {
emptyOutDir: false, // Prevents clearing other files
// We do not override outDir or rollupOptions.input here!
// Vite automatically outputs to the local 'dist' folder (which dist.sh expects)
// and automatically uses 'index.html' as the input because root is the workspaceDir.
},
preview: {
port: 8080,
},
define: {
'import.meta.env.GOOGLE_MAPS_API_KEY': JSON.stringify(apiKey),
},
};
});