opennextjs / opennextjs-cloudflare

Open Next.js adapter for Cloudflare
https://opennext.js.org/cloudflare
MIT License
265 stars 5 forks source link

Fail build (/throw errors) when code patching fails #28

Open dario-piotrowicz opened 2 weeks ago

dario-piotrowicz commented 2 weeks ago

In various places we do some code patching that is completely necessary for the worker to run, for example:

https://github.com/opennextjs/opennextjs-cloudflare/blob/42320e78e4e8f1abafd2336c3a384fedc2f8a394/packages/cloudflare/src/build/patches/to-investigate/patch-find-dir.ts#L12-L22

But we do not check if such patches have been completed successfully (e.g. in the example above we never check wether "function findDir(dir, name) {" is actually found and replaced in the code or not)

In order not to complete the build process in a seemingly successful way just to end up with a non-fully (or not-at-all) functional worker we should throw errors and fail the build process in case some of these patches aren't getting successfully applied.

dario-piotrowicz commented 2 weeks ago

[!NOTE] Later we should probably consider refactoring the code regex replacements with AST manipulations (using ts-morph as it is already use in the updateWebpackChunksFile utility)