@arcmantle/vite-plugin-副本
一个Vite插件,在构建过程中复制文件,具有文件转换,更名,平分等高级功能.
特征
- 🚀 快速高效( I): 用于图案匹配和优化文件操作的 fs- extra
- 🔄 文件转换: 复制时修改文件内容
- 📁 灵活目标: 复制到单个或多个目的地
- 🏷️ 文件重命名: 复制时以字符串或基于函数的重命名重命名文件
- 📂 目录平滑: 平整目录结构的选项
- ⚡ 复制一次模式: 适合监视模式以避免重复复制
- 🎯 可配置钩: 在构建过程中选择复制时间
- 📊 微博日志: 复制业务的可选详细记录
安装
npm install @arcmantle/vite-plugin-copy
# or
pnpm add @arcmantle/vite-plugin-copy
# or
yarn add @arcmantle/vite-plugin-copy
使用量
基本用途
import { viteCopy } from '@arcmantle/vite-plugin-copy';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
viteCopy({
targets: [
{
from: 'src/assets/**/*',
to: 'dist/assets'
}
]
})
]
});
高级使用
import { viteCopy } from '@arcmantle/vite-plugin-copy';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
viteCopy({
targets: [
// Copy vendor styles
{
from: './node_modules/@arcmantle/elements/styles/*',
to: './public/vendor/mimic-elements',
},
// Copy and rename files
{
from: 'src/config.json',
to: 'dist/',
rename: 'app-config.json'
},
// Copy to multiple destinations
{
from: 'src/shared/**/*',
to: ['dist/client/shared', 'dist/server/shared']
},
// Transform file contents
{
from: 'src/template.html',
to: 'dist/index.html',
transform: (contents, filename) => {
return contents.toString().replace('{{VERSION}}', process.env.npm_package_version);
}
}
],
hook: 'config',
copyOnce: true,
verbose: true
})
]
});
API 参考
viteCopy(options)
选项
| 选项 | 类型 | 默认 | 说明 |
|---|---|---|---|
targets | Target[] | [] | 复制目标阵列 |
copyOnce | boolean | false | 只复制一次项目( 在监视模式下有用) |
hook | string | 'buildEnd' | 滚动钩子( “ 配置 ” 、 “ 构建结束 ” 或任何有效的钩子) |
verbose | boolean | false | 启用动词记录 |
flatten | boolean | true | 删除已复制文件的基础目录结构 |
目标配置
| 选项 | 类型 | 说明 |
|---|---|---|
from | string | string[] | 源路径或 glob 模式 |
to | string | string[] | 目标路径 |
rename | string | function | 生成文件名的新文件名或函数 |
transform | function | 转换文件内容的函数( 只与文件一起工作) |
变形函数
transform: (contents, filename) => {
// contents: Buffer - file contents
// filename: string - original filename
// return: any - transformed content
}
重命名函数
rename: (name, extension, fullPath) => {
// name: string - filename without extension
// extension: string - file extension without dot
// fullPath: string - full source path
// return: string - new filename
}
实例
复制静态资产
viteCopy({
targets: [
{
from: 'src/assets/**/*',
to: 'dist/assets'
}
]
})
复制到模式匹配
viteCopy({
targets: [
{
from: ['src/**/*.json', 'src/**/*.xml'],
to: 'dist/config'
}
]
})
动态重命名
viteCopy({
targets: [
{
from: 'src/locales/*.json',
to: 'dist/i18n',
rename: (name, ext, fullPath) => `${name}.locale.${ext}`
}
]
})
环境转型
viteCopy({
targets: [
{
from: 'src/config.template.json',
to: 'dist/config.json',
transform: (contents) => {
const config = JSON.parse(contents.toString());
config.environment = process.env.NODE_ENV;
config.buildTime = new Date().toISOString();
return JSON.stringify(config, null, 2);
}
}
]
})
监视模式优化
viteCopy({
targets: [
{
from: 'node_modules/large-library/dist/**/*',
to: 'public/vendor/large-library'
}
],
copyOnce: true, // Only copy once in watch mode
hook: 'config' // Copy early in the build process
})
虎克计时
config: 配置设置时提前运行buildEnd: 构建完成后运行( 默认)- 任何有效的滚动钩子名称
光荣选项
此插件使用 荣耀 用于匹配文件。 您可以通过目标配置中的任何光滑选项 :
viteCopy({
targets: [
{
from: 'src/**/*',
to: 'dist/',
ignore: ['**/*.test.js'],
dot: true
}
]
})
所需资源
- 节点.js 22
- 维特 >= 7.0.0
依赖关系
- 超时: 增强文件系统操作并许诺
- 荣耀: 文件图案检测的快速拼接
许可证
阿帕奇 - 2.0
捐款
这个包裹是Arcmantle Weave monorepo的一部分. 欢迎捐款!