Skip to content

Commit

Permalink
feat: Add support for regex matches in to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Mar 1, 2018
1 parent ee78c06 commit f0dae4f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ Or, in case of just a `from` with the default destination, you can also use a `{
from: 'src/'
to: 'dest/[name].[hash].[ext]',
toType: 'template'
// Optional: provide a regExp to extract additional placeholders
// See: https://github.com/webpack-contrib/file-loader#regexp
toRegExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/,
}
], options)
]
Expand Down
1 change: 1 addition & 0 deletions src/processPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default function processPattern(globalRef, pattern) {
file.webpackTo = pattern.to || file.relativeFrom;
} else if (pattern.toType === 'template') {
file.webpackTo = pattern.to;
file.webpackToRegExp = pattern.toRegExp;
}

if (path.isAbsolute(file.webpackTo)) {
Expand Down
2 changes: 1 addition & 1 deletion src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function writeFile(globalRef, pattern, file) {
file.webpackTo = loaderUtils.interpolateName(
{resourcePath: file.relativeFrom},
file.webpackTo,
{content});
{content, regExp: file.webpackToRegExp});

// Add back removed dots
if (dotRemoved) {
Expand Down
17 changes: 17 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ describe('apply function', () => {
.then(done)
.catch(done);
});

it('can flatten or normalize glob matches', (done) => {
runEmit({
expectedAssetKeys: [
'[special?directory]-(special-*file).txt',
'[special?directory]-directoryfile.txt',
'directory-directoryfile.txt'
],
patterns: [{
from: '*/*.*',
toRegExp: /^([^\/]+)\/(.+)\.\w+$/,
to: '[1]-[2].[ext]'
}]
})
.then(done)
.catch(done);
});
});

describe('with file in from', () => {
Expand Down

0 comments on commit f0dae4f

Please # to comment.