-
-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathvite.config.js
More file actions
107 lines (96 loc) · 2.68 KB
/
vite.config.js
File metadata and controls
107 lines (96 loc) · 2.68 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
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import fs from "fs";
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "package.json"), "utf8"),
);
const appVersion = String(packageJson.version ?? "0.0.0");
const MAPLIBRE_DEP_PACKAGES = new Set([
"earcut",
"gl-matrix",
"kdbush",
"murmurhash-js",
"pbf",
"potpack",
"quickselect",
"supercluster",
"tinyqueue",
]);
function getPackageName(id) {
const nodeModulesMatch = id.match(/[\\/]node_modules[\\/](.*)$/);
if (!nodeModulesMatch || !nodeModulesMatch[1]) return null;
const modulePath = nodeModulesMatch[1];
const parts = modulePath.split(/[\\/]/);
if (parts.length === 0) return null;
if (parts[0].startsWith("@") && parts.length > 1) {
return `${parts[0]}/${parts[1]}`;
}
return parts[0];
}
function adsTxtPlugin() {
let resolvedConfig;
return {
name: "ads-txt",
configResolved(config) {
resolvedConfig = config;
},
closeBundle() {
const clientId = resolvedConfig.env.VITE_ADSENSE_CLIENT;
if (!clientId) {
console.warn("[ads-txt] VITE_ADSENSE_CLIENT is not set — skipping ads.txt generation");
return;
}
const outDir = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir);
fs.writeFileSync(
path.join(outDir, "ads.txt"),
`google.com, ${clientId}, DIRECT, f08c47fec0942fa0\n`,
"utf8",
);
},
};
}
export default defineConfig({
plugins: [react(), adsTxtPlugin()],
define: {
"import.meta.env.VITE_APP_VERSION": JSON.stringify(appVersion),
},
build: {
// maplibre-gl is distributed as a large prebundled module and remains a
// single chunk even with manual chunking.
chunkSizeWarningLimit: 1100,
rollupOptions: {
output: {
manualChunks(id) {
if (!id.includes("node_modules")) return;
const packageName = getPackageName(id);
if (packageName === "maplibre-gl") {
return "vendor-maplibre-core";
}
if (
packageName?.startsWith("@maplibre/") ||
packageName?.startsWith("@mapbox/") ||
MAPLIBRE_DEP_PACKAGES.has(packageName)
) {
return "vendor-maplibre-deps";
}
if (packageName?.startsWith("react-icons")) {
return "vendor-icons";
}
if (
packageName === "react" ||
packageName === "react-dom" ||
packageName === "react-colorful"
) {
return "vendor-react";
}
},
},
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
});