Repository: webpack-contrib/copy-webpack-plugin
Pull request #450 (2020-04-28 15:12:51)

refactor: 'from' option
<!--
  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**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

Avoid misleading configuration

### Breaking Changes

Yes, 

BREAKING CHANGE: the 'from' option now can only be a string, if you use `{ from: { glob: 'directory/**', dot: false } }` changed it to `{ from: 'directory/**', globOptions: { dot: false } }`

### Additional Info

No

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

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

```diff
@@            Coverage Diff             @@
##           master     #450      +/-   ##
==========================================
- Coverage   94.69%   94.59%   -0.11%     
==========================================
  Files           9        9              
  Lines         264      259       -5     
  Branches       66       63       -3     
==========================================
- Hits          250      245       -5     
  Misses         13       13              
  Partials        1        1              
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/450?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/preProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/450/diff?src=pr&el=tree#diff-c3JjL3ByZVByb2Nlc3NQYXR0ZXJuLmpz) | `97.40% <100.00%> (-0.16%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/450?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/450?src=pr&el=footer). Last update [57f3e61...0065623](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/450?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/options.json b/src/options.json
index fe7582f..9f1f293 100644
--- a/src/options.json
+++ b/src/options.json
@@ -4,15 +4,8 @@
       "type": "object",
       "properties": {
         "from": {
-          "anyOf": [
-            {
-              "type": "string",
-              "minLength": 1
-            },
-            {
-              "type": "object"
-            }
-          ]
+          "type": "string",
+          "minLength": 1
         },
         "to": {
           "type": "string"
diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js
index 4d79f6f..e41085a 100644
--- a/src/preProcessPattern.js
+++ b/src/preProcessPattern.js
@@ -5,7 +5,6 @@ import globParent from 'glob-parent';
 
 import normalize from './utils/normalize';
 import isTemplateLike from './utils/isTemplateLike';
-import isObject from './utils/isObject';
 import { stat } from './utils/promisify';
 
 /* eslint-disable no-param-reassign */
@@ -32,8 +31,7 @@ export default function preProcessPattern(globalRef, pattern) {
     pattern.context = path.join(context, pattern.context);
   }
 
-  const isFromGlobPatten =
-    (isObject(pattern.from) && pattern.from.glob) || pattern.globOptions;
+  const isFromGlobPatten = pattern.globOptions;
   // Todo remove this in next major
   const isToDirectory =
     path.extname(pattern.to) === '' || pattern.to.slice(-1) === path.sep;
@@ -67,21 +65,9 @@ export default function preProcessPattern(globalRef, pattern) {
 
     pattern.fromType = 'glob';
 
-    const globOptions = Object.assign(
-      {},
-      pattern.globOptions ? pattern.globOptions : pattern.from
-    );
-    delete globOptions.glob;
-
-    pattern.absoluteFrom = path.resolve(
-      pattern.context,
-      pattern.globOptions ? pattern.from : pattern.from.glob
-    );
-    pattern.glob = normalize(
-      pattern.context,
-      pattern.globOptions ? pattern.from : pattern.from.glob
-    );
-    pattern.globOptions = globOptions;
+    pattern.absoluteFrom = path.resolve(pattern.context, pattern.from);
+    pattern.glob = normalize(pattern.context, pattern.from);
+    pattern.globOptions = pattern.globOptions || {};
 
     return Promise.resolve(pattern);
   }


Test patch:
diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js
index 9acb361..7a5b242 100644
--- a/test/CopyPlugin.test.js
+++ b/test/CopyPlugin.test.js
@@ -240,9 +240,7 @@ describe('apply function', () => {
         expectedAssetKeys: ['directory/nested/nestedfile.txt'],
         patterns: [
           {
-            from: {
-              glob: 'directory\\nested\\*',
-            },
+            from: 'directory\\nested\\*',
           },
         ],
       })
@@ -255,9 +253,7 @@ describe('apply function', () => {
         expectedAssetKeys: ['directory/nested/nestedfile.txt'],
         patterns: [
           {
-            from: {
-              glob: 'directory/nested\\*',
-            },
+            from: 'directory/nested\\*',
           },
         ],
       })
@@ -370,9 +366,7 @@ describe('apply function', () => {
       run({
         patterns: [
           {
-            from: {
-              glob: 'directory/directoryfile.txt',
-            },
+            from: 'directory/directoryfile.txt',
           },
         ],
       })
diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap
index 2a74367..673fbe0 100644
--- a/test/__snapshots__/validate-options.test.js.snap
+++ b/test/__snapshots__/validate-options.test.js.snap
@@ -109,23 +109,20 @@ exports[`validate options should throw an error on the "patterns" option with "[
  - options.patterns[0].to should be a string."
 `;
 
+exports[`validate options should throw an error on the "patterns" option with "[{"from":{"glob":"**/*","dot":false},"to":"dir","context":"context"}]" value 1`] = `
+"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
+ - options.patterns[0].from should be a non-empty string."
+`;
+
 exports[`validate options should throw an error on the "patterns" option with "[{"from":true,"to":"dir","context":"context"}]" value 1`] = `
 "Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- - options.patterns[0] should be one of these:
-   non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
-   Details:
-    * options.patterns[0].from should be one of these:
-      non-empty string | object { … }
-      Details:
-       * options.patterns[0].from should be a non-empty string.
-       * options.patterns[0].from should be an object:
-         object { … }"
+ - options.patterns[0].from should be a non-empty string."
 `;
 
 exports[`validate options should throw an error on the "patterns" option with "[{}]" value 1`] = `
 "Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
  - options.patterns[0] misses the property 'from'. Should be:
-   non-empty string | object { … }"
+   non-empty string"
 `;
 
 exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `
diff --git a/test/from-option.test.js b/test/from-option.test.js
index 186b392..01d62db 100644
--- a/test/from-option.test.js
+++ b/test/from-option.test.js
@@ -470,85 +470,4 @@ describe('from option', () => {
         .catch(done);
     });
   });
-
-  describe('is a object with glob and glob options', () => {
-    it('should move files', (done) => {
-      runEmit({
-        expectedAssetKeys: ['file.txt'],
-        patterns: [
-          {
-            from: {
-              glob: '*.txt',
-            },
-          },
-        ],
-      })
-        .then(done)
-        .catch(done);
-    });
-
-    it('should move files exclude dot files', (done) => {
-      runEmit({
-        expectedAssetKeys: ['file.txt'],
-        patterns: [
-          {
-            from: {
-              glob: '*.txt',
-              dot: false,
-            },
-          },
-        ],
-      })
-        .then(done)
-        .catch(done);
-    });
-
-    it('should move files with dot files', (done) => {
-      runEmit({
-        expectedAssetKeys: ['file.txt', '.file.txt'],
-        patterns: [
-          {
-            from: {
-              glob: '*.txt',
-              dot: true,
-            },
-          },
-        ],
-      })
-        .then(done)
-        .catch(done);
-    });
-
-    it('should move files with the "globOptions" option', (done) => {
-      runEmit({
-        expectedAssetKeys: ['.file.txt', 'file.txt'],
-        patterns: [
-          {
-            from: '*.txt',
-            globOptions: {
-              dot: true,
-            },
-          },
-        ],
-      })
-        .then(done)
-        .catch(done);
-    });
-
-    it('should move files with the "globOptions" option #2', (done) => {
-      runEmit({
-        expectedAssetKeys: ['file.txt'],
-        patterns: [
-          {
-            from: '*.txt',
-            globOptions: {
-              dot: false,
-            },
-          },
-        ],
-      })
-        .then(done)
-        .catch(done);
-    });
-  });
 });
diff --git a/test/validate-options.test.js b/test/validate-options.test.js
index 3353450..6a86753 100644
--- a/test/validate-options.test.js
+++ b/test/validate-options.test.js
@@ -70,19 +70,6 @@ describe('validate options', () => {
             transformPath: () => {},
           },
         ],
-        [
-          {
-            from: {
-              glob: '**/*',
-              dot: false,
-            },
-            to: 'dir',
-            context: 'context',
-            globOptions: {
-              dot: false,
-            },
-          },
-        ],
         [
           {
             from: 'test.txt',
@@ -235,6 +222,16 @@ describe('validate options', () => {
             transformPath: true,
           },
         ],
+        [
+          {
+            from: {
+              glob: '**/*',
+              dot: false,
+            },
+            to: 'dir',
+            context: 'context',
+          },
+        ],
       ],
     },
   };