Copies individual files or entire directories to the build directory.
npm install --save-dev copy-webpack-plugin
new CopyWebpackPlugin([patterns], options)
A pattern looks like:
{ from: 'source', to: 'dest' }
Or, in the simple case of just a from
with the default destination, you can use a string primitive instead of an object:
'source'
###
from
fromArgs
{ cwd: context }
node-glob
options in addition to the ones below.
to
from
is file or dirfrom
is glob
toType
to
has extension or from
is filefrom
is directory, to
has no extension or ends in '/'to
contains a template pattern
context
from
path
flatten
ignore
transform
force
cache
transform
caching. You can use { cache: { key: 'my-cache-key'} }
to invalidate cache.
context
from
path, shared for all patterns
ignore
from
)
copyUnmodified
debug
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 some files using glob in nested directory
{
from: 'from/directory',
to: 'to/directory',
ignore: ['nested/**/*.extension']
}
], {
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
})
]
};
Globally patch fs with graceful-fs
npm install graceful-fs --save-dev
At the top of your webpack config, insert this
const fs = require('fs');
const gracefulFs = require('graceful-fs');
gracefulFs.gracefulify(fs);
See this issue for more details
Starting in version 3.0.0, we stopped using fs to copy files to the filesystem and started depending on webpack's in-memory filesystem:
... webpack-dev-server will serve the static files in your build folder. It’ll watch your source files for changes and when changes are made the bundle will be recompiled. This modified bundle is served from memory at the relative path specified in publicPath (see API). It will not be written to your configured output directory.
If you must have webpack-dev-server write to your output directory, you can force it with the write-file-webpack-plugin.
|
|
|
|