Skip to content

Commit 1861730

Browse files
jkremsmichael-ciniawsky
authored andcommitted
feat(processPattern): add support for {RegExp) matches (pattern.test) (#235)
1 parent 832f139 commit 1861730

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Or, in case of just a `from` with the default destination, you can also use a `{
5555
|[`fromArgs`](#fromArgs)|`{Object}`|`{ cwd: context }`|See the [`node-glob` options](https://github.com/isaacs/node-glob#options) in addition to the ones below|
5656
|[`to`](#to)|`{String\|Object}`|`undefined`|Output root if `from` is file or dir, resolved glob path if `from` is glob|
5757
|[`toType`](#toType)|`{String}`|``|[toType Options](#toType)|
58+
|[`test`](#test)|`{RegExp}`|``|Pattern for extracting elements to be used in `to` templates|
5859
|[`force`](#force)|`{Boolean}`|`false`|Overwrites files already in `compilation.assets` (usually added by other plugins/loaders)|
5960
|[`ignore`](#ignore)|`{Array}`|`[]`|Globs to ignore for this pattern|
6061
|`flatten`|`{Boolean}`|`false`|Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic|
@@ -143,6 +144,27 @@ Or, in case of just a `from` with the default destination, you can also use a `{
143144
]
144145
```
145146

147+
### `test`
148+
149+
Defines a `{RegExp}` to match some parts of the file path.
150+
These capture groups can be reused in the name property using `[N]` placeholder.
151+
Note that `[0]` will be replaced by the entire path of the file,
152+
whereas `[1]` will contain the first capturing parenthesis of your `{RegExp}`
153+
and so on...
154+
155+
**webpack.config.js**
156+
```js
157+
[
158+
new CopyWebpackPlugin([
159+
{
160+
from: '*/*',
161+
to: '[1]-[2].[hash].[ext]',
162+
test: /([^/]+)\/(.+)\.png$/
163+
}
164+
], options)
165+
]
166+
```
167+
146168
### `force`
147169

148170
**webpack.config.js**

src/processPattern.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default function processPattern(globalRef, pattern) {
7272
file.webpackTo = pattern.to || file.relativeFrom;
7373
} else if (pattern.toType === 'template') {
7474
file.webpackTo = pattern.to;
75+
file.webpackToRegExp = pattern.test;
7576
}
7677

7778
if (path.isAbsolute(file.webpackTo)) {

src/writeFile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default function writeFile(globalRef, pattern, file) {
7878
file.webpackTo,
7979
{
8080
content,
81+
regExp: file.webpackToRegExp,
8182
context: pattern.context
8283
}
8384
);

tests/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ describe('apply function', () => {
441441
.then(done)
442442
.catch(done);
443443
});
444+
445+
it('can flatten or normalize glob matches', (done) => {
446+
runEmit({
447+
expectedAssetKeys: [
448+
'[special?directory]-(special-*file).txt',
449+
'[special?directory]-directoryfile.txt',
450+
'directory-directoryfile.txt'
451+
],
452+
patterns: [{
453+
from: '*/*.*',
454+
test: /([^\/]+)\/([^\/]+)\.\w+$/,
455+
to: '[1]-[2].[ext]'
456+
}]
457+
})
458+
.then(done)
459+
.catch(done);
460+
});
444461
});
445462

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

0 commit comments

Comments
 (0)