1 Star 1 Fork 0

Blue/vite-plugin-files-copy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.js 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
tcly861204 提交于 2022-01-09 18:31 . fix: 优化可复制
const path = require('path')
const fs = require('fs')
const fse = require('fs-extra')
const checkDirExist = function (dir) {
try {
return fs.statSync(dir).isDirectory()
} catch (e) {
if (e.code === 'ENOENT') {
return false
}
}
}
const copy = function (from, dist) {
if (!checkDirExist(from)) {
console.error('可复制的文件或者目录不存在')
return false
}
if (!checkDirExist(dist)) {
fse.mkdirSync(dist)
}
const dir = fs.readdirSync(from)
dir.forEach((file) => {
const filePath = path.resolve(from, file)
fs.stat(filePath, (_, stat) => {
if (stat.isFile()) {
// 创建读取流
const readStream = fs.createReadStream(filePath)
// 创建写入流
const writeStream = fs.createWriteStream(path.resolve(dist, file))
// 复制写入文件
readStream.pipe(writeStream)
} else if (stat.isDirectory()) {
copy(filePath, path.resolve(dist, file))
}
})
})
}
let viteConfig = null
module.exports = function (options) {
return {
name: 'vite-plugin-files-copy',
apply: 'build',
configResolved(resolvedConfig) {
viteConfig = resolvedConfig
},
async writeBundle() {
const root = viteConfig.root
try {
options.patterns.forEach((item) => {
if (item.from && item.to) {
const fromdir = path.resolve(root, item.from)
const distDir = path.resolve(root, item.to)
copy(fromdir, distDir)
}
})
} catch (_) {}
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tcly861204/vite-plugin-files-copy.git
[email protected]:tcly861204/vite-plugin-files-copy.git
tcly861204
vite-plugin-files-copy
vite-plugin-files-copy
main

搜索帮助