Open History to see the last commit
1import { copyFile, mkdir } from 'node:fs/promises'
2import { dirname, join } from 'pathe'
3import { glob } from 'tinyglobby'
4import type { Plugin } from 'vite'
5β
6export function copyFilesPlugin({
7 fromDir,
8 toDir,
9 pattern = '**',
10}: {
11 pattern?: string | Array<string>
12 fromDir: string
13 toDir: string
14}): Plugin {
15 return {
16 name: 'copy-files',
17 async writeBundle() {
18 const entries = await glob(pattern, { cwd: fromDir })
19 if (entries.length === 0) {
20 throw new Error(
21 `No files found matching pattern "${pattern}" in directory "${fromDir}"`,
22 )
23 }
24β
25 for (const entry of entries) {
26 const srcPath = join(fromDir, entry)
27 const destPath = join(toDir, entry)
28 // Ensure the destination directory exists
29 await mkdir(dirname(destPath), { recursive: true })
30 await copyFile(srcPath, destPath)
31 }
32 },
33 }
34}