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-chunkable

v2.0.1

Published

💥 Simplest way to code split and load async chunks using Webpack 2 and React Router V4

Downloads

10

Readme

react-chunkable 🍕

Simplest way to code split and load async chunks

Prerequisite

Webpack 2.x.x/3.x.x
React Router 4.x.x

Installation

yarn add react-chunkable

or

npm install react-chunkable --save

Usage

Load as you go. In a traditional SPA, Instead of downloading the entire bundle on the client side, split your code which makes your user to download the piece of code as and when required. Code splitting is a Webpack feature that enables a JS bundle within a single build to be split up and loaded on-demand in smaller parts.

Without Code Splitting

The entire app is bundled in a single js file i.e HomePage and ProfilePage are loaded at the first start itself, though the ProfilePage is not required at the first start.

import HomePage from "./pages/home";
import ProfilePage from "./pages/profile";

class Routes extends PureComponent {
  render() {
    return (
      <Router>
        <Switch>
          <Route exact={true} path="/" component={props => <HomePage {...props} />} />
          <Route path="/profile/:username" component={props => <ProfilePage {...props} />} />
        </Switch>
      </Router>
    );
  }
}

With Code Splitting

With Code Splitting only the HomePage is loaded at the first load. When the user visits ProfilePage at that time the chunk for ProfilePage will be loaded.

import ComponentChunk from "react-chunkable";

const HomePage = props =>
  (<ComponentChunk
    componentProps={props}
    loadChunk={import(/*  webpackMode: "lazy",webpackChunkName: "home" */ "./pages/home")}
  />);

const ProfilePage = props =>
  (<ComponentChunk
    componentProps={props}
    loadChunk={import(/*  webpackMode: "lazy",webpackChunkName: "profile" */ "./pages/profile")}
  />);

class Routes extends PureComponent {
  render() {
    return (
      <Router>
        <Switch>
          <Route exact={true} path="/" component={props => <HomePage {...props} />} />
          <Route path="/profile/:username" component={props => <ProfilePage {...props} />} />
        </Switch>
      </Router>
    );
  }
}

Magic Comments

Webpack 2.4.0 introduced one feature called "magic comments". Now you can name the chunks corresponding to the modules/components you import.

import(/* webpackMode: "lazy",webpackChunkName: "profile" */ "./pages/profile") this statement indicates webpack to consider this as a split point and load it in a separate bundle.

webpackMode: "lazy" indicates webpack to lazily load this chunk.

webpackChunkName: "profile" allows you to name your chunk.

More Info on other available options

Production

In the project directory, you can run: yarn build Builds the library for production to the dist folder. It correctly bundles React in production mode and optimizes the build for the best performance.

Example

Check out the example applications to see how simple this is.

SSR?

Well, really do you need it? Check this

Support

Please open an issue for support.

Like it?

:star: this repo

License

MIT © Kamlesh Chandnani