opencode/packages/data/app/vite.config.ts

58 lines
2.4 KiB
TypeScript

import { readFile, rm, writeFile } from "node:fs/promises"
import path from "node:path"
import { solidStart } from "@solidjs/start/config"
import { nitro } from "nitro/vite"
import { defineConfig, type PluginOption } from "vite"
import { enabledDataFlags } from "./src/lib/flags"
import { stageHeaders } from "./src/lib/noindex"
export default defineConfig({
base: "/data/",
// The feature flags this build turns on (src/lib/flags.ts), for dataFlag() in the app.
define: { "import.meta.env.DATA_FLAGS": JSON.stringify(enabledDataFlags().join(",")) },
plugins: [
solidStart({ middleware: "./src/middleware.ts" }) as PluginOption,
nitro({
// The Worker owns opencode.ai/data/* directly, so static assets are uploaded under /data like the pages that
// reference them. Nitro's baseURL would do this too, but its router would then stop serving /<locale>/data.
output: { publicDir: "{{ output.dir }}/public/data" },
compatibilityDate: "2024-09-19",
preset: "cloudflare-module",
cloudflare: {
nodeCompat: true,
},
}),
{
// Cloudflare only reads _headers at the assets root, but Nitro writes it into publicDir with unprefixed paths.
name: "stats:headers",
buildApp: {
order: "post",
async handler() {
const source = path.join(import.meta.dirname, ".output/public/data/_headers")
// The stage's headers for the assets the Worker never sees (src/lib/noindex.ts).
const stage = Object.entries(stageHeaders()).map(([name, value]) => ` ${name}: ${value}\n`)
await writeFile(
path.join(import.meta.dirname, ".output/public/_headers"),
(await readFile(source, "utf8")).replace(/^\//gm, "/data/") +
(stage.length ? `\n/data/*\n${stage.join("")}` : ""),
)
await rm(source)
},
},
},
],
server: {
allowedHosts: true,
},
build: {
rollupOptions: {
external: ["cloudflare:workers"],
},
minify: "esbuild",
cssMinify: true,
// Browsers with native light-dark(), as services/site targets. For older targets the minifier lowers light-dark()
// to --lightningcss-light/--lightningcss-dark variables set only where a rule declares color-scheme, and the shared
// nav menus (packages/nav-menus), which set theirs inline, lost every colour.
cssTarget: ["chrome123", "edge123", "firefox120", "safari17.5"],
},
})