-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvite.config.ts
More file actions
71 lines (68 loc) · 1.96 KB
/
Copy pathvite.config.ts
File metadata and controls
71 lines (68 loc) · 1.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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import path from 'path';
import { componentTagger } from 'lovable-tagger';
import { execSync } from 'child_process';
import { VitePWA } from 'vite-plugin-pwa';
import fs from 'fs';
import { USERSCRIPT_VERSION } from './src/version';
let commitHash = 'dev';
let commitDate = 'dev';
try {
commitHash = execSync('git rev-parse --short HEAD').toString().trim();
commitDate = execSync('git show -s --date=format:%y%m%d --format=%cd')
.toString()
.trim();
} catch (error) {
console.warn('Git not available, using default values for commit info');
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const envBase = process.env.BASE_URL;
const base = envBase && envBase.startsWith('/') ? envBase : '/';
return {
server: {
host: '::',
port: 8080,
},
base,
plugins: [
react(),
VitePWA({
srcDir: 'public',
filename: 'sw.js',
strategies: 'injectManifest',
injectRegister: false,
includeManifestIcons: false,
}),
mode === 'development' && componentTagger(),
{
name: 'inject-userscript-meta',
apply: 'build',
writeBundle() {
const file = path.resolve(__dirname, 'dist/sora-userscript.user.js');
if (fs.existsSync(file)) {
let code = fs.readFileSync(file, 'utf8');
code = code
.replace(/__USERSCRIPT_VERSION__/g, USERSCRIPT_VERSION)
.replace(
/const DEBUG = false;/,
`const DEBUG = ${mode === 'development' ? 'true' : 'false'};`,
);
fs.writeFileSync(file, code);
}
},
},
].filter(Boolean),
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
define: {
'import.meta.env.VITE_COMMIT_HASH': JSON.stringify(commitHash),
'import.meta.env.VITE_COMMIT_DATE': JSON.stringify(commitDate),
__BASE_URL__: JSON.stringify(base),
},
};
});