Skip to content

Commit eaf4306

Browse files
fix: watch on windows (#359)
1 parent a8fc34e commit eaf4306

File tree

5 files changed

+189
-200
lines changed

5 files changed

+189
-200
lines changed

README.md

Lines changed: 127 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,34 @@ module.exports = {
5858

5959
### Patterns
6060

61-
| Name | Type | Default | Description |
62-
| :-------------------------------: | :-------------------: | :---------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
63-
| [`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. |
64-
| [`to`](#to) | `{String\|Object}` | `undefined` | Output root if `from` is file or dir, resolved glob path if `from` is glob. |
65-
| [`toType`](#toType) | `{String}` | `undefined` | `[toType Options](#totype)`. |
66-
| [`test`](#test) | `{RegExp}` | `undefined` | Pattern for extracting elements to be used in `to` templates. |
67-
| [`force`](#force) | `{Boolean}` | `false` | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders). |
68-
| [`ignore`](#ignore) | `{Array}` | `[]` | Globs to ignore for this pattern. |
69-
| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic. |
70-
| [`transform`](#transform) | `{Function\|Promise}` | `(content, path) => content` | Function or Promise that modifies file contents before copying. |
71-
| [`transformPath`](#transformPath) | `{Function\|Promise}` | `(targetPath, sourcePath) => path` | Function or Promise that modifies file writing path. |
72-
| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
73-
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
61+
| Name | Type | Default | Description |
62+
| :-------------------------------: | :-------------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
63+
| [`from`](#from) | `{String\|Object}` | `undefined` | Glob or path from where we сopy files. |
64+
| [`to`](#to) | `{String}` | `undefined` | Output path. |
65+
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
66+
| [`toType`](#toType) | `{String}` | `undefined` | Determinate what is `to` option - directory, file or template. |
67+
| [`test`](#test) | `{RegExp}` | `undefined` | Pattern for extracting elements to be used in `to` templates. |
68+
| [`force`](#force) | `{Boolean}` | `false` | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders). |
69+
| [`ignore`](#ignore) | `{Array}` | `[]` | Globs to ignore files. |
70+
| [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names. |
71+
| [`transform`](#transform) | `{Function\|Promise}` | `undefined` | Allows to modify the file contents. |
72+
| [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
73+
| [`transformPath`](#transformPath) | `{Function\|Promise}` | `undefined` | Allows to modify the writing path. |
7474

7575
#### `from`
7676

77+
Type: `String\|Object`
78+
Default: `undefined`
79+
80+
Glob or path from where we сopy files.
81+
Globs accept [minimatch options](https://github.com/isaacs/minimatch).
82+
83+
You can defined `from` as `Object` and use the [`node-glob` options](https://github.com/isaacs/node-glob#options).
84+
85+
> ⚠️ 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.
86+
> On Windows, the forward slash and the backward slash are both separators.
87+
> Instead please use `/` or `path` methods.
88+
7789
**webpack.config.js**
7890

7991
```js
@@ -93,6 +105,15 @@ module.exports = {
93105

94106
#### `to`
95107

108+
Type: `String`
109+
Default: `undefined`
110+
111+
Output path.
112+
113+
> ⚠️ 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.
114+
> On Windows, the forward slash and the backward slash are both separators.
115+
> Instead please use `/` or `path` methods.
116+
96117
**webpack.config.js**
97118

98119
```js
@@ -106,8 +127,43 @@ module.exports = {
106127
};
107128
```
108129

130+
#### `context`
131+
132+
Type: `String`
133+
Default: `options.context|compiler.options.context`
134+
135+
A path that determines how to interpret the `from` path.
136+
137+
> ⚠️ 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.
138+
> On Windows, the forward slash and the backward slash are both separators.
139+
> Instead please use `/` or `path` methods.
140+
141+
**webpack.config.js**
142+
143+
```js
144+
module.exports = {
145+
plugins: [
146+
new CopyPlugin([
147+
{
148+
from: 'src/*.txt',
149+
to: 'dest/',
150+
context: 'app/',
151+
},
152+
]),
153+
],
154+
};
155+
```
156+
109157
#### `toType`
110158

159+
Type: `String`
160+
Default: `undefined`
161+
162+
Determinate what is `to` option - directory, file or template.
163+
Sometimes it is hard to say what is `to`, example `path/to/dir-with.ext`.
164+
If you want to copy files in directory you need use `dir` option.
165+
We try to automatically determine the `type` so you most likely do not need this option.
166+
111167
| Name | Type | Default | Description |
112168
| :--------------: | :--------: | :---------: | :------------------------------------------------------------------------------------------------- |
113169
| **`'dir'`** | `{String}` | `undefined` | If `from` is directory, `to` has no extension or ends in `'/'` |
@@ -170,6 +226,11 @@ module.exports = {
170226

171227
#### `test`
172228

229+
Type: `RegExp`
230+
Default: `undefined`
231+
232+
Pattern for extracting elements to be used in `to` templates.
233+
173234
Defines a `{RegExp}` to match some parts of the file path.
174235
These capture groups can be reused in the name property using `[N]` placeholder.
175236
Note that `[0]` will be replaced by the entire path of the file,
@@ -194,6 +255,11 @@ module.exports = {
194255

195256
#### `force`
196257

258+
Type: `Boolean`
259+
Default: `false`
260+
261+
Overwrites files already in `compilation.assets` (usually added by other plugins/loaders).
262+
197263
**webpack.config.js**
198264

199265
```js
@@ -212,6 +278,11 @@ module.exports = {
212278

213279
#### `ignore`
214280

281+
Type: `Array`
282+
Default: `[]`
283+
284+
Globs to ignore files.
285+
215286
**webpack.config.js**
216287

217288
```js
@@ -230,6 +301,13 @@ module.exports = {
230301

231302
#### `flatten`
232303

304+
Type: `Boolean`
305+
Default: `false`
306+
307+
Removes all directory references and only copies file names.
308+
309+
> ⚠️ If files have the same name, the result is non-deterministic.
310+
233311
**webpack.config.js**
234312

235313
```js
@@ -248,6 +326,11 @@ module.exports = {
248326

249327
#### `transform`
250328

329+
Type: `Function|Promise`
330+
Default: `undefined`
331+
332+
Allows to modify the file contents.
333+
251334
##### `{Function}`
252335

253336
**webpack.config.js**
@@ -288,9 +371,13 @@ module.exports = {
288371
};
289372
```
290373

291-
#### `transformPath`
374+
#### `cache`
292375

293-
##### `{Function}`
376+
Type: `Boolean|Object`
377+
Default: `false`
378+
379+
Enable/disable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache.
380+
Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.
294381

295382
**webpack.config.js**
296383

@@ -301,36 +388,28 @@ module.exports = {
301388
{
302389
from: 'src/*.png',
303390
to: 'dest/',
304-
transformPath(targetPath, absolutePath) {
305-
return 'newPath';
391+
transform(content, path) {
392+
return optimize(content);
306393
},
394+
cache: true,
307395
},
308396
]),
309397
],
310398
};
311399
```
312400

313-
##### `{Promise}`
401+
#### `transformPath`
314402

315-
**webpack.config.js**
403+
Type: `Function|Promise`
404+
Default: `undefined`
316405

317-
```js
318-
module.exports = {
319-
plugins: [
320-
new CopyPlugin([
321-
{
322-
from: 'src/*.png',
323-
to: 'dest/',
324-
transformPath(targePath, absolutePath) {
325-
return Promise.resolve('newPath');
326-
},
327-
},
328-
]),
329-
],
330-
};
331-
```
406+
Allows to modify the writing path.
332407

333-
#### `cache`
408+
> ⚠️ 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.
409+
> On Windows, the forward slash and the backward slash are both separators.
410+
> Instead please use `/` or `path` methods.
411+
412+
##### `{Function}`
334413

335414
**webpack.config.js**
336415

@@ -341,17 +420,16 @@ module.exports = {
341420
{
342421
from: 'src/*.png',
343422
to: 'dest/',
344-
transform(content, path) {
345-
return optimize(content);
423+
transformPath(targetPath, absolutePath) {
424+
return 'newPath';
346425
},
347-
cache: true,
348426
},
349427
]),
350428
],
351429
};
352430
```
353431

354-
#### `context`
432+
##### `{Promise}`
355433

356434
**webpack.config.js**
357435

@@ -360,9 +438,11 @@ module.exports = {
360438
plugins: [
361439
new CopyPlugin([
362440
{
363-
from: 'src/*.txt',
441+
from: 'src/*.png',
364442
to: 'dest/',
365-
context: 'app/',
443+
transformPath(targePath, absolutePath) {
444+
return Promise.resolve('newPath');
445+
},
366446
},
367447
]),
368448
],
@@ -404,6 +484,8 @@ module.exports = {
404484

405485
#### `ignore`
406486

487+
Array of globs to ignore (applied to `from`).
488+
407489
**webpack.config.js**
408490

409491
```js
@@ -414,6 +496,8 @@ module.exports = {
414496

415497
#### `context`
416498

499+
A path that determines how to interpret the `from` path, shared for all patterns.
500+
417501
**webpack.config.js**
418502

419503
```js
@@ -424,6 +508,8 @@ module.exports = {
424508

425509
#### `copyUnmodified`
426510

511+
Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option.
512+
427513
> ℹ️ 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.
428514
429515
**webpack.config.js**

src/postProcessPattern.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import loaderUtils from 'loader-utils';
66
import cacache from 'cacache';
77
import serialize from 'serialize-javascript';
88
import findCacheDir from 'find-cache-dir';
9-
import normalizePath from 'normalize-path';
109

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

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

117-
// Developers can use invalid slashes in regex we should fix it
118-
file.webpackTo = normalizePath(
119-
loaderUtils.interpolateName(
120-
{ resourcePath: file.absoluteFrom },
121-
file.webpackTo,
122-
{
123-
content,
124-
regExp: file.webpackToRegExp,
125-
context: pattern.context,
126-
}
127-
)
116+
file.webpackTo = loaderUtils.interpolateName(
117+
{ resourcePath: file.absoluteFrom },
118+
file.webpackTo,
119+
{
120+
content,
121+
regExp: file.webpackToRegExp,
122+
context: pattern.context,
123+
}
128124
);
125+
126+
// Bug in `loader-utils`, package convert `\\` to `/`, need fix in loader-utils
127+
file.webpackTo = path.normalize(file.webpackTo);
129128
}
130129

131130
return content;
@@ -141,7 +140,7 @@ export default function postProcessPattern(globalRef, pattern, file) {
141140
)
142141
.then((newPath) => {
143142
// Developers can use invalid slashes we should fix it
144-
file.webpackTo = normalizePath(newPath);
143+
file.webpackTo = path.normalize(newPath);
145144
})
146145
.then(() => content);
147146
}

0 commit comments

Comments
 (0)