# @arcmantle/vite-plugin-copy 中文文档

规范地址：https://npmcn.com/package/%40arcmantle/vite-plugin-copy/

版本：1\.0\.3
npm 元数据更新时间：2025-07-17T22:55:20.437Z
中文译文更新时间：2026-11-06T14:51:25.771Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40arcmantle/vite-plugin-copy
原文对照：https://npmcn.com/package/%40arcmantle/vite-plugin-copy/?lang=original
项目主页：https://github.com/arcmantle/vite-plugin-copy#readme
源码仓库：https://github.com/arcmantle/vite-plugin-copy

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：25.4 kB，13 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：2 个
- 首次发布：2025-07-05T22:40:44.480Z
- 最新版本发布：2025-07-17T22:55:20.223Z
- 近 30 天下载量：8217（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

Vite 构建时复制文件 .

## 安装

```sh
npm install @arcmantle/vite-plugin-copy
```

也可以使用：`pnpm add @arcmantle/vite-plugin-copy`、`yarn add @arcmantle/vite-plugin-copy`、`bun add @arcmantle/vite-plugin-copy`

## README

# @arcmantle/vite-plugin-副本

一个Vite插件,在构建过程中复制文件,具有文件转换,更名,平分等高级功能.

## 特征

-   🚀 **快速高效( I)**: 用于图案匹配和优化文件操作的 fs- extra
-   🔄 **文件转换**: 复制时修改文件内容
-   📁 **灵活目标**: 复制到单个或多个目的地
-   🏷️ **文件重命名**: 复制时以字符串或基于函数的重命名重命名文件
-   📂 **目录平滑**: 平整目录结构的选项
-   ⚡ **复制一次模式**: 适合监视模式以避免重复复制
-   🎯 **可配置钩**: 在构建过程中选择复制时间
-   📊 **微博日志**: 复制业务的可选详细记录

## 安装

```bash
npm install @arcmantle/vite-plugin-copy
# or
pnpm add @arcmantle/vite-plugin-copy
# or
yarn add @arcmantle/vite-plugin-copy
```

## 使用量

### 基本用途

```javascript
import { viteCopy } from '@arcmantle/vite-plugin-copy';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    viteCopy({
      targets: [
        {
          from: 'src/assets/**/*',
          to: 'dist/assets'
        }
      ]
    })
  ]
});
```

### 高级使用

```javascript
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` | 转换文件内容的函数( 只与文件一起工作) |

### 变形函数

```javascript
transform: (contents, filename) => {
  // contents: Buffer - file contents
  // filename: string - original filename
  // return: any - transformed content
}
```

### 重命名函数

```javascript
rename: (name, extension, fullPath) => {
  // name: string - filename without extension
  // extension: string - file extension without dot
  // fullPath: string - full source path
  // return: string - new filename
}
```

## 实例

### 复制静态资产

```javascript
viteCopy({
  targets: [
    {
      from: 'src/assets/**/*',
      to: 'dist/assets'
    }
  ]
})
```

### 复制到模式匹配

```javascript
viteCopy({
  targets: [
    {
      from: ['src/**/*.json', 'src/**/*.xml'],
      to: 'dist/config'
    }
  ]
})
```

### 动态重命名

```javascript
viteCopy({
  targets: [
    {
      from: 'src/locales/*.json',
      to: 'dist/i18n',
      rename: (name, ext, fullPath) => `${name}.locale.${ext}`
    }
  ]
})
```

### 环境转型

```javascript
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);
      }
    }
  ]
})
```

### 监视模式优化

```javascript
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`**: 构建完成后运行( 默认)
-   任何有效的滚动钩子名称

## 光荣选项

此插件使用 [荣耀](https://github.com/sindresorhus/globby) 用于匹配文件。 您可以通过目标配置中的任何光滑选项 :

```javascript
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 是什么？

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 和提交记录判断。

