Imports & Bundle Size
Imports
There are two approaches to import Modules in Ox:
- Named Imports: Importing modules via the root
ox
namespace. - Entrypoint Imports: Importing modules via an
ox/{Module}
namespace.
Named Imports
Modules can be imported via their respective module export in the root ox
namespace:
import { Hex, Rlp } from 'ox'
const rlp = Rlp.fromHex([Hex.fromString('hello'), Hex.fromString('world')])
This approach does not compromise on tree-shakability, as most modern bundlers support Deep Scope Analysis. As a result, this will not impact the bundle size of your application.
Bundlers known to support Deep Scope Analysis include: Vite, Rollup, Webpack 5+, esbuild, swc, and more.
Entrypoint Imports
If your bundler does not support Deep Scope Analysis, you are also able to import modules via their respective entrypoint:
import * as Hex from 'ox/Hex'
import * as Rlp from 'ox/Rlp'
const rlp = Rlp.fromHex([Hex.fromString('hello'), Hex.fromString('world')])
Tree Shakability & Bundle Size
Each Module in Ox exports a number of functions (e.g. Hex
exports from
, concat
, padLeft
, etc). It is important to note that Modules are not stateful instances with methods (ie. you cannot instantiate a Hex
class/object), they are merely a collection of pure stateless functions. This is because function exports are tree-shakable, whereas instance methods are not.
When Modules are imported in Ox, only the functions that you use from that Module will be included in the final bundle of your application. Unused functions are automatically removed, resulting in a lower bundle size.
Whereas, methods that are attached to instances cannot be tree-shaken by bundlers, which will lead to all methods of a given instance being included in the bundle, regardless of whether they are used or not.