vue项目打包自动生成dist.zip压缩包

1.使用 npm i jszip -D 安装依赖

2.根目录创建craeteZip.ts文件

//  zip.js 打包自动生成zip包,需npm i jszip -D
const plugin = function(fileName = 'dist', output:any){
    const path = require('path');
    if(!output)output = path.resolve(__dirname,'./dist');//./distTest是根据实际路径而来
    fileName += '.zip';
    const makeZip = function () {
        const path = require('path')
        const fs = require('fs')
        const JSZip = require('jszip');
        const zip = new JSZip()
        const distPath:any = path.resolve(output)
        const readDir = function (zip:any, dirPath:string) {
            // 读取dist下的根文件目录
            const files = fs.readdirSync(dirPath);
            files.forEach((fileName:string) => {
                const fillPath = path.join(dirPath, "./", fileName)
                const file = fs.statSync(fillPath);
                // 如果是文件夹的话需要递归遍历下面的子文件
                if (file.isDirectory()) {
                    const dirZip = zip.folder(fileName);
                    readDir(dirZip, fillPath);
                } else {
                    // 读取每个文件为buffer存到zip中
                    zip.file(fileName, fs.readFileSync(fillPath))
                }
            });
        }
        const removeExistedZip = () => {
            const dest = path.join(distPath, './' + fileName)
            if (fs.existsSync(dest)) {
                fs.unlinkSync(dest)
            }
        }
        const zipDir = function () {
            const distZip =  zip.folder('dist');
            readDir(distZip, distPath);
            zip.generateAsync({
                type: "nodebuffer", // 压缩类型
                compression: "DEFLATE", // 压缩算法
                compressionOptions: { // 压缩级别
                    level: 9
                }
            }).then((content:any) => {
                const dest = path.join(distPath, '../' + fileName)
                removeExistedZip()
                // 把zip包写到硬盘中,这个content现在是一段buffer
                fs.writeFileSync(dest, content);
            });
        }
        removeExistedZip()
        zipDir()
    }
    return {
        name: 'vite-plugin-auto-zip',
        apply: 'build',
        closeBundle(){
            makeZip()
        }
    }
}
module.exports = plugin

3.在vite.config.ts 中的 plugins 中引入使用即可

import createZip from "./createZip"

........
  plugins: [
    createZip()
  ],
  // 其他 Vite 配置项

Vue项目中,`public`目录通常含了不需要经过webpack处理的静态资源,比如一些第三方库文件等。当你执行`npm run build`或者使用Vue CLI提供的`build`命令进行项目打包时,默认情况下,webpack会将`src`目录下的源代码编译打包到`dist`目录中,并且会在`dist`目录中创建一个`index.html`文件作为项目的入口文件。 如果你发现打包后的`dist`目录中没有`public`目录,这可能是因为在Vue项目构建的过程中,默认情况下webpack会将`public`目录下的内容直接复制到`dist`目录下。如果你的`public`目录中的文件没有出现在`dist`目录中,可能有以下几个原因: 1. 你没有在`vue.config.js`中进行相应的配置,导致webpack没有将`public`目录下的文件复制到`dist`目录。 2. 你可能在`vue.config.js`中或者在构建命令中添加了一些特定的配置,比如修改了`assetsDir`,导致资源被放在了不同的目录下。 3. 项目的某些配置可能有误,比如路径指定错误,导致`public`目录下的文件没有被正确复制。 要解决这个问题,你可以检查`vue.config.js`文件中的配置,确保你的`public`目录设置正确,并且有正确的路径指向。通常,你需要确认`publicDir`选项被设置为你的`public`目录的路径: ```javascript module.exports = { // ... publicDir: 'public', // ... } ``` 在配置正确的情况下,执行打包命令后,`public`目录下的文件应该会出现在`dist`目录中相应的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值