-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvite.config.renderer.mjs
128 lines (123 loc) · 3.76 KB
/
vite.config.renderer.mjs
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
import react from "@vitejs/plugin-react";
import merge from "lodash.merge";
import mimeTypes from "mime-types";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { ViteEjsPlugin } from "vite-plugin-ejs";
import svgrPlugin from "vite-plugin-svgr";
import { createEJSContext, ENVIRONMENT, getCommonViteConfig, getElectronVendorsCache, PROJECT_HOME, sourceEnv, sourcemap } from "./vite.config.common.mjs";
import pkg from "./package.json";
export function docsServer() {
return {
apply: "serve",
configureServer(server) {
return () => {
server.middlewares.use(async (req, res, next) => {
if (req.originalUrl?.startsWith("/VERSION") || req.originalUrl?.startsWith(`/VERSION-${os.type()}`)) {
res.setHeader("Content-Type", "text/plain");
res.writeHead(200);
res.write(pkg.version);
res.end();
} else if (req.originalUrl?.includes("/docs")) {
let resource = path.join(__dirname, `${req.originalUrl}`);
if (fs.lstatSync(resource).isDirectory()) {
resource = path.join(resource, "index.html");
res.setHeader("Content-Type", mimeTypes.lookup(resource) || "application/octet-stream");
res.setHeader("Location", "/docs/index.html");
res.writeHead(301);
} else {
if (fs.existsSync(resource)) {
res.setHeader("Content-Type", mimeTypes.lookup(resource) || "application/octet-stream");
res.writeHead(200);
res.write(fs.readFileSync(resource));
} else {
console.error(`Resource not found: ${resource}`);
res.setHeader("Content-Type", "text/plain");
res.writeHead(404);
res.write("Resource not found");
}
}
res.end();
}
next();
});
};
},
name: "docs-server"
};
}
/**
* @type {import('vite').UserConfig}
* @see https://vitejs.dev/config/
*/
export const createConfig = ({ mode, command, host, port }) => {
// Bootstrap
sourceEnv(ENVIRONMENT);
const cache = getElectronVendorsCache();
console.debug({ PROJECT_HOME, command, host, port });
console.debug(`Website running at http://${host === "0.0.0.0" ? "localhost" : host}:${port}/docs/index.html`);
// Bootstrap
// Build context
const ejsContext = createEJSContext();
// vite main
const viteConfig = getCommonViteConfig({
mode,
command,
// app specific
// outputName: APP_MAIN,
plugins: [
// All current plugins are extending the Vite configuration
// Frontend specific
react(),
svgrPlugin(),
ViteEjsPlugin(ejsContext)
],
publicDir: "./public",
build: {
target: `chrome${cache.chrome}`,
sourcemap: sourcemap,
emptyOutDir: false
},
rollupOptions: {
input: path.join(PROJECT_HOME, "index.html")
},
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis"
}
}
},
outputName: "renderer"
});
return viteConfig;
};
/**
* @type {import('vite').UserConfig}
* @see https://vitejs.dev/config/
*/
export default ({ mode, command }) => {
let host = process.env.HOST || "0.0.0.0";
const port = Number(process.env.PORT) || 3000;
const baseConfig = createConfig({ mode, command, host, port });
baseConfig.plugins.push(docsServer());
const config = merge(baseConfig, {
clearScreen: false,
root: PROJECT_HOME,
envDir: PROJECT_HOME,
server: {
host,
port,
strictPort: true,
cors: true,
watch: {},
fs: {
strict: true
}
}
});
config.base = "";
config.build.rollupOptions.external = ["electron"];
return config;
};