
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-serverwrite 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
fromwebpack.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)
]
towebpack.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)
]
forcewebpack.config.js
[
new CopyWebpackPlugin([
{ from: 'src/**/*' to: 'dest/', force: true }
], options)
]
ignorewebpack.config.js
[
new CopyWebpackPlugin([
{ from: 'src/**/*' to: 'dest/', ignore: [ '*.js' ] }
], options)
]
flattenwebpack.config.js
[
new CopyWebpackPlugin([
{ from: 'src/**/*', to: 'dest/', flatten: true }
], options)
]
transformwebpack.config.js
[
new CopyWebpackPlugin([
{
from: 'src/*.png',
to: 'dest/',
transform (content, path) {
return optimize(content)
}
}
], options)
]
cachewebpack.config.js
[
new CopyWebpackPlugin([
{
from: 'src/*.png',
to: 'dest/',
transform (content, path) {
return optimize(content)
},
cache: true
}
], options)
]
contextwebpack.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 }
)
]
ignorewebpack.config.js
[
new CopyWebpackPlugin(
[ ...patterns ],
{ ignore: [ '*.js', '*.css' ] }
)
]
contextwebpack.config.js
[
new CopyWebpackPlugin(
[ ...patterns ],
{ context: [ '/app' ] }
)
]
copyUnmodifiedℹ️ By default, we only copy modified files during a
webpack --watchorwebpack-dev-serverbuild. Setting this option totruewill copy all files.
webpack.config.js
[
new CopyWebpackPlugin(
[ ...patterns ],
{ copyUnmodified: true }
)
]
|
|
|
|
|