Skip to content

Commit 3ef3f25

Browse files
authoredMay 15, 2020
refactor: drop test option in favor transformPath (#472)
BREAKING CHANGE: the `test` option was removed in favor the `transformPath` option
1 parent 045f629 commit 3ef3f25

8 files changed

+83
-159
lines changed
 

‎README.md

-32
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ module.exports = {
8181
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
8282
| [`globOptions`](#globoptions) | `{Object}` | `undefined` | [Options][glob-options] passed to the glob pattern matching library including `ignore` option. |
8383
| [`toType`](#totype) | `{String}` | `undefined` | Determinate what is `to` option - directory, file or template. |
84-
| [`test`](#test) | `{String\|RegExp}` | `undefined` | Pattern for extracting elements to be used in `to` templates. |
8584
| [`force`](#force) | `{Boolean}` | `false` | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders). |
8685
| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names. |
8786
| [`transform`](#transform) | `{Function}` | `undefined` | Allows to modify the file contents. |
@@ -353,37 +352,6 @@ module.exports = {
353352
};
354353
```
355354

356-
#### `test`
357-
358-
Type: `string|RegExp`
359-
Default: `undefined`
360-
361-
Pattern for extracting elements to be used in `to` templates.
362-
363-
Defines a `{RegExp}` to match some parts of the file path.
364-
These capture groups can be reused in the name property using `[N]` placeholder.
365-
Note that `[0]` will be replaced by the entire path of the file,
366-
whereas `[1]` will contain the first capturing parenthesis of your `{RegExp}`
367-
and so on...
368-
369-
**webpack.config.js**
370-
371-
```js
372-
module.exports = {
373-
plugins: [
374-
new CopyPlugin({
375-
patterns: [
376-
{
377-
from: '*/*',
378-
to: '[1]-[2].[hash].[ext]',
379-
test: /([^/]+)\/(.+)\.png$/,
380-
},
381-
],
382-
}),
383-
],
384-
};
385-
```
386-
387355
#### `force`
388356

389357
Type: `Boolean`

‎src/options.json

-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
"toType": {
1717
"enum": ["dir", "file", "template"]
1818
},
19-
"test": {
20-
"anyOf": [
21-
{
22-
"type": "string"
23-
},
24-
{
25-
"instanceof": "RegExp"
26-
}
27-
]
28-
},
2919
"force": {
3020
"type": "boolean"
3121
},

‎src/postProcessPattern.js

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export default async function postProcessPattern(globalRef, pattern, file) {
117117
file.webpackTo,
118118
{
119119
content,
120-
regExp: file.webpackToRegExp,
121120
context: pattern.context,
122121
}
123122
);

‎src/processPattern.js

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export default async function processPattern(globalRef, pattern) {
5656
file.webpackTo = pattern.to || file.relativeFrom;
5757
} else if (pattern.toType === 'template') {
5858
file.webpackTo = pattern.to;
59-
file.webpackToRegExp = pattern.test;
6059
}
6160

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

‎test/__snapshots__/validate-options.test.js.snap

+5-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`validate options should throw an error on the "options" option with "{"
1414
exports[`validate options should throw an error on the "patterns" option with "" value 1`] = `
1515
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
1616
- options.patterns should be an array:
17-
[non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
17+
[non-empty string | object { from, to?, context?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
1818
`;
1919
2020
exports[`validate options should throw an error on the "patterns" option with "[""]" value 1`] = `
@@ -42,18 +42,6 @@ exports[`validate options should throw an error on the "patterns" option with "[
4242
- options.patterns[0].force should be a boolean."
4343
`;
4444
45-
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","test":true}]" value 1`] = `
46-
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
47-
- options.patterns[0] should be one of these:
48-
non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }
49-
Details:
50-
* options.patterns[0].test should be one of these:
51-
string | RegExp
52-
Details:
53-
* options.patterns[0].test should be a string.
54-
* options.patterns[0].test should be an instance of RegExp."
55-
`;
56-
5745
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","toType":"foo"}]" value 1`] = `
5846
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
5947
- options.patterns[0].toType should be one of these:
@@ -73,7 +61,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
7361
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context"}]" value 1`] = `
7462
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
7563
- options.patterns[0] should be one of these:
76-
non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }
64+
non-empty string | object { from, to?, context?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }
7765
Details:
7866
* options.patterns[0].cacheTransform should be one of these:
7967
boolean | object { … }
@@ -112,19 +100,19 @@ exports[`validate options should throw an error on the "patterns" option with "[
112100
exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `
113101
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
114102
- options.patterns should be an array:
115-
[non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
103+
[non-empty string | object { from, to?, context?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
116104
`;
117105
118106
exports[`validate options should throw an error on the "patterns" option with "true" value 1`] = `
119107
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
120108
- options.patterns should be an array:
121-
[non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
109+
[non-empty string | object { from, to?, context?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
122110
`;
123111
124112
exports[`validate options should throw an error on the "patterns" option with "true" value 2`] = `
125113
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
126114
- options.patterns should be an array:
127-
[non-empty string | object { from, to?, context?, toType?, test?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
115+
[non-empty string | object { from, to?, context?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
128116
`;
129117
130118
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `

‎test/test-option.test.js

-73
This file was deleted.

‎test/transformPath-option.test.js

+78
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,82 @@ describe('transformPath option', () => {
192192
.then(done)
193193
.catch(done);
194194
});
195+
196+
it('should move files', (done) => {
197+
runEmit({
198+
expectedAssetKeys: ['txt'],
199+
patterns: [
200+
{
201+
from: 'directory/nested/deep-nested',
202+
to: '[1]',
203+
transformPath(targetPath, absolutePath) {
204+
const mathes = absolutePath.match(/\.([^.]*)$/);
205+
const [, res] = mathes;
206+
const target = targetPath.replace(/\[[1]\]/, res);
207+
208+
return target;
209+
},
210+
},
211+
],
212+
})
213+
.then(done)
214+
.catch(done);
215+
});
216+
217+
it('should move files to a non-root directory with [1]', (done) => {
218+
runEmit({
219+
expectedAssetKeys: ['nested/txt'],
220+
patterns: [
221+
{
222+
from: 'directory/nested/deep-nested',
223+
to: 'nested/[1]',
224+
transformPath(targetPath, absolutePath) {
225+
const mathes = absolutePath.match(/\.([^.]*)$/);
226+
const [, res] = mathes;
227+
const target = targetPath.replace(/\[[1]\]/, res);
228+
229+
return target;
230+
},
231+
},
232+
],
233+
})
234+
.then(done)
235+
.catch(done);
236+
});
237+
238+
it('should move files', (done) => {
239+
runEmit({
240+
expectedAssetKeys: [
241+
'deep-nested-deepnested.txt',
242+
'directoryfile.txt',
243+
'nested-nestedfile.txt',
244+
],
245+
patterns: [
246+
{
247+
from: '**/*',
248+
context: 'directory',
249+
transformPath(targetPath) {
250+
const pathSegments = path.parse(targetPath);
251+
const result = [];
252+
253+
if (pathSegments.root) {
254+
result.push(pathSegments.root);
255+
}
256+
257+
if (pathSegments.dir) {
258+
result.push(pathSegments.dir.split(path.sep).pop());
259+
}
260+
261+
if (pathSegments.base) {
262+
result.push(pathSegments.base);
263+
}
264+
265+
return result.join('-');
266+
},
267+
},
268+
],
269+
})
270+
.then(done)
271+
.catch(done);
272+
});
195273
});

‎test/validate-options.test.js

-25
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ describe('validate options', () => {
5353
to: 'dir',
5454
context: 'context',
5555
toType: 'file',
56-
test: /test/,
5756
force: true,
5857
flatten: true,
5958
transform: () => {},
@@ -71,22 +70,6 @@ describe('validate options', () => {
7170
},
7271
},
7372
],
74-
[
75-
{
76-
from: 'test.txt',
77-
to: 'dir',
78-
context: 'context',
79-
test: 'test',
80-
},
81-
],
82-
[
83-
{
84-
from: 'test.txt',
85-
to: 'dir',
86-
context: 'context',
87-
test: /test/,
88-
},
89-
],
9073
[
9174
{
9275
from: 'test.txt',
@@ -142,14 +125,6 @@ describe('validate options', () => {
142125
toType: 'foo',
143126
},
144127
],
145-
[
146-
{
147-
from: 'test.txt',
148-
to: 'dir',
149-
context: 'context',
150-
test: true,
151-
},
152-
],
153128
[
154129
{
155130
from: 'test.txt',

0 commit comments

Comments
 (0)
Please sign in to comment.