-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvite.config.mjs
More file actions
152 lines (137 loc) · 4.88 KB
/
Copy pathvite.config.mjs
File metadata and controls
152 lines (137 loc) · 4.88 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
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { syncManifestVersions } from './scripts/sync-version.mjs'
const rootDir = path.dirname(fileURLToPath(import.meta.url))
const srcDir = path.resolve(rootDir, 'src')
const buildDir = path.resolve(rootDir, 'build')
const legacyClassicScript = path.resolve(srcDir, 'freshContent/starRating.js')
function walkFiles(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true })
return entries.flatMap((entry) => {
const fullPath = path.join(dir, entry.name)
return entry.isDirectory() ? walkFiles(fullPath) : [fullPath]
})
}
function toPosix(filePath) {
return filePath.split(path.sep).join('/')
}
function withoutExt(filePath) {
return filePath.slice(0, -path.extname(filePath).length)
}
function buildInputs() {
return Object.fromEntries(
walkFiles(srcDir)
.filter((file) => {
if (file.endsWith('.d.ts')) return false
if (path.resolve(file) === legacyClassicScript) return false
if (/\.(html|js|scss|ts)$/.test(file)) return true
return /\.sass$/.test(file) && !path.basename(file).startsWith('_')
})
.map((file) => {
const relativePath = toPosix(path.relative(srcDir, file))
return [file.endsWith('.html') ? relativePath : withoutExt(relativePath), file]
})
)
}
function copyStaticExtensionFiles() {
return {
name: 'copy-static-extension-files',
writeBundle() {
const manifestNames = new Set(['manifest.json', 'manifest.chrome.json', 'manifest.firefox.json'])
for (const file of walkFiles(srcDir)) {
const relativePath = path.relative(srcDir, file)
if (path.resolve(file) === legacyClassicScript) {
const target = path.join(buildDir, relativePath)
fs.mkdirSync(path.dirname(target), { recursive: true })
fs.copyFileSync(file, target)
continue
}
if (/\.(html|js|sass|scss|ts|vue)$/.test(relativePath) || relativePath.endsWith('.d.ts')) continue
const target = path.join(buildDir, relativePath)
fs.mkdirSync(path.dirname(target), { recursive: true })
if (manifestNames.has(path.basename(relativePath))) {
const source = fs.readFileSync(file, 'utf8')
try {
const obj = JSON.parse(source)
if (obj._comment) delete obj._comment
fs.writeFileSync(target, JSON.stringify(obj, null, 2) + '\n')
continue
} catch (err) {
// fallback: copy raw file
fs.copyFileSync(file, target)
continue
}
}
fs.copyFileSync(file, target)
}
}
}
}
function injectManifestVersions() {
return {
name: 'inject-manifest-versions',
writeBundle() {
syncManifestVersions({ buildDir })
}
}
}
function keepContentScriptsClassic() {
const shouldRewrite = (fileName) =>
fileName.startsWith('contentScripts/') &&
!fileName.startsWith('contentScripts/other/hisqis/gradeChart') &&
!fileName.startsWith('contentScripts/other/hisqis/newTable')
const rewrite = (code) => {
const helperMatch = code.match(/^import\{t as ([\w$]+)\}from"[^"]*vite\/pkg\/preload-helper\.js";/)
const withoutHelperImport = helperMatch ? code.slice(helperMatch[0].length) : code
return withoutHelperImport.replace(
/\b[\w$]+\(\(\)=>import\((chrome\.runtime\.getURL\([^)]*\))\),\[\]\)/g,
'import($1)'
)
}
return {
name: 'keep-content-scripts-classic',
renderChunk(code, chunk) {
if (!shouldRewrite(chunk.fileName)) return null
return { code: rewrite(code), map: null }
},
generateBundle(_options, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && shouldRewrite(chunk.fileName)) chunk.code = rewrite(chunk.code)
}
},
writeBundle() {
for (const file of walkFiles(path.join(buildDir, 'contentScripts'))) {
const relativePath = toPosix(path.relative(buildDir, file))
if (!shouldRewrite(relativePath)) continue
const code = fs.readFileSync(file, 'utf8')
const rewritten = rewrite(code)
if (rewritten !== code) fs.writeFileSync(file, rewritten)
}
}
}
}
export default defineConfig({
root: srcDir,
publicDir: false,
plugins: [vue(), copyStaticExtensionFiles(), injectManifestVersions(), keepContentScriptsClassic()],
build: {
outDir: buildDir,
emptyOutDir: true,
modulePreload: false,
rollupOptions: {
input: buildInputs(),
preserveEntrySignatures: 'strict',
output: {
entryFileNames: '[name].js',
chunkFileNames: 'vite/pkg/[name].js',
assetFileNames: (assetInfo) => {
if (assetInfo.name && assetInfo.name.endsWith('.css')) return '[name][extname]'
return 'assets/[name][extname]'
}
}
}
}
})