From f6da280abe3953990bfdad4dd1225c6e58382611 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Tue, 28 Apr 2020 19:14:13 +0300 Subject: [PATCH] refactor: rename the `cache` option to `cacheTransform` (#452) BREAKING CHANGE: the `cache` option was renamed to `cacheTransform` --- README.md | 112 +++++++++--------- src/options.json | 8 +- src/postProcessPattern.js | 6 +- .../validate-options.test.js.snap | 22 ++-- ....test.js => cacheTransform-option.test.js} | 10 +- test/validate-options.test.js | 6 +- 6 files changed, 82 insertions(+), 82 deletions(-) rename test/{cache-option.test.js => cacheTransform-option.test.js} (97%) diff --git a/README.md b/README.md index 5f4f34e7..8fe97fe1 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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` @@ -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** @@ -412,7 +438,6 @@ module.exports = { transform(content, path) { return optimize(content); }, - cache: true, }, ], }), @@ -420,13 +445,6 @@ module.exports = { }; ``` -#### `transform` - -Type: `Function` -Default: `undefined` - -Allows to modify the file contents. - **webpack.config.js** ```js @@ -438,7 +456,7 @@ module.exports = { from: 'src/*.png', to: 'dest/', transform(content, path) { - return optimize(content); + return Promise.resolve(optimize(content)); }, }, ], @@ -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 @@ -458,8 +484,9 @@ module.exports = { from: 'src/*.png', to: 'dest/', transform(content, path) { - return Promise.resolve(optimize(content)); + return optimize(content); }, + cacheTransform: true, }, ], }), @@ -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 | diff --git a/src/options.json b/src/options.json index d8b627ae..738bc297 100644 --- a/src/options.json +++ b/src/options.json @@ -45,7 +45,10 @@ "flatten": { "type": "boolean" }, - "cache": { + "transform": { + "instanceof": "Function" + }, + "cacheTransform": { "anyOf": [ { "type": "boolean" @@ -55,9 +58,6 @@ } ] }, - "transform": { - "instanceof": "Function" - }, "transformPath": { "instanceof": "Function" } diff --git a/src/postProcessPattern.js b/src/postProcessPattern.js index bc95494a..6cbc0602 100644 --- a/src/postProcessPattern.js +++ b/src/postProcessPattern.js @@ -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, diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index 78644995..e57173a1 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -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`] = ` @@ -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 { … } @@ -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 { … } @@ -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 @@ -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 { … }" `; @@ -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`] = ` diff --git a/test/cache-option.test.js b/test/cacheTransform-option.test.js similarity index 97% rename from test/cache-option.test.js rename to test/cacheTransform-option.test.js index d2f4b415..62aad976 100644 --- a/test/cache-option.test.js +++ b/test/cacheTransform-option.test.js @@ -27,7 +27,7 @@ describe('cache option', () => { patterns: [ { from, - cache: true, + cacheTransform: true, transform: function transform(content) { return new Promise((resolve) => { resolve(`${content}changed!`); @@ -74,7 +74,7 @@ describe('cache option', () => { patterns: [ { from, - cache: true, + cacheTransform: true, transform: function transform(content) { return new Promise((resolve) => { resolve(`${content}changed!`); @@ -114,7 +114,7 @@ describe('cache option', () => { patterns: [ { from, - cache: true, + cacheTransform: true, transform: function transform(content) { return new Promise((resolve) => { resolve(`${content}changed!`); @@ -156,7 +156,7 @@ describe('cache option', () => { patterns: [ { from, - cache: { + cacheTransform: { key: 'foobar', }, transform(content) { @@ -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); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index fde45c02..13ada824 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -64,8 +64,8 @@ describe('validate options', () => { }, ], flatten: true, - cache: true, transform: () => {}, + cacheTransform: true, transformPath: () => {}, }, ], @@ -100,7 +100,7 @@ describe('validate options', () => { from: 'test.txt', to: 'dir', context: 'context', - cache: { + cacheTransform: { foo: 'bar', }, }, @@ -203,7 +203,7 @@ describe('validate options', () => { from: 'test.txt', to: 'dir', context: 'context', - cache: () => {}, + cacheTransform: () => {}, }, ], [