[Feature Request]: Native port for `rollup-plugin-copy`
What problem does this feature solve?
Avoids requiring a different JS globber and file watcher in addition to the Rust one.
What does the proposed API look like?
https://www.npmjs.com/package/rollup-plugin-copy
A native copy plugin won't improve much the performance, since it's more of a IO-bound task. I think you might just use https://www.npmjs.com/package/rollup-plugin-copy in rolldown directly.
It won't improve performance, but it'll save 20MB+ of deps and globbing/watching inconsistencies between implementations. For example, the plugin you and I linked does not watch files.
I've made my own simple one for now:
import type { Plugin } from "rolldown";
import { dirname, join } from "node:path";
import { copy, ensureDir } from "@std/fs";
async function doCopy(srcDir: string, dstDir: string) {
for await (const dirEntry of Deno.readDir(srcDir)) {
const src = join(srcDir, dirEntry.name);
const dst = join(dstDir, dirEntry.name);
await ensureDir(dirname(dst));
await copy(src, dst, { overwrite: true });
}
}
export default function copyPlugin(paths: string[]): Plugin {
let cwd = Deno.cwd();
let outDir = '';
return {
name: "copy",
async renderStart(opts, inOpts) {
if (inOpts.cwd) cwd = inOpts.cwd;
if (opts.dir) {
outDir = opts.dir;
await Promise.all(paths.map((p) => doCopy(join(cwd, p), opts.dir!)));
paths.forEach((p) => this.addWatchFile(p));
}
},
async watchChange(absPath, { event }) {
for (const p of paths) {
const baseDir = join(cwd, p);
if (!absPath.startsWith(baseDir)) continue;
const extPath = absPath.substring(baseDir.length);
const dst = join(outDir, extPath);
if (event == 'delete') {
await Deno.remove(dst);
} else {
await ensureDir(dirname(dst));
await copy(absPath, dst, { overwrite: true });
}
}
},
};
}
A more proper one might resolve globs and support mapping different output directories.
@hyf0 hi, will this feature be added in the future? If so, I'd like to give it a try.
@hyf0 hi, will this feature be added in the future? If so, I'd like to give it a try.
It's not planned.