Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: watch on windows #359

Merged
merged 1 commit into from Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 127 additions & 41 deletions README.md
Expand Up @@ -58,22 +58,34 @@ module.exports = {

### Patterns

| 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. |
| Name | Type | Default | Description |
| :-------------------------------: | :-------------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
| [`from`](#from) | `{String\|Object}` | `undefined` | Glob or path from where we сopy files. |
| [`to`](#to) | `{String}` | `undefined` | 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) | `{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\|Promise}` | `undefined` | Allows to modify the file contents. |
| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
| [`transformPath`](#transformPath) | `{Function\|Promise}` | `undefined` | Allows to modify the writing path. |

#### `from`

Type: `String\|Object`
Default: `undefined`

Glob or path from where we сopy files.
Globs accept [minimatch options](https://github.com/isaacs/minimatch).

You can defined `from` as `Object` and use the [`node-glob` options](https://github.com/isaacs/node-glob#options).

> ⚠️ 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.
> Instead please use `/` or `path` methods.

**webpack.config.js**

```js
Expand All @@ -93,6 +105,15 @@ module.exports = {

#### `to`

Type: `String`
Default: `undefined`

Output path.

> ⚠️ Don't use directly `\\` in `to` (i.e `path\to\dest`) 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.
> Instead please use `/` or `path` methods.

**webpack.config.js**

```js
Expand All @@ -106,8 +127,43 @@ module.exports = {
};
```

#### `context`

Type: `String`
Default: `options.context|compiler.options.context`

A path that determines how to interpret the `from` path.

> ⚠️ Don't use directly `\\` in `context` (i.e `path\to\context`) 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.
> Instead please use `/` or `path` methods.

**webpack.config.js**

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

#### `toType`

Type: `String`
Default: `undefined`

Determinate what is `to` option - directory, file or template.
Sometimes it is hard to say what is `to`, example `path/to/dir-with.ext`.
If you want to copy files in directory you need use `dir` option.
We try to automatically determine the `type` so you most likely do not need this option.

| Name | Type | Default | Description |
| :--------------: | :--------: | :---------: | :------------------------------------------------------------------------------------------------- |
| **`'dir'`** | `{String}` | `undefined` | If `from` is directory, `to` has no extension or ends in `'/'` |
Expand Down Expand Up @@ -170,6 +226,11 @@ module.exports = {

#### `test`

Type: `RegExp`
Default: `undefined`

Pattern for extracting elements to be used in `to` templates.

Defines a `{RegExp}` to match some parts of the file path.
These capture groups can be reused in the name property using `[N]` placeholder.
Note that `[0]` will be replaced by the entire path of the file,
Expand All @@ -194,6 +255,11 @@ module.exports = {

#### `force`

Type: `Boolean`
Default: `false`

Overwrites files already in `compilation.assets` (usually added by other plugins/loaders).

**webpack.config.js**

```js
Expand All @@ -212,6 +278,11 @@ module.exports = {

#### `ignore`

Type: `Array`
Default: `[]`

Globs to ignore files.

**webpack.config.js**

```js
Expand All @@ -230,6 +301,13 @@ module.exports = {

#### `flatten`

Type: `Boolean`
Default: `false`

Removes all directory references and only copies file names.

> ⚠️ If files have the same name, the result is non-deterministic.

**webpack.config.js**

```js
Expand All @@ -248,6 +326,11 @@ module.exports = {

#### `transform`

Type: `Function|Promise`
Default: `undefined`

Allows to modify the file contents.

##### `{Function}`

**webpack.config.js**
Expand Down Expand Up @@ -288,9 +371,13 @@ module.exports = {
};
```

#### `transformPath`
#### `cache`

##### `{Function}`
Type: `Boolean|Object`
Default: `false`

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`.

**webpack.config.js**

Expand All @@ -301,36 +388,28 @@ module.exports = {
{
from: 'src/*.png',
to: 'dest/',
transformPath(targetPath, absolutePath) {
return 'newPath';
transform(content, path) {
return optimize(content);
},
cache: true,
},
]),
],
};
```

##### `{Promise}`
#### `transformPath`

**webpack.config.js**
Type: `Function|Promise`
Default: `undefined`

```js
module.exports = {
plugins: [
new CopyPlugin([
{
from: 'src/*.png',
to: 'dest/',
transformPath(targePath, absolutePath) {
return Promise.resolve('newPath');
},
},
]),
],
};
```
Allows to modify the writing path.

#### `cache`
> ⚠️ Don't return directly `\\` in `transformPath` (i.e `path\to\newFile`) 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.
> Instead please use `/` or `path` methods.

##### `{Function}`

**webpack.config.js**

Expand All @@ -341,17 +420,16 @@ module.exports = {
{
from: 'src/*.png',
to: 'dest/',
transform(content, path) {
return optimize(content);
transformPath(targetPath, absolutePath) {
return 'newPath';
},
cache: true,
},
]),
],
};
```

#### `context`
##### `{Promise}`

**webpack.config.js**

Expand All @@ -360,9 +438,11 @@ module.exports = {
plugins: [
new CopyPlugin([
{
from: 'src/*.txt',
from: 'src/*.png',
to: 'dest/',
context: 'app/',
transformPath(targePath, absolutePath) {
return Promise.resolve('newPath');
},
},
]),
],
Expand Down Expand Up @@ -404,6 +484,8 @@ module.exports = {

#### `ignore`

Array of globs to ignore (applied to `from`).

**webpack.config.js**

```js
Expand All @@ -414,6 +496,8 @@ module.exports = {

#### `context`

A path that determines how to interpret the `from` path, shared for all patterns.

**webpack.config.js**

```js
Expand All @@ -424,6 +508,8 @@ module.exports = {

#### `copyUnmodified`

Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option.

> ℹ️ By default, we only copy **modified** files during a `webpack --watch` or `webpack-dev-server` build. Setting this option to `true` will copy all files.

**webpack.config.js**
Expand Down
25 changes: 12 additions & 13 deletions src/postProcessPattern.js
Expand Up @@ -6,7 +6,6 @@ import loaderUtils from 'loader-utils';
import cacache from 'cacache';
import serialize from 'serialize-javascript';
import findCacheDir from 'find-cache-dir';
import normalizePath from 'normalize-path';

import { name, version } from '../package.json';

Expand Down Expand Up @@ -114,18 +113,18 @@ export default function postProcessPattern(globalRef, pattern, file) {
file.webpackTo = file.webpackTo.replace(/\.?\[ext\]/g, '');
}

// Developers can use invalid slashes in regex we should fix it
file.webpackTo = normalizePath(
loaderUtils.interpolateName(
{ resourcePath: file.absoluteFrom },
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context,
}
)
file.webpackTo = loaderUtils.interpolateName(
{ resourcePath: file.absoluteFrom },
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context,
}
);

// Bug in `loader-utils`, package convert `\\` to `/`, need fix in loader-utils
file.webpackTo = path.normalize(file.webpackTo);
}

return content;
Expand All @@ -141,7 +140,7 @@ export default function postProcessPattern(globalRef, pattern, file) {
)
.then((newPath) => {
// Developers can use invalid slashes we should fix it
file.webpackTo = normalizePath(newPath);
file.webpackTo = path.normalize(newPath);
})
.then(() => content);
}
Expand Down