Repository: webpack-contrib/copy-webpack-plugin
Pull request #485 (2020-05-16 14:05:38)

Refactor code
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [ ] **bugfix**
- [ ] new **feature**
- [x] **code refactor**
- [ ] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

improve perf

### Breaking Changes

No

### Additional Info

No

codecov[bot]: # [Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=h1) Report
> Merging [#485](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=desc) into [master](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/commit/bfc712d77b4ba66caf72341e31a1dd5957bfa36c&el=desc) will **increase** coverage by `0.78%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/graphs/tree.svg?width=650&height=150&src=pr&token=fEoU9iBFGL)](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #485      +/-   ##
==========================================
+ Coverage   96.72%   97.51%   +0.78%     
==========================================
  Files           8        8              
  Lines         214      201      -13     
  Branches       57       52       -5     
==========================================
- Hits          207      196      -11     
+ Misses          7        5       -2     
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/postProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/diff?src=pr&el=tree#diff-c3JjL3Bvc3RQcm9jZXNzUGF0dGVybi5qcw==) | `91.11% <ø> (+1.82%)` | :arrow_up: |
| [src/preProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/diff?src=pr&el=tree#diff-c3JjL3ByZVByb2Nlc3NQYXR0ZXJuLmpz) | `100.00% <ø> (ø)` | |
| [src/utils/createPatternGlob.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/diff?src=pr&el=tree#diff-c3JjL3V0aWxzL2NyZWF0ZVBhdHRlcm5HbG9iLmpz) | `100.00% <ø> (ø)` | |
| [src/index.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/diff?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |
| [src/processPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485/diff?src=pr&el=tree#diff-c3JjL3Byb2Nlc3NQYXR0ZXJuLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=footer). Last update [bfc712d...50b3e2d](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/485?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/index.js b/src/index.js
index 23dc616..97580c0 100644
--- a/src/index.js
+++ b/src/index.js
@@ -56,15 +56,15 @@ class CopyPlugin {
                   }
 
                   return Promise.all(
-                    files.filter(Boolean).map((file) =>
-                      limit(() => {
-                        return postProcessPattern(
+                    files
+                      .filter(Boolean)
+                      .map((file) =>
+                        postProcessPattern(
                           globalRef,
                           patternAfterPreProcess,
                           file
-                        );
-                      })
-                    )
+                        )
+                      )
                   );
                 })
               )
diff --git a/src/postProcessPattern.js b/src/postProcessPattern.js
index bc207af..86a7fc2 100644
--- a/src/postProcessPattern.js
+++ b/src/postProcessPattern.js
@@ -11,37 +11,13 @@ import { RawSource } from 'webpack-sources';
 
 import { version } from '../package.json';
 
-import { stat, readFile } from './utils/promisify';
+import { readFile } from './utils/promisify';
 
 /* eslint-disable no-param-reassign */
 
 export default async function postProcessPattern(globalRef, pattern, file) {
   const { logger, compilation, inputFileSystem } = globalRef;
 
-  logger.debug(`getting stats for '${file.absoluteFrom}' to write to assets`);
-
-  const getStats = pattern.stats
-    ? pattern.stats
-    : stat(inputFileSystem, file.absoluteFrom);
-
-  let stats;
-
-  try {
-    stats = await getStats;
-  } catch (error) {
-    compilation.errors.push(error);
-
-    return;
-  }
-
-  if (stats.isDirectory()) {
-    logger.debug(
-      `skipping '${file.absoluteFrom}' because it is empty directory`
-    );
-
-    return;
-  }
-
   // If this came from a glob, add it to the file watchlist
   if (pattern.fromType === 'glob') {
     logger.debug(`add ${file.absoluteFrom} as fileDependencies`);
diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js
index 9bba1b8..99f952d 100644
--- a/src/preProcessPattern.js
+++ b/src/preProcessPattern.js
@@ -63,7 +63,6 @@ export default async function preProcessPattern(globalRef, pattern) {
     pattern.fromType = 'dir';
   } else if (stats.isFile()) {
     pattern.fromType = 'file';
-    pattern.stats = stats;
   }
 
   return pattern;
diff --git a/src/processPattern.js b/src/processPattern.js
index 6fc5306..756e28c 100644
--- a/src/processPattern.js
+++ b/src/processPattern.js
@@ -31,37 +31,38 @@ export default async function processPattern(globalRef, pattern) {
     return Promise.resolve();
   }
 
-  return paths.map((filepath) => {
-    let from;
+  return (
+    paths
+      // Exclude directories
+      .filter((item) => item.dirent.isFile())
+      .map((item) => {
+        const from = item.path;
 
-    if (typeof filepath === 'string') {
-      from = filepath;
-    } else {
-      from = filepath.path;
-    }
+        const file = { absoluteFrom: path.resolve(pattern.context, from) };
 
-    const file = { absoluteFrom: path.resolve(pattern.context, from) };
+        file.relativeFrom = path.relative(pattern.context, file.absoluteFrom);
 
-    file.relativeFrom = path.relative(pattern.context, file.absoluteFrom);
+        if (pattern.flatten) {
+          file.relativeFrom = path.basename(file.relativeFrom);
+        }
 
-    if (pattern.flatten) {
-      file.relativeFrom = path.basename(file.relativeFrom);
-    }
+        logger.debug(`found ${from}`);
 
-    logger.debug(`found ${from}`);
+        // Change the to path to be relative for webpack
+        file.webpackTo =
+          pattern.toType === 'dir'
+            ? path.join(pattern.to, file.relativeFrom)
+            : pattern.to;
 
-    // Change the to path to be relative for webpack
-    file.webpackTo =
-      pattern.toType === 'dir'
-        ? path.join(pattern.to, file.relativeFrom)
-        : pattern.to;
+        if (path.isAbsolute(file.webpackTo)) {
+          file.webpackTo = path.relative(output, file.webpackTo);
+        }
 
-    if (path.isAbsolute(file.webpackTo)) {
-      file.webpackTo = path.relative(output, file.webpackTo);
-    }
+        logger.log(
+          `determined that '${from}' should write to '${file.webpackTo}'`
+        );
 
-    logger.log(`determined that '${from}' should write to '${file.webpackTo}'`);
-
-    return file;
-  });
+        return file;
+      })
+  );
 }
diff --git a/src/utils/createPatternGlob.js b/src/utils/createPatternGlob.js
index ceacefe..4e9033a 100644
--- a/src/utils/createPatternGlob.js
+++ b/src/utils/createPatternGlob.js
@@ -15,7 +15,7 @@ function createPatternGlob(pattern, globalRef) {
   pattern.globOptions = {
     ...{ followSymbolicLinks: true },
     ...(pattern.globOptions || {}),
-    ...{ cwd: pattern.context },
+    ...{ cwd: pattern.context, objectMode: true },
   };
 
   switch (pattern.fromType) {


Test patch:
diff --git a/test/__snapshots__/CopyPlugin.test.js.snap b/test/__snapshots__/CopyPlugin.test.js.snap
index f5ab4ad..b01ac51 100644
--- a/test/__snapshots__/CopyPlugin.test.js.snap
+++ b/test/__snapshots__/CopyPlugin.test.js.snap
@@ -16,10 +16,6 @@ Object {
     "found ./fixtures/directory/nested/deep-nested/deepnested.txt",
     "found ./fixtures/directory/nested/nestedfile.txt",
     "getting stats for './fixtures/directory' to determinate 'fromType'",
-    "getting stats for './fixtures/directory/.dottedfile' to write to assets",
-    "getting stats for './fixtures/directory/directoryfile.txt' to write to assets",
-    "getting stats for './fixtures/directory/nested/deep-nested/deepnested.txt' to write to assets",
-    "getting stats for './fixtures/directory/nested/nestedfile.txt' to write to assets",
     "processing from: 'directory' to: '.'",
     "reading './fixtures/directory/.dottedfile' to write to assets",
     "reading './fixtures/directory/directoryfile.txt' to write to assets",
@@ -44,7 +40,6 @@ Object {
     "end to adding additional assets",
     "found ./fixtures/file.txt",
     "getting stats for './fixtures/file.txt' to determinate 'fromType'",
-    "getting stats for './fixtures/file.txt' to write to assets",
     "processing from: 'file.txt' to: '.'",
     "reading './fixtures/file.txt' to write to assets",
     "start to adding additional assets",
@@ -63,28 +58,17 @@ Object {
     "begin globbing './fixtures/directory/**' with a context of './fixtures'",
     "determined './fixtures/directory/**' is a glob",
     "determined that './fixtures/directory/directoryfile.txt' should write to 'directory/directoryfile.txt'",
-    "determined that './fixtures/directory/nested' should write to 'directory/nested'",
-    "determined that './fixtures/directory/nested/deep-nested' should write to 'directory/nested/deep-nested'",
     "determined that './fixtures/directory/nested/deep-nested/deepnested.txt' should write to 'directory/nested/deep-nested/deepnested.txt'",
     "determined that './fixtures/directory/nested/nestedfile.txt' should write to 'directory/nested/nestedfile.txt'",
     "end to adding additional assets",
     "found ./fixtures/directory/directoryfile.txt",
-    "found ./fixtures/directory/nested",
-    "found ./fixtures/directory/nested/deep-nested",
     "found ./fixtures/directory/nested/deep-nested/deepnested.txt",
     "found ./fixtures/directory/nested/nestedfile.txt",
     "getting stats for './fixtures/directory/**' to determinate 'fromType'",
-    "getting stats for './fixtures/directory/directoryfile.txt' to write to assets",
-    "getting stats for './fixtures/directory/nested' to write to assets",
-    "getting stats for './fixtures/directory/nested/deep-nested' to write to assets",
-    "getting stats for './fixtures/directory/nested/deep-nested/deepnested.txt' to write to assets",
-    "getting stats for './fixtures/directory/nested/nestedfile.txt' to write to assets",
     "processing from: 'directory/**' to: '.'",
     "reading './fixtures/directory/directoryfile.txt' to write to assets",
     "reading './fixtures/directory/nested/deep-nested/deepnested.txt' to write to assets",
     "reading './fixtures/directory/nested/nestedfile.txt' to write to assets",
-    "skipping './fixtures/directory/nested' because it is empty directory",
-    "skipping './fixtures/directory/nested/deep-nested' because it is empty directory",
     "start to adding additional assets",
     "writing 'directory/directoryfile.txt' to compilation assets from './fixtures/directory/directoryfile.txt'",
     "writing 'directory/nested/deep-nested/deepnested.txt' to compilation assets from './fixtures/directory/nested/deep-nested/deepnested.txt'",