Repository: webpack-contrib/copy-webpack-plugin
Pull request #452 (2020-04-28 16:04:45)

refactor: rename the `cache` option to `cacheTransform`
<!--
  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

Rename option to clarify

### Breaking Changes

Yes

BREAKING CHANGE: the `cache` option was renamed to `cacheTransform`

### Additional Info

No

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

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

```diff
@@           Coverage Diff           @@
##           master     #452   +/-   ##
=======================================
  Coverage   94.59%   94.59%           
=======================================
  Files           9        9           
  Lines         259      259           
  Branches       64       64           
=======================================
  Hits          245      245           
  Misses         13       13           
  Partials        1        1           
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/452?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/postProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/452/diff?src=pr&el=tree#diff-c3JjL3Bvc3RQcm9jZXNzUGF0dGVybi5qcw==) | `89.39% <100.00%> (ø)` | |

------

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


Patch:
diff --git a/README.md b/README.md
index 5f4f34e..8fe97fe 100644
--- a/README.md
+++ b/README.md
@@ -74,20 +74,20 @@ module.exports = {
 
 ### Patterns
 
-|               Name                |        Type         |                     Default                     | Description                                                                                           |
-| :-------------------------------: | :-----------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
-|          [`from`](#from)          |     `{String}`      |                   `undefined`                   | Glob or path from where we сopy files.                                                                |
-|            [`to`](#to)            |     `{String}`      |            `compiler.options.output`            | Output path.                                                                                          |
-|       [`context`](#context)       |     `{String}`      | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path.                                              |
-|        [`toType`](#totype)        |     `{String}`      |                   `undefined`                   | Determinate what is `to` option - directory, file or template.                                        |
-|          [`test`](#test)          | `{String\|RegExp}`  |                   `undefined`                   | Pattern for extracting elements to be used in `to` templates.                                         |
-|         [`force`](#force)         |     `{Boolean}`     |                     `false`                     | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders).            |
-|        [`ignore`](#ignore)        |      `{Array}`      |                      `[]`                       | Globs to ignore files.                                                                                |
-|       [`flatten`](#flatten)       |     `{Boolean}`     |                     `false`                     | Removes all directory references and only copies file names.                                          |
-|         [`cache`](#cache)         | `{Boolean\|Object}` |                     `false`                     | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
-|     [`transform`](#transform)     |    `{Function}`     |                   `undefined`                   | Allows to modify the file contents.                                                                   |
-| [`transformPath`](#transformpath) |    `{Function}`     |                   `undefined`                   | Allows to modify the writing path.                                                                    |
-|   [`globOptions`](#globoptions)   |     `{Object}`      |                   `undefined`                   | [Options][glob-options] passed to the glob pattern matching library                                   |
+|                Name                 |        Type         |                     Default                     | Description                                                                                           |
+| :---------------------------------: | :-----------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
+|           [`from`](#from)           |     `{String}`      |                   `undefined`                   | Glob or path from where we сopy files.                                                                |
+|             [`to`](#to)             |     `{String}`      |            `compiler.options.output`            | Output path.                                                                                          |
+|        [`context`](#context)        |     `{String}`      | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path.                                              |
+|    [`globOptions`](#globoptions)    |     `{Object}`      |                   `undefined`                   | [Options][glob-options] passed to the glob pattern matching library                                   |
+|         [`toType`](#totype)         |     `{String}`      |                   `undefined`                   | Determinate what is `to` option - directory, file or template.                                        |
+|           [`test`](#test)           | `{String\|RegExp}`  |                   `undefined`                   | Pattern for extracting elements to be used in `to` templates.                                         |
+|          [`force`](#force)          |     `{Boolean}`     |                     `false`                     | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders).            |
+|         [`ignore`](#ignore)         |      `{Array}`      |                      `[]`                       | Globs to ignore files.                                                                                |
+|        [`flatten`](#flatten)        |     `{Boolean}`     |                     `false`                     | Removes all directory references and only copies file names.                                          |
+|      [`transform`](#transform)      |    `{Function}`     |                   `undefined`                   | Allows to modify the file contents.                                                                   |
+| [`cacheTransform`](#cacheTransform) | `{Boolean\|Object}` |                     `false`                     | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
+|  [`transformPath`](#transformpath)  |    `{Function}`     |                   `undefined`                   | Allows to modify the writing path.                                                                    |
 
 #### `from`
 
@@ -187,6 +187,33 @@ module.exports = {
 };
 ```
 
