starknet-io / starknet.js

JavaScript library for StarkNet
https://www.starknetjs.com
MIT License
1.23k stars 755 forks source link

Better exports #1241

Open wighawag opened 1 month ago

wighawag commented 1 month ago

Is your feature request related to a problem? Please describe. Currently starknet.js export many of its utilities as object, which prevent best dead code elimination

Describe the solution you'd like It would be better to expose the utils via package.json exports field

so if you were to export num utils this way in your package.json:

{
  "name": "starknet",
   ...
  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.js"
      }
    },
    "./utils": {
      "import": {
        "types": "./dist/utils/index.d.ts",
        "default": "./dist/utils/index.js"
      }
    },
    "./utils/num": {
      "import": {
        "types": "./dist/utils/num.d.ts",
        "default": "./dist/utils/num.js"
      }
    },
  },
...
}

we would then be able to import it this way and benefit from dead code elimination if we were to only use part of the num exports:

import {toBigInt} from 'starknet/utils/num';

Also another bonus is that it will let us access types defined there too

od-hunter commented 1 month ago

Hi @wighawag , can I be assigned this please? I’m ready to work

Benjtalkshow commented 1 month ago

hello @wighawag Can i be assigned to this task? I propose restructuring Starknet.js to export utilities more modularly by utilizing the package.json exports field. This change will allow for better dead code elimination and enhance the ability to import only the needed utilities.

The benefit of doing this are:

  1. By exposing individual utilities through the exports field, unused code can be excluded from the final bundle, resulting in smaller and more optimized builds.
  2. Type-Safe Imports: Developers will be able to directly access types from each utility, improving code clarity and the developer experience, especially in TypeScript environments.
0xdevcollins commented 1 month ago

@wighawag can I be assigned this task? I'll deliver as soon as possible

To tackle this task, I’d start by updating the package.json to configure exports for modular access. This involves restructuring the exports field so that individual utility functions, like those in utils and specifically num, are accessible as separate entry points. This setup will allow for better dead code elimination by letting users import only the utilities they need, such as toBigInt from starknet/utils/num, which reduces bundle size and improves performance. After setting up the exports, I’d test the changes by importing functions selectively in a test environment to confirm that only the required code is included in the output bundle. Additionally, I’d verify that type declarations are correctly accessible to ensure a seamless developer experience.