1 | <div align="center">
|
2 | <a href="https://github.com/webpack/webpack">
|
3 | <img width="200" height="200"
|
4 | src="https://webpack.js.org/assets/icon-square-big.svg">
|
5 | </a>
|
6 | </div>
|
7 |
|
8 | [![npm][npm]][npm-url]
|
9 | [![node][node]][node-url]
|
10 | [![deps][deps]][deps-url]
|
11 | [![tests][tests]][tests-url]
|
12 | [![cover][cover]][cover-url]
|
13 | [![chat][chat]][chat-url]
|
14 | [![size][size]][size-url]
|
15 |
|
16 | # copy-webpack-plugin
|
17 |
|
18 | Copies individual files or entire directories, which already exist, to the build directory.
|
19 |
|
20 | ## Getting Started
|
21 |
|
22 | To begin, you'll need to install `copy-webpack-plugin`:
|
23 |
|
24 | ```console
|
25 | $ npm install copy-webpack-plugin --save-dev
|
26 | ```
|
27 |
|
28 | Then add the plugin to your `webpack` config. For example:
|
29 |
|
30 | **webpack.config.js**
|
31 |
|
32 | ```js
|
33 | const CopyPlugin = require('copy-webpack-plugin');
|
34 |
|
35 | module.exports = {
|
36 | plugins: [
|
37 | new CopyPlugin([
|
38 | { from: 'source', to: 'dest' },
|
39 | { from: 'other', to: 'public' },
|
40 | ]),
|
41 | ],
|
42 | };
|
43 | ```
|
44 |
|
45 | > ℹ️ `webpack-copy-plugin` is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process.
|
46 |
|
47 | > ℹ️ If you want `webpack-dev-server` to write files to the output directory during development, you can force it with the [`writeToDisk`](https://github.com/webpack/webpack-dev-middleware#writetodisk) option or the [`write-file-webpack-plugin`](https://github.com/gajus/write-file-webpack-plugin).
|
48 |
|
49 | ## Options
|
50 |
|
51 | The plugin's signature:
|
52 |
|
53 | **webpack.config.js**
|
54 |
|
55 | ```js
|
56 | module.exports = {
|
57 | plugins: [new CopyPlugin(patterns, options)],
|
58 | };
|
59 | ```
|
60 |
|
61 | ### Patterns
|
62 |
|
63 | | Name | Type | Default | Description |
|
64 | | :-------------------------------: | :-----------------: | :---------------------------------------------: | :---------------------------------------------------------------------------------------------------- |
|
65 | | [`from`](#from) | `{String\|Object}` | `undefined` | Glob or path from where we сopy files. |
|
66 | | [`to`](#to) | `{String}` | `compiler.options.output` | Output path. |
|
67 | | [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. |
|
68 | | [`toType`](#totype) | `{String}` | `undefined` | Determinate what is `to` option - directory, file or template. |
|
69 | | [`test`](#test) | `{String\|RegExp}` | `undefined` | Pattern for extracting elements to be used in `to` templates. |
|
70 | | [`force`](#force) | `{Boolean}` | `false` | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders). |
|
71 | | [`ignore`](#ignore) | `{Array}` | `[]` | Globs to ignore files. |
|
72 | | [`flatten`](#flatten) | `{Boolean}` | `false` | Removes all directory references and only copies file names. |
|
73 | | [`cache`](#cache) | `{Boolean\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
|
74 | | [`transform`](#transform) | `{Function}` | `undefined` | Allows to modify the file contents. |
|
75 | | [`transformPath`](#transformpath) | `{Function}` | `undefined` | Allows to modify the writing path. |
|
76 |
|
77 | #### `from`
|
78 |
|
79 | Type: `String|Object`
|
80 | Default: `undefined`
|
81 |
|
82 | Glob or path from where we сopy files.
|
83 | Globs accept [minimatch options](https://github.com/isaacs/minimatch).
|
84 |
|
85 | You can define `from` as `Object` and use the [`node-glob` options](https://github.com/isaacs/node-glob#options).
|
86 |
|
87 | > ⚠️ 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.
|
88 | > On Windows, the forward slash and the backward slash are both separators.
|
89 | > Instead please use `/` or `path` methods.
|
90 |
|
91 | **webpack.config.js**
|
92 |
|
93 | ```js
|
94 | module.exports = {
|
95 | plugins: [
|
96 | new CopyPlugin([
|
97 | 'relative/path/to/file.ext',
|
98 | '/absolute/path/to/file.ext',
|
99 | 'relative/path/to/dir',
|
100 | '/absolute/path/to/dir',
|
101 | '**/*',
|
102 | {
|
103 | from: '**/*',
|
104 | globOptions: {
|
105 | dot: false,
|
106 | },
|
107 | },
|
108 | ]),
|
109 | ],
|
110 | };
|
111 | ```
|
112 |
|
113 | #### `to`
|
114 |
|
115 | Type: `String`
|
116 | Default: `compiler.options.output`
|
117 |
|
118 | Output path.
|
119 |
|
120 | > ⚠️ 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.
|
121 | > On Windows, the forward slash and the backward slash are both separators.
|
122 | > Instead please use `/` or `path` methods.
|
123 |
|
124 | **webpack.config.js**
|
125 |
|
126 | ```js
|
127 | module.exports = {
|
128 | plugins: [
|
129 | new CopyPlugin([
|
130 | {
|
131 | from: '**/*',
|
132 | to: 'relative/path/to/dest/',
|
133 | },
|
134 | {
|
135 | from: '**/*',
|
136 | to: '/absolute/path/to/dest/',
|
137 | },
|
138 | {
|
139 | from: '**/*',
|
140 | to: '[path][name].[contenthash].[ext]',
|
141 | },
|
142 | ]),
|
143 | ],
|
144 | };
|
145 | ```
|
146 |
|
147 | #### `context`
|
148 |
|
149 | Type: `String`
|
150 | Default: `options.context|compiler.options.context`
|
151 |
|
152 | A path that determines how to interpret the `from` path.
|
153 |
|
154 | > ⚠️ 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.
|
155 | > On Windows, the forward slash and the backward slash are both separators.
|
156 | > Instead please use `/` or `path` methods.
|
157 |
|
158 | **webpack.config.js**
|
159 |
|
160 | ```js
|
161 | module.exports = {
|
162 | plugins: [
|
163 | new CopyPlugin([
|
164 | {
|
165 | from: 'src/*.txt',
|
166 | to: 'dest/',
|
167 | context: 'app/',
|
168 | },
|
169 | ]),
|
170 | ],
|
171 | };
|
172 | ```
|
173 |
|
174 | #### `toType`
|
175 |
|
176 | Type: `String`
|
177 | Default: `undefined`
|
178 |
|
179 | Determinate what is `to` option - directory, file or template.
|
180 | Sometimes it is hard to say what is `to`, example `path/to/dir-with.ext`.
|
181 | If you want to copy files in directory you need use `dir` option.
|
182 | We try to automatically determine the `type` so you most likely do not need this option.
|
183 |
|
184 | | Name | Type | Default | Description |
|
185 | | :--------------: | :--------: | :---------: | :------------------------------------------------------------------------------------------------- |
|
186 | | **`'dir'`** | `{String}` | `undefined` | If `from` is directory, `to` has no extension or ends in `'/'` |
|
187 | | **`'file'`** | `{String}` | `undefined` | If `to` has extension or `from` is file |
|
188 | | **`'template'`** | `{String}` | `undefined` | If `to` contains [a template pattern](https://github.com/webpack-contrib/file-loader#placeholders) |
|
189 |
|
190 | ##### `'dir'`
|
191 |
|
192 | **webpack.config.js**
|
193 |
|
194 | ```js
|
195 | module.exports = {
|
196 | plugins: [
|
197 | new CopyPlugin([
|
198 | {
|
199 | from: 'path/to/file.txt',
|
200 | to: 'directory/with/extension.ext',
|
201 | toType: 'dir',
|
202 | },
|
203 | ]),
|
204 | ],
|
205 | };
|
206 | ```
|
207 |
|
208 | ##### `'file'`
|
209 |
|
210 | **webpack.config.js**
|
211 |
|
212 | ```js
|
213 | module.exports = {
|
214 | plugins: [
|
215 | new CopyPlugin([
|
216 | {
|
217 | from: 'path/to/file.txt',
|
218 | to: 'file/without/extension',
|
219 | toType: 'file',
|
220 | },
|
221 | ]),
|
222 | ],
|
223 | };
|
224 | ```
|
225 |
|
226 | ##### `'template'`
|
227 |
|
228 | **webpack.config.js**
|
229 |
|
230 | ```js
|
231 | module.exports = {
|
232 | plugins: [
|
233 | new CopyPlugin([
|
234 | {
|
235 | from: 'src/',
|
236 | to: 'dest/[name].[hash].[ext]',
|
237 | toType: 'template',
|
238 | },
|
239 | ]),
|
240 | ],
|
241 | };
|
242 | ```
|
243 |
|
244 | #### `test`
|
245 |
|
246 | Type: `string|RegExp`
|
247 | Default: `undefined`
|
248 |
|
249 | Pattern for extracting elements to be used in `to` templates.
|
250 |
|
251 | Defines a `{RegExp}` to match some parts of the file path.
|
252 | These capture groups can be reused in the name property using `[N]` placeholder.
|
253 | Note that `[0]` will be replaced by the entire path of the file,
|
254 | whereas `[1]` will contain the first capturing parenthesis of your `{RegExp}`
|
255 | and so on...
|
256 |
|
257 | **webpack.config.js**
|
258 |
|
259 | ```js
|
260 | module.exports = {
|
261 | plugins: [
|
262 | new CopyPlugin([
|
263 | {
|
264 | from: '*/*',
|
265 | to: '[1]-[2].[hash].[ext]',
|
266 | test: /([^/]+)\/(.+)\.png$/,
|
267 | },
|
268 | ]),
|
269 | ],
|
270 | };
|
271 | ```
|
272 |
|
273 | #### `force`
|
274 |
|
275 | Type: `Boolean`
|
276 | Default: `false`
|
277 |
|
278 | Overwrites files already in `compilation.assets` (usually added by other plugins/loaders).
|
279 |
|
280 | **webpack.config.js**
|
281 |
|
282 | ```js
|
283 | module.exports = {
|
284 | plugins: [
|
285 | new CopyPlugin([
|
286 | {
|
287 | from: 'src/**/*',
|
288 | to: 'dest/',
|
289 | force: true,
|
290 | },
|
291 | ]),
|
292 | ],
|
293 | };
|
294 | ```
|
295 |
|
296 | #### `ignore`
|
297 |
|
298 | Type: `Array`
|
299 | Default: `[]`
|
300 |
|
301 | Globs to ignore files.
|
302 |
|
303 | **webpack.config.js**
|
304 |
|
305 | ```js
|
306 | module.exports = {
|
307 | plugins: [
|
308 | new CopyPlugin([
|
309 | {
|
310 | from: 'src/**/*',
|
311 | to: 'dest/',
|
312 | ignore: ['*.js'],
|
313 | },
|
314 | ]),
|
315 | ],
|
316 | };
|
317 | ```
|
318 |
|
319 | > ⚠️ Note that only relative path should be provided to ignore option, an example to ignore `src/assets/subfolder/ignorfile.js` :
|
320 |
|
321 | **webpack.config.js**
|
322 |
|
323 | ```js
|
324 | module.exports = {
|
325 | plugins: [
|
326 | new CopyPlugin([
|
327 | {
|
328 | from: 'src/assets',
|
329 | to: 'dest/',
|
330 | ignore: ['subfolder/ingorefile.js'],
|
331 | },
|
332 | ]),
|
333 | ],
|
334 | };
|
335 | ```
|
336 |
|
337 | #### `flatten`
|
338 |
|
339 | Type: `Boolean`
|
340 | Default: `false`
|
341 |
|
342 | Removes all directory references and only copies file names.
|
343 |
|
344 | > ⚠️ If files have the same name, the result is non-deterministic.
|
345 |
|
346 | **webpack.config.js**
|
347 |
|
348 | ```js
|
349 | module.exports = {
|
350 | plugins: [
|
351 | new CopyPlugin([
|
352 | {
|
353 | from: 'src/**/*',
|
354 | to: 'dest/',
|
355 | flatten: true,
|
356 | },
|
357 | ]),
|
358 | ],
|
359 | };
|
360 | ```
|
361 |
|
362 | #### `cache`
|
363 |
|
364 | Type: `Boolean|Object`
|
365 | Default: `false`
|
366 |
|
367 | Enable/disable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache.
|
368 | Default path to cache directory: `node_modules/.cache/copy-webpack-plugin`.
|
369 |
|
370 | **webpack.config.js**
|
371 |
|
372 | ```js
|
373 | module.exports = {
|
374 | plugins: [
|
375 | new CopyPlugin([
|
376 | {
|
377 | from: 'src/*.png',
|
378 | to: 'dest/',
|
379 | transform(content, path) {
|
380 | return optimize(content);
|
381 | },
|
382 | cache: true,
|
383 | },
|
384 | ]),
|
385 | ],
|
386 | };
|
387 | ```
|
388 |
|
389 | #### `transform`
|
390 |
|
391 | Type: `Function`
|
392 | Default: `undefined`
|
393 |
|
394 | Allows to modify the file contents.
|
395 |
|
396 | **webpack.config.js**
|
397 |
|
398 | ```js
|
399 | module.exports = {
|
400 | plugins: [
|
401 | new CopyPlugin([
|
402 | {
|
403 | from: 'src/*.png',
|
404 | to: 'dest/',
|
405 | transform(content, path) {
|
406 | return optimize(content);
|
407 | },
|
408 | },
|
409 | ]),
|
410 | ],
|
411 | };
|
412 | ```
|
413 |
|
414 | **webpack.config.js**
|
415 |
|
416 | ```js
|
417 | module.exports = {
|
418 | plugins: [
|
419 | new CopyPlugin([
|
420 | {
|
421 | from: 'src/*.png',
|
422 | to: 'dest/',
|
423 | transform(content, path) {
|
424 | return Promise.resolve(optimize(content));
|
425 | },
|
426 | },
|
427 | ]),
|
428 | ],
|
429 | };
|
430 | ```
|
431 |
|
432 | #### `transformPath`
|
433 |
|
434 | Type: `Function`
|
435 | Default: `undefined`
|
436 |
|
437 | Allows to modify the writing path.
|
438 |
|
439 | > ⚠️ 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.
|
440 | > On Windows, the forward slash and the backward slash are both separators.
|
441 | > Instead please use `/` or `path` methods.
|
442 |
|
443 | **webpack.config.js**
|
444 |
|
445 | ```js
|
446 | module.exports = {
|
447 | plugins: [
|
448 | new CopyPlugin([
|
449 | {
|
450 | from: 'src/*.png',
|
451 | to: 'dest/',
|
452 | transformPath(targetPath, absolutePath) {
|
453 | return 'newPath';
|
454 | },
|
455 | },
|
456 | ]),
|
457 | ],
|
458 | };
|
459 | ```
|
460 |
|
461 | **webpack.config.js**
|
462 |
|
463 | ```js
|
464 | module.exports = {
|
465 | plugins: [
|
466 | new CopyPlugin([
|
467 | {
|
468 | from: 'src/*.png',
|
469 | to: 'dest/',
|
470 | transformPath(targetPath, absolutePath) {
|
471 | return Promise.resolve('newPath');
|
472 | },
|
473 | },
|
474 | ]),
|
475 | ],
|
476 | };
|
477 | ```
|
478 |
|
479 | ### Options
|
480 |
|
481 | | Name | Type | Default | Description |
|
482 | | :---------------------------------: | :---------: | :------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------ |
|
483 | | [`logLevel`](#loglevel) | `{String}` | **`'warn'`** | Level of messages that the module will log |
|
484 | | [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |
|
485 | | [`context`](#context) | `{String}` | `compiler.options.context` | A path that determines how to interpret the `from` path, shared for all patterns |
|
486 | | [`copyUnmodified`](#copyunmodified) | `{Boolean}` | `false` | Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option |
|
487 |
|
488 | #### `logLevel`
|
489 |
|
490 | This property defines the level of messages that the module will log. Valid levels include:
|
491 |
|
492 | - `trace`
|
493 | - `debug`
|
494 | - `info`
|
495 | - `warn` (default)
|
496 | - `error`
|
497 | - `silent`
|
498 |
|
499 | Setting a log level means that all other levels below it will be visible in the
|
500 | console. Setting `logLevel: 'silent'` will hide all console output. The module
|
501 | leverages [`webpack-log`](https://github.com/webpack-contrib/webpack-log#readme)
|
502 | for logging management, and more information can be found on its page.
|
503 |
|
504 | **webpack.config.js**
|
505 |
|
506 | ```js
|
507 | module.exports = {
|
508 | plugins: [new CopyPlugin([...patterns], { logLevel: 'debug' })],
|
509 | };
|
510 | ```
|
511 |
|
512 | #### `ignore`
|
513 |
|
514 | Array of globs to ignore (applied to `from`).
|
515 |
|
516 | **webpack.config.js**
|
517 |
|
518 | ```js
|
519 | module.exports = {
|
520 | plugins: [new CopyPlugin([...patterns], { ignore: ['*.js', '*.css'] })],
|
521 | };
|
522 | ```
|
523 |
|
524 | #### `context`
|
525 |
|
526 | A path that determines how to interpret the `from` path, shared for all patterns.
|
527 |
|
528 | **webpack.config.js**
|
529 |
|
530 | ```js
|
531 | module.exports = {
|
532 | plugins: [new CopyPlugin([...patterns], { context: '/app' })],
|
533 | };
|
534 | ```
|
535 |
|
536 | #### `copyUnmodified`
|
537 |
|
538 | Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option.
|
539 |
|
540 | > ℹ️ 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.
|
541 |
|
542 | **webpack.config.js**
|
543 |
|
544 | ```js
|
545 | module.exports = {
|
546 | plugins: [new CopyPlugin([...patterns], { copyUnmodified: true })],
|
547 | };
|
548 | ```
|
549 |
|
550 | ## Contributing
|
551 |
|
552 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
553 |
|
554 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
555 |
|
556 | ## License
|
557 |
|
558 | [MIT](./LICENSE)
|
559 |
|
560 | [npm]: https://img.shields.io/npm/v/copy-webpack-plugin.svg
|
561 | [npm-url]: https://npmjs.com/package/copy-webpack-plugin
|
562 | [node]: https://img.shields.io/node/v/copy-webpack-plugin.svg
|
563 | [node-url]: https://nodejs.org
|
564 | [deps]: https://david-dm.org/webpack-contrib/copy-webpack-plugin.svg
|
565 | [deps-url]: https://david-dm.org/webpack-contrib/copy-webpack-plugin
|
566 | [tests]: https://dev.azure.com/webpack-contrib/copy-webpack-plugin/_apis/build/status/webpack-contrib.copy-webpack-plugin?branchName=master
|
567 | [tests-url]: https://dev.azure.com/webpack-contrib/copy-webpack-plugin/_build/latest?definitionId=5&branchName=master
|
568 | [cover]: https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/branch/master/graph/badge.svg
|
569 | [cover-url]: https://codecov.io/gh/webpack-contrib/copy-webpack-plugin
|
570 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
571 | [chat-url]: https://gitter.im/webpack/webpack
|
572 | [size]: https://packagephobia.now.sh/badge?p=copy-webpack-plugin
|
573 | [size-url]: https://packagephobia.now.sh/result?p=copy-webpack-plugin
|