Sponsor webpack and get apparel from the official shop or get stickers here! All proceeds go to our open collective!

ag-grid is proud to partner with webpack

CopyWebpackPlugin

Copies individual files or entire directories to the build directory

Install

npm i -D copy-webpack-plugin

Usage

webpack.config.js

const CopyWebpackPlugin = require('copy-webpack-plugin')

const config = {
  plugins: [
    new CopyWebpackPlugin([ ...patterns ], options)
  ]
}

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

##

A simple pattern looks like this

{ from: 'source', to: 'dest' }

Or, in case of just a from with the default destination, you can also use a {String} as shorthand instead of an {Object}

'source'
Name
Type
Default
Description
Name
Type
{String|Object}
Default
undefined
Description
Globs accept minimatch options
Name
Type
{Object}
Default
{ cwd: context }
Description
See the node-glob options in addition to the ones below
Name
to
Type
{String|Object}
Default
undefined
Description
Output root if from is file or dir, resolved glob path if from is glob
Name
Type
{String}
Default
Description
Name
Type
{RegExp}
Default
Description
Pattern for extracting elements to be used in to templates
Name
Type
{Boolean}
Default
false
Description
Overwrites files already in compilation.assets (usually added by other plugins/loaders)
Name
Type
{Array}
Default
[]
Description
Globs to ignore for this pattern
Name
flatten
Type
{Boolean}
Default
false
Description
Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic
Name
Type
{Function|Promise}
Default
(content, path) => content
Description
Function or Promise that modifies file contents before copying
Name
Type
{Boolean|Object}
Default
false
Description
Enable transform caching. You can use { cache: { key: 'my-cache-key' } } to invalidate the cache
Name
Type
{String}
Default
options.context ||compiler.options.context
Description
A path that determines how to interpret the from path

from

webpack.config.js

[
  new CopyWebpackPlugin([
    'relative/path/to/file.ext',
    '/absolute/path/to/file.ext',
    'relative/path/to/dir',
    '/absolute/path/to/dir',
    '**/*',
    { glob: '\*\*/\*', dot: true }
  ], options)
]

to

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: '**/*', to: 'relative/path/to/dest/' },
    { from: '**/*', to: '/absolute/path/to/dest/' }
  ], options)
]

toType

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

[
  new CopyWebpackPlugin([
    {
      from: 'path/to/file.txt',
      to: 'directory/with/extension.ext',
      toType: 'dir'
    }
  ], options)
]

'file'

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'path/to/file.txt',
      to: 'file/without/extension',
      toType: 'file'
    },
  ], options)
]

'template'

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/',
      to: 'dest/[name].[hash].[ext]',
      toType: 'template'
    }
  ], options)
]

test

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

[
  new CopyWebpackPlugin([
    {
      from: '*/*',
      to: '[1]-[2].[hash].[ext]',
      test: /([^/]+)\/(.+)\.png$/
    }
  ], options)
]

force

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*', to: 'dest/', force: true }
  ], options)
]

ignore

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*', to: 'dest/', ignore: [ '*.js' ] }
  ], options)
]

flatten

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*', to: 'dest/', flatten: true }
  ], options)
]

transform

{Function}

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return optimize(content)
      }
    }
  ], options)
]

{Promise}

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return Promise.resolve(optimize(content))
      }
  }
  ], options)
]

cache

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return optimize(content)
      },
      cache: true
    }
  ], options)
]

context

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/*.txt', to: 'dest/', context: 'app/' }
  ], options)
]

Options

Name
Type
Default
Description
Name
Type
{String}
Default
'warning'
Description
Name
Type
{Array}
Default
[]
Description
Array of globs to ignore (applied to from)
Name
Type
{String}
Default
compiler.options.context
Description
A path that determines how to interpret the from path, shared for all patterns
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

debug

Name
Type
Default
Description
Name
'info'
Type
{String|Boolean}
Default
false
Description
File location and read info
Name
'debug'
Type
{String}
Default
false
Description
Very detailed debugging info
Name
'warning'
Type
{String}
Default
true
Description
Only warnings

'info'

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: 'info' }
  )
]

'debug'

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: 'debug' }
  )
]

'warning' (default)

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: true }
  )
]

ignore

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { ignore: [ '*.js', '*.css' ] }
  )
]

context

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { context: [ '/app' ] }
  )
]

copyUnmodified

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

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { copyUnmodified: true }
  )
]

Maintainers


      Juho Vepsäläinen


      Joshua Wiens


      Michael Ciniawsky


      Alexander Krasnoyarov