Copies individual files or entire directories to the build directory
npm i -D copy-webpack-plugin
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 thewrite-file-webpack-plugin
.
##
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'
{Object}
{ cwd: context }
node-glob
options in addition to the ones below
{String|Object}
undefined
from
is file or dir, resolved glob path if from
is glob
{Boolean}
false
compilation.assets
(usually added by other plugins/loaders)
flatten
{Boolean}
false
{Function}
(content, path) => content
{Boolean|Object}
false
transform
caching. You can use { cache: { key: 'my-cache-key' } }
to invalidate the cache
{String}
options.context ||compiler.options.context
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
'dir'
{String}
undefined
from
is directory, to
has no extension or ends in '/'
'file'
{String}
undefined
to
has extension or from
is file
'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)
]
{String}
compiler.options.context
from
path, shared for all patterns
{Boolean}
false
webpack-dev-server
. All files are copied on first build, regardless of this option
debug
'info'
{String|Boolean}
false
'debug'
{String}
false
'warning'
{String}
true
'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
orwebpack-dev-server
build. Setting this option totrue
will copy all files.
webpack.config.js
[
new CopyWebpackPlugin(
[ ...patterns ],
{ copyUnmodified: true }
)
]
|
|
|
|