53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
import { defineConfig } from 'vite'
|
|
import react from '@vitejs/plugin-react'
|
|
import { promises as fs } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { gzip } from 'node:zlib'
|
|
import { promisify } from 'node:util'
|
|
|
|
const gzipAsync = promisify(gzip)
|
|
|
|
// Emit pre-compressed assets so Nginx can serve them with `gzip_static on`.
|
|
function gzipAssets() {
|
|
const compressibleExtensions = new Set(['.html', '.css', '.js', '.mjs', '.json', '.svg', '.txt', '.xml'])
|
|
|
|
return {
|
|
name: 'gzip-assets',
|
|
apply: 'build',
|
|
async closeBundle() {
|
|
const outputDir = path.resolve(process.cwd(), 'dist')
|
|
|
|
async function compressDirectory(directory) {
|
|
const entries = await fs.readdir(directory, { withFileTypes: true })
|
|
|
|
await Promise.all(entries.map(async (entry) => {
|
|
const filePath = path.join(directory, entry.name)
|
|
|
|
if (entry.isDirectory()) {
|
|
await compressDirectory(filePath)
|
|
return
|
|
}
|
|
|
|
if (!compressibleExtensions.has(path.extname(entry.name).toLowerCase())) {
|
|
return
|
|
}
|
|
|
|
const source = await fs.readFile(filePath)
|
|
const compressed = await gzipAsync(source, { level: 9 })
|
|
await fs.writeFile(`${filePath}.gz`, compressed)
|
|
}))
|
|
}
|
|
|
|
await compressDirectory(outputDir)
|
|
},
|
|
}
|
|
}
|
|
|
|
export default defineConfig({
|
|
plugins: [react(), gzipAssets()],
|
|
server: {
|
|
host: '0.0.0.0',
|
|
port: 5173,
|
|
},
|
|
})
|