Skip to content

Commit

Permalink
refactor: rename the cache option to cacheTransform (#452)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `cache` option was renamed to `cacheTransform`
  • Loading branch information
evilebottnawi committed Apr 28, 2020
1 parent aedd2bd commit f6da280
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 82 deletions.
112 changes: 56 additions & 56 deletions README.md
Expand Up @@ -74,20 +74,20 @@ module.exports = {

### Patterns

| Name | Type | Default | Description |
| :-------------------------------: | :-----------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
| [`from`](#from) | `{String}` | `undefined` | Glob or path from where we сopy files. |
| [`to`](#to) | `{String}` | `compiler.options.output` | Output path. |
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
| [`toType`](#totype) | `{String}` | `undefined` | Determinate what is `to` option - directory, file or template. |
| [`test`](#test) | `{String\|RegExp}` | `undefined` | 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 files. |
| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names. |
| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
| [`transform`](#transform) | `{Function}` | `undefined` | Allows to modify the file contents. |
| [`transformPath`](#transformpath) | `{Function}` | `undefined` | Allows to modify the writing path. |
| [`globOptions`](#globoptions) | `{Object}` | `undefined` | [Options][glob-options] passed to the glob pattern matching library |
| Name | Type | Default | Description |
| :---------------------------------: | :-----------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
| [`from`](#from) | `{String}` | `undefined` | Glob or path from where we сopy files. |
| [`to`](#to) | `{String}` | `compiler.options.output` | Output path. |
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
| [`globOptions`](#globoptions) | `{Object}` | `undefined` | [Options][glob-options] passed to the glob pattern matching library |
| [`toType`](#totype) | `{String}` | `undefined` | Determinate what is `to` option - directory, file or template. |
| [`test`](#test) | `{String\|RegExp}` | `undefined` | 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 files. |
| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names. |
| [`transform`](#transform) | `{Function}` | `undefined` | Allows to modify the file contents. |
| [`cacheTransform`](#cacheTransform) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
| [`transformPath`](#transformpath) | `{Function}` | `undefined` | Allows to modify the writing path. |

#### `from`

Expand Down Expand Up @@ -187,6 +187,33 @@ module.exports = {
};
```

#### `globOptions`

Type: `Object`
Default: `undefined`

Allows to configute the glob pattern matching library used by the plugin. [See the list of supported options][glob-options]

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: 'public/**/*',
globOptions: {
dot: true,
gitignore: true,
},
},
],
}),
],
};
```

#### `toType`

Type: `String`
Expand Down Expand Up @@ -391,13 +418,12 @@ module.exports = {
};
```

#### `cache`
#### `transform`

Type: `Boolean|Object`
Default: `false`
Type: `Function`
Default: `undefined`

Enable/disable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache.
Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.
Allows to modify the file contents.

**webpack.config.js**

Expand All @@ -412,21 +438,13 @@ module.exports = {
transform(content, path) {
return optimize(content);
},
cache: true,
},
],
}),
],
};
```

#### `transform`

Type: `Function`
Default: `undefined`

Allows to modify the file contents.

**webpack.config.js**

```js
Expand All @@ -438,7 +456,7 @@ module.exports = {
from: 'src/*.png',
to: 'dest/',
transform(content, path) {
return optimize(content);
return Promise.resolve(optimize(content));
},
},
],
Expand All @@ -447,6 +465,14 @@ module.exports = {
};
```

#### `cacheTransform`

Type: `Boolean|Object`
Default: `false`

Enable/disable `transform` caching. You can use `{ cacheTransform: { key: 'my-cache-key' } }` to invalidate the cache.
Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.

**webpack.config.js**

```js
Expand All @@ -458,8 +484,9 @@ module.exports = {
from: 'src/*.png',
to: 'dest/',
transform(content, path) {
return Promise.resolve(optimize(content));
return optimize(content);
},
cacheTransform: true,
},
],
}),
Expand Down Expand Up @@ -518,33 +545,6 @@ module.exports = {
};
```

#### `globOptions`

Type: `Object`
Default: `undefined`

Allows to configute the glob pattern matching library used by the plugin. [See the list of supported options][glob-options]

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: 'public/**/*',
globOptions: {
dot: true,
gitignore: true,
},
},
],
}),
],
};
```

### Options

| Name | Type | Default | Description |
Expand Down
8 changes: 4 additions & 4 deletions src/options.json
Expand Up @@ -45,7 +45,10 @@
"flatten": {
"type": "boolean"
},
"cache": {
"transform": {
"instanceof": "Function"
},
"cacheTransform": {
"anyOf": [
{
"type": "boolean"
Expand All @@ -55,9 +58,6 @@
}
]
},
"transform": {
"instanceof": "Function"
},
"transformPath": {
"instanceof": "Function"
}
Expand Down
6 changes: 3 additions & 3 deletions src/postProcessPattern.js
Expand Up @@ -49,14 +49,14 @@ export default function postProcessPattern(globalRef, pattern, file) {
const transform = (content, absoluteFrom) =>
pattern.transform(content, absoluteFrom);

if (pattern.cache) {
if (pattern.cacheTransform) {
if (!globalRef.cacheDir) {
globalRef.cacheDir =
findCacheDir({ name: 'copy-webpack-plugin' }) || os.tmpdir();
}

const cacheKey = pattern.cache.key
? pattern.cache.key
const cacheKey = pattern.cacheTransform.key
? pattern.cacheTransform.key
: serialize({
name,
version,
Expand Down
22 changes: 11 additions & 11 deletions test/__snapshots__/validate-options.test.js.snap
Expand Up @@ -14,7 +14,7 @@ exports[`validate options should throw an error on the "options" option with "{"
exports[`validate options should throw an error on the "patterns" option with "" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "[""]" value 1`] = `
Expand Down Expand Up @@ -45,7 +45,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","ignore":["test.txt",true]}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
Details:
* options.patterns[0].ignore[1] should be one of these:
string | object { … }
Expand All @@ -58,7 +58,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","ignore":[true]}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
Details:
* options.patterns[0].ignore[0] should be one of these:
string | object { … }
Expand All @@ -77,7 +77,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context","test":true}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
Details:
* options.patterns[0].test should be one of these:
string | RegExp
Expand Down Expand Up @@ -105,13 +105,13 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context"}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }
Details:
* options.patterns[0].cache should be one of these:
* options.patterns[0].cacheTransform should be one of these:
boolean | object { … }
Details:
* options.patterns[0].cache should be a boolean.
* options.patterns[0].cache should be an object:
* options.patterns[0].cacheTransform should be a boolean.
* options.patterns[0].cacheTransform should be an object:
object { … }"
`;
Expand Down Expand Up @@ -144,19 +144,19 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "true" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "true" value 2`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, transform?, cacheTransform?, transformPath?, … }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
Expand Down
10 changes: 5 additions & 5 deletions test/cache-option.test.js → test/cacheTransform-option.test.js
Expand Up @@ -27,7 +27,7 @@ describe('cache option', () => {
patterns: [
{
from,
cache: true,
cacheTransform: true,
transform: function transform(content) {
return new Promise((resolve) => {
resolve(`${content}changed!`);
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('cache option', () => {
patterns: [
{
from,
cache: true,
cacheTransform: true,
transform: function transform(content) {
return new Promise((resolve) => {
resolve(`${content}changed!`);
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('cache option', () => {
patterns: [
{
from,
cache: true,
cacheTransform: true,
transform: function transform(content) {
return new Promise((resolve) => {
resolve(`${content}changed!`);
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('cache option', () => {
patterns: [
{
from,
cache: {
cacheTransform: {
key: 'foobar',
},
transform(content) {
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('cache option', () => {
patterns: [
{
from,
cache: true,
cacheTransform: true,
// eslint-disable-next-line no-shadow
transform: function transform(content) {
expect(isGzip(content)).toBe(true);
Expand Down
6 changes: 3 additions & 3 deletions test/validate-options.test.js
Expand Up @@ -64,8 +64,8 @@ describe('validate options', () => {
},
],
flatten: true,
cache: true,
transform: () => {},
cacheTransform: true,
transformPath: () => {},
},
],
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('validate options', () => {
from: 'test.txt',
to: 'dir',
context: 'context',
cache: {
cacheTransform: {
foo: 'bar',
},
},
Expand Down Expand Up @@ -203,7 +203,7 @@ describe('validate options', () => {
from: 'test.txt',
to: 'dir',
context: 'context',
cache: () => {},
cacheTransform: () => {},
},
],
[
Expand Down

0 comments on commit f6da280

Please sign in to comment.