Write bundle only if changed
New user here. We use esbuild for bundling/minifiying (not watching, which our dev environment handles itself). We use the esbuild cli itself (the executable, on linux), not npm or anything related to node.
When using the esbuild cli it writes bundles even if there's no change to the final file. That triggers all sorts of watchers unnecessarily, which triggers recompilation of our code, etc.
Does the cli have "write only if changed" behaviour?
Could it be solved by configuring your watching logic in the dev environment to exclude those output files?
Interesting idea, I think about that, thanks. However it seems to me somewhere a comparison should be made between existing/missing file and new file. That should probably be outside the scope of watching, and within the scope of writing.
I assume from your comment that esbuild doesn't have such capability?
- Yes esbuild doesn't do this currently, you can wait for Evan's response then.
- The comparison between old files and new files in memory needs at least one read call to the file system. Either it could be implemented inside esbuild, or your watcher could determine if a file is actually changed by diffing their contents. Another way is you can use the js api of esbuild and set
write: falseand do the write by yourself. - The behavior of writing to files even if unchanged, in my view, is also a notification to other tools that "esbuild has done 1 build task". There might be some other tools depending on this behavior.
- Thanks for confirming
- Interesting that this is already possible via the JS API, but not the CLI... we use the CLI
- True, it would break other tools, so it should be optional, e.g. --write-if-changed
Right now the logic for this only avoids overwriting identical files for subsequent builds. Specifically each build saves the hashes from the previous build and a file is only skipped if its hash and contents match the same file from the previous build. This intentionally doesn't work for the initial build due to the reason above (item 3).
I agree that it would be nice if this was configurable, although I haven't decided on my preferred API change for that yet.
Some relevant links:
- Prior to v0.17.0 esbuild always wrote everything: https://github.com/evanw/esbuild/issues/2104
- Rollup currently always writes everything: https://github.com/rollup/rollup/issues/4629
- Webpack only writes if changed, but this can now be configured: https://github.com/webpack/webpack/commit/07671f3481cee394d7329003f71a59c8ecaf652b?w=1
Right now ... avoids overwriting identical files for subsequent builds...
I think I understand now - when using the CLI every build is the "initial build".
I haven't decided on my preferred API change for that yet.
Frankly any API you choose would be fine. I can see though how it would be tricky to settle on something that would please everyone, an impossible task!