用来复制一个单独的文件或者整个目录到新建的文件夹下
通常用在我们打包的时候,将一些文件放到指定的文件夹下
npm install --save-dev copy-webpack-plugin
先检查自己的版本吧(我就被坑了╮(╯▽╰)╭)不同版本的文档不一样
文档地址
https://webpack.js.org/plugins/copy-webpack-plugin/
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{ from: 'source', to: 'dest' },
{ from: 'other', to: 'public' },
],
}),
],
};
我简单的示例
new CopyWebpackPlugin(
{
patterns: [
{ from: 'F:\\canvas\\day02-canvas\\img\\clock.jpg' },
// { from: 'other', to: 'public' },
],
}),
new CopyWebpackPlugin(
{
patterns: [
{ from: path.resolve(__dirname,'1.jpg') },
// { from: 'other', to: 'public' },
],
}),
4.xxx.xx版本的吧 做个记录
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
module.exports = {
context: path.join(__dirname, 'app'),
devServer: {
// This is required for older versions of webpack-dev-server
// if you use absolute 'to' paths. The path should be an
// absolute path to your build destination.
outputPath: path.join(__dirname, 'build')
},
plugins: [
new CopyWebpackPlugin([
// {output}/file.txt
{ from: 'from/file.txt' },
// equivalent
'from/file.txt',
// {output}/to/file.txt
{ from: 'from/file.txt', to: 'to/file.txt' },
// {output}/to/directory/file.txt
{ from: 'from/file.txt', to: 'to/directory' },
// Copy directory contents to {output}/
{ from: 'from/directory' },
// Copy directory contents to {output}/to/directory/
{ from: 'from/directory', to: 'to/directory' },
// Copy glob results to /absolute/path/
{ from: 'from/directory/**/*', to: '/absolute/path' },
// Copy glob results (with dot files) to /absolute/path/
{
from: {
glob:'from/directory/**/*',
dot: true
},
to: '/absolute/path'
},
// Copy glob results, relative to context
{
context: 'from/directory',
from: '**/*',
to: '/absolute/path'
},
// {output}/file/without/extension
{
from: 'path/to/file.txt',
to: 'file/without/extension',
toType: 'file'
},
// {output}/directory/with/extension.ext/file.txt
{
from: 'path/to/file.txt',
to: 'directory/with/extension.ext',
toType: 'dir'
}
], {
ignore: [
// Doesn't copy any files with a txt extension
'*.txt',
// Doesn't copy any file, even if they start with a dot
'**/*',
// Doesn't copy any file, except if they start with a dot
{ glob: '**/*', dot: false }
],
// By default, we only copy modified files during
// a watch or webpack-dev-server build. Setting this
// to `true` copies all files.
copyUnmodified: true
})
]
};