A friend asked me today what is a simple way to copy files from a directory (in his case, extract some images from a node module) to the build directory, using Rollup.

My solution is to create a simple plugin like this:

import path from "path"
import fs from "fs"

function copyFiles(from, to, overwrite = false) {
    return {
        name: 'copy-files',
        generateBundle() {
            const log = msg => console.log('\x1b[36m%s\x1b[0m', msg)
            log(`copy files: ${from} → ${to}`)
            fs.readdirSync(from).forEach(file => {
                const fromFile = `${from}/${file}`
                const toFile = `${to}/${file}`
                if (fs.existsSync(toFile) && !overwrite)
                log(`• ${fromFile} → ${toFile}`)

You can then use it as other plugins:

plugins: [
        copyFiles("./node_modules/leaflet/dist/images", "./public/build"),

No need for 3rd party dependencies, just a simple function that doesn't do anything fancy 😉.