-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvite.config.mjs
179 lines (177 loc) · 5.17 KB
/
vite.config.mjs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import { parse } from 'path';
import { Remarkable } from 'remarkable';
import legacy from '@vitejs/plugin-legacy';
import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
import generateFile from 'vite-plugin-generate-file';
import { VitePWA } from 'vite-plugin-pwa';
import { globSync } from 'glob';
import matter from 'gray-matter';
import pkg from './package.json';
import { injectInlineResources } from './src/vite/inject-inline-resources';
import { injectPreload } from './src/vite/inject-preload';
// Move the listed node modules into separate named chunks. Format: module name
// - chunk name.
const vendorChunks = [
// react
['react', 'react'],
['react-dom', 'react'],
// react-router
['react-router', 'react-router'],
['react-router-redux', 'react-router'],
// libs
['lodash-es', 'libs'],
['date-fns', 'libs'],
['socket.io-client', 'libs'],
// analytics
['autotrack', 'analytics'],
['@sentry/react', 'analytics'],
];
export default defineConfig(({ mode }) => ({
plugins: [
VitePWA({
registerType: 'prompt',
devOptions: { enabled: true },
manifest: {
name: 'FreeFeed',
short_name: 'FreeFeed',
description: 'A small and free social network',
id: '/?pwa=1',
start_url: '/',
icons: [
{
src: '/assets/images/pwa-64x64.png',
sizes: '64x64',
type: 'image/png',
},
{
src: '/assets/images/pwa-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: '/assets/images/pwa-512x512.png',
sizes: '512x512',
type: 'image/png',
},
{
src: '/assets/images/maskable-icon-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable',
},
],
},
workbox: {
// Don't use 'index.html' fallback
navigateFallback: null,
// Add '.woff2' and exclude (!) '.html' from the default 'globPatterns'.
// We don't want to cache index.html because of beta/non-beta switching
// and the possible config.json inclusion. So we only cache the assets
// but not the page itself.
globPatterns: ['**/*.{js,wasm,css,woff2}'],
runtimeCaching: [
// Cache profile pictures (up to 100 entries)
{
urlPattern: /^https:\/\/(stable-)?media\.freefeed\.net\/profilepics\//,
handler: 'CacheFirst',
options: {
cacheName: 'profile-pictures',
expiration: {
maxEntries: 100,
},
},
},
],
},
}),
!process.env.MODERN && legacy(),
react(),
injectPreload({
files: [
{
match: /\/app-\w+\.js$/,
attributes: { rel: 'modulepreload', type: 'application/javascript', as: 'script' },
},
{
match: /\/vendor-.+?\.js$/,
attributes: { rel: 'modulepreload', type: 'application/javascript', as: 'script' },
},
{ match: /\/app-\w+\.css$/, attributes: { type: 'text/css', as: 'style' } },
],
}),
injectInlineResources(),
mode === 'production' &&
generateFile([
{
type: 'template',
output: 'version.txt',
template: 'src/version.ejs',
data: {
name: pkg.name,
version: pkg.version,
date: new Date().toISOString(),
commitHash: execSync('git rev-parse --short HEAD').toString().trim(),
},
},
// Doc files from markdown sources
...globSync('./src/docs/texts/*.md').map((file) => {
const fileContent = readFileSync(file, 'utf8');
const fileData = matter(fileContent);
return {
type: 'template',
output: `docs/${parse(file).name}/index.html`,
template: 'src/docs/template.ejs',
data: {
...fileData.data,
content: new Remarkable().render(fileData.content),
},
};
}),
]),
],
build: {
outDir: '_dist',
sourcemap: true,
rollupOptions: {
output: {
manualChunks: (id) => {
for (const [module, chunk] of vendorChunks) {
if (id.includes(`/node_modules/${module}/`)) {
return `vendor-${chunk}`;
}
}
},
},
},
},
server: { host: true, port: 3333 },
preview: { host: true, port: 4444 },
define:
mode !== 'test'
? {
'process.platform': JSON.stringify('web'),
'process.env.NODE_ENV': JSON.stringify(mode),
}
: {},
test: {
include: ['test/unit/**/*.{js,jsx}', 'test/jest/**/*.test.{js,jsx}'],
poolOptions: { forks: { singleFork: true } },
globals: true,
clearMocks: true,
environment: 'jsdom',
setupFiles: './test/vitest-setup.js',
css: {
modules: {
classNameStrategy: 'non-scoped',
},
},
},
css: {
preprocessorOptions: {
scss: { api: 'modern-compiler' },
},
},
}));