Skip to content

Commit 52f8be6

Browse files
authoredMar 22, 2019
fix: better determinate template in to option (#363)
1 parent 3946473 commit 52f8be6

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed
 

‎src/preProcessPattern.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import isGlob from 'is-glob';
44
import globParent from 'glob-parent';
55

66
import normalize from './utils/normalize';
7+
import isTemplateLike from './utils/isTemplateLike';
78
import isObject from './utils/isObject';
89
import { stat } from './utils/promisify';
910

10-
// https://www.debuggex.com/r/VH2yS2mvJOitiyr3
11-
const isTemplateLike = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(:\d+)?\])|(\[(\w+:)?(hash|contenthash)(:\w+)?(:\d+)?\])|(\[\d+\])/;
12-
1311
/* eslint-disable no-param-reassign */
1412

1513
export default function preProcessPattern(globalRef, pattern) {
@@ -62,7 +60,7 @@ export default function preProcessPattern(globalRef, pattern) {
6260
// if toType already exists
6361
case !!pattern.toType:
6462
break;
65-
case isTemplateLike.test(pattern.to):
63+
case isTemplateLike(pattern.to):
6664
pattern.toType = 'template';
6765
break;
6866
case isToDirectory:

‎src/utils/isTemplateLike.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default (pattern) =>
2+
/(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(?::(\d+))?\])|(\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\])/.test(
Has conversations. Original line has conversations.
3+
pattern
4+
);

‎test/CopyPlugin.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,34 @@ describe('apply function', () => {
13501350
.catch(done);
13511351
});
13521352

1353+
it('allows pattern to contain custoh `contenthash` digest', (done) => {
1354+
runEmit({
1355+
expectedAssetKeys: ['directory/c2a6.txt'],
1356+
patterns: [
1357+
{
1358+
from: 'directory/directoryfile.txt',
1359+
to: 'directory/[sha1:contenthash:hex:4].txt',
1360+
},
1361+
],
1362+
})
1363+
.then(done)
1364+
.catch(done);
1365+
});
1366+
1367+
it('allows pattern to contain `hashType` without `hash` or `contenthash`', (done) => {
1368+
runEmit({
1369+
expectedAssetKeys: ['directory/[md5::base64:20].txt'],
1370+
patterns: [
1371+
{
1372+
from: 'directory/directoryfile.txt',
1373+
to: 'directory/[md5::base64:20].txt',
1374+
},
1375+
],
1376+
})
1377+
.then(done)
1378+
.catch(done);
1379+
});
1380+
13531381
it('transform with promise', (done) => {
13541382
runEmit({
13551383
expectedAssetKeys: ['file.txt'],

0 commit comments

Comments
 (0)
Please sign in to comment.