Skip to content

Commit

Permalink
refactor: remove the ignore option in favor globOptions.ignore (#463)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `ignore` option was removed in favor `globOptions.ignore`
  • Loading branch information
cap-Bernardito committed May 12, 2020
1 parent 6b07a63 commit 0be6470
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 430 deletions.
114 changes: 52 additions & 62 deletions README.md
Expand Up @@ -79,11 +79,10 @@ module.exports = {
| [`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 |
| [`globOptions`](#globoptions) | `{Object}` | `undefined` | [Options][glob-options] passed to the glob pattern matching library, including `ignore` option |
| [`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. |
Expand All @@ -95,7 +94,8 @@ Type: `String`
Default: `undefined`

Glob or path from where we сopy files.
Globs accept [micromatch options](https://github.com/micromatch/micromatch).
Globs accept [fast-glob pattern-syntax](https://github.com/mrmlnc/fast-glob#pattern-syntax).
Glob can only be a `string`.

> ⚠️ Don't use directly `\\` in `from` (i.e `path\to\file.ext`) option because on UNIX the backslash is a valid character inside a path component, i.e., it's not a separator.
> On Windows, the forward slash and the backward slash are both separators.
Expand Down Expand Up @@ -126,21 +126,62 @@ module.exports = {

If you define `from` as file path or folder path on `Windows`, you can use windows path segment (`\\`)

```
...
from: path.resolve('__dirname', 'file.txt'),
...
```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve('__dirname', 'file.txt'),
},
],
}),
],
};
```

But you should always use forward-slashes in `glob` expressions
See [fast-glob manual](https://github.com/mrmlnc/fast-glob#how-to-write-patterns-on-windows).

```js
const FIXTURES_DIR_NORMALIZED = path
.resolve(__dirname, 'fixtures')
.replace(/\\/g, '/');

module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: path.posix.join(FIXTURES_DIR_NORMALIZED, 'file.txt'),
},
],
}),
],
};
```
...
const FIXTURES_DIR_NORMALIZED = path.resolve(__dirname, 'fixtures').replace(/\\/g, '/');

from: path.posix.join(FIXTURES_DIR_NORMALIZED, 'file.txt'),
...
##### `For exclude files`

To exclude files from the selection, you should use [globOptions.ignore option](https://github.com/mrmlnc/fast-glob#ignore)

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: '**/*',
globOptions: {
ignore: ['**/file.*', '**/ignored-directory/**'],
},
},
],
}),
],
};
```

#### `to`
Expand Down Expand Up @@ -367,51 +408,6 @@ module.exports = {
};
```

#### `ignore`

Type: `Array`
Default: `[]`

Globs to ignore files.

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: 'src/**/*',
to: 'dest/',
ignore: ['*.js'],
},
],
}),
],
};
```

> ⚠️ Note that only relative path should be provided to ignore option, an example to ignore `src/assets/subfolder/ignorefile.js` :
**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: 'src/assets',
to: 'dest/',
ignore: ['subfolder/ignorefile.js'],
},
],
}),
],
};
```

#### `flatten`

Type: `Boolean`
Expand Down Expand Up @@ -566,12 +562,6 @@ module.exports = {
};
```

### Options

