Skip to content

tiavina-mika/esbuild-copy-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

194 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

esbuild-copy-files

An esbuild plugin to copy static files and folders from a source directory to destination directory

NPM Version Language Build

Why?

βœ”οΈ 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

Installation

npm install --save-dev esbuild-copy-files

or

yarn add --dev esbuild-copy-files

Get started

Simple usage

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

Ignore files to copy

copy({
  patterns: [
    {
      from: [`${sourceDir}/folder1`],
      to: [`${destDir}/folder1`],
      // copy only one folder
      ignore: ['subfolder1', '*.txt']
    }
  ]
})

Watch mode

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.

Types

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;
};

Contributing

Get started here.