ckb-js / lumos

A full featured dapp framework for Nervos CKB
https://lumos-website.vercel.app/
MIT License
67 stars 53 forks source link

Reduce the Lumos bundle size #516

Open homura opened 1 year ago

homura commented 1 year ago

Background

We've received some feedback from community developers that Lumos is quite large when bundled, which is not friendly for mobile devices

Bundle Analyzer Output

image

stats.json.zip lumos@487f3235fcfc83409f7752f94962e343684686b5 uncompressed version: 1.7 MB

Plan

homura commented 1 year ago

I encountered some difficulties when refactoring the createCipheriv in @ckb-lumos/hd, because I found that the brwoserify-cipher has a dependency on the browserify-aes, and browserify-aes still has a dependency on the safe-buffer, which may not help reduce the bundle size.

During the exploration, I found that we could refactor with the crypto.subtle, however, most of the subtle API are asynchronous and incompatible with the current lumos synchronous APIs, and we need to evaluate how we should move forward with this part of the work.

homura commented 1 year ago

https://github.com/homura/lumos/pull/13

homura commented 1 year ago
image

stats.json.zip lumos@82b2a9637e95175101dca3361696e26c3e39989e uncompressed version: 1.5 MB

Refactored the require("crypto") to import by demand, reduced about 11.7% bundle size.

This modification also brings more cross-platform benefits to Lumos, so if Lumos needs to be compatible with more JavaScript runtimes in the future, it can be done by implementing the interface in @ckb-lumos/crypto

homura commented 1 year ago
image

stats.json.zip

lumos@5c6be0ad2f37373e3293600bc15b2837cd581e1c uncompressed version: 1.31 MB

homura commented 1 year ago

The ES module can be tracked in this issue https://github.com/ckb-js/lumos/issues/528