Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Or, in case of just a `from` with the default destination, you can also use a `{
|[`fromArgs`](#fromArgs)|`{Object}`|`{ cwd: context }`|See the [`node-glob` options](https://github.com/isaacs/node-glob#options) in addition to the ones below|
|[`to`](#to)|`{String\|Object}`|`undefined`|Output root if `from` is file or dir, resolved glob path if `from` is glob|
|[`toType`](#toType)|`{String}`|``|[toType Options](#toType)|
|[`test`](#test)|`{RegExp}`|``|Pattern for extracting elements to be used in `to` templates|
|[`force`](#force)|`{Boolean}`|`false`|Overwrites files already in `compilation.assets` (usually added by other plugins/loaders)|
|[`ignore`](#ignore)|`{Array}`|`[]`|Globs to ignore for this pattern|
|`flatten`|`{Boolean}`|`false`|Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic|
Expand Down Expand Up @@ -143,6 +144,27 @@ Or, in case of just a `from` with the default destination, you can also use a `{
]
```

### `test`

Defines a `{RegExp}` to match some parts of the file path.
These capture groups can be reused in the name property using `[N]` placeholder.
Note that `[0]` will be replaced by the entire path of the file,
whereas `[1]` will contain the first capturing parenthesis of your `{RegExp}`
and so on...

**webpack.config.js**
```js
[
new CopyWebpackPlugin([
{
from: '*/*',
to: '[1]-[2].[hash].[ext]',
test: /([^/]+)\/(.+)\.png$/
}
], options)
]
```

### `force`

**webpack.config.js**
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.test;
}

if (path.isAbsolute(file.webpackTo)) {
Expand Down
1 change: 1 addition & 0 deletions src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default function writeFile(globalRef, pattern, file) {
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context
}
);
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: '*/*.*',
test: /([^\/]+)\/([^\/]+)\.\w+$/,
to: '[1]-[2].[ext]'
}]
})
.then(done)
.catch(done);
});
});

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