-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.server.config.ts
88 lines (86 loc) · 2.94 KB
/
vite.server.config.ts
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
import Ajv from "ajv";
import standaloneCode from "ajv/dist/standalone";
import { chmod, cp, readFile, stat, writeFile } from "fs/promises";
import { builtinModules } from "module";
import { resolve } from "path";
import tjs from "typescript-json-schema";
import { fileURLToPath } from "url";
import { defineConfig } from "vite";
import { version } from "./package.json";
const validatorFile = fileURLToPath(new URL("./src/server/validator.ts", import.meta.url));
const configFile = fileURLToPath(new URL("./src/server/config.ts", import.meta.url));
let serverConfigSchema = "";
export default defineConfig(async () => ({
resolve: {
browserField: false,
alias: {
ws: fileURLToPath(new URL("./node_modules/ws/wrapper.mjs", import.meta.url)),
},
},
define: {
"process.env.WS_NO_BUFFER_UTIL": "true",
"import.meta.env.VERSION": JSON.stringify(version),
},
build: {
emptyOutDir: false,
target: "node20",
outDir: "./dist",
rollupOptions: {
external: [...builtinModules, "vite"],
output: {
banner: (chunk) => (chunk.isEntry ? "#!/usr/bin/env node\n" : ""),
},
},
lib: {
entry: {
"obs-webrtc-server": "src/server/main.ts",
},
fileName: (format, entryName) => entryName,
formats: ["cjs"],
},
},
plugins: [
{
name: "schema",
enforce: "pre",
resolveId(id, source) {
if (source && resolve(source, "..", id) + ".ts" === validatorFile) {
return { id: validatorFile, external: false };
}
},
async load(id) {
if (id === validatorFile) {
const program = tjs.programFromConfig(fileURLToPath(new URL("./tsconfig.json", import.meta.url)), [configFile]);
const schema = tjs.generateSchema(program, "ServerConfig", { noExtraProps: true, required: true });
serverConfigSchema = JSON.stringify(schema);
const ajv = new Ajv({ code: { source: true, esm: true } });
const validate = ajv.compile(schema!);
const moduleCode = standaloneCode(ajv, validate);
return moduleCode;
}
},
async closeBundle() {
await writeFile(new URL("./dist/schema.json", import.meta.url), serverConfigSchema);
},
},
{
name: "files",
async closeBundle() {
console.log("Copying files...");
await cp(new URL("./README.md", import.meta.url), new URL("./dist/README.md", import.meta.url));
await cp(new URL("./LICENSE", import.meta.url), new URL("./dist/LICENSE", import.meta.url));
const pkgJson = JSON.parse(await readFile(new URL("./package.json", import.meta.url), "utf8"));
delete pkgJson.devDependencies;
delete pkgJson.scripts;
delete pkgJson.private;
delete pkgJson.packageManager;
delete pkgJson.type;
const binFile = new URL(`./dist/obs-webrtc-server`, import.meta.url);
const fileInfo = await stat(binFile);
await chmod(binFile, fileInfo.mode | ((fileInfo.mode & 0o444) >> 2)); // chmod +x
await writeFile(new URL("./dist/package.json", import.meta.url), JSON.stringify(pkgJson));
console.log("Finished copying files!");
},
},
],
}));