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-copyVerified import paths — ran on the pinned version, not inferred.
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.
Update `src` glob patterns to explicitly match files (e.g., use `path/to/dir/**/*` instead of `path/to/dir`).
Always wrap `path.resolve()` calls for `src` paths with `normalizePath` from Vite (e.g., `normalizePath(path.resolve(__dirname, './foo'))`).
Ensure `dest` paths are relative to your `build.outDir` or use an alternative plugin for copying outside this directory.
Review complex `rename` functions to ensure compatibility or leverage the new `name` and `stripBase` object forms for simpler configuration.
If copying is needed for non-client environments (e.g., SSR), use the `environment` option: `{ environment: ['client', 'ssr'] }` or `{ environment: 'all' }`.Ensure all `path.resolve` outputs used in `src` options are passed through Vite's `normalizePath`: `src: normalizePath(path.resolve(__dirname, 'public/images/**/*'))`.
Verify that each object within the `targets` array has a `src` property defined with a valid glob string or an array of strings.
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.