A Vite plugin that copies files during the build process with advanced features like file transformation, renaming, and flattening.
npm install @arcmantle/vite-plugin-copy
# or
pnpm add @arcmantle/vite-plugin-copy
# or
yarn add @arcmantle/vite-plugin-copy
import { viteCopy } from '@arcmantle/vite-plugin-copy';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
viteCopy({
targets: [
{
from: 'src/assets/**/*',
to: 'dist/assets'
}
]
})
]
});
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
})
]
});
viteCopy(options)| Option | Type | Default | Description |
|---|---|---|---|
targets |
Target[] |
[] |
Array of copy targets |
copyOnce |
boolean |
false |
Copy items only once (useful in watch mode) |
hook |
string |
'buildEnd' |
Rollup hook to use ('config', 'buildEnd', or any valid hook) |
verbose |
boolean |
false |
Enable verbose logging |
flatten |
boolean |
true |
Remove base directory structure of copied files |
| Option | Type | Description |
|---|---|---|
from |
string | string[] |
Source path or glob pattern(s) |
to |
string | string[] |
Destination path(s) |
rename |
string | function |
New filename or function to generate filename |
transform |
function |
Function to transform file contents (only works with files) |
transform: (contents, filename) => {
// contents: Buffer - file contents
// filename: string - original filename
// return: any - transformed content
}
rename: (name, extension, fullPath) => {
// name: string - filename without extension
// extension: string - file extension without dot
// fullPath: string - full source path
// return: string - new filename
}
viteCopy({
targets: [
{
from: 'src/assets/**/*',
to: 'dist/assets'
}
]
})
viteCopy({
targets: [
{
from: ['src/**/*.json', 'src/**/*.xml'],
to: 'dist/config'
}
]
})
viteCopy({
targets: [
{
from: 'src/locales/*.json',
to: 'dist/i18n',
rename: (name, ext, fullPath) => `${name}.locale.${ext}`
}
]
})
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);
}
}
]
})
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
})
config: Runs early during configuration setupbuildEnd: Runs after the build is complete (default)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
}
]
})
Apache-2.0
This package is part of the Arcmantle Weave monorepo. Contributions are welcome!