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

CopyWebpackPlugin

npm node deps tests cover chat size

Copies individual files or entire directories to the build directory.

Getting Started

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

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

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

webpack.config.js

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  plugins: [
    new CopyPlugin([
      { from: 'source', to: 'dest' },
      { from: 'other', to: 'public' },
    ]),
  ],
};

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

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

Patterns

Name Type Default Description

Name

Type

Default

Description

from

{String\|Object}

{String\|Object} undefined Globs accept minimatch options . See the node-glob options in addition to the ones below.

Name

Type

Default

Description

to

{String\|Object}

{String\|Object} undefined Output root if from is file or dir, resolved glob path if from is glob.

Name

Type

Default

Description

toType

{String}

{String} undefined [toType Options](#totype) .

Name

Type

Default

Description

test

{RegExp}

{RegExp} undefined Pattern for extracting elements to be used in to templates.

Name

Type

Default

Description

force

{Boolean}

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

Name

Type

Default

Description

ignore

{Array}

{Array} [] Globs to ignore for this pattern.

Name

Type

Default

Description

flatten

{Boolean}

{Boolean} false Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic.

Name

Type

Default

Description

transform

{Function\|Promise}

{Function\|Promise} (content, path) => content Function or Promise that modifies file contents before copying.

Name

Type

Default

Description

transformPath

{Function\|Promise}

{Function\|Promise} (targetPath, sourcePath) => path Function or Promise that modifies file writing path.

Name

Type

Default

Description

cache

{Boolean\|Object}

{Boolean\|Object} false Enable transform caching. You can use { cache: { key: 'my-cache-key' } } to invalidate the cache.

Name

Type

Default

Description

context

{String}

{String} options.context \|\| compiler.options.context A path that determines how to interpret the from path.

from

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      'relative/path/to/file.ext',
      '/absolute/path/to/file.ext',
      'relative/path/to/dir',
      '/absolute/path/to/dir',
      '**/*',
      { glob: '**/*', dot: false },
    ]),
  ],
};

to

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      { from: '**/*', to: 'relative/path/to/dest/' },
      { from: '**/*', to: '/absolute/path/to/dest/' },
    ]),
  ],
};

toType

Name Type Default Description

Name

Type

Default

Description

'dir'

{String}

{String} undefined If from is directory, to has no extension or ends in '/'

Name

Type

Default

Description

'file'

{String}

{String} undefined If to has extension or from is file

Name

Type

Default

Description

'template'

{String}

{String} undefined If to contains a template pattern
'dir'

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'path/to/file.txt',
        to: 'directory/with/extension.ext',
        toType: 'dir',
      },
    ]),
  ],
};
'file'

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'path/to/file.txt',
        to: 'file/without/extension',
        toType: 'file',
      },
    ]),
  ],
};
'template'

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/',
        to: 'dest/[name].[hash].[ext]',
        toType: 'template',
      },
    ]),
  ],
};

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

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: '*/*',
        to: '[1]-[2].[hash].[ext]',
        test: /([^/]+)\/(.+)\.png$/,
      },
    ]),
  ],
};

force

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/**/*',
        to: 'dest/',
        force: true,
      },
    ]),
  ],
};

ignore

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/**/*',
        to: 'dest/',
        ignore: ['*.js'],
      },
    ]),
  ],
};

flatten

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/**/*',
        to: 'dest/',
        flatten: true,
      },
    ]),
  ],
};

transform

{Function}

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.png',
        to: 'dest/',
        transform(content, path) {
          return optimize(content);
        },
      },
    ]),
  ],
};
{Promise}

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.png',
        to: 'dest/',
        transform(content, path) {
          return Promise.resolve(optimize(content));
        },
      },
    ]),
  ],
};

transformPath

{Function}

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.png',
        to: 'dest/',
        transformPath(targetPath, absolutePath) {
          return 'newPath';
        },
      },
    ]),
  ],
};
{Promise}

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.png',
        to: 'dest/',
        transformPath(targePath, absolutePath) {
          return Promise.resolve('newPath');
        },
      },
    ]),
  ],
};

cache

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.png',
        to: 'dest/',
        transform(content, path) {
          return optimize(content);
        },
        cache: true,
      },
    ]),
  ],
};

context

webpack.config.js

module.exports = {
  plugins: [
    new CopyPlugin([
      {
        from: 'src/*.txt',
        to: 'dest/',
        context: 'app/',
      },
    ]),
  ],
};

Options

Name Type Default Description

Name

Type

Default

Description

debug

{String}

{String} 'warning' Debug Options

Name

Type

Default

Description

ignore

{Array}

{Array} [] Array of globs to ignore (applied to from )

Name

Type

Default

Description

context

{String}

{String} compiler.options.context A path that determines how to interpret the from path, shared for all patterns

Name

Type

Default

Description

{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

debug

Name Type Default Description

Name

Type

Default

Description

'info'

{String\|Boolean}

{String\|Boolean} false File location and read info

Name

Type

Default

Description

'debug'

{String}

{String} false Very detailed debugging info

Name

Type

Default

Description

'warning'

{String}

{String} true Only warnings
'info'

webpack.config.js

module.exports = {
  plugins: [new CopyPlugin([...patterns], { debug: 'info' })],
};
'debug'

webpack.config.js

module.exports = {
  plugins: [new CopyPlugin([...patterns], { debug: 'debug' })],
};
'warning' (default)

webpack.config.js

module.exports = {
  plugins: [new CopyPlugin([...patterns], { debug: true })],
};

ignore

webpack.config.js

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

context

webpack.config.js

module.exports = {
  plugins: [new CopyPlugin([...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

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

Contributing

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

CONTRIBUTING

License

MIT