From 11c9aefe883cf2f5d8d21f57be591489fb50c52c Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Mon, 18 Feb 2019 17:08:00 +0300 Subject: [PATCH] refactor: `fromArgs` option (#332) --- README.md | 29 ++++++++++++++--------------- src/preProcessPattern.js | 10 +++++----- src/processPattern.js | 6 +++--- test/CopyPlugin.test.js | 22 ++++++++++++++++++++-- test/helpers/.file.txt | 1 + 5 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 test/helpers/.file.txt diff --git a/README.md b/README.md index a93cf6ea..6446c50f 100644 --- a/README.md +++ b/README.md @@ -58,20 +58,19 @@ module.exports = { ### Patterns -| Name | Type | Default | Description | -| :-------------------------------: | :-------------------: | :---------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------- | -| [`from`](#from) | `{String\|Object}` | `undefined` | Globs accept [minimatch options](https://github.com/isaacs/minimatch). | -| [`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}` | `undefined` | `[toType Options](#totype)`. | -| [`test`](#test) | `{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 for this pattern. | -| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic. | -| [`transform`](#transform) | `{Function\|Promise}` | `(content, path) => content` | Function or Promise that modifies file contents before copying. | -| [`transformPath`](#transformPath) | `{Function\|Promise}` | `(targetPath, sourcePath) => path` | Function or Promise that modifies file writing path. | -| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. | -| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. | +| Name | Type | Default | Description | +| :-------------------------------: | :-------------------: | :---------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [`from`](#from) | `{String\|Object}` | `undefined` | Globs accept [minimatch options](https://github.com/isaacs/minimatch). 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}` | `undefined` | `[toType Options](#totype)`. | +| [`test`](#test) | `{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 for this pattern. | +| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic. | +| [`transform`](#transform) | `{Function\|Promise}` | `(content, path) => content` | Function or Promise that modifies file contents before copying. | +| [`transformPath`](#transformPath) | `{Function\|Promise}` | `(targetPath, sourcePath) => path` | Function or Promise that modifies file writing path. | +| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. | +| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. | #### `from` @@ -86,7 +85,7 @@ module.exports = { 'relative/path/to/dir', '/absolute/path/to/dir', '**/*', - { glob: '**/*', dot: true }, + { glob: '**/*', dot: false }, ]), ], }; diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js index 128a1a24..b0a9364b 100644 --- a/src/preProcessPattern.js +++ b/src/preProcessPattern.js @@ -65,11 +65,11 @@ export default function preProcessPattern(globalRef, pattern) { if (isObject(pattern.from) && pattern.from.glob) { pattern.fromType = 'glob'; - const fromArgs = Object.assign({}, pattern.from); - delete fromArgs.glob; + const globOptions = Object.assign({}, pattern.from); + delete globOptions.glob; - pattern.fromArgs = fromArgs; pattern.glob = escape(pattern.context, pattern.from.glob); + pattern.globOptions = globOptions; pattern.absoluteFrom = path.resolve(pattern.context, pattern.from.glob); return Promise.resolve(pattern); @@ -123,7 +123,7 @@ export default function preProcessPattern(globalRef, pattern) { pattern.context = pattern.absoluteFrom; pattern.glob = escape(pattern.absoluteFrom, '**/*'); pattern.absoluteFrom = path.join(pattern.absoluteFrom, '**/*'); - pattern.fromArgs = { + pattern.globOptions = { dot: true, }; } else if (stats.isFile()) { @@ -132,7 +132,7 @@ export default function preProcessPattern(globalRef, pattern) { pattern.fromType = 'file'; pattern.context = path.dirname(pattern.absoluteFrom); pattern.glob = escape(pattern.absoluteFrom); - pattern.fromArgs = { + pattern.globOptions = { dot: true, }; } else if (!pattern.fromType) { diff --git a/src/processPattern.js b/src/processPattern.js index 023354a8..e7370ae1 100644 --- a/src/processPattern.js +++ b/src/processPattern.js @@ -9,11 +9,11 @@ import isObject from './utils/isObject'; export default function processPattern(globalRef, pattern) { const { info, debug, output, concurrency, contextDependencies } = globalRef; - const globArgs = Object.assign( + const globOptions = Object.assign( { cwd: pattern.context, }, - pattern.fromArgs || {} + pattern.globOptions || {} ); if (pattern.fromType === 'nonexistent') { @@ -26,7 +26,7 @@ export default function processPattern(globalRef, pattern) { `begin globbing '${pattern.glob}' with a context of '${pattern.context}'` ); - return globby(pattern.glob, globArgs).then((paths) => + return globby(pattern.glob, globOptions).then((paths) => Promise.all( paths.map((from) => limit(() => { diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js index 4b50e211..81415da4 100644 --- a/test/CopyPlugin.test.js +++ b/test/CopyPlugin.test.js @@ -331,6 +331,22 @@ describe('apply function', () => { .catch(done); }); + it('can use a glob object to move a file to the root directory and respect glob options', (done) => { + runEmit({ + expectedAssetKeys: ['file.txt'], + patterns: [ + { + from: { + glob: '*.txt', + dot: false, + }, + }, + ], + }) + .then(done) + .catch(done); + }); + it('can use a glob to move multiple files to the root directory', (done) => { runEmit({ expectedAssetKeys: [ @@ -1680,7 +1696,7 @@ describe('apply function', () => { 'noextension', ], options: { - ignore: ['.dottedfile'], + ignore: ['.dottedfile', '.file.txt'], }, patterns: [ { @@ -1694,7 +1710,7 @@ describe('apply function', () => { it('ignores all files except those with dots', (done) => { runEmit({ - expectedAssetKeys: ['directory/.dottedfile'], + expectedAssetKeys: ['.file.txt', 'directory/.dottedfile'], options: { ignore: [ { @@ -1732,6 +1748,7 @@ describe('apply function', () => { it('ignores nested directory', (done) => { runEmit({ expectedAssetKeys: [ + '.file.txt', '[!]/hello.txt', 'binextension.bin', 'file.txt', @@ -1760,6 +1777,7 @@ describe('apply function', () => { it('ignores nested directory(can use "\\" to escape if path.sep is "/")', (done) => { runEmit({ expectedAssetKeys: [ + '.file.txt', '[!]/hello.txt', 'binextension.bin', 'file.txt', diff --git a/test/helpers/.file.txt b/test/helpers/.file.txt new file mode 100644 index 00000000..a2373c72 --- /dev/null +++ b/test/helpers/.file.txt @@ -0,0 +1 @@ +dot