forked from andreasbm/web-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup-plugin-copy.ts
45 lines (41 loc) · 1.13 KB
/
rollup-plugin-copy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { yellow } from "colors";
import { existsSync, copy as fsCopy } from "fs-extra";
import { OutputBundle, OutputOptions } from "rollup";
export interface IRollupPluginCopyConfig {
resources: [string, string][];
verbose: boolean;
overwrite: boolean;
}
/**
* Default configuration for the copy plugin.
* @type {{resources: Array}}
*/
const defaultConfig: IRollupPluginCopyConfig = {
resources: [],
verbose: true,
overwrite: true
};
/**
* A Rollup plugin that copies resources from one location to another.
* @param config
*/
export function copy(config: Partial<IRollupPluginCopyConfig> = {}) {
const { resources, verbose, overwrite } = { ...defaultConfig, ...config };
return {
name: "copy",
generateBundle: async (outputOptions: OutputOptions, bundle: OutputBundle, isWrite: boolean): Promise<void> => {
if (!isWrite) return;
for (const [from, to] of resources) {
try {
if (overwrite || !existsSync(to)) {
await fsCopy(from, to);
}
} catch (err) {
if (verbose) {
console.log(yellow(`[copy] - The file "${from}" could not be copied to "${to}"\n`), err.message);
}
}
}
}
};
}