Socket
Socket
Sign inDemoInstall

copy-webpack-plugin

Package Overview
Dependencies
7
Maintainers
1
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
Maintainers
1
Install size
1.33 MB
Created

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

Readme

Source

Copy 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 copy-webpack-plugin

Usage

new CopyWebpackPlugin([patterns], options)

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

Pattern properties:

  • from
    • is required
    • can be an absolute or path relative to the context
    • can be a file or directory
  • to
    • is optional
    • is relative to the context root
    • defaults to '/'
    • must be a directory if from is a directory
  • toType
    • is optional
    • is ignored if from is a directory
    • defaults to 'file' if to has an extension
    • defaults to 'dir' if to doesn't have an extension
  • force
    • is optional
    • defaults to false
    • forces the plugin to overwrite files staged by previous plugins

Available options:

  • ignore
    • an array of files and directories to ignore
    • accepts globs
    • globs are evaluated on the from path, relative to the context

Examples

var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');

module.exports = {
    context: path.join(__dirname, 'app'),
    plugins: [
        new CopyWebpackPlugin([
            // {context}/file.txt
            { from: 'path/to/file.txt' },
            
            // {context}/path/to/build/file.txt
            { from: 'path/to/file.txt', to: 'path/to/build/file.txt' },
            
            // {context}/path/to/build/directory/file.txt
            { from: 'path/to/file.txt', to: 'path/to/build/directory' },

            // Copy directory contents to {context}/
            { from: 'path/to/directory' },
            
            // Copy directory contents to {context}/path/to/build/directory
            { from: 'path/to/directory', to: 'path/to/build/directory' },
            
            // {context}/file/without/extension
            {
                from: 'path/to/file.txt',
                to: 'file/without/extension',
                toType: 'file'
            },
            
            // {context}/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'
            ]
        })
    ]
};

Testing

Build Status

Run npm test

License

MIT

Keywords

FAQs

Last updated on 27 Nov 2015

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