rolldown icon indicating copy to clipboard operation
rolldown copied to clipboard

[Feature Request]: Native port for `rollup-plugin-copy`

Open thesmartwon opened this issue 1 year ago • 4 comments

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

thesmartwon avatar Nov 07 '24 17:11 thesmartwon

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.

hyf0 avatar Nov 07 '24 17:11 hyf0

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.

thesmartwon avatar Nov 07 '24 18:11 thesmartwon

@hyf0 hi, will this feature be added in the future? If so, I'd like to give it a try.

AliceLanniste avatar Mar 11 '25 03:03 AliceLanniste

@hyf0 hi, will this feature be added in the future? If so, I'd like to give it a try.

It's not planned.

hyf0 avatar Mar 11 '25 04:03 hyf0