vite-plugin-copy2

raw JSON →
1.1.0 verified Mon Apr 27 auth: no javascript

A Vite plugin for copying single or multiple files during the build process. Current stable version is 1.1.0. It is a lightweight alternative to rollup-plugin-copy, specifically designed for Vite's build pipeline. The plugin supports copying files and folders before or after the build, with glob patterns and renaming. It provides a simple API by passing an array of source-destination pairs. Unlike other copy plugins, it integrates seamlessly with Vite's plugin system and respects the build lifecycle.

error Error: Cannot find module 'vite-plugin-copy2'
cause Package not installed or not listed in devDependencies.
fix
Run 'npm install vite-plugin-copy2 -D' or 'yarn add vite-plugin-copy2 -D'.
error TypeError: VitePluginCopy is not a function
cause Using default import instead of named import.
fix
Use 'import { VitePluginCopy } from 'vite-plugin-copy2'' instead of 'import VitePluginCopy from ...'.
error Error: The plugin only works in build mode. Skipping during dev.
cause Expected behavior: plugin is only active during build.
fix
This is not an error; if you need file copying during dev, use a different approach or configure separate dev copy logic.
error Error: ENOENT: no such file or directory, stat 'path/to/source'
cause The source file or folder specified in the config does not exist.
fix
Verify that all source paths are correct and exist. Use absolute or correct relative paths.
error Error: The 'dest' option must be a string
cause dest field missing or not a string in one of the copy entries.
fix
Ensure each copy entry has a 'dest' property that is a string.
breaking The plugin only works with Vite's build pipeline; it does not function during dev server. Source files are copied only at build time.
deprecated
gotcha If a source file does not exist, the plugin may throw an error and fail the build. Ensure all source paths are correct.
gotcha The dest path must be relative to the project root. Absolute paths may not work as expected.
gotcha When copying a folder, the entire directory structure is preserved; use the 'rename' option to flatten if needed.
npm install vite-plugin-copy2
yarn add vite-plugin-copy2
pnpm add vite-plugin-copy2

Shows how to configure the plugin in a Vite config file to copy multiple files and folders during the build.

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { VitePluginCopy } from 'vite-plugin-copy2'

export default defineConfig({
  plugins: [
    vue(),
    VitePluginCopy([
      { src: ['demo/a.js', 'demo/b.js'], dest: 'dist/' },
      { src: 'demo/c.js', dest: 'dist/static/' },
      { src: 'node_modules', dest: 'dist/node_modules' }
    ])
  ]
})