-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
vite.config.js
91 lines (87 loc) · 2.64 KB
/
vite.config.js
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
import path from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue2';
import { VitePWA } from 'vite-plugin-pwa';
export default defineConfig(({ mode }) => {
const PRODUCTION = mode === 'production';
const CSP = PRODUCTION ? '' : '*:5600 *:5666 ws://*:27180';
// Sets the CSP
const setCsp = () => {
return {
name: 'html-transform',
transformIndexHtml(html) {
const pattern = '<%= htmlWebpackPlugin.options.templateParameters.cspDefaultSrc %>';
// check if the pattern exists in the html, if not, throw error
if (!html.includes(pattern)) {
throw new Error(`Could not find pattern ${pattern} in the html file`);
}
return html.replace(pattern, CSP);
},
};
};
// Auto-injects /src/main.js into index.html on a new line after the one which has VITE_AUTOINJECT
const autoInject = () => {
return {
name: 'html-transform',
transformIndexHtml: {
order: 'pre',
handler(html) {
const pattern = /<!--.*VITE_AUTOINJECT.*-->/;
// check if the pattern exists in the html, if not, throw error
if (!pattern.test(html)) {
throw new Error(`Could not find pattern ${pattern} in the html file`);
}
return html.replace(
pattern,
'<!-- Vite injected! --><script type="module" src="/src/main.js"></script>'
);
},
},
};
};
// Return the configuration
return {
plugins: [
setCsp(),
autoInject(),
vue(),
VitePWA({
devOptions: {
enabled: true,
},
manifest: {
name: 'ActivityWatch',
short_name: 'ActivityWatch',
description: 'Automatically track your computer usage',
theme_color: '#ffffff',
icons: [
{
src: 'logo.png',
sizes: '512x512',
type: 'image/png',
},
],
},
}),
],
server: {
port: 27180,
// TODO: Fix this.
// Breaks a bunch of style-related stuff etc.
// We'd need to move in the entire CSP config in here (not just the default-src) if we want to use this.
//headers: {
// 'Content-Security-Policy': PRODUCTION ? "default-src 'self'" : "default-src 'self' *:5666",
//},
},
publicDir: './static',
resolve: {
alias: { '~': path.resolve(__dirname, 'src') },
},
define: {
PRODUCTION,
AW_SERVER_URL: process.env.AW_SERVER_URL,
COMMIT_HASH: process.env.COMMIT_HASH,
'process.env.VUE_APP_ON_ANDROID': process.env.VUE_APP_ON_ANDROID,
},
};
});