mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
138 lines
3.4 KiB
TypeScript
138 lines
3.4 KiB
TypeScript
import fs from 'fs'
|
|
import type { Plugin } from 'vite'
|
|
import { defineConfig } from 'vite'
|
|
import Components from 'unplugin-vue-components/vite'
|
|
import Unocss from 'unocss/vite'
|
|
import { presetAttributify, presetIcons, presetUno } from 'unocss'
|
|
import { resolve } from 'pathe'
|
|
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
|
|
import { VitePWA } from 'vite-plugin-pwa'
|
|
import fg from 'fast-glob'
|
|
import {
|
|
pwaFontStylesRegex,
|
|
pwaFontsRegex,
|
|
vitestDescription,
|
|
vitestName,
|
|
vitestShortName,
|
|
} from './.vitepress/meta'
|
|
import { optimizePages } from './.vitepress/scripts/assets'
|
|
|
|
const PWA = VitePWA({
|
|
outDir: '.vitepress/dist',
|
|
registerType: 'autoUpdate',
|
|
// include all static assets under public/
|
|
includeAssets: fg.sync('**/*.{png,svg,ico,txt}', { cwd: resolve(__dirname, 'public') }),
|
|
manifest: {
|
|
id: '/',
|
|
name: vitestName,
|
|
short_name: vitestShortName,
|
|
description: vitestDescription,
|
|
theme_color: '#ffffff',
|
|
icons: [
|
|
{
|
|
src: 'pwa-192x192.png',
|
|
sizes: '192x192',
|
|
type: 'image/png',
|
|
},
|
|
{
|
|
src: 'pwa-512x512.png',
|
|
sizes: '512x512',
|
|
type: 'image/png',
|
|
},
|
|
{
|
|
src: 'logo.svg',
|
|
sizes: '165x165',
|
|
type: 'image/svg',
|
|
purpose: 'any maskable',
|
|
},
|
|
],
|
|
},
|
|
workbox: {
|
|
navigateFallbackDenylist: [/^\/new$/],
|
|
runtimeCaching: [
|
|
{
|
|
urlPattern: pwaFontsRegex,
|
|
handler: 'CacheFirst',
|
|
options: {
|
|
cacheName: 'google-fonts-cache',
|
|
expiration: {
|
|
maxEntries: 10,
|
|
maxAgeSeconds: 60 * 60 * 24 * 365, // <== 365 days
|
|
},
|
|
cacheableResponse: {
|
|
statuses: [0, 200],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
urlPattern: pwaFontStylesRegex,
|
|
handler: 'CacheFirst',
|
|
options: {
|
|
cacheName: 'gstatic-fonts-cache',
|
|
expiration: {
|
|
maxEntries: 10,
|
|
maxAgeSeconds: 60 * 60 * 24 * 365, // <== 365 days
|
|
},
|
|
cacheableResponse: {
|
|
statuses: [0, 200],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
})
|
|
|
|
export default defineConfig({
|
|
plugins: [
|
|
Components({
|
|
include: [/\.vue/, /\.md/],
|
|
dirs: '.vitepress/components',
|
|
dts: '.vitepress/components.d.ts',
|
|
}),
|
|
Unocss({
|
|
shortcuts: [
|
|
['btn', 'px-4 py-1 rounded inline-flex justify-center gap-2 text-white leading-30px children:mya !no-underline cursor-pointer disabled:cursor-default disabled:bg-gray-600 disabled:opacity-50'],
|
|
],
|
|
presets: [
|
|
presetUno({
|
|
dark: 'media',
|
|
}),
|
|
presetAttributify(),
|
|
presetIcons({
|
|
scale: 1.2,
|
|
}),
|
|
],
|
|
}),
|
|
IncludesPlugin(),
|
|
PWA,
|
|
{
|
|
name: 'pwa:post',
|
|
enforce: 'post',
|
|
async buildEnd() {
|
|
const pwaPlugin: VitePluginPWAAPI = PWA.find(i => i.name === 'vite-plugin-pwa')?.api
|
|
const pwa = pwaPlugin && !pwaPlugin.disabled
|
|
await optimizePages(pwa)
|
|
if (pwa)
|
|
await pwaPlugin.generateSW()
|
|
},
|
|
},
|
|
],
|
|
})
|
|
|
|
function IncludesPlugin(): Plugin {
|
|
return {
|
|
name: 'include-plugin',
|
|
enforce: 'pre',
|
|
transform(code, id) {
|
|
let changed = false
|
|
code = code.replace(/\[@@include\]\((.*?)\)/, (_, url) => {
|
|
changed = true
|
|
const full = resolve(id, url)
|
|
return fs.readFileSync(full, 'utf-8')
|
|
})
|
|
if (changed)
|
|
return code
|
|
},
|
|
}
|
|
}
|