diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js index 086b89f1..4b4a0ae1 100644 --- a/src/preProcessPattern.js +++ b/src/preProcessPattern.js @@ -4,12 +4,10 @@ import isGlob from 'is-glob'; 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'; -// https://www.debuggex.com/r/VH2yS2mvJOitiyr3 -const isTemplateLike = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(:\d+)?\])|(\[(\w+:)?(hash|contenthash)(:\w+)?(:\d+)?\])|(\[\d+\])/; - /* eslint-disable no-param-reassign */ export default function preProcessPattern(globalRef, pattern) { @@ -62,7 +60,7 @@ export default function preProcessPattern(globalRef, pattern) { // if toType already exists case !!pattern.toType: break; - case isTemplateLike.test(pattern.to): + case isTemplateLike(pattern.to): pattern.toType = 'template'; break; case isToDirectory: diff --git a/src/utils/isTemplateLike.js b/src/utils/isTemplateLike.js new file mode 100644 index 00000000..ead6d5c9 --- /dev/null +++ b/src/utils/isTemplateLike.js @@ -0,0 +1,4 @@ +export default (pattern) => + /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(?::(\d+))?\])|(\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\])/.test( + pattern + ); diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js index 835b6a8a..24ab24fd 100644 --- a/test/CopyPlugin.test.js +++ b/test/CopyPlugin.test.js @@ -1350,6 +1350,34 @@ describe('apply function', () => { .catch(done); }); + it('allows pattern to contain custoh `contenthash` digest', (done) => { + runEmit({ + expectedAssetKeys: ['directory/c2a6.txt'], + patterns: [ + { + from: 'directory/directoryfile.txt', + to: 'directory/[sha1:contenthash:hex:4].txt', + }, + ], + }) + .then(done) + .catch(done); + }); + + it('allows pattern to contain `hashType` without `hash` or `contenthash`', (done) => { + runEmit({ + expectedAssetKeys: ['directory/[md5::base64:20].txt'], + patterns: [ + { + from: 'directory/directoryfile.txt', + to: 'directory/[md5::base64:20].txt', + }, + ], + }) + .then(done) + .catch(done); + }); + it('transform with promise', (done) => { runEmit({ expectedAssetKeys: ['file.txt'],