-
-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathvite.config.ts
More file actions
113 lines (104 loc) · 3.3 KB
/
Copy pathvite.config.ts
File metadata and controls
113 lines (104 loc) · 3.3 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
import { execSync } from "node:child_process";
import { readFile } from "node:fs/promises";
import { reactRouter } from "@react-router/dev/vite";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "vite";
import { parse } from "yaml";
import { headplaneDevServer } from "./runtime/vite-plugin";
const PROD_ENTRY = "./app/server/main.ts";
const DEV_ENTRY = "./app/server/app.ts";
const REACT_ROUTER_SSR_NO_EXTERNAL = ["@react-router/node", "react-router"];
const PREFIX = process.env.__INTERNAL_PREFIX || "/admin";
if (PREFIX.endsWith("/")) {
throw new Error("Prefix must not end with a slash");
}
// Derive version: HEADPLANE_VERSION env > git describe > package.json
const isNext = process.env.IMAGE_TAG?.includes("next");
let VERSION: string;
if (process.env.HEADPLANE_VERSION) {
VERSION = process.env.HEADPLANE_VERSION;
} else {
try {
const describe = execSync("git describe --tags", { encoding: "utf-8" })
.trim()
.replace(/^v/, "");
const tag = execSync("git describe --tags --abbrev=0", { encoding: "utf-8" })
.trim()
.replace(/^v/, "");
VERSION = describe === tag ? tag : `${tag}-dev+${describe.split("-").pop()}`;
} catch {
const pkg = await readFile("package.json", "utf-8");
VERSION = JSON.parse(pkg).version;
}
}
if (!VERSION) {
throw new Error("Unable to determine version");
}
// Load the config without any environment variables (not needed here)
const config = await readFile("config.example.yaml", "utf-8");
const { server } = parse(config);
export default defineConfig(({ command }) => {
const ssrNoExternal = command === "build" ? true : REACT_ROUTER_SSR_NO_EXTERNAL;
return {
base: command === "build" ? `${PREFIX}/` : undefined,
plugins: [
headplaneDevServer({
entry: DEV_ENTRY,
basename: PREFIX,
publicDir: new URL("./public", import.meta.url).pathname,
}),
reactRouter(),
tailwindcss(),
],
server: {
host: server.host,
port: server.port,
},
resolve: {
tsconfigPaths: true,
},
build: {
target: "baseline-widely-available",
sourcemap: true,
},
environments: {
client: {
build: {
rollupOptions:
command === "build"
? {
// Exclude WASM from the client since it fetches from the server
external: [/\.wasm(\?url)?$/],
}
: undefined,
},
},
ssr: {
resolve: {
noExternal: ssrNoExternal,
},
build: {
rollupOptions:
command === "build"
? {
// Override the SSR build entry so React Router emits the
// production bootstrap (`app/server/main.ts`) as
// `build/server/index.js`. It transitively imports the
// SSR entry, which pulls in the React Router server build
// via the virtual module `virtual:react-router/server-build`.
input: PROD_ENTRY,
external: [],
}
: undefined,
},
},
},
ssr: {
noExternal: ssrNoExternal,
},
define: {
__VERSION__: JSON.stringify(isNext ? `${VERSION}-next` : VERSION),
__PREFIX__: JSON.stringify(PREFIX),
},
};
});