Copify 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 copify-webpack-plugin
Usage
new CopifyWebpackPlugin([patterns], options)
A pattern looks like:
{ from: 'source', to: 'dest' }
Pattern properties:
Name | Required | Default | Details |
---|
from | Y | | examples: 'relative/file.txt' '/absolute/file.txt' 'relative/dir' '/absolute/dir' '**/*' {glob:'**/*', dot: true}
Globs accept minimatch options |
to | N | output root if from is file or dir
resolved glob path if from is glob | examples: 'relative/file.txt' '/absolute/file.txt' 'relative/dir' '/absolute/dir' 'relative/[name].[ext]' '/absolute/[name].[ext]'
Templates are file-loader patterns |
toType | N | 'file' if to has extension or from is file
'dir' if from is directory, to has no extension or ends in '/'
'template' if to contains a template pattern | |
context | N | compiler.options.context | A path that determines how to interpret the from path |
flatten | N | false | Removes all directory references and only copies file names
If files have the same name, the result is non-deterministic |
ignore | N | [] | Additional globs to ignore for this pattern |
transform | N | function(content, path) { return content; } | Function that modifies file contents before writing to webpack |
force | N | false | Overwrites files already in compilation.assets (usually added by other plugins) |
excludeFromManifest | N | false | Exclude from manifest |
Available options:
Name | Default | Details |
---|
ignore | [] | Array of globs to ignore (applied to from ) |
manifest | N | Manifest options |
copyUnmodified | false | Copies files, regardless of modification when using watch or webpack-dev-server. All files are copied on first build, regardless of this option. |
debug | 'warning' | options: 'warning' - only warnings 'info' or true - file location and read info 'debug' - very detailed debugging info |
Examples
var CopifyWebpackPlugin = require('copify-webpack-plugin');
var path = require('path');
module.exports = {
context: path.join(__dirname, 'app'),
devServer: {
outputPath: path.join(__dirname, 'build')
},
plugins: [
new CopifyWebpackPlugin([
{ 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: false }
],
copyUnmodified: true
})
]
};
Testing
Run npm test
FAQ
"EMFILE: too many open files" or "ENFILE: file table overflow"
Globally patch fs with graceful-fs
npm install graceful-fs --save-dev
At the top of your webpack config, insert this
var fs = require('fs');
var gracefulFs = require('graceful-fs');
gracefulFs.gracefulify(fs);
See this issue for more details
This doesn't copy my files with webpack-dev-server
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.
License
MIT