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

copy-webpack-plugin-hash

v6.0.0

Published

Copy files && directories with webpack

Downloads

216

Readme

npm node deps test coverage chat

npm i -D copy-webpack-plugin

webpack.config.js

const CopyWebpackPlugin = require('copy-webpack-plugin')

const config = {
  plugins: [
    new CopyWebpackPlugin([ ...patterns ], options)
  ]
}

ℹ️ If you must have webpack-dev-server write files to output directory during development, you can force it with the write-file-webpack-plugin.

Patterns

A simple pattern looks like this

{ from: 'source', to: 'dest' }

Or, in case of just a from with the default destination, you can also use a {String} as shorthand instead of an {Object}

'source'

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |from|{String\|Object}|undefined|Globs accept minimatch options| |fromArgs|{Object}|{ cwd: context }|See the node-glob options in addition to the ones below| |to|{String\|Object}|undefined|Output root if from is file or dir, resolved glob path if from is glob| |toType|{String}||[toType Options](#totype)| |[`test`](#test)|`{RegExp}`||Pattern for extracting elements to be used in to templates| |force|{Boolean}|false|Overwrites files already in compilation.assets (usually added by other plugins/loaders)| |ignore|{Array}|[]|Globs to ignore for this pattern| |flatten|{Boolean}|false|Removes all directory references and only copies file names.⚠️ If files have the same name, the result is non-deterministic| |transform|{Function\|Promise}|(content, path) => content|Function or Promise that modifies file contents before copying| |cache|{Boolean\|Object}|false|Enable transform caching. You can use { cache: { key: 'my-cache-key' } } to invalidate the cache| |context|{String}|options.context \|\| compiler.options.context|A path that determines how to interpret the from path|

from

webpack.config.js

[
  new CopyWebpackPlugin([
    'relative/path/to/file.ext'
    '/absolute/path/to/file.ext'
    'relative/path/to/dir'
    '/absolute/path/to/dir'
    '**/*'
    { glob: '\*\*/\*', dot: true }
  ], options)
]

to

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: '**/*', to: 'relative/path/to/dest/' }
    { from: '**/*', to: '/absolute/path/to/dest/' }
  ], options)
]

toType

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |'dir'|{String}|undefined|If from is directory, to has no extension or ends in '/'| |'file'|{String}|undefined|If to has extension or from is file| |'template'|{String}|undefined|If to contains a template pattern|

'dir'

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'path/to/file.txt',
      to: 'directory/with/extension.ext',
      toType: 'dir'
    }
  ], options)
]

'file'

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'path/to/file.txt',
      to: 'file/without/extension',
      toType: 'file'
    },
  ], options)
]

'template'

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/'
      to: 'dest/[name].[hash].[ext]',
      toType: 'template'
    }
  ], options)
]

test

Defines a {RegExp} to match some parts of the file path. These capture groups can be reused in the name property using [N] placeholder. Note that [0] will be replaced by the entire path of the file, whereas [1] will contain the first capturing parenthesis of your {RegExp} and so on...

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: '*/*',
      to: '[1]-[2].[hash].[ext]',
      test: /([^/]+)\/(.+)\.png$/
    }
  ], options)
]

force

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*' to: 'dest/', force: true }
  ], options)
]

ignore

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*' to: 'dest/', ignore: [ '*.js' ] }
  ], options)
]

flatten

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/**/*', to: 'dest/', flatten: true }
  ], options)
]

transform

{Function}

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return optimize(content)
      }
    }
  ], options)
]

{Promise}

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return Promise.resolve(optimize(content))
      }
  }
  ], options)
]

cache

webpack.config.js

[
  new CopyWebpackPlugin([
    {
      from: 'src/*.png',
      to: 'dest/',
      transform (content, path) {
        return optimize(content)
      },
      cache: true
    }
  ], options)
]

context

webpack.config.js

[
  new CopyWebpackPlugin([
    { from: 'src/*.txt', to: 'dest/', context: 'app/' }
  ], options)
]

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |debug|{String}|'warning'|Debug Options| |ignore|{Array}|[]|Array of globs to ignore (applied to from)| |context|{String}|compiler.options.context|A path that determines how to interpret the from path, shared for all patterns| |copyUnmodified|{Boolean}|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

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |'info'|{String\|Boolean}|false|File location and read info| |'debug'|{String}|false|Very detailed debugging info| |'warning'|{String}|true|Only warnings|

'info'

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: 'info' }
  )
]

'debug'

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: 'debug' }
  )
]

'warning' (default)

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { debug: true }
  )
]

ignore

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { ignore: [ '*.js', '*.css' ] }
  )
]

context

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { context: [ '/app' ] }
  )
]

copyUnmodified

ℹ️ By default, we only copy modified files during a webpack --watch or webpack-dev-server build. Setting this option to true will copy all files.

webpack.config.js

[
  new CopyWebpackPlugin(
    [ ...patterns ],
    { copyUnmodified: true }
  )
]