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

webpage项目使用方式

安装filemanager-webpack-plugin插件

npm install filemanager-webpack-plugin -D
# or
yarn add filemanager-webpack-plugin -D

在vue.config.js中配置

# 引入filemanager-webpack-plugin
const FileManagerPlugin = require('filemanager-webpack-plugin');


# 在module.exports中配置
configureWebpack: {
    plugins: [
      new FileManagerPlugin({
        events: {
          onEnd: {
            delete: [`./dist/*.zip`],
            archive: [
              {source: `./dist`, destination: `./dist/dist.zip`}
            ]
          }
        }
      })
    ]
  },

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

vite项目使用方式

安装jszip

npm install jszip -D
# or
yarn add jszip -D

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

//  zip-compress.ts
import { resolve } from 'path'
import fs from 'fs'
import JSZip from 'jszip'
const plugin = function (fileName = 'dist', output) {
    if (!output) output = resolve(__dirname, './dist')
    const fileZipName = fileName + '.zip'
    const makeZip = function () {
        const zip = new JSZip()
        const distPath = resolve(output)
        // 因为我想压缩包中的dist文件夹层级保留 且重新命名为dist 所以这里设置了allFolder  ,如果不要该层级 则直接用zip即可
        let allFolder = zip.folder(fileName)

        const readDir = function (allFolder, dirPath) {
            // 读取dist下的根文件目录
            const files = fs.readdirSync(dirPath)
            files.forEach((fileName) => {
                const fillPath = `${dirPath}\\${fileName}`
                const file = fs.statSync(fillPath)
                // 如果是文件夹的话需要递归遍历下面的子文件
                if (file.isDirectory()) {
                    const dirZip = allFolder.folder(fileName)
                    readDir(dirZip, fillPath)
                } else {
                    // 读取每个文件为buffer存到zip中
                    allFolder.file(fileName, fs.readFileSync(fillPath))
                }
            })
        }
        const removeExistedZip = () => {
            const dest = `${distPath}\\${fileZipName}`
            if (fs.existsSync(dest)) {
                fs.unlinkSync(dest)
            }
        }
        const zipDir = function () {
            readDir(allFolder, distPath)
            zip.generateAsync({
                type: 'nodebuffer', // 压缩类型
                compression: 'DEFLATE', // 压缩算法
                compressionOptions: {
                    // 压缩级别
                    level: 9
                }
            }).then((content) => {
                const dest = `${distPath}\\${fileZipName}`
                removeExistedZip()
                // 把zip包写到硬盘中,这个content现在是一段buffer
                fs.writeFileSync(dest, content)
            })
        }
        removeExistedZip()
        zipDir(distPath)
    }
    return {
        name: 'vite-plugin-auto-zip',
        apply: 'build',
        closeBundle() {
            makeZip()
        }
    }
}

export { plugin as default };

在vite.config.js中引入插件

// 引入zip压缩插件
import zipCompress from './zip-compress'

// 在plugins中使用

plugins: [
    vue(),
    zipCompress("fileName"),
    ...
],

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue项目中使用Echarts,可以通过以下步骤自动生成X轴数据: 1. 在Vue组件中,引入Echarts的库文件。可以通过CDN引入,或者使用npm安装echarts并引入。 2. 在Vue组件的data中定义一个数组,用来存储自动生成的X轴数据。 3. 在mounted或者created钩子函数中,使用循环或者其他方式生成X轴数据,并将生成的数据存储到之前定义的数组中。 4. 在Echarts的配置项中,使用刚才生成的X轴数据。 以下是一个示例代码: ```javascript <template> <div id="chart"></div> </template> <script> import echarts from 'echarts'; export default { data() { return { xAxisData: [] }; }, mounted() { this.generateXAxisData(); this.renderChart(); }, methods: { generateXAxisData() { // 生成X轴数据,可以根据需求自定义逻辑 for (let i = 1; i <= 10; i++) { this.xAxisData.push(`Category ${i}`); } }, renderChart() { const chart = echarts.init(document.getElementById('chart')); const option = { xAxis: { type: 'category', data: this.xAxisData }, // 其他配置项 }; chart.setOption(option); } } }; </script> <style> #chart { width: 100%; height: 300px; } </style> ``` 在上述示例代码中,我们在mounted钩子函数中先调用generateXAxisData方法生成X轴数据,然后在renderChart方法中使用这些数据来配置Echarts的xAxis项。最后,通过chart.setOption方法将配置项应用到图表中。 这样,在Vue项目中使用Echarts时,X轴数据就能够自动生成了。你可以根据实际需求修改generateXAxisData方法来生成符合你项目需求的X轴数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值