@calmdown/rollup-plugin-copy 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +23 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -14,18 +14,18 @@ const EK_UNKNOWN = "unknown";
14
14
 
15
15
  /**
16
16
  * @typedef {Object} CopySingleTarget
17
- * @property {string} srcFile path to the file to be copied
18
- * @property {string} dstFile path to where the file should be copied to
19
- * @property {string} [baseDir] base directory for relative paths (defaults to current directory)
17
+ * @property {string} srcFile path to the file to be copied; relative paths resolve against the current directory
18
+ * @property {string} dstFile path to where the file should be copied to; relative paths resolve against the current directory
20
19
  * @property {"before"|"after"} [trigger="after"] when to run the operation (defaults to "after")
21
20
  */
22
21
 
23
22
  /**
24
23
  * @typedef {Object} CopyManyTarget
25
- * @property {string} dstDir directory to where files should be copied or linked
24
+ * @property {string} dstDir directory to where files should be copied or linked; relative paths resolve against the current directory
26
25
  * @property {string|string[]} include glob pattern(s) of files to include
27
26
  * @property {string|string[]} [exclude] glob pattern(s) to exclude (optional)
28
- * @property {string} [baseDir] base directory for relative paths (defaults to current directory)
27
+ * @property {string} [baseDir] when set, directory structure under this path will be copied to dstDir, otherwise the structure is flattened; relative paths resolve against the current directory
28
+ * @property {string} [flatten=false] when to run the operation (defaults to "after")
29
29
  * @property {"before"|"after"} [trigger="after"] when to run the operation (defaults to "after")
30
30
  */
31
31
 
@@ -34,7 +34,7 @@ const EK_UNKNOWN = "unknown";
34
34
  * @property {(CopySingleTarget | CopyManyTarget)[]} targets desired copy/link operations
35
35
  * @property {boolean} [dryRun=false] whether to perform a dry run, only logging actions without executing them (defaults to false)
36
36
  * @property {boolean} [runOnce=true] when in watch mode, controls whether to only delete files on the first build (defaults to true)
37
- * @property {"ignore"|"copy-file"|"link-absolute"|"link-relative"} [symLinks="ignore"] how to handle symlinks (defaults to "ignore")
37
+ * @property {"ignore"|"copy-file"|"link-relative"|"link-absolute"} [symLinks="ignore"] how to handle symlinks (defaults to "ignore")
38
38
  */
39
39
 
40
40
  /**
@@ -59,15 +59,14 @@ export default function CopyPlugin(pluginOptions) {
59
59
  };
60
60
 
61
61
  const execTarget = async (context, cwd, target) => {
62
- const baseDir = target.baseDir ? Path.resolve(cwd, target.baseDir) : cwd;
63
62
  const entries = [];
64
63
  if (target.srcFile) {
65
64
  // single file
66
- const src = Path.resolve(baseDir, target.srcFile);
65
+ const src = Path.resolve(cwd, target.srcFile);
67
66
  const stats = await FS.stat(src);
68
67
  entries.push({
69
68
  src,
70
- dst: Path.resolve(baseDir, target.dstFile),
69
+ dst: Path.resolve(cwd, target.dstFile),
71
70
  kind: getKind(stats),
72
71
  });
73
72
  }
@@ -80,12 +79,24 @@ export default function CopyPlugin(pluginOptions) {
80
79
  withFileTypes: true,
81
80
  };
82
81
 
83
- const dstDir = Path.resolve(baseDir, target.dstDir);
82
+ const dstDir = Path.resolve(cwd, target.dstDir);
83
+ const baseDir = target.baseDir ? Path.resolve(cwd, target.baseDir) : null;
84
84
  for (const includePattern of include) {
85
85
  for await (const entry of FS.glob(includePattern, globOptions)) {
86
+ const src = Path.join(entry.parentPath, entry.name);
87
+ let dst = null;
88
+
89
+ if (baseDir) {
90
+ const rel = Path.relative(baseDir, src);
91
+ if (!rel.startsWith("..")) {
92
+ dst = Path.join(dstDir, rel);
93
+ }
94
+ }
95
+
96
+ dst ??= dst = Path.join(dstDir, entry.name);
86
97
  entries.push({
87
- src: Path.join(entry.parentPath, entry.name),
88
- dst: Path.join(dstDir, entry.name),
98
+ src,
99
+ dst,
89
100
  kind: getKind(entry),
90
101
  });
91
102
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calmdown/rollup-plugin-copy",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "exports": {