-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathvite.config.ts
More file actions
157 lines (144 loc) · 4.78 KB
/
Copy pathvite.config.ts
File metadata and controls
157 lines (144 loc) · 4.78 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { defineConfig, type Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import { crx } from '@crxjs/vite-plugin'
import tsconfigPaths from 'vite-tsconfig-paths'
import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import manifest from './src/manifest.config'
const HINT_MIN_LEN = 4
const HINT_MAX_COUNT = 3
const REGEX_LITERAL_SPLIT = /[\\^$.|?*+()[\]{}]/
const REGEX_CONTROL_ESCAPE = /\\[bBdDsSwW]/g
const REGEX_ESCAPE = /[.*+?^${}()|[\]\\]/g
const escapeForRegex = (value: string) => value.replace(REGEX_ESCAPE, '\\$&')
const normalizeHintCandidate = (value: string): string => value.toLowerCase().replace(/\s+/g, ' ').trim()
const extractRuleHints = (patterns: unknown, isKeyword: boolean): string[] => {
if (!Array.isArray(patterns) || !patterns.length) return []
const candidates: string[] = []
for (const pattern of patterns) {
const text = String(pattern || '')
if (!text) continue
if (isKeyword) {
const lower = text.toLowerCase().trim()
if (lower.length >= HINT_MIN_LEN) candidates.push(lower)
continue
}
for (const segment of text.replace(REGEX_CONTROL_ESCAPE, ' ').split(REGEX_LITERAL_SPLIT)) {
const lower = normalizeHintCandidate(segment)
if (lower.length >= HINT_MIN_LEN) candidates.push(lower)
}
}
return [...new Set(candidates)].sort((a, b) => b.length - a.length).slice(0, HINT_MAX_COUNT)
}
const buildKeywordCombinedSource = (patterns: unknown): string => {
if (!Array.isArray(patterns) || !patterns.length) return ''
const segments = patterns
.map(pattern => String(pattern || '').trim())
.filter(Boolean)
.map(escapeForRegex)
return segments.length ? segments.join('|') : ''
}
const isLeafRule = (node: any): boolean => Boolean(node) && typeof node === 'object' && !Array.isArray(node) && Array.isArray(node.patterns)
const precompileRuleTree = (node: any): void => {
if (!node) return
if (Array.isArray(node)) {
for (const item of node) precompileRuleTree(item)
return
}
if (typeof node !== 'object') return
if (isLeafRule(node)) {
const isKeyword = node.matchType === 'keyword'
const hints = Array.isArray(node.__hints) && node.__hints.length ? node.__hints : extractRuleHints(node.patterns, isKeyword)
if (hints.length) node.__hints = hints
if (isKeyword) {
const combined = buildKeywordCombinedSource(node.patterns)
if (combined) node.__keywordCombined = combined
}
return
}
for (const key in node) precompileRuleTree(node[key])
}
const precompileRulesPlugin = (): Plugin => ({
name: 'stackprism:precompile-rules',
apply: 'build',
closeBundle() {
const rulesDir = path.resolve(__dirname, 'dist/rules')
let dirStat
try {
dirStat = statSync(rulesDir)
} catch {
return
}
if (!dirStat.isDirectory()) return
const walk = (dir: string): string[] => {
const out: string[] = []
for (const name of readdirSync(dir)) {
const full = path.join(dir, name)
const stat = statSync(full)
if (stat.isDirectory()) out.push(...walk(full))
else if (name.endsWith('.json') && name !== 'index.json') out.push(full)
}
return out
}
for (const file of walk(rulesDir)) {
const original = readFileSync(file, 'utf8')
let parsed
try {
parsed = JSON.parse(original)
} catch {
continue
}
precompileRuleTree(parsed)
writeFileSync(file, JSON.stringify(parsed), 'utf8')
}
}
})
const minifyJsonAssets = (): Plugin => ({
name: 'stackprism:minify-json-assets',
apply: 'build',
closeBundle() {
const distDir = path.resolve(__dirname, 'dist')
const walk = (dir: string): string[] => {
const out: string[] = []
for (const name of readdirSync(dir)) {
const full = path.join(dir, name)
const stat = statSync(full)
if (stat.isDirectory()) out.push(...walk(full))
else if (name.endsWith('.json')) out.push(full)
}
return out
}
for (const file of walk(distDir)) {
const original = readFileSync(file, 'utf8')
let parsed
try {
parsed = JSON.parse(original)
} catch {
continue
}
const minified = JSON.stringify(parsed)
if (minified.length < original.length) {
writeFileSync(file, minified, 'utf8')
}
}
}
})
export default defineConfig({
plugins: [vue(), crx({ manifest }), tsconfigPaths(), precompileRulesPlugin(), minifyJsonAssets()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
vue: 'vue/dist/vue.runtime.esm-bundler.js'
}
},
publicDir: 'public',
build: {
outDir: 'dist',
emptyOutDir: true,
rollupOptions: {
input: {
help: path.resolve(__dirname, 'src/ui/help/index.html')
}
}
}
})