Skip to content

Commit

Permalink
fix: better determinate template in to option (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Mar 22, 2019
1 parent 3946473 commit 52f8be6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/preProcessPattern.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions src/utils/isTemplateLike.js
@@ -0,0 +1,4 @@
export default (pattern) =>
/(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(?::(\d+))?\])|(\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\])/.test(

This comment has been minimized.

Copy link
@andyburke

andyburke Apr 6, 2019

Removing |(\[\d+\]) from this regex is causing #369

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Apr 8, 2019

Member

Can you send a PR?

This comment has been minimized.

Copy link
@andyburke
pattern
);
28 changes: 28 additions & 0 deletions test/CopyPlugin.test.js
Expand Up @@ -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'],
Expand Down

0 comments on commit 52f8be6

Please sign in to comment.