-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvite.config.ts
155 lines (146 loc) · 4.38 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
/// <reference types="vitest/config" />
import fs from 'node:fs';
import { fileURLToPath, URL } from 'node:url';
import react from '@vitejs/plugin-react-swc';
import { visualizer } from 'rollup-plugin-visualizer';
import { Connect, defineConfig, Plugin, UserConfig } from 'vite';
import checker from 'vite-plugin-checker';
import compileTime from 'vite-plugin-compile-time';
import { createHtmlPlugin } from 'vite-plugin-html';
import { VitePWA } from 'vite-plugin-pwa';
import { viteStaticCopy } from 'vite-plugin-static-copy';
const { NODE_ENV, PORT } = process.env;
export default defineConfig(() => {
const config: UserConfig = {
build: {
assetsDir: 'packs',
assetsInlineLimit: 0,
rollupOptions: {
output: {
assetFileNames: 'packs/assets/[name]-[hash].[ext]',
chunkFileNames: 'packs/js/[name]-[hash].js',
entryFileNames: 'packs/[name]-[hash].js',
},
},
sourcemap: true,
},
assetsInclude: ['**/*.oga'],
server: {
port: Number(PORT ?? 3036),
},
plugins: [
checker({ typescript: true }),
compileTime(),
createHtmlPlugin({
template: 'index.html',
minify: {
collapseWhitespace: true,
removeComments: false,
},
inject: {
data: {
snippets: readFileContents('custom/snippets.html'),
csp: buildCSP(NODE_ENV),
},
},
}),
react(),
VitePWA({
injectRegister: null,
strategies: 'injectManifest',
injectManifest: {
injectionPoint: undefined,
plugins: [
// @ts-ignore
compileTime(),
],
},
manifest: {
name: 'Soapbox',
short_name: 'Soapbox',
description: 'A social media frontend with a focus on custom branding and ease of use.',
theme_color: '#0482d8',
},
srcDir: 'src/service-worker',
filename: 'sw.ts',
}),
viteStaticCopy({
targets: [{
src: './src/instance',
dest: '.',
}, {
src: './custom/instance',
dest: '.',
}],
}),
visualizer({
emitFile: true,
filename: 'report.html',
title: 'Soapbox Bundle',
}) as Plugin,
{
// Vite's default behavior is to serve index.html (HTTP 200) for unmatched routes, like a PWA.
// Instead, 404 on known backend routes to more closely match a real server.
name: 'vite-mastodon-dev',
configureServer(server) {
const notFoundHandler: Connect.SimpleHandleFunction = (_req, res) => {
res.statusCode = 404;
res.end();
};
server.middlewares.use('/api/', notFoundHandler);
server.middlewares.use('/oauth/', notFoundHandler);
server.middlewares.use('/nodeinfo/', notFoundHandler);
server.middlewares.use('/.well-known/', notFoundHandler);
},
},
],
resolve: {
alias: [
{ find: 'soapbox', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
],
},
// @ts-ignore
test: {
globals: true,
environment: 'jsdom',
setupFiles: 'src/jest/test-setup.ts',
},
};
return config;
});
/** Build a sane default CSP string to embed in index.html in case the server doesn't return one. */
/* eslint-disable quotes */
function buildCSP(env: string | undefined): string {
const csp = [
"default-src 'none'",
"script-src 'self' 'wasm-unsafe-eval'",
"style-src 'self' 'unsafe-inline'",
"frame-src 'self' https:",
"font-src 'self'",
"base-uri 'self'",
"manifest-src 'self'",
];
if (env === 'development') {
csp.push(
"connect-src 'self' blob: https: wss: http://localhost:* http://127.0.0.1:* ws://localhost:* ws://127.0.0.1:*",
"img-src 'self' data: blob: https: http://localhost:* http://127.0.0.1:*",
"media-src 'self' https: http://localhost:* http://127.0.0.1:*",
);
} else {
csp.push(
"connect-src 'self' blob: https: wss:",
"img-src 'self' data: blob: https:",
"media-src 'self' https:",
);
}
return csp.join('; ');
}
/* eslint-enable quotes */
/** Return file as string, or return empty string if the file isn't found. */
function readFileContents(path: string) {
try {
return fs.readFileSync(path, 'utf8');
} catch {
return '';
}
}