mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
refactor(app): take the base path from Vite's base config
Drop VITE_BASE_URL. vite build --base=/opencode/ (or base in the config) now drives everything: the icons plugin and service worker read the resolved base, and a base without its trailing slash gets one.
This commit is contained in:
parent
fd40bb220e
commit
770ccb4940
5 changed files with 55 additions and 32 deletions
2
packages/app/src/env.d.ts
vendored
2
packages/app/src/env.d.ts
vendored
|
|
@ -1,5 +1,5 @@
|
|||
interface ImportMetaEnv {
|
||||
/** The path the build is served under, from `VITE_BASE_URL`; `/` at the root. */
|
||||
/** Vite's `base`, the path the build is served under, such as `/opencode/`; `/` at the root. */
|
||||
readonly BASE_URL: string
|
||||
readonly VITE_OPENCODE_SERVER_MODE?: "none" | "origin"
|
||||
readonly VITE_OPENCODE_SERVER_HOST: string
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { sentryVitePlugin } from "@sentry/vite-plugin"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import { defineConfig } from "vite"
|
||||
import { defineConfig, type Plugin } from "vite"
|
||||
import desktopPlugin, { channel } from "./vite.js"
|
||||
import { icons } from "./vite.icons"
|
||||
import { serviceWorker } from "./vite.pwa"
|
||||
|
|
@ -22,15 +22,21 @@ const sentry =
|
|||
})
|
||||
: false
|
||||
|
||||
// `VITE_BASE_URL=/opencode` builds the app to be served under that path; it is the root by default.
|
||||
const base = `/${(process.env.VITE_BASE_URL ?? "").replace(/^\/+|\/+$/g, "")}/`.replace(/^\/\/$/, "/")
|
||||
// The app is built for the root by default; `vite build --base=/opencode/` serves it under that path. A base without
|
||||
// its trailing slash would reach `import.meta.env.BASE_URL` and `%BASE_URL%` as is and glue paths onto it.
|
||||
const basePath: Plugin = {
|
||||
name: "opencode-app:base-path",
|
||||
config(config) {
|
||||
if (config.base?.startsWith("/") && !config.base.endsWith("/")) return { base: `${config.base}/` }
|
||||
},
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
base,
|
||||
plugins: [
|
||||
basePath,
|
||||
desktopPlugin,
|
||||
icons(channel, base),
|
||||
serviceWorker(fileURLToPath(new URL("./dist", import.meta.url)), base),
|
||||
icons(channel),
|
||||
serviceWorker(fileURLToPath(new URL("./dist", import.meta.url))),
|
||||
sentry,
|
||||
] as any,
|
||||
server: {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ test("bundles app icons under a base path", async () => {
|
|||
logLevel: "silent",
|
||||
base: "/opencode/",
|
||||
plugins: [
|
||||
icons("prod", "/opencode/"),
|
||||
icons("prod"),
|
||||
{
|
||||
name: "icons-only-fixture",
|
||||
transformIndexHtml: {
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import { readFileSync } from "node:fs"
|
|||
import type { Plugin } from "vite"
|
||||
import manifest from "./manifest.json" with { type: "json" }
|
||||
|
||||
export function icons(channel: string, base = "/"): Plugin {
|
||||
export function icons(channel: string): Plugin {
|
||||
const selected = channel === "beta" || channel === "prod" ? channel : "dev"
|
||||
const prefix = `icons/${selected}`
|
||||
|
||||
const files = [
|
||||
const iconFiles = [
|
||||
...Object.entries({
|
||||
"favicon.ico": "icon.ico",
|
||||
"apple-touch-icon.png": "ios/AppIcon-60x60@3x.png",
|
||||
|
|
@ -17,27 +17,36 @@ export function icons(channel: string, base = "/"): Plugin {
|
|||
source: readFileSync(new URL(`../desktop/icons/${selected}/${source}`, import.meta.url)),
|
||||
type: name.endsWith(".ico") ? "image/x-icon" : "image/png",
|
||||
})),
|
||||
{
|
||||
fileName: "site.webmanifest",
|
||||
source: JSON.stringify({
|
||||
...manifest,
|
||||
id: base,
|
||||
start_url: base,
|
||||
scope: base,
|
||||
icons: manifest.icons.map((icon) => ({ ...icon, src: `${base}${prefix}${icon.src}` })),
|
||||
}),
|
||||
type: "application/manifest+json",
|
||||
},
|
||||
]
|
||||
|
||||
// Vite's resolved `base`, such as `/opencode/` for `vite build --base=/opencode/`.
|
||||
let base = "/"
|
||||
|
||||
const webManifest = () => ({
|
||||
fileName: "site.webmanifest",
|
||||
source: JSON.stringify({
|
||||
...manifest,
|
||||
id: base,
|
||||
start_url: base,
|
||||
scope: base,
|
||||
icons: manifest.icons.map((icon) => ({ ...icon, src: `${base}${prefix}${icon.src}` })),
|
||||
}),
|
||||
type: "application/manifest+json",
|
||||
})
|
||||
|
||||
const files = () => [...iconFiles, webManifest()]
|
||||
|
||||
return {
|
||||
name: "opencode-app:icons",
|
||||
configResolved(config) {
|
||||
base = config.base
|
||||
},
|
||||
generateBundle() {
|
||||
files.forEach((file) => this.emitFile({ type: "asset", fileName: file.fileName, source: file.source }))
|
||||
files().forEach((file) => this.emitFile({ type: "asset", fileName: file.fileName, source: file.source }))
|
||||
},
|
||||
configureServer(server) {
|
||||
server.middlewares.use((request, response, next) => {
|
||||
const file = files.find((file) => `${base}${file.fileName}` === request.url?.split("?")[0])
|
||||
const file = files().find((file) => `${base}${file.fileName}` === request.url?.split("?")[0])
|
||||
|
||||
if (!file) return next()
|
||||
response.setHeader("Content-Type", file.type)
|
||||
|
|
|
|||
|
|
@ -13,14 +13,28 @@ export function onDemandAsset(path: string) {
|
|||
return officeAsset.test(path)
|
||||
}
|
||||
|
||||
export function serviceWorker(directory: string, base = "/") {
|
||||
const prefix = base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
||||
|
||||
export function serviceWorker(directory: string) {
|
||||
return VitePWA({
|
||||
strategies: "generateSW",
|
||||
registerType: "prompt",
|
||||
injectRegister: false,
|
||||
manifest: false,
|
||||
integration: {
|
||||
// The fallback and denylist follow Vite's resolved `base`, such as `/opencode/` for `vite build --base=/opencode/`.
|
||||
configureOptions(config, options) {
|
||||
const prefix = config.base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
||||
|
||||
options.workbox = {
|
||||
...options.workbox,
|
||||
navigateFallback: `${config.base}index.html`,
|
||||
// Pairing links must reach the server so it can set the session cookie.
|
||||
navigateFallbackDenylist: [
|
||||
new RegExp(`^${prefix}(?:api|auth)(?:/|$)`),
|
||||
new RegExp(`^${prefix}(?:_assets|assets)(?:/|$)`),
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
workbox: {
|
||||
// Workbox runs after Sentry's upload and cleanup, so do not publish an unuploaded map.
|
||||
sourcemap: false,
|
||||
|
|
@ -29,12 +43,6 @@ export function serviceWorker(directory: string, base = "/") {
|
|||
// Keep each open tab on its complete build until all old clients close.
|
||||
skipWaiting: false,
|
||||
inlineWorkboxRuntime: true,
|
||||
navigateFallback: `${base}index.html`,
|
||||
// Pairing links must reach the server so it can set the session cookie.
|
||||
navigateFallbackDenylist: [
|
||||
new RegExp(`^${prefix}(?:api|auth)(?:/|$)`),
|
||||
new RegExp(`^${prefix}(?:_assets|assets)(?:/|$)`),
|
||||
],
|
||||
// Include lazy chunks and non-JS dependencies, not just the startup bundle.
|
||||
globPatterns: ["**/*"],
|
||||
globIgnores: ["**/*.map", "_headers", "_redirects"],
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue