Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

rollup-plugin-copy


Copy files and folders, with glob support.

Installation


  1. ``` shell
  2. # yarn
  3. yarn add rollup-plugin-copy -D

  4. # npm
  5. npm install rollup-plugin-copy -D
  6. ```

Usage


  1. ``` js
  2. // rollup.config.js
  3. import copy from 'rollup-plugin-copy'

  4. export default {
  5.   input: 'src/index.js',
  6.   output: {
  7.     file: 'dist/app.js',
  8.     format: 'cjs'
  9.   },
  10.   plugins: [
  11.     copy({
  12.       targets: [
  13.         { src: 'src/index.html', dest: 'dist/public' },
  14.         { src: ['assets/fonts/arial.woff', 'assets/fonts/arial.woff2'], dest: 'dist/public/fonts' },
  15.         { src: 'assets/images/**/*', dest: 'dist/public/images' }
  16.       ]
  17.     })
  18.   ]
  19. }
  20. ```

Configuration


There are some useful options:

targets


Type: Array | Default: []

Array of targets to copy. A target is an object with properties:

src(string Array ): Path or glob of what to copy
dest(string Array ): One or more destinations where to copy
rename(string Function ): Change destination file or folder name
transform(Function ): Modify file contents

Each object should have srcand destproperties, renameand transformare optional. globby is used inside, check it for glob pattern examples.

File

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'src/index.html', dest: 'dist/public' }]
  4. })
  5. ```

Folder

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/images', dest: 'dist/public' }]
  4. })
  5. ```

Glob

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/*', dest: 'dist/public' }]
  4. })
  5. ```

Glob: multiple items

  1. ``` js
  2. copy({
  3.   targets: [{ src: ['src/index.html', 'src/styles.css', 'assets/images'], dest: 'dist/public' }]
  4. })
  5. ```

Glob: negated patterns

  1. ``` js
  2. copy({
  3.   targets: [{ src: ['assets/images/**/*', '!**/*.gif'], dest: 'dist/public/images' }]
  4. })
  5. ```

Multiple targets

  1. ``` js
  2. copy({
  3.   targets: [
  4.     { src: 'src/index.html', dest: 'dist/public' },
  5.     { src: 'assets/images/**/*', dest: 'dist/public/images' }
  6.   ]
  7. })
  8. ```

Multiple destinations

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'src/index.html', dest: ['dist/public', 'build/public'] }]
  4. })
  5. ```

Rename with a string

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'src/app.html', dest: 'dist/public', rename: 'index.html' }]
  4. })
  5. ```

Rename with a function

  1. ``` js
  2. copy({
  3.   targets: [{
  4.     src: 'assets/docs/*',
  5.     dest: 'dist/public/docs',
  6.     rename: (name, extension, fullPath) => `${name}-v1.${extension}`
  7.   }]
  8. })
  9. ```

Transform file contents

  1. ``` js
  2. copy({
  3.   targets: [{
  4.     src: 'src/index.html',
  5.     dest: 'dist/public',
  6.     transform: (contents, filename) => contents.toString().replace('__SCRIPT__', 'app.js')
  7.   }]
  8. })
  9. ```

verbose


Type: boolean | Default: false

Output copied items to console.

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/*', dest: 'dist/public' }],
  4.   verbose: true
  5. })
  6. ```

hook


Type: string | Default: buildEnd

Rollup hook the plugin should use. By default, plugin runs when rollup has finished bundling, before bundle is written to disk.

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/*', dest: 'dist/public' }],
  4.   hook: 'writeBundle'
  5. })
  6. ```

copyOnce


Type: boolean | Default: false

Copy items once. Useful in watch mode.

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/*', dest: 'dist/public' }],
  4.   copyOnce: true
  5. })
  6. ```

flatten


Type: boolean | Default: true

Remove the directory structure of copied files.

  1. ``` js
  2. copy({
  3.   targets: [{ src: 'assets/**/*', dest: 'dist/public' }],
  4.   flatten: false
  5. })
  6. ```

All other options are passed to packages, used inside:

globby
fs-extra copy function

Original Author


Cédric Meuter

License


MIT
Last Updated: 2023-07-23 19:12:03