npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-plugin-copy

v1.0.1

Published

Copy files and directories in webpack

Downloads

429

Readme

This is a fork of Copy Webpack Plugin

Primarily forked to add the option to copy file permissions

Webpack Plugin Copy

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

Usage

new WebpackPluginCopy([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 dirresolved 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 namesIf 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) | | copyPermissions | N | false | Applies source file permissions to destination files |

Available options:

| Name | Default | Details | | ---- | ------- | ------- | | ignore | [] | Array of globs to ignore (applied to from) | | 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 WebpackPluginCopy = require('webpack-plugin-copy');
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 WebpackPluginCopy([
            // {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
        })
    ]
};

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

This plugin is not using fs to copy files to the filesystem but is 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