• VUE项目build后自动生成zip压缩包


    webpage项目使用方式

    安装filemanager-webpack-plugin插件

    1. npm install filemanager-webpack-plugin -D
    2. # or
    3. yarn add filemanager-webpack-plugin -D

    在vue.config.js中配置

    1. # 引入filemanager-webpack-plugin
    2. const FileManagerPlugin = require('filemanager-webpack-plugin');
    3. # 在module.exports中配置
    4. configureWebpack: {
    5. plugins: [
    6. new FileManagerPlugin({
    7. events: {
    8. onEnd: {
    9. delete: [`./dist/*.zip`],
    10. archive: [
    11. {source: `./dist`, destination: `./dist/dist.zip`}
    12. ]
    13. }
    14. }
    15. })
    16. ]
    17. },

    npm run build 或者 yarn run build打包即可

    vite项目使用方式

    安装jszip

    1. npm install jszip -D
    2. # or
    3. yarn add jszip -D

    新建 zip-compress.ts文件,放在和vite.config.ts同一级(js也可以)

    1. // zip-compress.ts
    2. import { resolve } from 'path'
    3. import fs from 'fs'
    4. import JSZip from 'jszip'
    5. const plugin = function (fileName = 'dist', output) {
    6. if (!output) output = resolve(__dirname, './dist')
    7. const fileZipName = fileName + '.zip'
    8. const makeZip = function () {
    9. const zip = new JSZip()
    10. const distPath = resolve(output)
    11. // 因为我想压缩包中的dist文件夹层级保留 且重新命名为dist 所以这里设置了allFolder ,如果不要该层级 则直接用zip即可
    12. let allFolder = zip.folder(fileName)
    13. const readDir = function (allFolder, dirPath) {
    14. // 读取dist下的根文件目录
    15. const files = fs.readdirSync(dirPath)
    16. files.forEach((fileName) => {
    17. const fillPath = `${dirPath}\\${fileName}`
    18. const file = fs.statSync(fillPath)
    19. // 如果是文件夹的话需要递归遍历下面的子文件
    20. if (file.isDirectory()) {
    21. const dirZip = allFolder.folder(fileName)
    22. readDir(dirZip, fillPath)
    23. } else {
    24. // 读取每个文件为buffer存到zip中
    25. allFolder.file(fileName, fs.readFileSync(fillPath))
    26. }
    27. })
    28. }
    29. const removeExistedZip = () => {
    30. const dest = `${distPath}\\${fileZipName}`
    31. if (fs.existsSync(dest)) {
    32. fs.unlinkSync(dest)
    33. }
    34. }
    35. const zipDir = function () {
    36. readDir(allFolder, distPath)
    37. zip.generateAsync({
    38. type: 'nodebuffer', // 压缩类型
    39. compression: 'DEFLATE', // 压缩算法
    40. compressionOptions: {
    41. // 压缩级别
    42. level: 9
    43. }
    44. }).then((content) => {
    45. const dest = `${distPath}\\${fileZipName}`
    46. removeExistedZip()
    47. // 把zip包写到硬盘中,这个content现在是一段buffer
    48. fs.writeFileSync(dest, content)
    49. })
    50. }
    51. removeExistedZip()
    52. zipDir(distPath)
    53. }
    54. return {
    55. name: 'vite-plugin-auto-zip',
    56. apply: 'build',
    57. closeBundle() {
    58. makeZip()
    59. }
    60. }
    61. }
    62. export { plugin as default };

    在vite.config.js中引入插件

    1. // 引入zip压缩插件
    2. import zipCompress from './zip-compress'
    3. // 在plugins中使用
    4. plugins: [
    5. vue(),
    6. zipCompress("fileName"),
    7. ...
    8. ],

    npm run build 或者 yarn run build打包即可

  • 相关阅读:
    『忘了再学』Shell基础 — 30、sed命令的使用
    一款针对EF Core轻量级分表分库、读写分离的开源项目
    Coursera Algorithm Ⅱ week2 Seam
    ROS机器人应用(6)—— 激光雷达建图和导航
    直播课堂系统09--腾讯云点播管理模块(一)
    TikTok搬运视频怎么做,搬运怎样的视频最好
    leetcode笔记
    第二章《Java程序世界初探》第8节:条件运算符
    halcon 圆点标定板相关算法说明及使用方法
    【牛客刷题-SQL】SQL4 查询结果限制返回行数
  • 原文地址:https://blog.csdn.net/Coder_Arley/article/details/133695168