Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

copy-webpack-plugin-t2

kevlened45MIT3.0.1

Copy files and directories in webpack

webpack, plugin, transfer, move, copy

readme

Copy Webpack Plugin

This is a webpack plugin that copies individual files or entire directories to the build directory.

Getting started

Install the plugin:

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

Usage

new CopyWebpackPlugin([patterns], options)

A pattern looks like: { from: 'source', to: 'dest' }

Pattern properties:

  • from
    • is required
    • can be an absolute or path relative to the context
    • can be a file or directory
    • can be a glob
  • to
    • is optional
    • if not absolute, it's relative to the build root
    • must be a directory if from is a directory
  • toType
    • is optional
    • is ignored if from is a directory
    • defaults to 'file' if to has an extension
    • defaults to 'dir' if to doesn't have an extension
  • force
    • is optional
    • defaults to false
    • forces the plugin to overwrite files staged by previous plugins
  • context
    • is optional
    • defaults to the base context
    • is a pattern specific context
  • flatten
    • is optional
    • defaults to false
    • removes all directory references and only copies file names
    • if files have the same name, the result is non-deterministic
  • ignore
    • additional globs to ignore for this pattern

Available options:

  • ignore
    • an array of files and directories to ignore
    • accepts globs
    • globs are evaluated on the from path, relative to the context
  • copyUnmodified
    • is optional
    • defaults to false (only copies modified files)
    • true copies all files while using watch or webpack-dev-server

Examples

var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');

module.exports = {
    context: path.join(__dirname, 'app'),
    devServer: {
        // This is required for webpack-dev-server if using a version <3.0.0.
        // The path should be an absolute path to your build destination.
        outputPath: path.join(__dirname, 'build')
    },
    plugins: [
        new CopyWebpackPlugin([
            // {output}/file.txt
            { from: 'from/file.txt' },

            // {output}/to/file.txt
            { from: 'from/file.txt', to: 'to/file.txt' },

            // {output}/to/directory/file.txt
            { from: 'from/file.txt', to: 'to/directory' },

            // Copy directory contents to {output}/
            { from: 'from/directory' },

            // Copy directory contents to {output}/to/directory/
            { from: 'from/directory', to: 'to/directory' },

            // Copy glob results to /absolute/path/
            { from: 'from/directory/**/*', to: '/absolute/path' },

            // Copy glob results (with dot files) to /absolute/path/
            {
                from: {
                    glob:'from/directory/**/*',
                    dot: true
                },
                to: '/absolute/path'
            },

            // Copy glob results, relative to context
            {
                context: 'from/directory',
                from: '**/*',
                to: '/absolute/path'
            },

            // {output}/file/without/extension
            {
                from: 'path/to/file.txt',
                to: 'file/without/extension',
                toType: 'file'
            },

            // {output}/directory/with/extension.ext/file.txt
            {
                from: 'path/to/file.txt',
                to: 'directory/with/extension.ext',
                toType: 'dir'
            }
        ], {
            ignore: [
                // Doesn't copy any files with a txt extension    
                '*.txt',

                // Doesn't copy any file, even if they start with a dot
                { glob: '**/*', dot: true }
            ],

            // By default, we only copy modified files during
            // a watch or webpack-dev-server build. Setting this
            // to `true` copies all files.
            copyUnmodified: true
        })
    ]
};

Testing

Build Status

Run npm test

License

MIT

changelog

3.0.1 (May 29, 2016)

  • Fix error thrown when subdirectories are in glob results

3.0.0 (May 14, 2016)

BREAKING CHANGE

  • No longer writing to filesystem when webpack-dev-server is running. Use the write-file-webpack-plugin to force writing files to the filesystem

2.1.6 (May 14, 2016)

  • Readded Node v6.0.0 compatibility after finding root cause

2.1.5 (May 13, 2016)

  • Reverted Node v6.0.0 compatibility due to import errors

2.1.4 (May 12, 2016)

  • Fix Node v6.0.0 compatibility
  • Fix tests running in Node v6.0.0
  • Fix ERROR in Path must be a string. Received undefined. (undefined to when writing directory)

2.1.3 (April 23, 2016)

  • Fix TypeError when working with webpack-dev-server

2.1.1 (April 16, 2016)

  • Fixed nested directories in blobs

2.1.0 (April 16, 2016)

  • Added pattern-level context
  • Added pattern-level ignore
  • Added flattening

2.0.0 (Apr 14, 2016)

  • Several bug fixes
  • Added support for webpack-dev-server
  • from now accepts glob options
  • Added copyUnmodified option

1.1.1 (Jan 25, 2016)

  • to absolute paths are now tracked by webpack
  • Reverted dot matching default for minimatch
  • Params can now be passed to the ignore option

1.0.0 (Jan 24, 2016)

  • Added globbing support for from
  • Added absolute path support for to
  • Changed default for minimatch to match dots for globs

0.3.0 (Nov 27, 2015)

  • Added ignore option that functions like .gitignore
  • Improved Windows support

0.2.0 (Oct 28, 2015)

  • Added force option in patterns to overwrite prestaged assets
  • Files and directories are now added to webpack's rebuild watchlist
  • Only includes changed files while using webpack --watch

0.1.0 (Oct 26, 2015)

  • Basic functionality