Skip to content

Commit

Permalink
refactor: fromArgs option (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Feb 18, 2019
1 parent e870664 commit 11c9aef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
29 changes: 14 additions & 15 deletions README.md
Expand Up @@ -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`

Expand All @@ -86,7 +85,7 @@ module.exports = {
'relative/path/to/dir',
'/absolute/path/to/dir',
'**/*',
{ glob: '**/*', dot: true },
{ glob: '**/*', dot: false },
]),
],
};
Expand Down
10 changes: 5 additions & 5 deletions src/preProcessPattern.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/processPattern.js
Expand Up @@ -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') {
Expand All @@ -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(() => {
Expand Down
22 changes: 20 additions & 2 deletions test/CopyPlugin.test.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -1680,7 +1696,7 @@ describe('apply function', () => {
'noextension',
],
options: {
ignore: ['.dottedfile'],
ignore: ['.dottedfile', '.file.txt'],
},
patterns: [
{
Expand All @@ -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: [
{
Expand Down Expand Up @@ -1732,6 +1748,7 @@ describe('apply function', () => {
it('ignores nested directory', (done) => {
runEmit({
expectedAssetKeys: [
'.file.txt',
'[!]/hello.txt',
'binextension.bin',
'file.txt',
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions test/helpers/.file.txt
@@ -0,0 +1 @@
dot

0 comments on commit 11c9aef

Please sign in to comment.