@arcmantle/vite-plugin-copy

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

Copies files during vite build.

原始使用文档
文档目录21 节
  1. Features
  2. Installation
  3. Usage
  4. Basic Usage
  5. Advanced Usage
  6. API Reference
  7. viteCopy(options)
  8. Transform Function
  9. Rename Function
  10. Examples
  11. Copy Static Assets
  12. Copy with Pattern Matching
  13. Dynamic Renaming
  14. Environment-based Transformation
  15. Watch Mode Optimization
  16. Hook Timing
  17. Globby Options
  18. Requirements
  19. Dependencies
  20. License
  21. Contributing

@arcmantle/vite-plugin-copy

A Vite plugin that copies files during the build process with advanced features like file transformation, renaming, and flattening.

Features

  • 🚀 Fast & Efficient: Uses globby for pattern matching and fs-extra for optimized file operations
  • 🔄 File Transformation: Modify file contents during copy
  • 📁 Flexible Destination: Copy to single or multiple destinations
  • 🏷️ File Renaming: Rename files during copy with string or function-based renaming
  • 📂 Directory Flattening: Option to flatten directory structure
  • ⚡ Copy Once Mode: Perfect for watch mode to avoid redundant copying
  • 🎯 Configurable Hooks: Choose when copying occurs during the build process
  • 📊 Verbose Logging: Optional detailed logging of copy operations

Installation

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

Usage

Basic Usage

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

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

Advanced Usage

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 Reference

viteCopy(options)

Options

OptionTypeDefaultDescription
targetsTarget[][]Array of copy targets
copyOncebooleanfalseCopy items only once (useful in watch mode)
hookstring'buildEnd'Rollup hook to use ('config', 'buildEnd', or any valid hook)
verbosebooleanfalseEnable verbose logging
flattenbooleantrueRemove base directory structure of copied files

Target Configuration

OptionTypeDescription
fromstring | string[]Source path or glob pattern(s)
tostring | string[]Destination path(s)
renamestring | functionNew filename or function to generate filename
transformfunctionFunction to transform file contents (only works with files)

Transform Function

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

Rename Function

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

Examples

Copy Static Assets

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

Copy with Pattern Matching

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

Dynamic Renaming

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

Environment-based Transformation

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

Watch Mode Optimization

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

Hook Timing

  • config: Runs early during configuration setup
  • buildEnd: Runs after the build is complete (default)
  • Any valid Rollup hook name

Globby Options

This plugin uses globby for file matching. You can pass any globby options in the target configuration:

viteCopy({
  targets: [
    {
      from: 'src/**/*',
      to: 'dist/',
      ignore: ['**/*.test.js'],
      dot: true
    }
  ]
})

Requirements

  • Node.js >= 22
  • Vite >= 7.0.0

Dependencies

  • fs-extra: Enhanced file system operations with promises
  • globby: Fast glob matching for file pattern detection

License

Apache-2.0

Contributing

This package is part of the Arcmantle Weave monorepo. Contributions are welcome!

版本与发布记录

@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 是什么?

Copies files during vite build.

如何安装 @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 和提交记录判断。