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-async-component-loader

v1.0.1

Published

Lazy load your components, wrapping them into preloader component

Downloads

4

Readme

react-async-component-loader

Webpack loader to lazy load your components, wrapping them into preloader component

Inspired by https://github.com/lavrton/Progressive-Web-App-Loading

Installation

npm install --save-dev react-async-component-loader

Basic usage

// HomePage.js

import HugeHeavyComponent from "react-async-component!./HugeHeavyComponent.js"

...

<HomePage>
  <HugeHeavyComponent />
</HomePage>

What it actually does?

It loads target module via bundle-loader with lazy option. The resulting chunk loader function is passed to a special component, which will call that function when mounted. And while chunk is loading, it will render a progress indicator of your choice.

Query parameters

name: String

It is passed directly to bundle-loader. Defines chunk name for your async module. Use it to group modules.

If not specified, each module goes to own chunk.

Example:

import MyComponent from "react-async-component?name=my-chunk!./MyComponent.js"

loader: String

Path to module, containing your preloader indicator component.

If not specified, just a div with "Loading" text will be rendered.

Path is resolved in exactly same way as any other module in project, i.e., depending on your webpack config. Which means, you can refer either your custom component or some package from node_modules – for example, use react-loader.

Example:

import MyComponent from "react-async-component?loader=./components/MySuperLoader!./MyComponent.js"

import MyComponent from "react-async-component?loader=react-loader!./MyComponent.js"

NOTE: On practice, unlikely you will use this inline option, as it's looks quite bad and hard to read. Also, unlikely you will ever have different preloaders for different modules, so you may want to once define a default preloader and forget about it. See how to do this below.

Global defaults

It is possible to define default options directly in webpack config. For this, add asyncComponentLoader section:

// webpack.config.js
module.exports = {
    asyncComponentLoader: {
        defaults: {
            loader: String, 

            // Note, that by defining this, you will put all async modules 
            // with no explicit name specified to the same chunk
            name: String, 
        },
    },
}

On my view, this section should be used to define default preloader component, and module.loaders section should be used to define name parameter for specific modules.

Example:

// webpack.config.js
module.exports = {
	module: {
		loaders: [
			{
				test: /\/containers\/Profile/,
				loader: "react-async-component",
				query: {
					name: "Profile",
				},
			},
			{
				test: /\/containers\/Inbox/,
				loader: "react-async-component",
				query: {
					name: "Inbox",
				},
			},
		],
	},

    asyncComponentLoader: {
        defaults: {
            loader: "react-loader", 
        },
    },
}

Caveats

You can't put same module in two different chunks, importing it with different name option. Webpack knows that it's the same module, and the first chunk name it met will be used in all other cases. I.e., with code like this:

// file1.js
import Component from "react-async-component?name=Foo!./Component";

// file2.js
import Component from "react-async-component?name=Bar!./Component";

// main.js
import "file1"
import "file2"

only Foo chunk will be created, and both file1 and file2 will refer to it.

Same for name, defined in module.loaders: inline option will override it.