Socket
Socket
Sign inDemoInstall

copy-webpack-plugin

Package Overview
Dependencies
8
Maintainers
3
Versions
80
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    copy-webpack-plugin

Copy files and directories in webpack


Version published
Weekly downloads
7.1M
decreased by-5.67%
Maintainers
3
Install size
2.49 MB
Created
Weekly downloads
 

Package description

What is copy-webpack-plugin?

The copy-webpack-plugin is a webpack plugin that allows you to copy files and directories from one location to another within your build process. It is commonly used to copy static assets such as images, fonts, and HTML files to the output directory defined in your webpack configuration.

What are copy-webpack-plugin's main functionalities?

Copying individual files or entire directories

This feature allows you to copy individual files or entire directories from a specified source to a destination within your output directory. The 'from' field specifies the relative or absolute path to the source file or directory, and the 'to' field specifies the relative path to the destination within the output directory.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest' }] })

Ignoring files

This feature allows you to exclude specific files or patterns from being copied. In the provided code sample, all JavaScript files are ignored and will not be copied to the destination.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', globOptions: { ignore: ['**/*.js'] } }] })

Adding context to file paths

This feature allows you to specify a context for the file paths. The 'context' option sets a base path for the 'from' property. In the code sample, the actual path that will be copied is 'app/source'.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', context: 'app' }] })

Transforming file content

This feature allows you to modify the content of files before they are copied. The 'transform' function receives the content of the file and its path, and it should return the new content. This can be used to minify files, add banners, or perform other transformations.

new CopyPlugin({ patterns: [{ from: 'source', to: 'dest', transform: (content, path) => { return modifyContent(content); } }] })

Other packages similar to copy-webpack-plugin

Changelog

Source

4.1.1 (2017-10-05)

Chore

  • Update dependencies for NSP security advisory (#151) (6d4346e)

    • Reference issue: https://nodesecurity.io/advisories/minimatch_regular-expression-denial-of-service

<a name="4.1.0"></a>

Readme

Source

npm node deps test coverage chat

Copy Webpack Plugin

Copies individual files or entire directories to the build directory.

Install

npm install --save-dev copy-webpack-plugin

Usage

new CopyWebpackPlugin([patterns], options)

A pattern looks like: { from: 'source', to: 'dest' }

Pattern properties:
NameRequiredDefaultDetails
fromYexamples:
'relative/file.txt'
'/absolute/file.txt'
'relative/dir'
'/absolute/dir'
'**/*'
{glob:'**/*', dot: true}

Globs accept minimatch options
toNoutput 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
toTypeN'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
contextNcompiler.options.contextA path that determines how to interpret the from path
flattenNfalseRemoves all directory references and only copies file names

If files have the same name, the result is non-deterministic
ignoreN[]Additional globs to ignore for this pattern
transformNfunction(content, path) {
  return content;
}
Function that modifies file contents before writing to webpack
forceNfalseOverwrites files already in compilation.assets (usually added by other plugins)
Available options:
NameDefaultDetails
ignore[]Array of globs to ignore (applied to from)
copyUnmodifiedfalseCopies 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 CopyWebpackPlugin = require('copy-webpack-plugin');
var 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' },
            
            // {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
        })
    ]
};

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.

Maintainers


Juho Vepsäläinen

Joshua Wiens

Michael Ciniawsky

Alexander Krasnoyarov

Keywords

FAQs

Last updated on 05 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc