Skip to content

Commit

Permalink
增加打包后生成存档文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Jan 13, 2024
1 parent 2e34d88 commit 3210b80
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ VITE_BUILD_MOCK = true
VITE_BUILD_SOURCEMAP = false
# 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip,brotli
# 是否在打包后生成存档,支持 zip 和 tar
VITE_BUILD_ARCHIVE =
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@vitejs/plugin-legacy": "^5.2.0",
"@vitejs/plugin-vue": "^5.0.3",
"@vitejs/plugin-vue-jsx": "^3.1.0",
"archiver": "^6.0.1",
"autoprefixer": "^10.4.16",
"boxen": "^7.1.1",
"eslint": "^8.56.0",
Expand Down
153 changes: 153 additions & 0 deletions pnpm-lock.yaml

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

33 changes: 33 additions & 0 deletions vite/plugins/archiver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'node:fs'
import dayjs from 'dayjs'
import archiver from 'archiver'
import type { Plugin } from 'vite'

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

export default function createArchiver(env): Plugin {
const { VITE_BUILD_ARCHIVE } = env
let outDir: string
return {
name: 'vite-plugin-archiver',
apply: 'build',
configResolved(resolvedConfig) {
outDir = resolvedConfig.build.outDir
},
async closeBundle() {
if (['zip', 'tar'].includes(VITE_BUILD_ARCHIVE)) {
await sleep(1000)
const archive = archiver(VITE_BUILD_ARCHIVE, {
...(VITE_BUILD_ARCHIVE === 'zip' && { zlib: { level: 9 } }),
...(VITE_BUILD_ARCHIVE === 'tar' && { gzip: true, gzipOptions: { level: 9 } }),
})
const output = fs.createWriteStream(`${outDir}.${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.${VITE_BUILD_ARCHIVE === 'zip' ? 'zip' : 'tar.gz'}`)
archive.pipe(output)
archive.directory(outDir, false)
archive.finalize()
}
},
}
}
2 changes: 2 additions & 0 deletions vite/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import createMock from './mock'
import createLayouts from './layouts'
import createPages from './pages'
import createCompression from './compression'
import createArchiver from './archiver'
import createConsole from './console'
import createBanner from './banner'

Expand All @@ -38,6 +39,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
vitePlugins.push(createLayouts())
vitePlugins.push(createPages())
vitePlugins.push(...createCompression(viteEnv, isBuild))
vitePlugins.push(createArchiver(viteEnv))
vitePlugins.push(createConsole())
vitePlugins.push(createBanner())
return vitePlugins
Expand Down

0 comments on commit 3210b80

Please sign in to comment.