Registry / web-framework / vite-plugin-static-copy

vite-plugin-static-copy

JSON →
library4.1.0jsnpmunverified

vite-plugin-static-copy is a Vite plugin designed to copy static assets during both development and production builds, mirroring the functionality of `rollup-plugin-copy` but optimized for Vite's ecosystem. It is currently at version 4.1.0 and maintains an active release cadence with frequent updates and bug fixes, often several per month. Key differentiators include its dedicated dev server support, which avoids copying files during development for faster startup times by serving them directly. Unlike Vite's built-in public directory, this plugin allows for more granular control over specific files and directories, offering options like renaming, stripping base paths (`stripBase`), and transformation of content. It specifically targets scenarios where assets need to be programmatically managed or placed outside the standard public directory, or when `import` statements are not suitable for asset inclusion. It uses `tinyglobby` for pattern matching, aligning with Vite's internal dependencies, and preserves directory structure by default, unlike `rollup-plugin-copy`'s default `flatten: true`.

npm install vite-plugin-static-copy
INSTALL
IMPORT
SIG · VITE-PLUGIN-STATIC
V
vite-plugin-static-copy
web-frameworkjavascriptv4.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

viteStaticCopy
import { viteStaticCopy } from 'vite-plugin-static-copy'
const viteStaticCopy = require('vite-plugin-static-copy')
This package is primarily consumed as an ESM module within `vite.config.ts` or `vite.config.js`.
ViteStaticCopyOptions
import type { ViteStaticCopyOptions } from 'vite-plugin-static-copy'
Type import for configuring the plugin options. Useful for TypeScript users to ensure correct configuration.
RenameOptions
import type { RenameOptions } from 'vite-plugin-static-copy'
Type import for the `rename` option's object form, including `stripBase` and `name` properties.

Demonstrates how to configure `vite-plugin-static-copy` in `vite.config.ts` to copy various static assets, including renaming and stripping base directories, using `normalizePath` for Windows compatibility.

import { defineConfig, normalizePath } from 'vite'; import { viteStaticCopy } from 'vite-plugin-static-copy'; import path from 'node:path'; export default defineConfig({ plugins: [ viteStaticCopy({ targets: [ { src: normalizePath(path.resolve(__dirname, 'public/images/**/*')), // Copy all images dest: 'assets/images', // To dist/assets/images }, { src: 'node_modules/some-lib/dist/web-worker.js', // Copy a specific web worker dest: 'workers', // To dist/workers rename: 'my-worker.js', // Rename it }, { src: 'src/data/**/*.json', // Copy JSON files, stripping base path dest: 'data', rename: { stripBase: 1 }, // `src/data/config.json` -> `dist/data/config.json` }, { src: normalizePath(path.resolve(__dirname, 'assets/favicon.ico')), dest: '', // Copy to the root of the output directory } ] }) ] });
Debug
Known issues
breakingStarting with v4.0.0, the plugin's glob behavior was simplified. Glob patterns now only match files, not directories. Previously, matching a directory would recursively copy its contents. This means you must explicitly match files (e.g., `dir/**/*` instead of `dir`).
fix
Update `src` glob patterns to explicitly match files (e.g., use `path/to/dir/**/*` instead of `path/to/dir`).
affects: >=4.0.0
gotchaOn Windows, `path.resolve` returns backslash-separated paths (e.g., `C:\project\foo`), which are treated as escape characters by the underlying `tinyglobby` library. This can lead to incorrect or no matches for `src` paths.
fix
Always wrap `path.resolve()` calls for `src` paths with `normalizePath` from Vite (e.g., `normalizePath(path.resolve(__dirname, './foo'))`).
affects: >=3.0.0
gotchaThe `dest` option is relative to Vite's `build.outDir` (default `dist`). If you need to copy files outside of `build.outDir`, this plugin is not suitable during the build phase; consider `rollup-plugin-copy` instead for non-dev server related copying.
fix
Ensure `dest` paths are relative to your `build.outDir` or use an alternative plugin for copying outside this directory.
affects: >=3.0.0
breakingVersion 4.1.0 introduced new capabilities for the `rename` option, allowing it to be an object with a `name` property to replace the file's basename or a `RenameObject` returned by a rename function. While an enhancement, ensure existing complex rename functions handle this new type if they were previously returning only strings.
fix
Review complex `rename` functions to ensure compatibility or leverage the new `name` and `stripBase` object forms for simpler configuration.
affects: >=4.1.0
gotchaPrior to v3.1.5, the plugin would copy for all environments if multiple existed. Since v3.1.5, it defaults to only copying for the client environment, which was then made configurable via the `environment` option in v3.2.0. If your build process involves multiple environments and requires static copy in non-client contexts, you might need to adjust this option.
fix
If copying is needed for non-client environments (e.g., SSR), use the `environment` option: `{ environment: ['client', 'ssr'] }` or `{ environment: 'all' }`.
affects: >=3.1.5
Errors
Common errors & fixes
Error: EEXIST: file already exists, mkdir 'C:\project\dist\assets\images\C:\project\public\images'
Using `path.resolve` directly on Windows for `src` paths without `normalizePath`, leading to incorrect absolute paths being passed to the glob library, causing unexpected nested directory creation.
fix
Ensure all `path.resolve` outputs used in `src` options are passed through Vite's `normalizePath`: `src: normalizePath(path.resolve(__dirname, 'public/images/**/*'))`.
Vite config error: [vite-plugin-static-copy] 'src' must be a string or array of strings, or an array of objects. Received: undefined
Incorrectly defined `targets` array or missing `src` property within a target object in the `viteStaticCopy` configuration.
fix
Verify that each object within the `targets` array has a `src` property defined with a valid glob string or an array of strings.
Files are not being copied to the `dist` directory, but there are no errors.
The glob pattern specified in `src` is incorrect or too restrictive, failing to match any files, or the `environment` option is not configured correctly for the build process.
fix
Double-check `src` glob patterns for correctness (e.g., ensure `**/*` is used to match files inside directories). If running a non-client build, explicitly set the `environment` option (e.g., `{ environment: 'all' }`). Enable debug logging with `DEBUG=vite:plugin-static-copy npm run dev` to see matched files.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies
viterequiredPeer dependency for Vite plugin integration.
Agent activity
12 hits · last 30 days
node
10
Bingbot
1
OpenAI (training)
1
Resources
vite-plugin-static-copy — npm install vite-plugin-static-copy · libregistry