+#### `globOptions`
+
+Type: `Object`
+Default: `undefined`
+
+Allows to configute the glob pattern matching library used by the plugin. [See the list of supported options][glob-options]
+
+**webpack.config.js**
+
+```js
+module.exports = {
+  plugins: [
+    new CopyPlugin({
+      patterns: [
+        {
+          from: 'public/**/*',
+          globOptions: {
+            dot: true,
+            gitignore: true,
+          },
+        },
+      ],
+    }),
+  ],
+};
+```
+
 #### `toType`
 
 Type: `String`
@@ -391,13 +418,12 @@ module.exports = {
 };
 ```
 
-#### `cache`
+#### `transform`
 
-Type: `Boolean|Object`
-Default: `false`
+Type: `Function`
+Default: `undefined`
 
-Enable/disable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache.
-Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.
+Allows to modify the file contents.
 
 **webpack.config.js**
 
@@ -412,7 +438,6 @@ module.exports = {
           transform(content, path) {
             return optimize(content);
           },
-          cache: true,
         },
       ],
     }),
@@ -420,13 +445,6 @@ module.exports = {
 };
 ```
 
-#### `transform`
-
-Type: `Function`
-Default: `undefined`
-
-Allows to modify the file contents.
-
 **webpack.config.js**
 
 ```js
@@ -438,7 +456,7 @@ module.exports = {
           from: 'src/*.png',
           to: 'dest/',
           transform(content, path) {
-            return optimize(content);
+            return Promise.resolve(optimize(content));
           },
         },
       ],
@@ -447,6 +465,14 @@ module.exports = {
 };
 ```
 
+#### `cacheTransform`
+
+Type: `Boolean|Object`
+Default: `false`
+
+Enable/disable `transform` caching. You can use `{ cacheTransform: { key: 'my-cache-key' } }` to invalidate the cache.
+Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.
+
 **webpack.config.js**
 
 ```js
@@ -458,8 +484,9 @@ module.exports = {
           from: 'src/*.png',
           to: 'dest/',
           transform(content, path) {
-            return Promise.resolve(optimize(content));
+            return optimize(content);
           },
+          cacheTransform: true,
         },
       ],
     }),
@@ -518,33 +545,6 @@ module.exports = {
 };
 ```
 
-#### `globOptions`
-
-Type: `Object`
-Default: `undefined`
-
-Allows to configute the glob pattern matching library used by the plugin. [See the list of supported options][glob-options]
-
-**webpack.config.js**
-
-```js
-module.exports = {
-  plugins: [
-    new CopyPlugin({
-      patterns: [
-        {
-          from: 'public/**/*',
-          globOptions: {
-            dot: true,
-            gitignore: true,
-          },
-        },
-      ],
-    }),
-  ],
-};
-```
-
 ### Options
 
 |         Name          |    Type    |          Default           | Description                                                                      |
diff --git a/src/options.json b/src/options.json
index d8b627a..738bc29 100644
--- a/src/options.json
+++ b/src/options.json
@@ -45,7 +45,10 @@
         "flatten": {
           "type": "boolean"
         },
