-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.mjs
More file actions
98 lines (92 loc) · 2.93 KB
/
vite.config.mjs
File metadata and controls
98 lines (92 loc) · 2.93 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
/**
* Vite Configuration for OpenCut CEP Panel
*
* Currently serves as a minifier and CEP packaging helper.
* The panel runs inside CEP's embedded Chromium (~v57+),
* so the build preserves classic runtime scripts instead of
* forcing them through module bundling.
*
* Usage:
* npm run build -> produces client/dist/
* npm run dev -> watch mode for development
*
* Note: The CEP panel currently loads client/index.html directly.
* To use the built output, update CSXS/manifest.xml to point to
* client/dist/index.html instead.
*/
import { existsSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { defineConfig } from "vite";
const clientRoot = resolve(__dirname, "client");
const classicScriptPlaceholders = ["CSInterface.js", "main.js"];
function preserveCepClassicScripts() {
return {
name: "preserve-cep-classic-scripts",
transformIndexHtml: {
order: "pre",
handler(html) {
return html.replace(
/<script\s+src="(CSInterface\.js|main\.js)"><\/script>/g,
(_, src) => `<!-- OPENCUT_CLASSIC_SCRIPT:${src} -->`,
);
},
},
generateBundle(_, bundle) {
classicScriptPlaceholders.forEach((fileName) => {
if (bundle[fileName]) return;
this.emitFile({
type: "asset",
fileName,
source: readFileSync(resolve(clientRoot, fileName), "utf8"),
});
});
},
writeBundle() {
const outDir = resolve(__dirname, "client/dist");
const indexPath = resolve(outDir, "index.html");
if (existsSync(indexPath)) {
const html = readFileSync(indexPath, "utf8").replace(
/<!-- OPENCUT_CLASSIC_SCRIPT:([^>]+?) -->/g,
'<script src="$1"></script>',
);
writeFileSync(indexPath, html, "utf8");
}
classicScriptPlaceholders.forEach((fileName) => {
const sourcePath = resolve(clientRoot, fileName);
const targetPath = resolve(outDir, fileName);
writeFileSync(targetPath, readFileSync(sourcePath, "utf8"), "utf8");
});
},
};
}
export default defineConfig({
root: clientRoot,
plugins: [preserveCepClassicScripts()],
build: {
outDir: resolve(__dirname, "client/dist"),
emptyOutDir: true,
// No code splitting — CEP panel is a single-page app
rollupOptions: {
input: resolve(__dirname, "client/index.html"),
output: {
// Keep filenames predictable (no hashes) for CEP
entryFileNames: "main.js",
chunkFileNames: "[name].js",
assetFileNames: "[name].[ext]",
},
},
// Generate source maps for debugging in CEP DevTools
sourcemap: true,
// Minify for production, but keep it readable for debugging
minify: "terser",
terserOptions: {
compress: {
drop_console: false, // Keep console.log for debugging
},
},
},
// No dev server needed — CEP has its own Chromium
server: {
open: false,
},
});