Skip to content

Commit

Permalink
feat: 增加打包后生成存档文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Jan 13, 2024
1 parent b131056 commit 66d9e31
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ VITE_BUILD_MOCK = false
VITE_BUILD_SOURCEMAP = false
# 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip,brotli
# 是否在打包后生成存档,支持 zip 和 tar
VITE_BUILD_ARCHIVE =
2 changes: 2 additions & 0 deletions .env.test
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 @@ -52,6 +52,7 @@
"@unocss/eslint-plugin": "^0.58.3",
"@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",
"bumpp": "^9.2.1",
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 @@ -9,6 +9,7 @@ import createUnocss from './unocss'
import createSvgIcon from './svg-icon'
import createMock from './mock'
import createCompression from './compression'
import createArchiver from './archiver'
import createConsole from './console'
import createBanner from './banner'

Expand All @@ -24,6 +25,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
vitePlugins.push(createSvgIcon(isBuild))
vitePlugins.push(createMock(viteEnv, isBuild))
vitePlugins.push(...createCompression(viteEnv, isBuild))
vitePlugins.push(createArchiver(viteEnv))
vitePlugins.push(createConsole())
vitePlugins.push(createBanner())
return vitePlugins
Expand Down

0 comments on commit 66d9e31

Please sign in to comment.