-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.ts
112 lines (102 loc) · 3.13 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
/* eslint-disable import/no-extraneous-dependencies */
import { fileURLToPath, URL } from 'url';
import 'vite-ssg';
import { defineConfig } from 'vite';
import { generateSitemap } from 'sitemap-ts';
import vue from '@vitejs/plugin-vue';
import tosource from 'tosource';
import { parse } from 'yaml';
import mdPlugin, { Mode } from 'vite-plugin-markdown';
import MarkdownIt from 'markdown-it';
import emojiPlugin from 'markdown-it-emoji';
import containerPlugin from 'markdown-it-container';
import glob from 'fast-glob';
import viteCompression from 'vite-plugin-compression';
// vite-plugin-imagemin
// import viteImagemin from 'vite-plugin-imagemin';
import svgIcon from './plugin/svgIcon';
const md = MarkdownIt({
html: true,
})
.use(emojiPlugin)
.use(containerPlugin);
// https://vitejs.dev/config/
export default defineConfig({
base: process.env.BASE_URL,
plugins: [
vue(),
svgIcon(),
viteCompression(),
{
name: 'vite:transform-yaml:',
transform(code, id) {
if (!/\.ya?ml$/.test(id)) return null;
return `const data = ${tosource(parse(code))};\nexport default data`;
},
},
mdPlugin({
mode: [Mode.VUE],
markdownIt: md,
}),
// viteImagemin({
// gifsicle: { optimizationLevel: 7, interlaced: false },
// optipng: { optimizationLevel: 7 },
// mozjpeg: { quality: 20 },
// pngquant: { quality: [0.8, 0.9], speed: 4 },
// svgo: {
// plugins: [
// { name: 'removeViewBox' },
// { name: 'removeEmptyAttrs', active: false },
// ],
// },
// }),
],
resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
build: {
terserOptions: {
compress: {
// Prevent Infinity from being compressed to 1/0, may cause performance issues on Chrome
keep_infinity: true,
drop_console: false,
},
format: { comments: false },
},
minify: 'terser',
// Speed up packing
brotliSize: false,
chunkSizeWarningLimit: 2000,
rollupOptions: {
output: {
entryFileNames: `static/js/[name]-[hash].js`,
chunkFileNames: `static/js/[name]-[hash].js`,
assetFileNames: `static/[ext]/[name]-[hash].[ext]`,
},
},
},
ssgOptions: {
rootContainerId: 'app-mount',
formatting: 'minify',
dirStyle: 'nested',
script: 'async',
async includedRoutes(paths, _routes) {
const projects: string[] = [];
const newPaths = await glob('./src/data/new/**/*.md');
for (const path of newPaths) {
const projectNames = path.slice(15, -3).split('-'); // './src/data/new/' >> 15, '.md' >> 3
const [y, m, d] = projectNames.shift()?.split('/') || [];
projects.push(`/new/${y}-${m}-${d}-${projectNames.join('-')}`);
}
return paths
.filter((route) => !['/:pathMatch(.*)*'].includes(<string>route))
.flatMap((route) => {
return route === '/new/:id' ? projects : route;
});
},
onFinished() {
generateSitemap({
hostname: process.env.HOSTNAME || 'http://localhost/',
robots: [{ userAgent: '*', allow: '/' }],
});
},
},
});