CopyWebpackPlugin

[![npm][npm]][npm-url] [![node][node]][node-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![cover][cover]][cover-url] [![chat][chat]][chat-url] [![size][size]][size-url]

Copies individual files or entire directories to the build directory.

Getting Started

To begin, you’ll need to install copy-webpack-plugin:

  1. $ npm install copy-webpack-plugin --save-dev

Then add the loader to your webpack config. For example:

webpack.config.js

  1. const CopyPlugin = require('copy-webpack-plugin');
  2. module.exports = {
  3. plugins: [
  4. new CopyPlugin([
  5. { from: 'source', to: 'dest' },
  6. { from: 'other', to: 'public' },
  7. ]),
  8. ],
  9. };

ℹ️ If you want webpack-dev-server to write files to the output directory during development, you can force it with the writeToDisk option or the write-file-webpack-plugin.

Options

The plugin’s signature:

webpack.config.js

  1. module.exports = {
  2. plugins: [new CopyPlugin(patterns, options)],
  3. };

Patterns

Name

Type

Default

Description

Name

from

Type

{String|Object}

Default

undefined

Description

Glob or path from where we сopy files.

Name

to

Type

{String}

Default

undefined

Description

Output path.

Name

context

Type

{String}

Default

options.context ||compiler.options.context

Description

A path that determines how to interpret the from path.

Name

toType

Type

{String}

Default

undefined

Description

Determinate what is to option - directory, file or template.

Name

test

Type

{RegExp}

Default

undefined

Description

Pattern for extracting elements to be used in to templates.

Name

force

Type

{Boolean}

Default

false

Description

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

Name

ignore

Type

{Array}

Default

[]

Description

Globs to ignore files.

Name

flatten

Type

{Boolean}

Default

false

Description

Removes all directory references and only copies file names.

Name

transform

Type

{Function|Promise}

Default

undefined

Description

Allows to modify the file contents.

Name

cache

Type

{Boolean|Object}

Default

false

Description

Enable transform caching. You can use { cache: { key: 'my-cache-key' } } to invalidate the cache.

Name

transformPath

Type

{Function|Promise}

Default

undefined

Description

Allows to modify the writing path.

from

Type: String\|Object Default: undefined

Glob or path from where we сopy files. Globs accept minimatch options.

You can defined from as Object and use the 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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. 'relative/path/to/file.ext',
  5. '/absolute/path/to/file.ext',
  6. 'relative/path/to/dir',
  7. '/absolute/path/to/dir',
  8. '**/*',
  9. { glob: '**/*', dot: false },
  10. ]),
  11. ],
  12. };

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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. { from: '**/*', to: 'relative/path/to/dest/' },
  5. { from: '**/*', to: '/absolute/path/to/dest/' },
  6. ]),
  7. ],
  8. };

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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.txt',
  6. to: 'dest/',
  7. context: 'app/',
  8. },
  9. ]),
  10. ],
  11. };

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

Name

'dir'

Type

{String}

Default

undefined

Description

If from is directory, to has no extension or ends in '/'

Name

'file'

Type

{String}

Default

undefined

Description

If to has extension or from is file

Name

'template'

Type

{String}

Default

undefined

Description

If to contains a template pattern

'dir'

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'path/to/file.txt',
  6. to: 'directory/with/extension.ext',
  7. toType: 'dir',
  8. },
  9. ]),
  10. ],
  11. };
'file'

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'path/to/file.txt',
  6. to: 'file/without/extension',
  7. toType: 'file',
  8. },
  9. ]),
  10. ],
  11. };
'template'

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/',
  6. to: 'dest/[name].[hash].[ext]',
  7. toType: 'template',
  8. },
  9. ]),
  10. ],
  11. };

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, whereas [1] will contain the first capturing parenthesis of your {RegExp} and so on…

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: '*/*',
  6. to: '[1]-[2].[hash].[ext]',
  7. test: /([^/]+)\/(.+)\.png$/,
  8. },
  9. ]),
  10. ],
  11. };

force

Type: Boolean Default: false

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

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/**/*',
  6. to: 'dest/',
  7. force: true,
  8. },
  9. ]),
  10. ],
  11. };

ignore

Type: Array Default: []

Globs to ignore files.

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/**/*',
  6. to: 'dest/',
  7. ignore: ['*.js'],
  8. },
  9. ]),
  10. ],
  11. };

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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/**/*',
  6. to: 'dest/',
  7. flatten: true,
  8. },
  9. ]),
  10. ],
  11. };

transform

Type: Function|Promise Default: undefined

Allows to modify the file contents.

{Function}

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.png',
  6. to: 'dest/',
  7. transform(content, path) {
  8. return optimize(content);
  9. },
  10. },
  11. ]),
  12. ],
  13. };
{Promise}

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.png',
  6. to: 'dest/',
  7. transform(content, path) {
  8. return Promise.resolve(optimize(content));
  9. },
  10. },
  11. ]),
  12. ],
  13. };

cache

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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.png',
  6. to: 'dest/',
  7. transform(content, path) {
  8. return optimize(content);
  9. },
  10. cache: true,
  11. },
  12. ]),
  13. ],
  14. };

transformPath

Type: Function|Promise Default: undefined

Allows to modify the writing path.

⚠️ 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

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.png',
  6. to: 'dest/',
  7. transformPath(targetPath, absolutePath) {
  8. return 'newPath';
  9. },
  10. },
  11. ]),
  12. ],
  13. };
{Promise}

webpack.config.js

  1. module.exports = {
  2. plugins: [
  3. new CopyPlugin([
  4. {
  5. from: 'src/*.png',
  6. to: 'dest/',
  7. transformPath(targePath, absolutePath) {
  8. return Promise.resolve('newPath');
  9. },
  10. },
  11. ]),
  12. ],
  13. };

Options

Name

Type

Default

Description

Name

logLevel

Type

{String}

Default

'warn'

Description

Level of messages that the module will log

Name

ignore

Type

{Array}

Default

[]

Description

Array of globs to ignore (applied to from)

Name

context

Type

{String}

Default

compiler.options.context

Description

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

Name

copyUnmodified

Type

{Boolean}

Default

false

Description

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

logLevel

This property defines the level of messages that the module will log. Valid levels include:

  • trace
  • debug
  • info
  • warn (default)
  • error
  • silent

Setting a log level means that all other levels below it will be visible in the console. Setting logLevel: 'silent' will hide all console output. The module leverages webpack-log for logging management, and more information can be found on its page.

webpack.config.js

  1. module.exports = {
  2. plugins: [new CopyPlugin([...patterns], { logLevel: 'debug' })],
  3. };

ignore

Array of globs to ignore (applied to from).

webpack.config.js

  1. module.exports = {
  2. plugins: [new CopyPlugin([...patterns], { ignore: ['*.js', '*.css'] })],
  3. };

context

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

webpack.config.js

  1. module.exports = {
  2. plugins: [new CopyPlugin([...patterns], { context: '/app' })],
  3. };

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

  1. module.exports = {
  2. plugins: [new CopyPlugin([...patterns], { copyUnmodified: true })],
  3. };

Contributing

Please take a moment to read our contributing guidelines if you haven’t yet done so.

CONTRIBUTING

License

MIT