Replaces chalk with picocolors & fixes Hermes runtime #181
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
The primary goal of this PR is to enable the use of Adze in React-native projects. RN runs on the Hermes JS Engine, although it can sometimes run in the browser context using v8 (debugging with dev tools).
Any library that wants to be truly isomorphic and reliably run on RN can't use runtime-specific APIs (Browser API or Node API) without safeguards. We can do this on adze.
Motivation
React-native lacks a great log library such as adze. IMO, adding support for react-native could drive community collaboration. Also, the constraints required to run on a strict EcmaScript-compliant runtime such as Hermes can help to make the library more resilient to future runtime changes.
Details
Replacing Chalk
The chalk library makes assumptions about the runtime it is on. The main problem is that it has side effects (runs some code when imported) that break when running it on Hermes. There are workarounds that involve patching and "polyfilling" using the Metro Bundler, but these are not ideal.
For this reason, this PR replaces chalk with picocolors.
Environment Checks
When running on expo for android/ios, the window object is defined, although it's technically running on Hermes. The current checks are not sufficient to check if we are running in a browser.
This PR changes the
isBrowser
function to check for additional properties in the window object, that adze expects to find.StructureClone Polyfill
Adze makes use of the
structuredClone
function. This is a function provided by the runtimes and is not part of the language. As such, it's not implemented on Hermes, and probably won't be until (if ever) it's added to the EcmaScript spec.For this reason a polyfill for the structuredClone was added. We could revisit this later, and choose a different deep clone implementation, that doesn't rely on runtimes API.