Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
copy-webpack-plugin
Advanced tools
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.
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); } }] })
The file-loader resolves import/require() on a file into a url and emits the file into the output directory. It is similar to copy-webpack-plugin in that it helps manage static assets, but it is more focused on handling assets within the module system.
The html-webpack-plugin simplifies the creation of HTML files to serve your webpack bundles. It can copy HTML files and automatically inject script tags for bundle files. It differs from copy-webpack-plugin by focusing on HTML files and their dependencies.
The assets-webpack-plugin generates a JSON file with the assets produced by webpack. It is similar to copy-webpack-plugin in that it deals with the output of assets, but it does not copy files; instead, it provides a manifest of the generated assets.
This is a webpack plugin that copies individual files or entire directories to the build directory.
Install the plugin:
npm install --save-dev copy-webpack-plugin
new CopyWebpackPlugin([patterns], options)
A pattern looks like:
{ from: 'source', to: 'dest' }
from
to
from
is a directorytoType
from
is a directory'file'
if to
has an extension'dir'
if to
doesn't have an extensionforce
false
ignore
from
path, relative to the contextvar CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');
module.exports = {
context: path.join(__dirname, 'app'),
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' },
// {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
{ glob: '**/*', dot: true }
]
})
]
};
Run npm test
MIT
FAQs
Copy files && directories with webpack
The npm package copy-webpack-plugin receives a total of 6,949,292 weekly downloads. As such, copy-webpack-plugin popularity was classified as popular.
We found that copy-webpack-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.