Repository: webpack-contrib/copy-webpack-plugin
Pull request #328 (2019-02-15 19:57:47)

fix: handle `[contenthash]` as template
<!--
  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:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

Handle `[contenthash]` as template

### Breaking Changes

No

### Additional Info

No

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

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

```diff
@@           Coverage Diff           @@
##           master     #328   +/-   ##
=======================================
  Coverage   92.88%   92.88%           
=======================================
  Files           8        8           
  Lines         295      295           
  Branches       78       78           
=======================================
  Hits          274      274           
  Misses         20       20           
  Partials        1        1
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/328?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/preProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/328/diff?src=pr&el=tree#diff-c3JjL3ByZVByb2Nlc3NQYXR0ZXJuLmpz) | `95.71% <100%> (ø)` | :arrow_up: |

------

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


Patch:
diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js
index bf38bef..ad7618a 100644
--- a/src/preProcessPattern.js
+++ b/src/preProcessPattern.js
@@ -7,7 +7,7 @@ import isObject from './utils/isObject';
 import { stat } from './utils/promisify';
 
 // https://www.debuggex.com/r/VH2yS2mvJOitiyr3
-const isTemplateLike = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(:\d+)?\])|(\[(\w+:)?hash(:\w+)?(:\d+)?\])|(\[\d+\])/;
+const isTemplateLike = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(:\d+)?\])|(\[(\w+:)?(hash|contenthash)(:\w+)?(:\d+)?\])|(\[\d+\])/;
 
 /* eslint-disable no-param-reassign */
 


Test patch:
diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js
index db6601f..1733ff1 100644
--- a/test/CopyPlugin.test.js
+++ b/test/CopyPlugin.test.js
@@ -1184,6 +1184,20 @@ describe('apply function', () => {
         .catch(done);
     });
 
+    it('allows pattern to contain contenthash', (done) => {
+      runEmit({
+        expectedAssetKeys: ['directory/22af64.txt'],
+        patterns: [
+          {
+            from: 'directory/directoryfile.txt',
+            to: 'directory/[contenthash:6].txt',
+          },
+        ],
+      })
+        .then(done)
+        .catch(done);
+    });
+
     it('transform with promise', (done) => {
       runEmit({
         expectedAssetKeys: ['file.txt'],