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
file-loader
The file-loader resolves import/require() on a file into a url and emits the file into the output directory. It is similar to copy-webpack-plugin in that it helps manage static assets, but it is more focused on handling assets within the module system.
html-webpack-plugin
The html-webpack-plugin simplifies the creation of HTML files to serve your webpack bundles. It can copy HTML files and automatically inject script tags for bundle files. It differs from copy-webpack-plugin by focusing on HTML files and their dependencies.
assets-webpack-plugin
The assets-webpack-plugin generates a JSON file with the assets produced by webpack. It is similar to copy-webpack-plugin in that it deals with the output of assets, but it does not copy files; instead, it provides a manifest of the generated assets.
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: {
outputPath: path.join(__dirname, 'build')
},
plugins: [
new CopyWebpackPlugin([
{ from: 'from/file.txt' },
{ from: 'from/file.txt', to: 'to/file.txt' },
{ from: 'from/file.txt', to: 'to/directory' },
{ from: 'from/directory' },
{ from: 'from/directory', to: 'to/directory' },
{ from: 'from/directory/**/*', to: '/absolute/path' },
{
from: {
glob:'from/directory/**/*',
dot: true
},
to: '/absolute/path'
},
{
context: 'from/directory',
from: '**/*',
to: '/absolute/path'
},
{
from: 'path/to/file.txt',
to: 'file/without/extension',
toType: 'file'
},
{
from: 'path/to/file.txt',
to: 'directory/with/extension.ext',
toType: 'dir'
}
], {
ignore: [
'*.txt',
{ glob: '**/*', dot: true }
],
copyUnmodified: true
})
]
};
Testing
Run npm test
License
MIT