82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
import { defineConfig, loadEnv } from 'vite'
|
|
import { mkdirSync, writeFileSync } from 'node:fs'
|
|
import { fileURLToPath, URL } from 'node:url'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import tailwindcss from '@tailwindcss/vite'
|
|
|
|
const publicRoutes = [
|
|
'/',
|
|
'/gallery',
|
|
'/community',
|
|
]
|
|
|
|
const DEFAULT_SITE_URL = 'http://localhost:5173'
|
|
|
|
function getSiteUrl(value?: string) {
|
|
const siteUrl = new URL(value?.trim() || DEFAULT_SITE_URL)
|
|
if (!['http:', 'https:'].includes(siteUrl.protocol)) {
|
|
throw new Error('VITE_SITE_URL 必须使用 http 或 https 协议')
|
|
}
|
|
return siteUrl.href.replace(/\/$/, '')
|
|
}
|
|
|
|
function seoFilesPlugin(siteUrl: string) {
|
|
return {
|
|
name: 'opencloud-seo-files',
|
|
closeBundle() {
|
|
mkdirSync('dist/.well-known', { recursive: true })
|
|
|
|
const robots = [
|
|
'User-agent: *',
|
|
'Allow: /',
|
|
'Disallow: /admin',
|
|
'Disallow: /auth/',
|
|
'Disallow: /login',
|
|
'Disallow: /register',
|
|
'Disallow: /upload',
|
|
'Disallow: /encyclopedia',
|
|
'Disallow: /profile/settings',
|
|
`Sitemap: ${siteUrl}/sitemap.xml`,
|
|
'',
|
|
].join('\n')
|
|
|
|
const sitemapUrls = publicRoutes
|
|
.map(route => ` <url>\n <loc>${siteUrl}${route}</loc>\n </url>`)
|
|
.join('\n')
|
|
|
|
const sitemap = [
|
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
sitemapUrls,
|
|
'</urlset>',
|
|
'',
|
|
].join('\n')
|
|
|
|
writeFileSync('dist/robots.txt', robots)
|
|
writeFileSync('dist/sitemap.xml', sitemap)
|
|
},
|
|
}
|
|
}
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), 'VITE_')
|
|
const siteUrl = getSiteUrl(env.VITE_SITE_URL)
|
|
|
|
return {
|
|
plugins: [vue(), tailwindcss(), seoFilesPlugin(siteUrl)],
|
|
server: {
|
|
port: 5173,
|
|
strictPort: true,
|
|
},
|
|
preview: {
|
|
port: 5173,
|
|
strictPort: true,
|
|
},
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
},
|
|
},
|
|
}
|
|
})
|