手写webpack插件,将第三方插件引用的文件添加到dist文件~

taro小程序引入第三方ui组件 vant 打包时,无法将插件中引用的文件加入到dist文件,每次打包之后需要手动将文件添加到dist文件中,因此写了这个插件,编译后,将文件写入到dist文件对应位置,避免手动添加

在这里插入图片描述
copyDirFile.js

var fs = require('fs');
var path = require('path');
  
// 根目录
let BASEPATHURL = path.resolve(__dirname, '../../')
  
// 移动目录
let startFileDirectory = BASEPATHURL + "/src/components/vant-weapp/wxs"
// 放置目录
let endFileDirectory = BASEPATHURL + "/dist/components/vant-weapp/wxs"
  
var startDate = new Date().getTime()
 
  
// 文本替换
function replaceText(url) {
    let testHtml = fs.readFileSync(url + "/index.html", 'utf8')
    console.log("修改之前", testHtml)
    fs.writeFileSync(url + '/index.jsp', '888888' + testHtml, 'utf8')
    console.log("新建index.jsp文件完成")
    let newTestHtml = fs.readFileSync(url + "/index.jsp", 'utf8')
    console.log("index.jsp文件内容", newTestHtml)
    console.log("删除index.html文件开始")
    fs.unlinkSync(url + "/index.html");
    console.log("删除index.html文件完成")
    // 耗时
    let endDate = new Date().getTime()
  
    console.log("本次耗时", (endDate - startDate) , "ms")
}
  
// 删除
function rmDirFile(path, cb) {
    let files = [];
    console.log("开始删除")
    if (fs.existsSync(path)) {
        var count = 0
        var checkEnd = function () {
            console.log("进度", count)
            console.log('files', files)
            cb && cb()
            // ++count == files.length && cb && cb()
        }
        files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            let curPath = path + "/" + file;
            if (fs.statSync(curPath).isDirectory()) {
                console.log("遇到文件夹", curPath)
                rmDirFile(curPath, checkEnd);
            } else {
                fs.unlinkSync(curPath);
                console.log("删除文件完成", curPath)
                checkEnd()
            }
        });
        // 如果删除文件夹为放置文件夹根目录  不执行删除
        if (path == endFileDirectory) {
            console.log("删除文件夹完成", path)
        } else {
            fs.rmdirSync(path);
        }
        //为空时直接回调
        files.length === 0 && cb && cb()
    } else {
        cb && cb()
    }
}
  
// 复制文件
function copyFile(srcPath, tarPath, cb) {
    var rs = fs.createReadStream(srcPath)
    rs.on('error', function (err) {
        if (err) {
            console.log('read error', srcPath)
        }
        cb && cb(err)
    })
  
    var ws = fs.createWriteStream(tarPath)
    ws.on('error', function (err) {
        if (err) {
            console.log('write error', tarPath)
        }
        cb && cb(err)
    })
  
    ws.on('close', function (ex) {
        cb && cb(ex)
    })
  
    rs.pipe(ws)
    console.log("复制文件完成", srcPath)
}
  
// 复制文件夹所有
function copyDir(srcDir, tarDir, cb) {
    console.log('复制文件夹')
    if (fs.existsSync(tarDir)) {
        fs.readdir(srcDir, function (err, files) {
            console.log('files',srcDir, files)
            var count = 0
            var checkEnd = function () {
                console.log("进度", count)
                ++count == files.length && cb && cb()
            }
  
            if (err) {
                checkEnd()
                return
            }
  
            files.forEach(function (file) {
                var srcPath = path.join(srcDir, file)
                var tarPath = path.join(tarDir, file)
  
                fs.stat(srcPath, function (err, stats) {
                    if (stats.isDirectory()) {
                        fs.mkdir(tarPath, function (err) {
                            if (err) {
                                console.log(err)
                                return
                            }
  
                            copyDir(srcPath, tarPath, checkEnd)
                            console.log("复制文件完成", srcPath)
                        })
                    } else {
                        copyFile(srcPath, tarPath, checkEnd)
                        console.log("复制文件完成", srcPath)
                    }
                })
            })
  
            //为空时直接回调
            files.length === 0 && cb && cb()
        })
  
    } else {
        console.log('创建文件夹', tarDir)
        fs.mkdir(tarDir, {recursive:true},function (err) {
            if (err) {
                console.log(err)
                return
            }
            console.log('创建文件夹', tarDir)
            copyDir(srcDir, tarDir, cb)
        })
    }
  
}
 
class CopyDirFile {
    apply (compiler) {
      // hooks大全
      // https://www.webpackjs.com/api/compiler-hooks/
      compiler.hooks.done.tap('copyDirFile', rmDirFile)
    }
  }
 
 
    
// 删除复制执行
rmDirFile(endFileDirectory, () => {
    copyDir(startFileDirectory, endFileDirectory, (res) => {
        console.log("全部复制完成")
        console.log("修改文件内容")
        let startFile = BASEPATHURL + "/src/components/vant-weapp/dropdown-menu"
        let endFile = BASEPATHURL + "/dist/components/vant-weapp/dropdown-menu"
        copyDir(startFile, endFile)
    })
});
 
module.exports = CopyDirFile
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值