-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
182 lines (164 loc) · 5.42 KB
/
Copy pathvite.config.ts
File metadata and controls
182 lines (164 loc) · 5.42 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import process from "node:process";
import { execSync } from "node:child_process";
import { createRequire } from "node:module";
import fs from "node:fs";
import path from "node:path";
import react from "@vitejs/plugin-react";
import { visualizer } from "rollup-plugin-visualizer";
import { defineConfig, loadEnv, type Plugin } from "vite";
import { DittoConfigSchema } from "./src/lib/schemas";
/**
* Load and validate the build-time ditto.json configuration file.
* Returns the parsed config object, or `undefined` if the file doesn't exist.
* Set the CONFIG_FILE env var to override the default path ("./ditto.json").
*/
function loadDittoConfig(): object | undefined {
const configPath = path.resolve(process.env.CONFIG_FILE ?? "./ditto.json");
let raw: string;
try {
raw = fs.readFileSync(configPath, "utf-8");
} catch {
// File not found — no build-time config
return undefined;
}
const json = JSON.parse(raw);
const result = DittoConfigSchema.parse(json);
return result;
}
/**
* Copy all files from `src` into `dest`, overwriting existing files.
* Recursively handles subdirectories.
*/
function copyDirSync(src: string, dest: string): void {
if (!fs.existsSync(src)) return;
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirSync(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
/**
* Vite plugin that merges an external public directory on top of the default one.
* Set the PUBLIC_DIR env var to a directory path. Files in that directory take
* precedence over files in the built-in `public/` directory.
*
* - In build mode, files are copied into the output after the default public dir.
* - In dev mode, the external directory is served with higher priority.
*/
function mergePublicDir(externalDir: string): Plugin {
const resolved = path.resolve(externalDir);
return {
name: "ditto:merge-public-dir",
configureServer(server) {
// Serve files from the external public dir before the default public dir.
server.middlewares.use((req, res, next) => {
if (!req.url) return next();
const urlPath = decodeURIComponent(new URL(req.url, "http://localhost").pathname);
const filePath = path.join(resolved, urlPath);
try {
const stat = fs.statSync(filePath);
if (stat.isFile()) {
// Let Vite's static middleware handle it by pointing to the file.
const stream = fs.createReadStream(filePath);
stream.pipe(res);
return;
}
} catch {
// File not found in external dir — fall through to default public dir
}
next();
});
},
writeBundle(options) {
const outDir = options.dir ?? path.resolve("dist");
copyDirSync(resolved, outDir);
},
};
}
const dittoConfig = loadDittoConfig();
const publicDir = process.env.PUBLIC_DIR;
const require = createRequire(import.meta.url);
const pkg = require("./package.json") as { version: string };
/** Short commit SHA — prefer CI env var, fall back to git. */
function getCommitSha(): string {
if (process.env.CI_COMMIT_SHORT_SHA) return process.env.CI_COMMIT_SHORT_SHA;
try {
return execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();
} catch {
return "";
}
}
/** Git tag for the current commit — prefer CI env var, fall back to git. Empty string if untagged. */
function getCommitTag(): string {
if (process.env.CI_COMMIT_TAG) return process.env.CI_COMMIT_TAG;
try {
return execSync("git describe --exact-match --tags HEAD 2>/dev/null", { encoding: "utf-8" }).trim();
} catch {
return "";
}
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
server: {
host: "::",
port: 8080,
allowedHosts: env.ALLOWED_HOSTS === "*" ? true : undefined,
},
plugins: [
react(),
visualizer({
filename: "dist/bundle.html",
template: "treemap",
gzipSize: true,
}),
...(publicDir ? [mergePublicDir(publicDir)] : []),
],
define: {
'import.meta.env.DITTO_CONFIG': JSON.stringify(JSON.stringify(dittoConfig ?? null)),
'import.meta.env.VERSION': JSON.stringify(pkg.version),
'import.meta.env.BUILD_DATE': JSON.stringify(new Date().toISOString()),
'import.meta.env.COMMIT_SHA': JSON.stringify(getCommitSha()),
'import.meta.env.COMMIT_TAG': JSON.stringify(getCommitTag()),
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/test/setup.ts',
onConsoleLog(log) {
return !log.includes("React Router Future Flag Warning");
},
env: {
DEBUG_PRINT_LIMIT: '0', // Suppress DOM output that exceeds AI context windows
},
},
build: {
target: 'esnext',
rollupOptions: {
output: {
manualChunks(id) {
// Consolidate lucide icons into a single chunk instead of 60+ micro-chunks.
if (id.includes('node_modules/lucide-react')) {
return 'lucide-icons';
}
},
},
},
},
optimizeDeps: {
exclude: ['@capacitor/filesystem', '@capacitor/share'],
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
dedupe: ['react', 'react-dom', 'react/jsx-runtime'],
},
};
});