An esbuild plugin to copy static files and folders from a source directory to destination directory
✔️ Easy to use
✔️ Lightweight
✔️ Typed
✔️ Copy newly added files in watch mode
✔️ Filter the files and folders you want to copy
✔️ Run only once or only when directory or file changed
npm install --save-dev esbuild-copy-files
or
yarn add --dev esbuild-copy-files
const esbuild = require('esbuild');
const { copy } = require("esbuild-copy-files");
const sourceDir = path.resolve(__dirname, './src');
const destDir = path.resolve(__dirname, './dist');
esbuild.build({
entryPoints: ["src/index.ts"],
bundle: true,
outfile: "dist/index.js",
plugins: [
copy({
patterns: [
{
from: [`${sourceDir}/folder1`],
to: [`${destDir}/folder1`],
}
]
})
],
})
# source directories
src/
├── folder1/
│ ├── file1.png
│ ├── subfolder1/
│ │ └── file2.json
│ │ └── file3.txt
# destination directories
dist/
├── folder1/
│ ├── file1.png
│ ├── subfolder1/
│ │ └── file2.json
│ │ └── file3.txt
copy({
patterns: [
{
from: [`${sourceDir}/folder1`],
to: [`${destDir}/folder1`],
// copy only one folder
ignore: ['subfolder1', '*.txt']
}
]
})
Watch files in the source directory for changes or when a new files are created
copy({
// When setting to true, make sure using esbuild's watch mode (ctx.watch())
watch: true,
patterns: [
{
from: [`${sourceDir}/folder1`, `${sourceDir}/folder2`],
to: [`${destDir}/folder1`],
// watch change on src/folder1 and src/folder2
watch: true
},
{
from: [`${sourceDir}/folder3`],
to: [`${destDir}/folder3`],
// do not watch change on ./src/folder3
watch: false
}
]
})
# source directories
src/
├── folder1/
│ ├── file1.json
├── folder2/
│ ├── file2.json
# destination directories
dist/
├── folder3/
│ ├── file1.json
├── folder4/
│ ├── file2.json
See here
for more examples that use esbuild-copy-files
.
export type ArrayLike<T = string> = T | T[];
export type Pattern = {
/**
* The source directory to copy files from
* it can be a string or an array of strings
* it should be a relative path to the current working directory
* example: `['./src/assets', './public']`
*/
from?: ArrayLike;
/**
* The destination directory to copy files to
* it can be a string or an array of strings
* it should be a relative path to the current working directory
* example: `['./dist/assets', './public']`
*/
to?: ArrayLike;
/**
* Ignore files or directory to copy
* it can be a string or an array of strings
* it should be the name of the file or a pattern to match the file name from the source directory
* example: `['package.json', '*.txt', 'myFolder']`
*/
ignore?: ArrayLike;
/**
* Watch for changes in the source directory
* when a file is added or changed, copy or change the file to the destination directory
* @default false
*/
watch?: boolean;
};
export type Options = {
/**
* The list of assets to copy
*/
patterns: Pattern[];
/**
* Manually top watching for changes in the source directory
* @default false
*/
stopWatching?: boolean;
/**
* Watch for changes in the source directory
* When setting to true, make sure using esbuild's watch mode (ctx.watch())
* @see https://esbuild.github.io/api/#watch
* @default false
*/
watch?: boolean;
};
Get started here.