-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvite.main.config.ts
More file actions
125 lines (109 loc) · 4.45 KB
/
Copy pathvite.main.config.ts
File metadata and controls
125 lines (109 loc) · 4.45 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
import { defineConfig } from "vite";
import path from "path";
import fs from "fs";
import ffmpegFfprobeStatic from "ffmpeg-ffprobe-static";
import { createBrandDefines } from "./scripts/branding";
// Packages to exclude from bundle and load directly from node_modules
const external_packages = ["jsdom", "mulmocast-vision", "puppeteer", "puppeteer-core"];
// Additional packages to copy (e.g., nested dependencies not auto-detected)
const additional_packages = ["punycode"];
const isDev = process.env.NODE_ENV === "development";
const brandDefines = createBrandDefines();
// https://vitejs.dev/config
export default defineConfig({
define: brandDefines,
build: {
rolldownOptions: {
external: external_packages,
platform: "node",
},
},
plugins: [
{
name: "copy-ffmpeg-after-build",
apply: "build",
closeBundle() {
if (isDev) return;
const destDir = path.resolve(__dirname, ".vite/build/ffmpeg");
fs.mkdirSync(destDir, { recursive: true });
const ffmpegBinary = path.basename(ffmpegFfprobeStatic.ffmpegPath);
const ffprobeBinary = path.basename(ffmpegFfprobeStatic.ffprobePath);
fs.copyFileSync(ffmpegFfprobeStatic.ffmpegPath, path.join(destDir, ffmpegBinary));
fs.copyFileSync(ffmpegFfprobeStatic.ffprobePath, path.join(destDir, ffprobeBinary));
console.log("✅ ffmpeg and ffprobe copied by Vite plugin");
},
},
{
name: "copy-splash-html",
apply: "build",
closeBundle() {
if (isDev) return;
// Copy splash.html
const srcPath = path.resolve(__dirname, "splash.html");
const destPath = path.resolve(__dirname, ".vite/build/splash.html");
fs.copyFileSync(srcPath, destPath);
// Ensure splash image exists alongside splash.html path expectations
const imgSrc = path.resolve(__dirname, "images/mulmocast_credit.png");
const imgDir = path.resolve(__dirname, ".vite/build/images");
const imgDest = path.join(imgDir, "mulmocast_credit.png");
try {
fs.mkdirSync(imgDir, { recursive: true });
if (fs.existsSync(imgSrc)) {
fs.copyFileSync(imgSrc, imgDest);
console.log("✅ splash image copied to build/images");
} else {
console.warn("⚠️ splash image not found:", imgSrc);
}
} catch (e) {
console.warn("⚠️ failed to copy splash image:", e);
}
console.log("✅ splash.html copied to build directory");
},
},
{
name: "copy-external-node-modules",
apply: "build",
closeBundle() {
if (isDev) return;
const buildNodeModules = path.resolve(__dirname, ".vite/build/node_modules");
fs.mkdirSync(buildNodeModules, { recursive: true });
// Root packages to include (externalized in main bundle)
const roots = external_packages;
const visited = new Set<string>();
const pkgRoot = (name: string) => path.resolve(__dirname, `node_modules/${name}`);
const pkgJsonPath = (name: string) => path.join(pkgRoot(name), "package.json");
const copyPackage = (name: string) => {
const src = pkgRoot(name);
const dest = path.join(buildNodeModules, name);
if (!fs.existsSync(src)) {
console.warn(`⚠️ ${name} not found in root node_modules`);
return;
}
// Ensure parent dir exists for scoped packages
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.cpSync(src, dest, { recursive: true });
console.log(`✅ copied ${name}`);
};
const walk = (name: string) => {
if (visited.has(name)) return;
visited.add(name);
try {
const jsonPath = pkgJsonPath(name);
const json = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
copyPackage(name);
const deps = Object.assign({}, json.dependencies || {}, json.optionalDependencies || {});
for (const depName of Object.keys(deps)) {
// Skip builtins and electron
if (/^(electron|node:|fs|path|crypto|stream|events|url|buffer)$/.test(depName)) continue;
walk(depName);
}
} catch (e) {
console.warn(`⚠️ Failed to process ${name}: ${String((e as any)?.message || e)}`);
}
};
roots.forEach(walk);
additional_packages.forEach(walk);
},
},
],
});