-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathvite.config.ts
More file actions
149 lines (138 loc) · 4.8 KB
/
Copy pathvite.config.ts
File metadata and controls
149 lines (138 loc) · 4.8 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
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'
import { buildRuleHintFields } from './src/utils/rule-hints'
const REGEX_ESCAPE = /[.*+?^${}()|[\]\\]/g
const escapeForRegex = (value: string) => value.replace(REGEX_ESCAPE, '\\$&')
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 isPlainNode = (node: any): boolean => Boolean(node) && typeof node === 'object' && !Array.isArray(node)
const isLeafRule = (node: any): boolean => isPlainNode(node) && Array.isArray(node.patterns)
const isRuleGroup = (node: any): boolean => isPlainNode(node) && Array.isArray(node.rules)
// 规则组的 defaults 会被展开到每条规则上(matchType、resourceHints 常写在 defaults 里),预编译时按展开后的字段判断
const precompileRuleTree = (node: any, inherited: Record<string, unknown> = {}): void => {
if (!node) return
if (Array.isArray(node)) {
for (const item of node) precompileRuleTree(item, inherited)
return
}
if (!isPlainNode(node)) return
if (isRuleGroup(node)) {
const defaults = {
...inherited,
...(isPlainNode(node.defaults) ? node.defaults : {}),
...(isPlainNode(node.$defaults) ? node.$defaults : {})
}
for (const item of node.rules) precompileRuleTree(item, defaults)
return
}
if (isLeafRule(node)) {
const rule = { ...inherited, ...node }
const isKeyword = rule.matchType === 'keyword'
const { hints, legacyHints } = buildRuleHintFields(node.patterns, isKeyword, node.matchType === 'keyword', node.__hints)
if (hints.length) node.__hints = hints
else delete node.__hints
// 旧版带 resourceHints 的规则不看自动 hint,旧门槛只留给没有 resourceHints 的规则
if (legacyHints.length && !(Array.isArray(rule.resourceHints) && rule.resourceHints.length)) node.__legacyHints = legacyHints
if (node.matchType === 'keyword') {
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')
}
}
}
})