-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
87 lines (79 loc) · 2.96 KB
/
Copy pathvite.config.ts
File metadata and controls
87 lines (79 loc) · 2.96 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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
function urlFetchPlugin() {
return {
name: 'url-fetch-proxy',
configureServer(server: import('vite').ViteDevServer) {
server.middlewares.use('/api/fetch-url', async (req, res) => {
const qs = req.url?.split('?')[1] ?? ''
const target = new URLSearchParams(qs).get('url')
if (!target) {
res.statusCode = 400
res.end(JSON.stringify({ error: 'missing url param' }))
return
}
try {
const r = await fetch(target, {
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; Brain/1.0; +http://localhost)' },
signal: AbortSignal.timeout(12000),
})
const html = await r.text()
const stripTags = (s: string) =>
s.replace(/<script[\s\S]*?<\/script>/gi, '')
.replace(/<style[\s\S]*?<\/style>/gi, '')
.replace(/<[^>]+>/g, ' ')
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
.replace(/ /g, ' ').replace(/'/g, "'").replace(/"/g, '"')
.replace(/\s+/g, ' ').trim()
const ogTitle = html.match(/property="og:title"\s+content="([^"]{0,200})"/i)?.[1]
?? html.match(/content="([^"]{0,200})"\s+property="og:title"/i)?.[1]
const tagTitle = html.match(/<title[^>]*>([^<]{0,200})<\/title>/i)?.[1]
const title = stripTags(ogTitle ?? tagTitle ?? '').slice(0, 200)
const mainBlock = html.match(/<(?:article|main)[^>]*>([\s\S]*?)<\/(?:article|main)>/i)?.[1]
?? html.match(/<body[^>]*>([\s\S]*?)<\/body>/i)?.[1]
?? html
const text = stripTags(mainBlock).slice(0, 8000)
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ title, text }))
} catch (e) {
res.statusCode = 500
res.end(JSON.stringify({ error: String(e) }))
}
})
},
}
}
const API_TARGET = process.env.VITE_API_BASE_URL ?? 'http://localhost:8090'
const proxyConfig = {
'/v1': { target: API_TARGET, changeOrigin: true },
'/sse': { target: API_TARGET, changeOrigin: true, ws: true },
}
export default defineConfig({
plugins: [react(), urlFetchPlugin()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@api': path.resolve(__dirname, './src/api'),
'@hooks': path.resolve(__dirname, './src/hooks'),
'@store': path.resolve(__dirname, './src/store'),
'@components': path.resolve(__dirname, './src/components'),
'@pages': path.resolve(__dirname, './src/pages'),
'@types': path.resolve(__dirname, './src/types'),
},
},
server: {
host: true,
port: 5173,
proxy: proxyConfig,
},
preview: {
host: true,
port: 5173,
proxy: proxyConfig,
},
build: {
target: 'esnext',
sourcemap: process.env.NODE_ENV !== 'production',
},
})