Socket
Socket
Sign inDemoInstall

copy-webpack-plugin

Package Overview
Dependencies
82
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.3M
decreased by-2.09%
Maintainers
4
Install size
1.81 MB
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

4.4.0 (2018-02-08)

Bug Fixes

  • package: add prepare script (9bf0d99)
  • preProcessPatterns: support glob context paths with special characters (#208) (ea0c05f)
  • support webpack >= v4.0.0 (6a16b3c)

Features

  • use compiler.inputFileSystem instead fs (#205) (158f821)

<a name="4.3.1"></a>

Readme

Source

npm node deps test coverage chat

Copy Webpack Plugin

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 must have webpack-dev-server write files to output directory during development, you can force it with the write-file-webpack-plugin.

Patterns

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'
NameTypeDefaultDescription
from{String|Object}undefinedGlobs accept minimatch options
fromArgs{Object}{ cwd: context }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}``toType Options
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}(content, path) => contentFunction that modifies file contents before copying
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

[
  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

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

[
  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)
]

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

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return 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

NameTypeDefaultDescription
debug{String}'warning'Debug Options
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

debug

NameTypeDefaultDescription
'info'{String|Boolean}falseFile location and read info
'debug'{String}falseVery detailed debugging info
'warning'{String}trueOnly 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

Keywords

FAQs

Last updated on 08 Feb 2018

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc