11import { fileURLToPath , URL } from "node:url"
22import fs from "fs/promises"
33
4- import { PluginOption , UserConfig } from "vite"
4+ import { UserConfig } from "vite"
55import { ViteSSGOptions } from "vite-ssg"
66import generateSitemap from "vite-ssg-sitemap"
77import vue from "@vitejs/plugin-vue"
88import vueDevTools from "vite-plugin-vue-devtools"
99import { ViteImageOptimizer as viteImageOptimizer } from "vite-plugin-image-optimizer"
10+ import convertToWebp from "./plugins/convertToWebp"
11+ import webfontDownload from "vite-plugin-webfont-dl"
12+ import { compression , defineAlgorithm } from "vite-plugin-compression2"
13+ import zlib from "zlib"
1014
1115import { projects } from "./src/script/projects"
1216import type { CustomRouteMetadata } from "./src/types"
1317import type { RouteRecordRaw } from "vue-router"
1418
1519import * as child from "child_process"
16- import { globSync } from "node:fs"
17- import path from "node:path"
18- import sharp from "sharp"
1920
2021type ViteImageOptimizerOptions = Required < NonNullable < Parameters < typeof viteImageOptimizer > [ 0 ] > >
2122
@@ -30,57 +31,6 @@ const imageOptimizerOptions: Pick<
3031 avif : { quality : 75 } ,
3132}
3233
33- const convertToWebp : ( webpOptions : sharp . WebpOptions ) => PluginOption = ( webpOptions ) => {
34- const generated : string [ ] = [ ]
35-
36- return {
37- name : "convert-to-webp" ,
38- apply : "build" ,
39- enforce : "pre" ,
40-
41- buildStart : async ( ) => {
42- const files = globSync ( "src/**/*.{jpg,jpeg,png}" )
43- for ( const file of files ) {
44- const out = file . replace ( / \. ( j p g | j p e g | p n g ) $ / , ".webp" )
45-
46- try {
47- await fs . access ( out )
48- // File exists, do nothing
49- } catch {
50- // File doesn't exist, generate it
51- await sharp ( file ) . webp ( webpOptions ) . toFile ( out )
52- generated . push ( out )
53- console . log ( `✓ ${ path . basename ( file ) } → webp` )
54- }
55- }
56- } ,
57-
58- resolveId : async ( source , importer ) => {
59- if ( / \. ( j p g | j p e g | p n g ) $ / . test ( source ) && importer ) {
60- const dir = path . dirname ( importer )
61- const resolved = path . resolve ( dir , source )
62- const webp = resolved . replace ( / \. ( j p g | j p e g | p n g ) $ / , ".webp" )
63-
64- try {
65- await fs . access ( webp )
66- // File exists, redirect to webp version
67- return webp
68- } catch {
69- // File doesn't exist, do nothing
70- }
71- }
72- } ,
73-
74- closeBundle : async ( ) => {
75- await Promise . all (
76- generated . map ( ( file ) =>
77- fs . rm ( file ) . then ( ( ) => console . log ( `🗑 ${ path . basename ( file ) } deleted` ) ) ,
78- ) ,
79- )
80- } ,
81- }
82- }
83-
8434const badUA = await fs . readFile ( "src/knownBadUA.txt" , "utf-8" )
8535
8636process . env . VITE_GIT_COMMIT_HASH = child . execSync ( "git rev-parse --short HEAD" ) . toString ( )
@@ -95,6 +45,13 @@ export default {
9545 vueDevTools ( ) ,
9646 convertToWebp ( imageOptimizerOptions . webp ) ,
9747 viteImageOptimizer ( imageOptimizerOptions ) ,
48+ webfontDownload ( [ "https://fonts.googleapis.com/css2?family=Fira+Sans&display=swap" ] ) ,
49+ compression ( {
50+ algorithms : [
51+ "gzip" ,
52+ defineAlgorithm ( "br" , { params : { [ zlib . constants . BROTLI_PARAM_QUALITY ] : 11 } } ) ,
53+ ] ,
54+ } ) ,
9855 ] ,
9956 resolve : {
10057 alias : {
0 commit comments