{"id":29143887,"url":"https://github.com/nickjj/esbuild-copy-static-files","last_synced_at":"2025-06-30T20:39:42.077Z","repository":{"id":42588811,"uuid":"442940383","full_name":"nickjj/esbuild-copy-static-files","owner":"nickjj","description":"An esbuild plugin to copy static files that changed from a source directory to a destination directory.","archived":false,"fork":false,"pushed_at":"2022-09-24T07:47:45.000Z","size":41,"stargazers_count":39,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-22T01:47:51.492Z","etag":null,"topics":["copy","esbuild","esbuild-plugin","static-files"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nickjj.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-30T02:06:43.000Z","updated_at":"2025-04-06T15:29:51.000Z","dependencies_parsed_at":"2022-08-25T12:21:43.790Z","dependency_job_id":null,"html_url":"https://github.com/nickjj/esbuild-copy-static-files","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nickjj/esbuild-copy-static-files","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickjj%2Fesbuild-copy-static-files","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickjj%2Fesbuild-copy-static-files/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickjj%2Fesbuild-copy-static-files/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickjj%2Fesbuild-copy-static-files/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickjj","download_url":"https://codeload.github.com/nickjj/esbuild-copy-static-files/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickjj%2Fesbuild-copy-static-files/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262847739,"owners_count":23374069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copy","esbuild","esbuild-plugin","static-files"],"created_at":"2025-06-30T20:39:39.756Z","updated_at":"2025-06-30T20:39:42.048Z","avatar_url":"https://github.com/nickjj.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# esbuild-copy-static-files\n\n![CI](https://github.com/nickjj/esbuild-copy-static-files/workflows/CI/badge.svg?branch=main)\n\nAn [esbuild](https://esbuild.github.io/) plugin to efficiently copy static\nfiles from a source directory to a destination directory.\n\n✔️ Only copy files that changed *(it compares an MD5 hash of each `src` and `dest` file)*  \n✔️  No 3rd party dependencies *(it only uses a few functions from Node's standard library)*\n\n## Demo Video\n\nThis video mostly covers what's in this README along with a bit more detail\naround the \"why\".\n\n[![Demo\nVideo](https://img.youtube.com/vi/bMkmwq5tzSs/0.jpg)](https://www.youtube.com/watch?v=bMkmwq5tzSs)\n\n## Why?\n\nSo you've decided that you want to copy HTML, icons, images, fonts and other\nstatic files from a source directory to a destination directory through\nesbuild. Great, I do too.\n\nBlindly recursively copying a `src` to `dest` directory can be both inefficient\nand distracting. For example if you have 50 static files and update a single JS\nfile then you'll end up copying all 50 static files over.\n\nThat's creating a lot of unnecessary disk writes. Depending on which web\nframework you use, that might cause a lot of log spam in development too. That\ncould be due to live reload picking up a bunch of files that technically\nchanged.\n\nThis plugin fixes the above because it'll only copy static files if they\nchanged on disk since they were last copied. You can think of this plugin as\nbeing similar to using `rsync` instead of `cp` on the command line except\nthere's no extra watcher or command that you need to run. It's all seamlessly\nintegrated into esbuild.\n\n### How it works at a high level\n\nFor most web apps, when it comes to my front-end I like the idea of this type of directory structure:\n\n```sh\n# This is where your assets are located and is used as input for esbuild.\nassets/\n├── css/\n│   └── app.css\n├── js/\n│   └── app.js\n├── static/\n│   ├── images/\n│   │   └── logo.png\n│   ├── favicon.ico\n│   ├── robots.txt\n├── esbuild.config.js\n├── package.json\n├── postcss.config.js\n├── tailwind.config.js\n└── yarn.lock\n\n# This is the output of where esbuild will write its file(s) to.\npublic/\n├── css/\n│   └── app.css\n├── js/\n│   └── app.js\n├── images/\n│   └── logo.png\n├── favicon.ico\n├── robots.txt\n```\n\nIn my opinion it's really nice having all of your assets in 1 spot which then\nget output to another location.\n\nesbuild manages the `js/` directory and TailwindCSS (or whatever you prefer)\nmanages the `css/` directory but that leaves us with a bunch of static files\nthat are composed of HTML, icons, images, fonts and more.\n\nThis esbuild plugin will copy your static files to your esbuild output\ndirectory. No processing will be done on these static files. It's a quick `cp`\nand only files that have changed are copied so it's really efficient.\n\nYou can name your `static/` and `public/` directories and configure their paths\nhowever you want. We'll go over all of the configuration options in a bit.\n\n## Installation\n\nJust a heads up, this package uses Node's `fs.cpSync` function which depends on\nusing at least Node v16.7.\n\n```sh\n# Prefer Yarn?\nyarn add --dev esbuild-copy-static-files\n\n# Or NPM instead?\nnpm install --save-dev esbuild-copy-static-files\n```\n\nYou can check it out on: \u003chttps://www.npmjs.com/package/esbuild-copy-static-files\u003e\n\n## Getting Started\n\nIf you had the same directory structure as above, here's the bare minimum\nesbuild config to get going:\n\n```js\nconst esbuild = require('esbuild')\nconst copyStaticFiles = require('esbuild-copy-static-files')\n\nesbuild.build({\n  entryPoints: ['./js/app.js'],\n  outfile: '../public/js/app.js',\n  bundle: true,\n  minify: true,\n  sourcemap: false,\n  watch: false,\n  plugins: [copyStaticFiles()],\n})\n```\n\n## Configuration\n\nThis plugin uses Node's [`fs.cpSync`\nfunction](https://nodejs.org/api/fs.html#fscpsyncsrc-dest-options) under the\nhood. Any option that it has can be configured here.\n\nHere's a list of what you can configure and the default values if you don't\noverride them:\n\n```js\n  plugins: [\n    copyStaticFiles({\n      src: './static',\n      dest: '../public',\n      dereference: true,\n      errorOnExist: false,\n      filter: EXPLAINED_IN_MORE_DETAIL_BELOW,\n      preserveTimestamps: true,\n      recursive: true,\n    })\n  ],\n```\n\n**In most cases you'll likely only change the `src` and `dest` to fit your\nproject's directory structure.**\n\nHere's the docs of every configurable option from Node's documentation:\n\n- `src` source path to copy.\n- `dest` destination path to copy to.\n- `dereference` dereference symlinks.\n- `errorOnExist` when `force` is `false` and the destination exists, throw an error.\n- `filter` function to filter copied files / directories. Return `true` to copy the item, `false` to ignore it.\n- `force` overwrite existing file or directory. The copy operation will ignore errors if you set this to false and the destination exists. Use the `errorOnExist` option to change this behavior.\n- `preserveTimestamps` when `true` timestamps from `src` will be preserved.\n- `recursive` copy directories recursively.\n\n### How does the filter function work?\n\nBy default it will filter out, AKA skip copying any files that haven't changed.\nIt does this by getting the MD5 hash of each `src` and `dest` file. If both\nfiles have the same MD5 hash then it gets skipped.\n\nThis is nice because if you had let's say 50 static files and only 1 of them\nchanged then only 1 file will get copied.\n\n#### Using a custom filter function\n\nIf you can think of a more efficient way of detecting which file(s) should get\ncopied you can customize the filter function by providing your own function\nthat returns `true` or `false` based on whatever criteria you prefer.\n\nHere's an example of a simple filter function that always returns `true`:\n\n```js\n  plugins: [\n    copyStaticFiles({\n      filter: function () { return true },\n    })\n  ],\n```\n\n## About the author\n\n- Nick Janetakis | \u003chttps://nickjanetakis.com\u003e | [@nickjanetakis](https://twitter.com/nickjanetakis)\n\nI'm a self taught developer and have been freelancing for the last ~20 years.\nYou can read about everything I've learned along the way on my site at\n[https://nickjanetakis.com](https://nickjanetakis.com/).\n\nThere's hundreds of [blog posts / videos](https://nickjanetakis.com/blog/) and\na couple of [video courses](https://nickjanetakis.com/courses/) on web\ndevelopment and deployment topics. I also have a\n[podcast](https://runninginproduction.com) where I talk with folks about\nrunning web apps in production.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickjj%2Fesbuild-copy-static-files","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickjj%2Fesbuild-copy-static-files","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickjj%2Fesbuild-copy-static-files/lists"}