Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
73 lines (69 loc) · 2.62 KB
/
Copy pathvite.config.js
File metadata and controls
73 lines (69 loc) · 2.62 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
import { defineConfig } from 'vite';
import dotenv from 'dotenv';
import { resolve } from 'path';
dotenv.config({ path: resolve(import.meta.dirname, '.env') });
export default defineConfig(({ command }) => {
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) {
let transformed = code;
if (transformed.includes('GOOGLE_MAPS_API_KEY')) {
transformed = transformed.replace(
/GOOGLE_MAPS_API_KEY/g,
apiKey
);
}
const publicUrlRegex =
/new URL\(['"]\.\/public\/['"],\s*import\.meta\.url\)/g;
if (
command === 'build' &&
publicUrlRegex.test(transformed)
) {
transformed = transformed.replace(
publicUrlRegex,
"new URL(/* @vite-ignore */ '../', import.meta.url)"
);
}
if (transformed !== code) {
return {
code: transformed,
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),
},
};
});