near / near-api-js

JavaScript library to interact with NEAR Protocol via RPC API
https://near.github.io/near-api-js
MIT License
390 stars 244 forks source link

feat: modularization and bundling enhancements #1353

Closed andy-haynes closed 1 month ago

andy-haynes commented 3 months ago

Pre-flight checklist

Motivation

This PR contains several enhancements intended to modernize and simplify usage of the @near-js packages:

One of the more substantial changes required to target es2022 was the removal of the Assignable class and modification of the Enum class, both used primarily in @near-js/transactions for (de)serialization of actions. With public class fields, the behavior around setting class fields from the base constructor has changed and the dynamic setting logic in Assignable is no longer valid. However, this provides a good opportunity to define class properties per action, which while redundant, provides more intuitive typing support. See relevant SO question.

While Assignable could be removed*, I effectively ran into the opposite problem with Enum. Serialization breaks if any of the following are violated:

I'd need to look into this further to understand what's exactly going on, but the current implementation works and is only a little less confusing than the existing one. *Assignable will remain for backward compatibility reasons, as outdated NAJ peer dependencies in Wallet Selector dependencies break otherwise.

Test Plan

Due to the scope of these changes, a next (near-api-js@next) tag has been created in NPM referencing packages built from this branch. Long term the next tag will require a dedicated build step in order to remain current. Wallet Selector update looks good (React example works): https://github.com/near/wallet-selector/pull/1183

The attempt at an esbuild bundle was missing some functionality and will require further debugging to get working (see branch feat/dist-bundling for original implementation). For now we will be deprecating as part of this major release in favor of using ES modules in browser.

So going from supporting this:

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/near-api-js@4.0.3/dist/near-api-js.min.js"></script>
<script type="module">
  const { providers: { JsonRpcProvider } } = window.nearApi;
  ...
</script>

To relying on modern (starting ~2017-18) browsers supporting modules:

<script type="module">
  import { providers } from 'https://esm.sh/near-api-js@next';
  const { JsonRpcProvider } = providers;
  ...
</script>

This will reduce bundle size substantially for most use cases and is expected to be an easy lift for applications using the old format.

Related issues/PRs

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: f027f1bdbbd21122a5922cccbc30d76139eae479

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 15 packages | Name | Type | | -------------------------- | ----- | | @near-js/accounts | Minor | | @near-js/biometric-ed25519 | Minor | | @near-js/crypto | Minor | | @near-js/iframe-rpc | Minor | | @near-js/keystores | Minor | | @near-js/keystores-browser | Minor | | @near-js/keystores-node | Minor | | near-api-js | Major | | @near-js/providers | Major | | @near-js/signers | Minor | | @near-js/transactions | Minor | | @near-js/types | Minor | | @near-js/utils | Major | | @near-js/wallet-account | Minor | | @near-js/cookbook | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR