Skip to content

Commit

Permalink
使用 vite-plugin-archiver 插件代替框架存档功能
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Sep 17, 2024
1 parent d557879 commit e6099cc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"@vitejs/plugin-legacy": "^5.4.2",
"@vitejs/plugin-vue": "^5.1.3",
"@vitejs/plugin-vue-jsx": "^4.0.1",
"archiver": "^7.0.1",
"autoprefixer": "^10.4.20",
"boxen": "^8.0.1",
"eslint": "^9.9.1",
Expand Down Expand Up @@ -116,6 +115,7 @@
"unplugin-vue-components": "^0.27.4",
"vite": "^5.4.2",
"vite-plugin-app-loading": "^0.3.0",
"vite-plugin-archiver": "^0.1.1",
"vite-plugin-banner": "^0.7.1",
"vite-plugin-compression2": "^1.2.0",
"vite-plugin-fake-server": "^2.1.1",
Expand Down
22 changes: 19 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 6 additions & 32 deletions vite/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from 'node:path'
import process from 'node:process'
import fs from 'node:fs'
import dayjs from 'dayjs'
import type { PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
Expand All @@ -15,17 +13,13 @@ import { vitePluginFakeServer } from 'vite-plugin-fake-server'
import Layouts from 'vite-plugin-vue-meta-layouts'
import Pages from 'vite-plugin-pages'
import { compression } from 'vite-plugin-compression2'
import archiver from 'archiver'
import Archiver from 'vite-plugin-archiver'
import AppLoading from 'vite-plugin-app-loading'
import TurboConsole from 'unplugin-turbo-console/vite'
import banner from 'vite-plugin-banner'
import boxen from 'boxen'
import picocolors from 'picocolors'

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

export default function createVitePlugins(viteEnv, isBuild = false) {
const vitePlugins: (PluginOption | PluginOption[])[] = [
vue(),
Expand Down Expand Up @@ -95,35 +89,15 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
}),

// https://github.com/nonzzz/vite-plugin-compression
isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('gzip') && compression(),
isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('brotli') && compression({
viteEnv.VITE_BUILD_COMPRESS.split(',').includes('gzip') && compression(),
viteEnv.VITE_BUILD_COMPRESS.split(',').includes('brotli') && compression({
exclude: [/\.(br)$/, /\.(gz)$/],
algorithm: 'brotliCompress',
}),

(function () {
let outDir: string
return {
name: 'vite-plugin-archiver',
apply: 'build',
configResolved(resolvedConfig) {
outDir = resolvedConfig.build.outDir
},
async closeBundle() {
if (['zip', 'tar'].includes(viteEnv.VITE_BUILD_ARCHIVE)) {
await sleep(1000)
const archive = archiver(viteEnv.VITE_BUILD_ARCHIVE, {
...(viteEnv.VITE_BUILD_ARCHIVE === 'zip' && { zlib: { level: 9 } }),
...(viteEnv.VITE_BUILD_ARCHIVE === 'tar' && { gzip: true, gzipOptions: { level: 9 } }),
})
const output = fs.createWriteStream(`${outDir}.${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.${viteEnv.VITE_BUILD_ARCHIVE === 'zip' ? 'zip' : 'tar.gz'}`)
archive.pipe(output)
archive.directory(outDir, false)
archive.finalize()
}
},
}
})(),
viteEnv.VITE_BUILD_ARCHIVE && Archiver({
archiveType: viteEnv.VITE_BUILD_ARCHIVE,
}),

AppLoading('loading.html'),

Expand Down

0 comments on commit e6099cc

Please sign in to comment.