-        "cache": {
+        "transform": {
+          "instanceof": "Function"
+        },
+        "cacheTransform": {
           "anyOf": [
             {
               "type": "boolean"
@@ -55,9 +58,6 @@
             }
           ]
         },
-        "transform": {
-          "instanceof": "Function"
-        },
         "transformPath": {
           "instanceof": "Function"
         }
diff --git a/src/postProcessPattern.js b/src/postProcessPattern.js
index bc95494..6cbc060 100644
--- a/src/postProcessPattern.js
+++ b/src/postProcessPattern.js
@@ -49,14 +49,14 @@ export default function postProcessPattern(globalRef, pattern, file) {
           const transform = (content, absoluteFrom) =>
             pattern.transform(content, absoluteFrom);
 
-          if (pattern.cache) {
+          if (pattern.cacheTransform) {
             if (!globalRef.cacheDir) {
               globalRef.cacheDir =
                 findCacheDir({ name: 'copy-webpack-plugin' }) || os.tmpdir();
             }
 
-            const cacheKey = pattern.cache.key
-              ? pattern.cache.key
+            const cacheKey = pattern.cacheTransform.key
+              ? pattern.cacheTransform.key
               : serialize({
                   name,
                   version,


Test patch:
diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap
index 7864499..e57173a 100644
--- a/test/__snapshots__/validate-options.test.js.snap
+++ b/test/__snapshots__/validate-options.test.js.snap
@@ -14,7 +14,7 @@ exports[`validate options should throw an error on the "options" option with "{"
 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 should be an array:
-   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
+   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
 `;
 
 exports[`validate options should throw an error on the "patterns" option with "[""]" value 1`] = `
@@ -45,7 +45,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
 exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","ignore":["test.txt",true]}]" 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?, … }
+   non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
    Details:
     * options.patterns[0].ignore[1] should be one of these:
       string | object { … }
@@ -58,7 +58,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
 exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","ignore":[true]}]" 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?, … }
+   non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
    Details:
     * options.patterns[0].ignore[0] should be one of these:
       string | object { … }
@@ -77,7 +77,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
 exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","test":true}]" 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?, … }
+   non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
    Details:
     * options.patterns[0].test should be one of these:
       string | RegExp
@@ -105,13 +105,13 @@ exports[`validate options should throw an error on the "patterns" option with "[
 exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","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?, … }
+   non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
    Details:
-    * options.patterns[0].cache should be one of these:
+    * options.patterns[0].cacheTransform should be one of these:
       boolean | object { … }
       Details:
-       * options.patterns[0].cache should be a boolean.
-       * options.patterns[0].cache should be an object:
+       * options.patterns[0].cacheTransform should be a boolean.
+       * options.patterns[0].cacheTransform should be an object:
          object { … }"
 `;
 
@@ -144,19 +144,19 @@ exports[`validate options should throw an error on the "patterns" option with "[
 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 should be an array:
-   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
+   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
 `;
 
 exports[`validate options should throw an error on the "patterns" option with "true" value 1`] = `
 "Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
  - options.patterns should be an array:
-   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
+   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
 `;
 
 exports[`validate options should throw an error on the "patterns" option with "true" value 2`] = `
 "Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
  - options.patterns should be an array:
-   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
+   [non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
 `;
 
 exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
diff --git a/test/cache-option.test.js b/test/cacheTransform-option.test.js
similarity index 97%
rename from test/cache-option.test.js
rename to test/cacheTransform-option.test.js
index d2f4b41..62aad97 100644
--- a/test/cache-option.test.js
+++ b/test/cacheTransform-option.test.js
@@ -27,7 +27,7 @@ describe('cache option', () => {
       patterns: [
         {
           from,
-          cache: true,
+          cacheTransform: true,
           transform: function transform(content) {
             return new Promise((resolve) => {
               resolve(`${content}changed!`);
@@ -74,7 +74,7 @@ describe('cache option', () => {
       patterns: [
         {
           from,
-          cache: true,
+          cacheTransform: true,
           transform: function transform(content) {
             return new Promise((resolve) => {
               resolve(`${content}changed!`);
@@ -114,7 +114,7 @@ describe('cache option', () => {
       patterns: [
         {
           from,
-          cache: true,
+          cacheTransform: true,
           transform: function transform(content) {
             return new Promise((resolve) => {
               resolve(`${content}changed!`);
@@ -156,7 +156,7 @@ describe('cache option', () => {
       patterns: [
         {
           from,
-          cache: {
+          cacheTransform: {
             key: 'foobar',
           },
           transform(content) {
@@ -198,7 +198,7 @@ describe('cache option', () => {
       patterns: [
         {
           from,
-          cache: true,
+          cacheTransform: true,
           // eslint-disable-next-line no-shadow
           transform: function transform(content) {
             expect(isGzip(content)).toBe(true);
diff --git a/test/validate-options.test.js b/test/validate-options.test.js
index fde45c0..13ada82 100644
--- a/test/validate-options.test.js
+++ b/test/validate-options.test.js
@@ -64,8 +64,8 @@ describe('validate options', () => {
               },
             ],
             flatten: true,
-            cache: true,
             transform: () => {},
+            cacheTransform: true,
             transformPath: () => {},
           },
         ],
@@ -100,7 +100,7 @@ describe('validate options', () => {
             from: 'test.txt',
             to: 'dir',
             context: 'context',
-            cache: {
+            cacheTransform: {
               foo: 'bar',
             },
           },
@@ -203,7 +203,7 @@ describe('validate options', () => {
             from: 'test.txt',
             to: 'dir',
             context: 'context',
-            cache: () => {},
+            cacheTransform: () => {},
           },
         ],
         [