npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

treeshakable

v0.0.1

Published

Enhance any state management library with full tree-shaking capabilities using this universal library

Downloads

10

Readme

Treeshakable

test Maintainability codecov Version Downloads npm bundle size Gitpod ready-to-code

Treeshakable

Treeshakable is a comprehensive library designed to unlock the full potential of React 18 server components. It enhances any state management library with full tree-shaking capabilities, ensuring efficient state management and reducing package size by facilitating shared state even when importing components from separate files. Additionally, it improves performance by avoiding redundant state creation steps.

If you're in need of basic global state management for your library, consider using "React18 Global Store", as its smaller npm bundle size npm bundle size will lead to improved performance and reduced overall bundle size.

Why Treeshakable?

In modern JavaScript applications, especially those using React, tree-shaking is a crucial optimization technique. Tree-shaking helps eliminate dead code, reducing the overall bundle size and improving load times. However, when importing components from separate files, state management libraries can create separate stores, leading to redundancy and increased package size. Additionally, the creation of multiple stores can break the functionality of the library, as different components or hooks imported from different files end up interacting with isolated stores. This issue is particularly prevalent with libraries built using Zustand and similar libraries.

Without Treeshakable:

  • Importing components from different files may lead to multiple instances of the same store.
  • This redundancy increases package size and memory usage.
  • It can degrade performance due to unnecessary state management overhead.
  • Importing components from different files may break functionality that depends on a central global store.

Treeshakable addresses these challenges by ensuring a single shared state across different imports, optimizing tree-shaking, and reducing overall package size.

For a live example demonstrating these concerns see the official website

✅ Universal Compatibility: Works with various state management libraries.

✅ Tree-Shaking Optimization: Enables full tree-shaking for efficient code splitting and reduced package size.

✅ Shared State Management: Facilitates shared state across different imports, preventing the creation of multiple stores.

✅ Create fully Treeshakable (import from treeshakable/client/loader-container)

✅ Fully TypeScript Supported

✅ Leverage the power of React 18 Server components

✅ Compatible with all React 18 build systems/tools/frameworks

✅ Documented with Typedoc (Docs)

✅ Examples for Next.js, Vite, and Remix

Please consider starring this repository and sharing it with your friends.

Getting Started

Installation

$ pnpm add treeshakable

or

$ npm install treeshakable

or

$ yarn add treeshakable

Usage

Here is a basic example of how to use Treeshakable with a state management library:

import treeshakable from "treeshakable";
import { create } from "zustand";

interface CounterState {
  count: number;
  setCount: (count: number) => void;
}

export const useTreeshakableCounterStore = treeshakable("counter-store", () =>
  create<CounterState>(set => ({
    count: 0,
    setCount: count => set({ count }),
  })),
);

In this example, Treeshakable is applied as a higher-order function to enhance the Zustand store with tree-shaking capabilities.

Why use treeshakable('my-store', () => createStore(...)) and not treeshakable('my-store', createStore(...))?

The distinction here is critical:

  • treeshakable('my-store', createStore(...)) would immediately invoke createStore and create the store instance during module initialization. This approach can defeat the purpose of tree-shaking because the store would be created regardless of whether it is used or not.
  • treeshakable('my-store', () => createStore(...)) passes a function that returns the store instance. This approach ensures the store creation happens only once, optimizing performance and reducing bundle size through lazy initialization.

For detailed API and options, refer to the API documentation.

Motivation

I developed Treeshakable after encountering issues with importing from specific folders for better tree-shaking, which resulted in the creation of separate Zustand stores and increased package size. Treeshakable addresses this by ensuring shared state across different imports, optimizing tree-shaking, and reducing overall package size.

Repo Stats

License

This library is licensed under the MPL-2.0 open-source license.

Please consider enrolling in our courses or sponsoring our work.