-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
166 lines (154 loc) · 4.69 KB
/
vite.config.ts
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
158
159
160
161
162
163
164
165
166
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import react from '@vitejs/plugin-react';
import { defineConfig, loadEnv } from 'vite';
import checker from 'vite-plugin-checker';
import compileTime from 'vite-plugin-compile-time';
import EnvironmentPlugin from 'vite-plugin-environment';
import { createHtmlPlugin } from 'vite-plugin-html';
import { VitePWA } from 'vite-plugin-pwa';
import vitePluginRequire from 'vite-plugin-require';
import { viteStaticCopy } from 'vite-plugin-static-copy';
const availableLocales = JSON.stringify(fs.readdirSync('./src/locales'));
/** Return file as string, or return empty string. */
const readFile = (filename: string) => {
try {
return fs.readFileSync(filename, 'utf8');
} catch {
return '';
}
};
export default defineConfig(({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd(), '') };
const nonce = 'NONCE_PLACEHOLDER';
const {
DEVSERVER_URL,
BACKEND_URL,
} = process.env;
const DEFAULTS = {
DEVSERVER_URL: 'http://localhost:3036',
PATRON_URL: 'http://localhost:3037',
BACKEND_URL: 'http://localhost:4000',
};
const backendUrl = (() => {
try {
return new URL(BACKEND_URL || DEFAULTS.BACKEND_URL);
} catch {
return new URL(DEFAULTS.BACKEND_URL);
}
})();
const devServerUrl = (() => {
try {
return new URL(DEVSERVER_URL || DEFAULTS.DEVSERVER_URL);
} catch {
return new URL(DEFAULTS.DEVSERVER_URL);
}
})();
return {
build: {
assetsDir: 'packs',
assetsInlineLimit: 0,
rollupOptions: {
output: {
assetFileNames: 'packs/assets/[name]-[hash].[ext]',
chunkFileNames: 'packs/js/[name]-[hash].js',
entryFileNames: 'packs/[name]-[hash].js',
},
},
outDir: 'static',
emptyOutDir: true,
},
assetsInclude: ['**/*.oga'],
server: {
port: 2264,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Security-Policy': `upgrade-insecure-requests;style-src 'self' 'nonce-${nonce}';font-src 'self';script-src 'self' 'nonce-${nonce}' ;connect-src 'self' https://${backendUrl.hostname} wss://${backendUrl.hostname} http://${devServerUrl.hostname} ws://localhost:*/ ws://localhost:*/ws;media-src 'self' https:;img-src 'self' data: blob: https:;default-src 'none';base-uri 'none';frame-ancestors 'none';manifest-src 'self';`,
},
},
plugins: [
checker({ typescript: true }),
EnvironmentPlugin({
NODE_ENV: 'production',
// null = optional, undefined = required
BACKEND_URL: null,
DEVSERVER_URL: null,
AVAILABLE_LOCALES: availableLocales,
}, { loadEnvFiles: false }),
// @ts-ignore
vitePluginRequire(),
compileTime(),
createHtmlPlugin({
template: 'index.html',
minify: {
collapseWhitespace: true,
removeComments: false,
},
inject: {
data: {
snippets: readFile('custom/snippets.html'),
},
},
}),
{
name: 'html-inject-nonce',
enforce: 'post',
transformIndexHtml(html) {
if (mode !== 'development') return html;
const regex = /<(style|script)/gi;
const replacement = `<$1 nonce="${nonce}"`;
return html.replace(regex, replacement);
},
},
react({
// Use React plugin in all *.jsx and *.tsx files
include: '**/*.{jsx,tsx}',
babel: {
configFile: './babel.config.cjs',
},
}),
VitePWA({
injectRegister: null,
strategies: 'injectManifest',
injectManifest: {
injectionPoint: undefined,
plugins: [
// @ts-ignore
compileTime(),
],
},
manifestFilename: 'manifest.json',
manifest: {
name: 'Ghostbox',
short_name: 'Ghostbox',
description: 'An alternative frontend for Akkoma',
},
srcDir: 'src/soapbox/service-worker',
filename: 'sw.ts',
}),
viteStaticCopy({
targets: [
{
src: './node_modules/twemoji/assets/svg/*',
dest: './packs/emoji/',
}, {
src: './src/instance',
dest: '.',
}, {
src: './custom/instance',
dest: '.',
}, {
src: './src/locales/*',
dest: 'locales/',
},
],
}),
],
resolve: {
alias: [
{ find: 'soapbox', replacement: fileURLToPath(new URL('./src/soapbox', import.meta.url)) },
{ find: 'assets', replacement: fileURLToPath(new URL('./src/assets', import.meta.url)) },
],
},
};
});