| Name | Type | Default | Description |
| :-----------------: | :-------: | :-----: | :------------------------------------------- |
| [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |

#### `ignore`

Array of globs to ignore (applied to `from`).
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -46,7 +46,6 @@
"glob-parent": "^5.1.1",
"globby": "^11.0.0",
"loader-utils": "^2.0.0",
"minimatch": "^3.0.4",
"normalize-path": "^3.0.0",
"p-limit": "^2.3.0",
"schema-utils": "^2.6.6",
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Expand Up @@ -33,7 +33,6 @@ class CopyPlugin {
compilation,
inputFileSystem: compiler.inputFileSystem,
output: compiler.options.output.path,
ignore: this.options.ignore || [],
concurrency: this.options.concurrency,
};

Expand Down
19 changes: 1 addition & 18 deletions src/options.json
Expand Up @@ -29,19 +29,6 @@
"force": {
"type": "boolean"
},
"ignore": {
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
}
},
"flatten": {
"type": "boolean"
},
Expand Down Expand Up @@ -89,11 +76,7 @@
"options": {
"type": "object",
"additionalProperties": false,
"properties": {
"ignore": {
"type": "array"
}
}
"properties": {}
}
}
}
2 changes: 0 additions & 2 deletions src/preProcessPattern.js
Expand Up @@ -29,8 +29,6 @@ export default async function preProcessPattern(globalRef, pattern) {
pattern.context = path.normalize(pattern.context);
pattern.to = path.normalize(pattern.to);

pattern.ignore = globalRef.ignore.concat(pattern.ignore || []);

logger.debug(`processing from: '${pattern.from}' to: '${pattern.to}'`);

switch (true) {
Expand Down
43 changes: 0 additions & 43 deletions src/processPattern.js
Expand Up @@ -2,9 +2,7 @@ import path from 'path';

import globby from 'globby';
import pLimit from 'p-limit';
import minimatch from 'minimatch';

import isObject from './utils/isObject';
import createPatternGlob from './utils/createPatternGlob';

/* eslint-disable no-param-reassign */
Expand Down Expand Up @@ -56,47 +54,6 @@ export default async function processPattern(globalRef, pattern) {

logger.debug(`found ${from}`);

// Check the ignore list
let il = pattern.ignore.length;

// eslint-disable-next-line no-plusplus
while (il--) {
const ignoreGlob = pattern.ignore[il];

let globParams = {
dot: true,
matchBase: true,
};

let glob;

if (typeof ignoreGlob === 'string') {
glob = ignoreGlob;
} else if (isObject(ignoreGlob)) {
glob = ignoreGlob.glob || '';

const ignoreGlobParams = Object.assign({}, ignoreGlob);
delete ignoreGlobParams.glob;

// Overwrite minimatch defaults
globParams = Object.assign(globParams, ignoreGlobParams);
} else {
glob = '';
}

logger.debug(`testing ${glob} against ${file.relativeFrom}`);

if (minimatch(file.relativeFrom, glob, globParams)) {
logger.log(
`ignoring '${file.relativeFrom}', because it matches the ignore glob '${glob}'`
);

return Promise.resolve();
}

logger.debug(`${glob} doesn't match ${file.relativeFrom}`);
}

// Change the to path to be relative for webpack
if (pattern.toType === 'dir') {
file.webpackTo = path.join(pattern.to, file.relativeFrom);
Expand Down
17 changes: 10 additions & 7 deletions src/utils/createPatternGlob.js
Expand Up @@ -38,9 +38,11 @@ function createPatternGlob(pattern, globalRef) {
'**/*'
);
pattern.absoluteFrom = path.join(pattern.absoluteFrom, '**/*');
pattern.globOptions = {
dot: true,
};

if (typeof pattern.globOptions.dot === 'undefined') {
pattern.globOptions.dot = true;
}

break;

case 'file':
Expand All @@ -50,9 +52,11 @@ function createPatternGlob(pattern, globalRef) {

pattern.context = path.dirname(pattern.absoluteFrom);
pattern.glob = getAbsoluteContext(pattern.absoluteFrom);
pattern.globOptions = {
dot: true,
};

if (typeof pattern.globOptions.dot === 'undefined') {
pattern.globOptions.dot = true;
}

break;

default:
Expand All @@ -67,7 +71,6 @@ function createPatternGlob(pattern, globalRef) {
compilation.contextDependencies.add(contextDependencies);

pattern.fromType = 'glob';
pattern.globOptions = pattern.globOptions || {};
pattern.glob = path.isAbsolute(pattern.fromOrigin)
? pattern.fromOrigin
: path.posix.join(
Expand Down
2 changes: 0 additions & 2 deletions src/utils/isObject.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/__snapshots__/CopyPlugin.test.js.snap
Expand Up @@ -10,14 +10,6 @@ Object {
"add ./fixtures/file.txt as fileDependencies",
"begin globbing './fixtures/file.txt' with a context of './fixtures'",
"found ./fixtures/file.txt",
"testing watch/**/* against file.txt",
"watch/**/* doesn't match file.txt",
"testing directory-ln against file.txt",
"directory-ln doesn't match file.txt",
"testing file-ln.txt against file.txt",
"file-ln.txt doesn't match file.txt",
"testing symlink/**/* against file.txt",
"symlink/**/* doesn't match file.txt",
"determined that './fixtures/file.txt' should write to 'file.txt'",
"getting stats for './fixtures/file.txt' to write to assets",
"reading './fixtures/file.txt' to write to assets",
Expand Down

0 comments on commit 0be6470

Please sign in to comment.