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 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 loader 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 | > ℹ️ 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).
|
46 |
|
47 | ## Options
|
48 |
|
49 | The plugin's signature:
|
50 |
|
51 | **webpack.config.js**
|
52 |
|
53 | ```js
|
54 | module.exports = {
|
55 | plugins: [new CopyPlugin(patterns, options)],
|
56 | };
|
57 | ```
|
58 |
|
59 | ### Patterns
|
60 |
|
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. |
|
74 |
|
75 | #### `from`
|
76 |
|
77 | **webpack.config.js**
|
78 |
|
79 | ```js
|
80 | module.exports = {
|
81 | plugins: [
|
82 | new CopyPlugin([
|
83 | 'relative/path/to/file.ext',
|
84 | '/absolute/path/to/file.ext',
|
85 | 'relative/path/to/dir',
|
86 | '/absolute/path/to/dir',
|
87 | '**/*',
|
88 | { glob: '**/*', dot: false },
|
89 | ]),
|
90 | ],
|
91 | };
|
92 | ```
|
93 |
|
94 | #### `to`
|
95 |
|
96 | **webpack.config.js**
|
97 |
|
98 | ```js
|
99 | module.exports = {
|
100 | plugins: [
|
101 | new CopyPlugin([
|
102 | { from: '**/*', to: 'relative/path/to/dest/' },
|
103 | { from: '**/*', to: '/absolute/path/to/dest/' },
|
104 | ]),
|
105 | ],
|
106 | };
|
107 | ```
|
108 |
|
109 | #### `toType`
|
110 |
|
111 | | Name | Type | Default | Description |
|
112 | | :--------------: | :--------: | :---------: | :------------------------------------------------------------------------------------------------- |
|
113 | | **`'dir'`** | `{String}` | `undefined` | If `from` is directory, `to` has no extension or ends in `'/'` |
|
114 | | **`'file'`** | `{String}` | `undefined` | If `to` has extension or `from` is file |
|
115 | | **`'template'`** | `{String}` | `undefined` | If `to` contains [a template pattern](https://github.com/webpack-contrib/file-loader#placeholders) |
|
116 |
|
117 | ##### `'dir'`
|
118 |
|
119 | **webpack.config.js**
|
120 |
|
121 | ```js
|
122 | module.exports = {
|
123 | plugins: [
|
124 | new CopyPlugin([
|
125 | {
|
126 | from: 'path/to/file.txt',
|
127 | to: 'directory/with/extension.ext',
|
128 | toType: 'dir',
|
129 | },
|
130 | ]),
|
131 | ],
|
132 | };
|
133 | ```
|
134 |
|
135 | ##### `'file'`
|
136 |
|
137 | **webpack.config.js**
|
138 |
|
139 | ```js
|
140 | module.exports = {
|
141 | plugins: [
|
142 | new CopyPlugin([
|
143 | {
|
144 | from: 'path/to/file.txt',
|
145 | to: 'file/without/extension',
|
146 | toType: 'file',
|
147 | },
|
148 | ]),
|
149 | ],
|
150 | };
|
151 | ```
|
152 |
|
153 | ##### `'template'`
|
154 |
|
155 | **webpack.config.js**
|
156 |
|
157 | ```js
|
158 | module.exports = {
|
159 | plugins: [
|
160 | new CopyPlugin([
|
161 | {
|
162 | from: 'src/',
|
163 | to: 'dest/[name].[hash].[ext]',
|
164 | toType: 'template',
|
165 | },
|
166 | ]),
|
167 | ],
|
168 | };
|
169 | ```
|
170 |
|
171 | #### `test`
|
172 |
|
173 | Defines a `{RegExp}` to match some parts of the file path.
|
174 | These capture groups can be reused in the name property using `[N]` placeholder.
|
175 | Note that `[0]` will be replaced by the entire path of the file,
|
176 | whereas `[1]` will contain the first capturing parenthesis of your `{RegExp}`
|
177 | and so on...
|
178 |
|
179 | **webpack.config.js**
|
180 |
|
181 | ```js
|
182 | module.exports = {
|
183 | plugins: [
|
184 | new CopyPlugin([
|
185 | {
|
186 | from: '*/*',
|
187 | to: '[1]-[2].[hash].[ext]',
|
188 | test: /([^/]+)\/(.+)\.png$/,
|
189 | },
|
190 | ]),
|
191 | ],
|
192 | };
|
193 | ```
|
194 |
|
195 | #### `force`
|
196 |
|
197 | **webpack.config.js**
|
198 |
|
199 | ```js
|
200 | module.exports = {
|
201 | plugins: [
|
202 | new CopyPlugin([
|
203 | {
|
204 | from: 'src/**/*',
|
205 | to: 'dest/',
|
206 | force: true,
|
207 | },
|
208 | ]),
|
209 | ],
|
210 | };
|
211 | ```
|
212 |
|
213 | #### `ignore`
|
214 |
|
215 | **webpack.config.js**
|
216 |
|
217 | ```js
|
218 | module.exports = {
|
219 | plugins: [
|
220 | new CopyPlugin([
|
221 | {
|
222 | from: 'src/**/*',
|
223 | to: 'dest/',
|
224 | ignore: ['*.js'],
|
225 | },
|
226 | ]),
|
227 | ],
|
228 | };
|
229 | ```
|
230 |
|
231 | #### `flatten`
|
232 |
|
233 | **webpack.config.js**
|
234 |
|
235 | ```js
|
236 | module.exports = {
|
237 | plugins: [
|
238 | new CopyPlugin([
|
239 | {
|
240 | from: 'src/**/*',
|
241 | to: 'dest/',
|
242 | flatten: true,
|
243 | },
|
244 | ]),
|
245 | ],
|
246 | };
|
247 | ```
|
248 |
|
249 | #### `transform`
|
250 |
|
251 | ##### `{Function}`
|
252 |
|
253 | **webpack.config.js**
|
254 |
|
255 | ```js
|
256 | module.exports = {
|
257 | plugins: [
|
258 | new CopyPlugin([
|
259 | {
|
260 | from: 'src/*.png',
|
261 | to: 'dest/',
|
262 | transform(content, path) {
|
263 | return optimize(content);
|
264 | },
|
265 | },
|
266 | ]),
|
267 | ],
|
268 | };
|
269 | ```
|
270 |
|
271 | ##### `{Promise}`
|
272 |
|
273 | **webpack.config.js**
|
274 |
|
275 | ```js
|
276 | module.exports = {
|
277 | plugins: [
|
278 | new CopyPlugin([
|
279 | {
|
280 | from: 'src/*.png',
|
281 | to: 'dest/',
|
282 | transform(content, path) {
|
283 | return Promise.resolve(optimize(content));
|
284 | },
|
285 | },
|
286 | ]),
|
287 | ],
|
288 | };
|
289 | ```
|
290 |
|
291 | #### `transformPath`
|
292 |
|
293 | ##### `{Function}`
|
294 |
|
295 | **webpack.config.js**
|
296 |
|
297 | ```js
|
298 | module.exports = {
|
299 | plugins: [
|
300 | new CopyPlugin([
|
301 | {
|
302 | from: 'src/*.png',
|
303 | to: 'dest/',
|
304 | transformPath(targetPath, absolutePath) {
|
305 | return 'newPath';
|
306 | },
|
307 | },
|
308 | ]),
|
309 | ],
|
310 | };
|
311 | ```
|
312 |
|
313 | ##### `{Promise}`
|
314 |
|
315 | **webpack.config.js**
|
316 |
|
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 | ```
|
332 |
|
333 | #### `cache`
|
334 |
|
335 | **webpack.config.js**
|
336 |
|
337 | ```js
|
338 | module.exports = {
|
339 | plugins: [
|
340 | new CopyPlugin([
|
341 | {
|
342 | from: 'src/*.png',
|
343 | to: 'dest/',
|
344 | transform(content, path) {
|
345 | return optimize(content);
|
346 | },
|
347 | cache: true,
|
348 | },
|
349 | ]),
|
350 | ],
|
351 | };
|
352 | ```
|
353 |
|
354 | #### `context`
|
355 |
|
356 | **webpack.config.js**
|
357 |
|
358 | ```js
|
359 | module.exports = {
|
360 | plugins: [
|
361 | new CopyPlugin([
|
362 | {
|
363 | from: 'src/*.txt',
|
364 | to: 'dest/',
|
365 | context: 'app/',
|
366 | },
|
367 | ]),
|
368 | ],
|
369 | };
|
370 | ```
|
371 |
|
372 | ### Options
|
373 |
|
374 | | Name | Type | Default | Description |
|
375 | | :---------------------------------: | :---------: | :------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------ |
|
376 | | [`logLevel`](#logLevel) | `{String}` | **`'warning'`** | Level of messages that the module will log |
|
377 | | [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |
|
378 | | [`context`](#context) | `{String}` | `compiler.options.context` | A path that determines how to interpret the `from` path, shared for all patterns |
|
379 | | [`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 |
|
380 |
|
381 | #### `logLevel`
|
382 |
|
383 | This property defines the level of messages that the module will log. Valid levels include:
|
384 |
|
385 | - `trace`
|
386 | - `debug`
|
387 | - `info`
|
388 | - `warn`
|
389 | - `error`
|
390 | - `silent`
|
391 |
|
392 | Setting a log level means that all other levels below it will be visible in the
|
393 | console. Setting `logLevel: 'silent'` will hide all console output. The module
|
394 | leverages [`webpack-log`](https://github.com/webpack-contrib/webpack-log#readme)
|
395 | for logging management, and more information can be found on its page.
|
396 |
|
397 | ##### `'info'`
|
398 |
|
399 | **webpack.config.js**
|
400 |
|
401 | ```js
|
402 | module.exports = {
|
403 | plugins: [new CopyPlugin([...patterns], { debug: 'info' })],
|
404 | };
|
405 | ```
|
406 |
|
407 | ##### `'debug'`
|
408 |
|
409 | **webpack.config.js**
|
410 |
|
411 | ```js
|
412 | module.exports = {
|
413 | plugins: [new CopyPlugin([...patterns], { debug: 'debug' })],
|
414 | };
|
415 | ```
|
416 |
|
417 | ##### `'warning' (default)`
|
418 |
|
419 | **webpack.config.js**
|
420 |
|
421 | ```js
|
422 | module.exports = {
|
423 | plugins: [new CopyPlugin([...patterns], { debug: true })],
|
424 | };
|
425 | ```
|
426 |
|
427 | #### `ignore`
|
428 |
|
429 | **webpack.config.js**
|
430 |
|
431 | ```js
|
432 | module.exports = {
|
433 | plugins: [new CopyPlugin([...patterns], { ignore: ['*.js', '*.css'] })],
|
434 | };
|
435 | ```
|
436 |
|
437 | #### `context`
|
438 |
|
439 | **webpack.config.js**
|
440 |
|
441 | ```js
|
442 | module.exports = {
|
443 | plugins: [new CopyPlugin([...patterns], { context: '/app' })],
|
444 | };
|
445 | ```
|
446 |
|
447 | #### `copyUnmodified`
|
448 |
|
449 | > ℹ️ 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.
|
450 |
|
451 | **webpack.config.js**
|
452 |
|
453 | ```js
|
454 | module.exports = {
|
455 | plugins: [new CopyPlugin([...patterns], { copyUnmodified: true })],
|
456 | };
|
457 | ```
|
458 |
|
459 | ## Contributing
|
460 |
|
461 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
462 |
|
463 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
464 |
|
465 | ## License
|
466 |
|
467 | [MIT](./LICENSE)
|
468 |
|
469 | [npm]: https://img.shields.io/npm/v/copy-webpack-plugin.svg
|
470 | [npm-url]: https://npmjs.com/package/copy-webpack-plugin
|
471 | [node]: https://img.shields.io/node/v/copy-webpack-plugin.svg
|
472 | [node-url]: https://nodejs.org
|
473 | [deps]: https://david-dm.org/webpack-contrib/copy-webpack-plugin.svg
|
474 | [deps-url]: https://david-dm.org/webpack-contrib/copy-webpack-plugin
|
475 | [tests]: https://secure.travis-ci.org/webpack-contrib/copy-webpack-plugin.svg
|
476 | [tests-url]: http://travis-ci.org/webpack-contrib/copy-webpack-plugin
|
477 | [cover]: https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/branch/master/graph/badge.svg
|
478 | [cover-url]: https://codecov.io/gh/webpack-contrib/copy-webpack-plugin
|
479 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
480 | [chat-url]: https://gitter.im/webpack/webpack
|
481 | [size]: https://packagephobia.now.sh/badge?p=copy-webpack-plugin
|
482 | [size-url]: https://packagephobia.now.sh/result?p=copy-webpack-plugin
|