forked from bangle-io/bangle-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
203 lines (189 loc) · 4.48 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* eslint-disable no-process-env */
import Unocss from '@unocss/vite';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import { createHtmlPlugin } from 'vite-plugin-html';
import { VitePWA } from 'vite-plugin-pwa';
import getEnvVars from '@bangle.io/env-vars';
const argv = require('minimist')(process.argv.slice(2));
export default defineConfig(async ({ command, mode }) => {
const isProduction = mode === 'production';
const envVars = await getEnvVars({
isProduction: isProduction,
isVite: true,
});
const hot = envVars.hot;
const port = argv.port;
// NOTE: we are relying on cli passing port
// as I couldnt find a reliable way to get the port
// from vite. and we need port to do the proxy hack below
if (command !== 'build' && !port) {
throw new Error('Port must be defined');
}
if (isProduction && hot) {
throw new Error('Hot not allowed in production');
}
if (hot) {
console.info('**** RUNNING IN HOT RELOAD MODE ****');
}
const appEnv = envVars.appEnv;
let sourcemap = true;
// if (appEnv === 'production') {
// sourcemap = false;
// }
/**
* @type {import('vite').UserConfig}
*/
const config = {
build: {
target: 'es2018',
sourcemap: sourcemap,
emptyOutDir: true,
outDir: './build',
chunkSizeWarningLimit: 3000,
},
worker: {
format: 'es',
},
plugins: [
createHtmlPlugin({
minify: isProduction,
inject: {
data: { ...envVars.htmlInjections },
},
}),
react({
fastRefresh: hot,
exclude: [
'**/node_modules/**/*',
'**/dist/**/*',
'**/build/**/*',
'.yarn/**/*',
/\.stories\.(t|j)sx?$/,
],
include: '**/*.(tsx|ts)',
}),
Unocss(),
VitePWA({
minify: false,
includeAssets: [
'favicon.svg',
'favicon-dev.svg',
'favicon-staging.svg',
'favicon.ico',
'favicon-dev.ico',
'favicon-staging.ico',
'robots.txt',
'apple-touch-icon.png',
],
manifest: generateManifest(appEnv),
}),
],
publicDir: './tooling/public',
define: {
...envVars.appEnvs,
},
server: {
strictPort: true,
hmr: hot,
proxy: {
// string shorthand
'^.*\\.md$': {
target: 'http://localhost:' + port,
rewrite: (path) => path.split('.md').join(''),
},
},
},
};
return config;
});
function generateManifest(appEnv) {
const isProd = appEnv === 'production';
let icons = [];
if (isProd) {
icons = [
{
src: 'maskable_icon_x48.png',
sizes: '48x48',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'maskable_icon_x128.png',
sizes: '128x128',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'maskable_icon_x192.png',
sizes: '192x192',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'maskable_icon_x384.png',
sizes: '384x384',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'maskable_icon_x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'maskable_icon_x1024.png',
sizes: '1024x1024',
type: 'image/png',
purpose: 'maskable',
},
{
src: 'icon-transparent_x128.png',
sizes: '128x128',
type: 'image/png',
purpose: 'any',
},
{
src: 'icon-transparent_x192.png',
sizes: '192x192',
type: 'image/png',
purpose: 'any',
},
{
src: 'icon-transparent_x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any',
},
];
} else if (appEnv === 'staging') {
icons = [
{
src: 'icon-transparent-staging_x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any',
},
];
} else {
icons = [
{
src: 'icon-transparent-dev_x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any',
},
];
}
const manifest = {
name: 'bangle.io' + (isProd ? '' : '/' + appEnv),
short_name: 'bangle',
theme_color: '#ffffff',
background_color: '#ffffff',
display: 'minimal-ui',
start_url: '/?type=installed_pwa',
icons,
};
return manifest;
}