Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
333 lines (319 loc) · 12.8 KB
/
Copy pathvite.config.ts
File metadata and controls
333 lines (319 loc) · 12.8 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { URL, fileURLToPath } from 'node:url';
import { resolve } from 'node:path';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import wasm from 'vite-plugin-wasm';
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import markdown from 'unplugin-vue-markdown/vite';
import svgLoader from 'vite-svg-loader';
import { VitePWA } from 'vite-plugin-pwa';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
import Unocss from 'unocss/vite';
import { configDefaults } from 'vitest/config';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import VueI18n from '@intlify/unplugin-vue-i18n/vite';
import fs from 'node:fs';
import fg from 'fast-glob';
import Sitemap from 'vite-plugin-sitemap';
import { visualizer } from 'rollup-plugin-visualizer';
// Where the app will be served from. The build itself is path-agnostic -- `base` below is
// relative, so every asset URL is resolved against the file that references it -- and this
// only seeds the `<base href>` in index.html, which is what the app and the browser read
// the deployment path from at runtime (see src/utils/base-url.ts).
//
// Setting it is only needed for a plain static deployment into a subfolder (GitHub Pages,
// `dist` dropped into a directory), because there is no server to fill it in. The Docker
// image leaves it at `/` and rewrites the tag per request from the BASE_URL environment
// variable instead (see nginx.conf), which is why one image can serve any subpath.
const baseUrl = normalizeBaseUrl(process.env.BASE_URL);
const hostname = process.env.HOSTNAME;
function normalizeBaseUrl(value: string | undefined): string {
const trimmed = (value ?? '').replace(/^\/+|\/+$/g, '');
if (trimmed === '') {
return '/';
}
// The same rule the container applies to the runtime value
// (docker-entrypoint.d/18-resolve-base-url.envsh): a plain path built from unreserved
// characters. This one ends up spliced into an HTML attribute below, so a value carrying
// a quote would rewrite the tag around it, and one carrying a space or `..` would just
// quietly produce a page that loads from the wrong place. Fail the build instead.
const segments = trimmed.split('/');
if (segments.some(segment => segment === '..' || !/^[\w.~-]+$/.test(segment))) {
throw new Error(`BASE_URL must be a plain path such as "/it-tools/", got ${JSON.stringify(value)}`);
}
return `/${segments.join('/')}/`;
}
// index.html gets exactly one `<base href>`, and it has to come before the first relative
// URL in <head> -- Vite injects the entry script and the stylesheet links there, so this
// splices it in right after the opening tag rather than relying on tag-injection order.
// The emitted string is kept byte-stable on purpose: nginx.conf matches it literally to
// swap in the runtime BASE_URL.
function baseHref(base: string): Plugin {
return {
name: 'it-tools:base-href',
// Build only: the dev server always serves from the root (Vite normalizes a relative
// base to `/` for `serve`), and with no `<base>` the app falls back to `/` as well.
apply: 'build',
transformIndexHtml: {
order: 'post',
handler(html) {
const withBase = html.replace(/<head(\s[^>]*)?>/i, match => `${match}\n <base href="${base}">`);
if (withBase === html) {
throw new Error('it-tools:base-href: no <head> to inject the <base href> into');
}
return withBase;
},
},
};
}
// Locales are code-split: only en is bundled eagerly, the rest become lazy chunks fetched on
// first use (see src/plugins/i18n.plugin.ts). VITE_AVAILABLE_LOCALES filters the locales
// offered at runtime instead of trimming the build.
const includeLocales = [resolve(__dirname, 'src/tools/*/locales/**'), resolve(__dirname, 'locales/**')];
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
baseHref(baseUrl),
VueI18n({
runtimeOnly: true,
compositionOnly: true,
fullInstall: true,
include: includeLocales,
strictMessage: false,
escapeHtml: true,
}),
AutoImport({
imports: [
'vue',
'vue-router',
'@vueuse/core',
'vue-i18n',
{
'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
},
],
vueTemplate: true,
}),
Icons({ compiler: 'vue3' }),
vue({
include: [/\.vue$/, /\.md$/],
}),
vueJsx(),
markdown(),
svgLoader(),
VitePWA({
registerType: 'autoUpdate',
// src/main.ts registers the worker itself, against the base the app resolved at
// runtime. Turning the injection off means the plugin no longer infers the
// `autoUpdate` workbox flags either, hence skipWaiting/clientsClaim below.
injectRegister: false,
workbox: {
skipWaiting: true,
clientsClaim: true,
// Precache only the app shell so the service worker doesn't download every
// tool chunk and WASM binary (~160 MB) on first visit; hashed assets are
// cached on demand as tools are opened. Set VITE_PWA_FULL_PRECACHE=true to
// restore full offline precaching of everything.
globPatterns:
process.env.VITE_PWA_FULL_PRECACHE === 'true' && !process.env.VITE_VERCEL_DEPLOY
? ['**\/*.{js,wasm,css,html}']
: ['**\/*.{css,html}'],
maximumFileSizeToCacheInBytes: 25 * 1024 ** 2,
// Relative, like every other precache entry: workbox resolves them against the
// service worker's own URL, so the same sw.js works under any deployment path.
navigateFallback: 'index.html',
runtimeCaching: [
{
urlPattern: ({ sameOrigin, request }) =>
sameOrigin && (request.destination === 'script' || request.destination === 'worker'),
handler: 'CacheFirst',
options: {
cacheName: 'app-chunks',
expiration: { maxEntries: 2000, maxAgeSeconds: 60 * 60 * 24 * 30 },
cacheableResponse: { statuses: [0, 200] },
},
},
{
urlPattern: ({ sameOrigin, url }) => sameOrigin && url.pathname.endsWith('.wasm'),
handler: 'CacheFirst',
options: {
cacheName: 'app-wasm',
expiration: { maxEntries: 30, maxAgeSeconds: 60 * 60 * 24 * 30 },
cacheableResponse: { statuses: [0, 200] },
},
},
],
},
strategies: 'generateSW',
// Every URL in here is relative to the manifest itself, which the browser fetches
// from the app root -- so the manifest, like the rest of the build, does not need to
// know where that root is.
manifest: {
name: 'IT Tools',
description: 'Aggregated set of useful tools for developers.',
display: 'standalone',
start_url: './?utm_source=pwa&utm_medium=pwa',
scope: './',
orientation: 'any',
theme_color: '#18a058',
background_color: '#f1f5f9',
icons: [
{
src: './favicon-16x16.png',
type: 'image/png',
sizes: '16x16',
},
{
src: './favicon-32x32.png',
type: 'image/png',
sizes: '32x32',
},
{
src: './android-chrome-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: './android-chrome-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any maskable',
},
],
},
}),
Components({
dirs: ['src/'],
extensions: ['vue', 'md'],
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
resolvers: [NaiveUiResolver(), IconsResolver({ prefix: 'icon' })],
// dtsTsx generates invalid `const 'Name.demo':` declarations for
// components whose file names contain dots
dtsTsx: false,
}),
Unocss(),
nodePolyfills(),
wasm(),
visualizer(),
hostname
? Sitemap({
hostname,
generateRobotsTxt: true,
robots: [{ userAgent: '*', allow: '/' }],
dynamicRoutes: (() => {
const paths = ['/', '/about'];
fg.sync('src/tools/*/index.ts').forEach((file) => {
const content = fs.readFileSync(file, 'utf-8');
const pathMatch = content.match(/path:\s*['"`]([^'"`]+)['"`]/);
if (pathMatch) {
paths.push(pathMatch[1]);
}
const redirectMatch = content.match(/redirectFrom:\s*\[([^\]]+)\]/);
if (redirectMatch?.[1]) {
const redirectPaths = redirectMatch[1].match(/['"`]([^'"`]+)['"`]/g);
redirectPaths?.forEach((p) => paths.push(p.replace(/['"`]/g, '')));
}
});
return paths;
})(),
})
: undefined,
],
// Relative, so a built asset is addressed from the chunk that imports it rather than
// from a path fixed at build time. This is what makes the output portable across
// deployment paths; `<base href>` (see baseHref above) covers the entry points in
// index.html, which the browser resolves against the document instead.
base: './',
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'node:fs/promises': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'node:fs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
// Must come before the bare `fs` entry: aliases match by prefix, so `fs` alone would rewrite
// `fs/promises` to `_empty.ts/promises`. cron-parser's CronFileParser reaches for it.
'fs/promises': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
fs: fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'@babel/core': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'isolated-vm': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'onnxruntime-node': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'unpdf/pdfjs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
'webcrypto-liner-shim': !process.env.VERCEL
? 'webcrypto-liner-shim'
: fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
},
},
define: {
'import.meta.env.PACKAGE_VERSION': JSON.stringify(process.env.npm_package_version),
},
test: {
exclude: [...configDefaults.exclude, '**/*.e2e.spec.ts'],
// Several suites assert on formatted dates and offsets (date-time-converter,
// days-calculator, crontab-generator). Without a fixed zone they fail on any
// machine that is not on UTC.
env: { TZ: 'UTC' },
// Only a handful of suites touch the DOM; they opt in with a
// `// @vitest-environment jsdom` docblock. Building a jsdom for all 128
// files cost ~2 minutes of environment setup for the 7 that need it.
environment: 'node',
server: {
deps: {
inline: ['otpauth-migration', 'proto'],
},
},
},
build: {
target: 'esnext',
// sourcemap: !process.env.VERCEL,
// minify: !process.env.VERCEL,
reportCompressedSize: !process.env.VERCEL,
// cssMinify: false,
// modulePreload: false,
rolldownOptions: {
external: ['regex', './out/isolated_vm', 'isolated-vm', 'onnxruntime-node', 'unpdf/pdfjs'],
output: {
format: 'es',
codeSplitting: {
// Tool icons are loaded through per-icon dynamic imports (see src/tools/*/index.ts);
// merge them into a single lazy chunk instead of ~450 tiny ones.
// includeDependenciesRecursively must stay off: with it, shared helper modules
// get captured into this chunk and entry chunks end up statically importing it,
// which drags the whole icon set back into the startup payload.
groups: [
{
name: 'tool-icons',
test: /node_modules[\\/](?:@vicons[\\/]|@tabler[\\/]icons-vue[\\/]dist[\\/]esm[\\/]icons[\\/])/,
includeDependenciesRecursively: false,
},
],
},
},
},
},
optimizeDeps: {
include: [
'isolated-vm',
'@lezer/highlight',
'pdfjs-dist',
'onnxruntime-node',
'onnxruntime-web',
'unpdf',
'unpdf/pdfjs',
...(process.env.VERCEL ? ['webcrypto-liner-shim'] : []),
], // optionally specify dependency name
},
server: {
watch: {
ignored: ['**/.pnpm-store/**'],
},
headers: {
// 'Cross-Origin-Resource-Policy': 'same-site',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
});