newsnow/vite.config.ts

114 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-09-26 19:16:42 +08:00
import process from "node:process"
2024-10-03 23:11:10 +08:00
import { join } from "node:path"
2024-09-26 11:30:21 +08:00
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react-swc"
import nitro from "vite-plugin-with-nitro"
import { TanStackRouterVite } from "@tanstack/router-plugin/vite"
import tsconfigPath from "vite-tsconfig-paths"
2024-09-26 19:16:42 +08:00
import unocss from "unocss/vite"
2024-10-14 00:02:58 +08:00
import dotenv from "dotenv"
2024-10-20 01:30:10 +08:00
import type { VitePWAOptions } from "vite-plugin-pwa"
import { VitePWA } from "vite-plugin-pwa"
2024-10-03 23:11:10 +08:00
import { projectDir } from "./shared/dir"
2024-10-14 00:02:58 +08:00
dotenv.config({
2024-10-14 17:18:57 +08:00
path: join(projectDir, ".env.server"),
2024-10-14 00:02:58 +08:00
})
2024-10-20 01:30:10 +08:00
const pwaOption: Partial<VitePWAOptions> = {
includeAssets: ["icon.svg", "apple-touch-icon.png"],
manifest: {
name: "NewsNow",
short_name: "NewsNow",
description: "Elegant reading of real-time and hottest news",
theme_color: "#F14D42",
icons: [
{
src: "pwa-192x192.png",
sizes: "192x192",
type: "image/png",
},
{
src: "pwa-512x512.png",
sizes: "512x512",
type: "image/png",
},
{
src: "pwa-512x512.png",
sizes: "512x512",
type: "image/png",
purpose: "any",
},
{
src: "pwa-512x512.png",
sizes: "512x512",
type: "image/png",
purpose: "maskable",
},
],
},
devOptions: {
enabled: process.env.SW_DEV === "true",
type: "module",
navigateFallback: "index.html",
},
}
2024-10-24 02:17:47 +08:00
const nitroOption: Parameters<typeof nitro>[0] = {
experimental: {
database: true,
},
sourceMap: false,
database: {
default: {
connector: "sqlite",
},
},
2024-10-24 19:29:44 +08:00
preset: "node-server",
2024-10-24 02:17:47 +08:00
alias: {
"@shared": join(projectDir, "shared"),
"#": join(projectDir, "server"),
},
}
2024-10-24 19:29:44 +08:00
if (process.env.VERCEL) {
2024-10-24 02:17:47 +08:00
nitroOption.preset = "vercel-edge"
// You can use other online database, do it yourself. For more info: https://db0.unjs.io/connectors
nitroOption.database = undefined
2024-10-24 19:29:44 +08:00
} else if (process.env.CF_PAGES) {
2024-10-24 02:27:29 +08:00
nitroOption.preset = "cloudflare-pages"
2024-10-24 02:17:47 +08:00
nitroOption.database = {
default: {
connector: "cloudflare-d1",
options: {
bindingName: "NEWSNOW_DB",
},
},
}
2024-10-24 19:29:44 +08:00
} else if (process.env.BUN) {
nitroOption.preset = "bun"
nitroOption.database = {
default: {
connector: "bun-sqlite",
},
}
2024-10-24 02:17:47 +08:00
}
2024-09-26 11:30:21 +08:00
export default defineConfig({
2024-10-14 00:02:58 +08:00
define: {
__G_CLIENT_ID__: `"${process.env.G_CLIENT_ID}"`,
2024-10-14 17:18:57 +08:00
__ENABLE_LOGIN__: ["JWT_SECRET", "G_CLIENT_ID", "G_CLIENT_SECRET"].every(k => process.env[k]),
2024-10-14 00:02:58 +08:00
},
2024-09-26 11:30:21 +08:00
plugins: [
2024-10-15 17:57:22 +08:00
tsconfigPath(),
2024-09-26 11:30:21 +08:00
TanStackRouterVite({
autoCodeSplitting: true,
}),
2024-09-26 19:16:42 +08:00
unocss(),
2024-09-26 11:30:21 +08:00
react(),
2024-10-20 01:30:10 +08:00
VitePWA(pwaOption),
2024-10-24 02:17:47 +08:00
nitro(nitroOption),
2024-09-26 11:30:21 +08:00
],
})