Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better determinate template in to option #363

Merged
merged 1 commit into from Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
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