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

react-bndl

v1.0.2

Published

A library that allows users to split their React application into multiple bundles and load them each dynamically.

Downloads

3

Readme

react-bndl

A small package used to dynamically load React applications and / or components that are bundled independently. This package aims to fill a space where a user wants to control how their application is bundled and not be tied to any specific bundle tool.

Why

For most, using React.lazyload in combination with split chunks will be the preferable choice or utilizing the package loadable. However these methods rely on the bundler to split code into chunks by parsing dynamic import statements placed by a user to designate where code should be split.

react-bndl takes a different approach, allowing the user to create dedicated bundles using the compiling tools of their choice. Finally, bundles are loaded via the document script process, which allows for more complex management when it comes to splitting and loading application code.

Getting Started

There are two main pieces to react-bndl, BundleComponent and useBundle. Each has their own use case, but most will probably use BundleComponent.

Package installation:

npm install react-bndl

Component

BundleComponent is a react component that will load deployed react bundles asynchronously, displaying a loading state until completed. A custom loader can be supplied via LoaderComponent. The component also has an onError callback in the event loading the bundle encounters an error internally.

Example: Component + Custom Loader

import CircularProgress from '@mui/material/CircularProgress';
import Box from '@mui/material/Box';

import { BundleComponent } from 'react-bndl';

function App() {

  return (
    <>
      <BundleComponent
        path="./assets/home.js"
        name="@app/home"
        LoaderComponent={
          <Box sx={{ display: 'flex' }}>
            <CircularProgress />
          </Box>
        }
        onError={
          (error) => console.log(error)
        }
      />
    <>
  )
}

Example: React Router

import React from 'react';
import { Routes, Route } from 'react-router-dom';
import { BundleComponent } from 'react-bndl';

export function App() {
  return (
    <Routes>
      <Route path="about"
        element={
          <BundleComponent
            path="./assets/about.js"
            name="@app/about"
          />
        }
      />
      <Route path="/"
        element={
          <BundleComponent
            path="./assets/home.js"
            name="@app/home"
          />
        }
      />
    </Routes>
  );
};

Hook

The useBundle hook allows you to manage when each bundle is loaded, and the current state of loading them.

Example: Hook & DynamicComponent

import { DynamicComponent, useBundle } from 'react-bndl';

export function App() {
  const { isLoading, data, error } = useBundle({
    path: './assets/home.js',
    name:'@app/home'
  });

  useEffect(() => {
    if (error) {
      // Report error
    }

  }, [error]);

  return (
    <>
      {(isLoading && !data)?
        <div>Loading</div> :
        (!isLoading && error)?
          <div>{error.message}</div>:
          <DynamicComponent component={data}>
            Dynamic Component
          </DynamicComponent>
      }
    </>
  );
};

Development

A demo application was created to mimic the use cases for this package, it imports the library source code by using pnpm workspaces. All information related to the demo can be found here.

  • Requires NodeJS v18.16+ and PNPM v8.0+

Installation

Install all dependencies, create vendors bundle for demo application

pnpm install

Start

Run demo application in development mode to test react-bndl library

pnpm start

Scripts

  • start: Starts development server and builds demo application in development mode
  • start: Starts development server only
  • build:lib: Create production distribution that exports a CommonJS and ESM version of the react-bndl library
  • build:app: Create a production bundle of the demo application
  • fix:lint: Attempt to fix any lint errors
  • test:lint: Check lint in packages
  • test:types: Type check packages
  • test:component: Run Jest unit tests for lib/src
  • test:end-to-end: Run Playwright tests that utilize the demo application to mock a real implementation
  • test:integration: Run Playwright Component tests against the distribution of the react-bndl lib ESM file

Testing

For all unit and component testing, the React version of Testing Library is used. For end to end testing, and integration testing for the package distribution Playwright testing is leveraged. Playwright's experimental component testing is used as well, see more here.