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

Entry point and bundling changes #368

Merged
merged 16 commits into from
Aug 29, 2024
Merged

Entry point and bundling changes #368

merged 16 commits into from
Aug 29, 2024

Conversation

elboletaire
Copy link
Member

@elboletaire elboletaire commented Mar 26, 2024

This PR leaves the bundle exactly identical as the one we're currently generating, Now the bundle is a 98% of its original size! And also this PR brings some improvements to the way we were bundling the SDK:

Bundling changes

The external packages are now all taken from package.json dependencies field. The only exception is @vocdoni/proto/vocdhain, since the import does not match with the field in package.json.

Other than that, right now deciding if a dependency should be bundled or not is just a matter of placing it in devDependencies or dependencies.

Also, sourcemap generation has been set to be only done if NODE_ENV == 'development', since the final npm package should not have sourcemaps. Rollup.js' documentation is not very clear about what node env is being used during --watch mode, so right now it may be causing the same effect as having it set to false.

Anyway, since in this PR we change the entrypoint to be the ts files directly, there should be no need for sourcemaps.

I ended up disabling sourcemaps, due to the previous comment (you're live-building the ts files, your dev server should handle these sourcemaps).

Dramatic size change

The huge size change in the bundle is due mostly to the fact that we were bundling everything in our package in order to ensure people didn't had issues, specially from browser environments (since some of the libraries used were made only with nodejs in mind). Now, some of these dependencies, like snarkjs or circomlibjs, that were bundled in the package have been added to the dependencies array.

This causes the bundling process to ignore these packages during bundling, but also ensures people installing the SDK will properly install the dependencies as we typed them in our package.json file.

Entry point changes

The entry point has been changed to src/index.ts. This is done this way because, while developing, having the typescript as an entry point allows us to link the SDK to other projects and directly make changes to the SDK without requiring us to build in-between. This, of course, requires the end-developer to be bundling with something that is typescript compatible.

Since we don't need to build in-between anymore, the prepare npm script has been removed too.

To properly have the entry points defined for the npm package, a prepack script calling clean-package has been added. This script will ensure to modify the package.json file and add the entries required to access the bundled packages (exactly as we had it).

Note: due to this prepack script, running yarn pack locally will result in a modified package.json that should not be committed, ever. A postpack script ensures to leave your package.json file like it was before the pack process.

Things I think this PR may address

We had issues in the past dealing with packages like axios, and I think that was due to the confusing way we were defining external packages, and because we were (accidentally) trying to bundle it into our SDK. Axios should not be bundled into our final package, instead, it should be defined as a dependency. Doing it this way, and given the changes done to the bundling configuration, this package shouldn't give any problems whatsoever.

Note: peerDependencies were not considered into the bundling because we're not using them here (and I don't see a reason for it tbh, although you may see some of my commits trying related things, but ended up all removed).

Forks

In order to make this new bundling work as we really wanted to, I had to fork and change how some packages where being bundled. This was required because these packages are mostly abandoned, not receiving updates since years ago, and accepting PRs with the same intensity (none).

Here's the list of packages that I had to fork and the changes I had to make to them:

  • snarkjs: we already had this forked, so I just created a branch updating package versions. ffjavascript received an update and I had to ensure that was the version used everywhere, to avoid using overrides/resolutions.
  • circomlibjs: I installed buffer so the package was able to properly build versions for both node and the browser.
  • blake-hash: same as with circomlibjs, I had to install the buffer package and use it where needed.

Note all forks have an f/browser branch, where I'm pointing to.

List of checks I'd like to review if they work:

  • Adding the package via tgz file (resulting from yarn pack)
  • " via yarn link
  • " via git direct link in package.json
  • " via the yarn short way github_username/repo_name (this actually is new, the old bundling didn't allow us to use this because it does not run the prepare script this way)

Copy link

github-actions bot commented Mar 26, 2024

size-limit report 📦

Path Size
dist/index.js 40.56 KB (-97.55% 🔽)
dist/index.mjs 40.2 KB (-97.57% 🔽)
dist/index.umd.js 41.23 KB (-97.51% 🔽)

@elboletaire
Copy link
Member Author

elboletaire commented Mar 26, 2024

I'll need to review how the import checks are done too, since I changed the entry points.

Edit: I simply forgot about updating the github actions and run a required yarn build (not required previously due to the prepare script now removed).

@elboletaire elboletaire marked this pull request as ready for review August 7, 2024 14:48
@elboletaire elboletaire marked this pull request as draft August 7, 2024 15:08
@elboletaire
Copy link
Member Author

Set back to draft because I've broken something related to types that I need to check.

@elboletaire
Copy link
Member Author

Set back to draft because I've broken something related to types that I need to check.

Ok this is an issue I didn't count on. The problem here is the SDK is using strict: false, while the project that started crashing (ui-scaffold, but any other in vocdoni importing the SDK works the same) is using strict: true, so all the errors I've seen were related to this.

I also found an issue with how the exports for protobuf where being treated, that's why I created a new dvote-proto version and updated it here in this PR.

@elboletaire elboletaire marked this pull request as ready for review August 7, 2024 16:55
Copy link
Member

@emmdim emmdim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did anonymous and more E2E tests and everything seems to work fine.
Congrats for the great result.
LGTM.

@marcvelmer marcvelmer merged commit 5156639 into main Aug 29, 2024
9 of 10 checks passed
@marcvelmer marcvelmer deleted the f/rebundling branch August 29, 2024 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDK Bundle
3 participants