Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces chalk with picocolors & fixes Hermes runtime #181

Merged
merged 3 commits into from
Oct 29, 2024

Conversation

tiagobnobrega
Copy link
Contributor

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.

fixes demo page

adds workaround for picocolors not being exported as esm

fix isBrowser test

adds polyfill to structuredClone api

Fixes browser demo
package.json Show resolved Hide resolved
@AJStacy
Copy link
Collaborator

AJStacy commented Oct 27, 2024

This is great! I was not aware of picocolors and I think that's a much better solution than chalk for this library.

package.json Show resolved Hide resolved
@AJStacy
Copy link
Collaborator

AJStacy commented Oct 27, 2024

Other than the comment about the bundle release these changes look great. If you can apply that one change to the PR then we can get it merged. Thank you!

@AJStacy AJStacy merged commit 7bf7d93 into adzejs:master Oct 29, 2024
3 checks passed
Copy link

🎉 This PR is included in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants