Socket
Socket
Sign inDemoInstall

copy-webpack-plugin

Package Overview
Dependencies
301
Maintainers
4
Versions
80
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    copy-webpack-plugin

Copy files && directories with webpack


Version published
Weekly downloads
7.6M
decreased by-0.22%
Maintainers
4
Created
Weekly downloads
 

Package description

What is copy-webpack-plugin?

The copy-webpack-plugin is a webpack plugin that allows you to copy files and directories from one location to another within your build process. It is commonly used to copy static assets such as images, fonts, and HTML files to the output directory defined in your webpack configuration.

What are copy-webpack-plugin's main functionalities?

Copying individual files or entire directories

This feature allows you to copy individual files or entire directories from a specified source to a destination within your output directory. The 'from' field specifies the relative or absolute path to the source file or directory, and the 'to' field specifies the relative path to the destination within the output directory.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest' }] })

Ignoring files

This feature allows you to exclude specific files or patterns from being copied. In the provided code sample, all JavaScript files are ignored and will not be copied to the destination.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', globOptions: { ignore: ['**/*.js'] } }] })

Adding context to file paths

This feature allows you to specify a context for the file paths. The 'context' option sets a base path for the 'from' property. In the code sample, the actual path that will be copied is 'app/source'.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', context: 'app' }] })

Transforming file content

This feature allows you to modify the content of files before they are copied. The 'transform' function receives the content of the file and its path, and it should return the new content. This can be used to minify files, add banners, or perform other transformations.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', transform: (content, path) => { return modifyContent(content); } }] })

Other packages similar to copy-webpack-plugin

Changelog

Source

5.0.0 (2019-02-20)

Bug Fixes

  • copy only modified files when you use patterns with difference to and same context (#341) (e808aa2)
  • handle [contenthash] as template (#328) (61dfe52)
  • handles when you add new files in watch mode and use glob (#333) (49a28f0)
  • normalize path segment separation, no problems when you mixed / and \\ (#339) (8f5e638)
  • throw error if from is an empty string #278 (#285) (adf1046)

Features

  • emit warning instead error if file doesn't exist (#338) (a1c5372)
  • supports copy nested directories/files in symlink (#335) (f551c0d)

BREAKING CHANGES

  • drop support for webpack < 4
  • drop support for node < 6.9
  • debug option was renamed to logLevel, it only accepts string values: trace, debug, info, warn, error and silent
  • plugin emit warning instead error if file doesn't exist
  • change prototype of plugin, now you can to get correct plugin name

<a name="4.6.0"></a>

Readme

Source

npm node deps tests cover chat size

copy-webpack-plugin

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

NameTypeDefaultDescription
from{String|Object}undefinedGlobs accept minimatch options. See the node-glob options in addition to the ones below.
to{String|Object}undefinedOutput root if from is file or dir, resolved glob path if from is glob.
toType{String}undefined[toType Options](#totype).
test{RegExp}undefinedPattern for extracting elements to be used in to templates.
force{Boolean}falseOverwrites files already in compilation.assets (usually added by other plugins/loaders).
ignore{Array}[]Globs to ignore for this pattern.
flatten{Boolean}falseRemoves all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic.
transform{Function|Promise}(content, path) => contentFunction or Promise that modifies file contents before copying.
transformPath{Function|Promise}(targetPath, sourcePath) => pathFunction or Promise that modifies file writing path.
cache{Boolean|Object}falseEnable transform caching. You can use { cache: { key: 'my-cache-key' } } to invalidate the cache.
context{String}options.context || compiler.options.contextA 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
NameTypeDefaultDescription
'dir'{String}undefinedIf from is directory, to has no extension or ends in '/'
'file'{String}undefinedIf to has extension or from is file
'template'{String}undefinedIf 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

NameTypeDefaultDescription
logLevel{String}'warning'Level of messages that the module will log
ignore{Array}[]Array of globs to ignore (applied to from)
context{String}compiler.options.contextA path that determines how to interpret the from path, shared for all patterns
copyUnmodified{Boolean}falseCopies 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
  • 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.

'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

Keywords

FAQs

Last updated on 20 Feb 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc