@arcmantle/vite-plugin-copy

中文文档
1.0.3发布于 2025/07/18Apache-2.0TypeScriptES Module近 30 天下载 8217

Vite 构建时复制文件 .

中文使用文档
文档目录21 节
  1. 特征
  2. 安装
  3. 使用量
  4. 基本用途
  5. 高级使用
  6. API 参考
  7. viteCopy(options)
  8. 变形函数
  9. 重命名函数
  10. 实例
  11. 复制静态资产
  12. 复制到模式匹配
  13. 动态重命名
  14. 环境转型
  15. 监视模式优化
  16. 虎克计时
  17. 光荣选项
  18. 所需资源
  19. 依赖关系
  20. 许可证
  21. 捐款

@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)

选项

选项类型默认说明
targetsTarget[][]复制目标阵列
copyOncebooleanfalse只复制一次项目( 在监视模式下有用)
hookstring'buildEnd'滚动钩子( “ 配置 ” 、 “ 构建结束 ” 或任何有效的钩子)
verbosebooleanfalse启用动词记录
flattenbooleantrue删除已复制文件的基础目录结构

目标配置

选项类型说明
fromstring | string[]源路径或 glob 模式
tostring | string[]目标路径
renamestring | function生成文件名的新文件名或函数
transformfunction转换文件内容的函数( 只与文件一起工作)

变形函数

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的一部分. 欢迎捐款!

版本与发布记录

@arcmantle/vite-plugin-copy 共发布 4 个版本,首次发布于 2025/07/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2025/07/181.3 年前
1.0.22025/07/181.3 年前
1.0.12025/07/171.3 年前
1.0.02025/07/061.3 年前

关于 @arcmantle/vite-plugin-copy 的常见问题

@arcmantle/vite-plugin-copy 是什么?

Vite 构建时复制文件 .

如何安装 @arcmantle/vite-plugin-copy?

在项目目录运行 npm install @arcmantle/vite-plugin-copy,或使用 pnpm add / yarn add / bun add。

@arcmantle/vite-plugin-copy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@arcmantle/vite-plugin-copy 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@arcmantle/vite-plugin-copy 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@arcmantle/vite-plugin-copy 可以商用吗?

@arcmantle/vite-plugin-copy 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@arcmantle/vite-plugin-copy 的下载量有多少?

根据 npm 官方下载统计,@arcmantle/vite-plugin-copy 在 2026/10/07 – 2026/11/05 这 30 天内被下载 8,217 次,日均约 274 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@arcmantle/vite-plugin-copy 还在维护吗?

最新版本 1.0.3 发布于 2025/07/18(1.3 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/07/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。