Use the `copyfiles` command in front-end development to manage file copying
As front-end developers, we often need to copy various static resource files during the build process.
I’d like to share a highly practical tool — copyfiles, which makes file copying simple and efficient.
What is copyfiles?
copyfiles is a Node.js command-line tool specifically designed for copying files and directories. Its primary purpose is to streamline the copying of static resources in front-end project builds.
Installation
1 |
|
Basic Usage
The simplest way to use it is by specifying the source files and target directory:
1 |
|
This will copy all PNG files from the src/assets
directory to the dist/images
directory.
Common Options
copyfiles provides several useful options:
-u, --up <n>
- Remove n levels of directories from the source path1
2copyfiles -u 1 src/assets/**/*.png dist
# This preserves the structure under assets but excludes the src directory-f, --flat
- Flatten the directory structure1
2copyfiles --flat src/assets/**/*.{jpg,png} dist/images
# All files will be copied directly to the target directory, ignoring the original structure-e, --exclude
- Exclude specific files1
copyfiles -e "**/*.temp.*" src/**/* dist
Configuration in package.json
Integrating copyfiles into npm scripts is very convenient:
1 |
|
Real-World Use Case
In my latest project, I needed to copy various types of resource files to different target directories:
1 |
|
With this setup, running npm run build
will first compile the code with webpack and then automatically copy all necessary resource files to their respective target directories.