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

@sector-labs/react-loadable-revised

v1.7.0-sl2

Published

A higher order component for loading components with promises

Downloads

4

Readme

A bug-free and actively maintained version of react-loadable

Check the old readme here.

The new package name: @react-loadable/revised.

Background

There are several bugs in the original react-loadable package. The author abandoned it a long time ago. This is a revised and actively maintained version of the original package.

Usage

Exported APIs

import babelPlugin from '@react-loadable/revised/babel'
import {ReactLoadablePlugin} from '@react-loadable/revised/webpack'
import loadable, {preloadReady, preloadAll, Capture} from '@react-loadable/revised'
import {Capture} from '@react-loadable/revised'
  • babelPlugin: babel plugin.
  • ReactLoadablePlugin: webpack plugin.
  • loadable: the main component to wrap your component.
  • preloadReady: to load the pre-loaded components, used in client.
  • preloadAll: to load all, used in server.
  • getBundles: determine which bundles are required.
  • Capture: the wrapper context, used in server, to capture the pre-loaded components.

Babel config

Include '@react-loadable/revised/babel' in your babel plugin list. This is required for both client and server builds.

In .babelrc:

{
	"plugins": [
		["@react-loadable/revised/babel", { "absPath": true }]
	]
}

The babel plugin finds all calls to loadable({loader() {}, ...}), scans all import() call in loader's body, and inject the module identifiers to the object passed to loadable() for later uses.

For example, it transforms:

loadable({
	loader() {
		return import('./ExampleNested')
	},
	loading: Loading,
})

into:

loadable({
	loader() {
		return import('./ExampleNested')
	},
	modules: ['./ExampleNested'],
	webpack() {
		return [require.resolveWeak('./ExampleNested')] // will be evaluated by Webpack to ['./example/components/ExampleNested.js']
	},
	loading: Loading,
})

Webpack config

Webpack plugin is required only in your client build. Include it in the webpack config's plugin list.

See example

const {writeFile} = require('fs/promises')

plugins: [
	new ReactLoadablePlugin({
		async callback(manifest) {
			// save the manifest somewhere to be read by the server
			await writeFile(
				path.join(__dirname, 'dist/react-loadable.json'),
				JSON.stringify(manifest, null, 2)
			)
		},
		absPath: true,
	}),
]

In react code

Wrap your split components with loadable({loader() {}, ...}) to get the lazily loadable component. Sample code:

const LoadableNested = loadable({
	loader() {
		return import('./ExampleNested')
	},
	loading: Loading
})

Note: you must call loadable({...}) at the top-level of the module. Otherwise, make sure to call them all (via importing) before calling preloadAll() or preloadReady().

In server side

await preloadAll()
app.listen(3000, () => {
	console.log('Running on http://localhost:3000/')
})
  • Load the exported react-loadable.json file, which is generated by the webpack plugin, to get the manifest.
// in production, this should be cached in the memory to reduce IO calls.
const getStats = () => JSON.parse(fs.readFileSync(path.resolve(__dirname, 'dist/react-loadable.json'), 'utf8'))
  • Wrap the rendered component with Capture to capture the pre-loaded components.

See example

const modules = [] // one list for one request, don't share
const body = ReactDOMServer.renderToString(
	<Capture report={moduleName => modules.push(moduleName)}>
		<App/>
	</Capture>
)
  • After rendering the component, use getBundles() to determine which bundles are required.
const {assets, preload, prefetch} = getBundles(getStats(), modules)
  • Inject the required bundles and rendered body to the html document and returns to the client.
const Links = ({assets, prefetch}) => {
	const urls = assets.filter(file => file.endsWith('.css'))
	return prefetch
		? urls.map((url, index) => <link rel={prefetch} as="style" href={url} key={index}/>)
		: urls.map((url, index) => <link rel="stylesheet" href={url} key={index}/>)
}
const Scripts = ({assets, prefetch}) => {
	const urls = assets.filter(file => file.endsWith('.js'))
	return prefetch
		? urls.map((url, index) => <link rel={prefetch} as="script" href={url} key={index}/>)
		: urls.map((url, index) => <script src={url} key={index}/>)
}
const Html = ({assets, body, preload, prefetch}) => {
	return <html lang="en">
		<head>
			<meta charSet="UTF-8"/>
			<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
			<title>My App</title>
			<Links assets={assets}/>
			<Links assets={preload} prefetch="preload"/>
			<Links assets={prefetch} prefetch="prefetch"/>
			<Scripts assets={preload} prefetch="preload"/>
			<Scripts assets={prefetch} prefetch="prefetch"/>
		</head>
		<body>
			<div id="app" dangerouslySetInnerHTML={{__html: body}}/>
			<Scripts assets={assets}/>
		</body>
	</html>
}

// note: use renderToStaticMarkup, NOT renderToString()
res.send(`<!doctype html>
${ReactDOMServer.renderToStaticMarkup(<Html
	assets={assets}
	body={body}
	preload={preload}
	prefetch={prefetch}
/>)}`)

In client side

  • Call and await for preloadReady() before hydration. Example
await preloadReady()
ReactDOM.hydrate(<App/>, document.getElementById('app'))

API reference

Babel plugin

  • Default import from @react-loadable/revised/babel
  • Option: {shortenPath?: string, absPath?: boolean}

For example: the project root dir is /home/my-project. In example/Example.js, there is import(./nested/ExampleNested).

  • {absPath: false}: shortenPath is ignored. Module identifier becomes './nested/ExampleNested'.

Note: the server will not be able to distinguish if two modules have the same relative import path. It will load both of them.

  • {absPath: true, shortenPath: undefined}: Module identifier becomes '/home/my-project/example/nested/ExampleNested'.

Note: this will make your build less portable because the module identifier will be different in different environments.

  • {absPath: true, shortenPath: ''}: Module identifier becomes '/example/nested/ExampleNested'.
  • (recommended) {absPath: true, shortenPath: '~'}: Module identifier becomes '~/example/nested/ExampleNested'.

Note: this requires the accompanied from the webpack plugin configuration.

Webpack plugin

The webpack plugin ReactLoadablePlugin has the following options:

class ReactLoadablePlugin {
	constructor(options: {
		callback(manifest: LoadableManifest): any
		moduleNameTransform?(moduleName: string): string
		absPath?: boolean
	})
}
  • absPath: should be true if absPath is true in the babel plugin option.
  • moduleNameTransform?(moduleName: string): string: take the module name (absolute path if absPath is true) and return the transformed path. If shortenPath is '~' in the babel plugin option. Use the following implementation:
{
	moduleNameTransform(moduleName)
	{
		return moduleName?.startsWith(rootDir)
			? `~${moduleName.slice(rootDir.length)}`
			: moduleName
	}
}
  • callback(manifest: LoadableManifest): any: this callback should store the manifest somewhere for the server to use.

loadable(opts)

Where opts's interface is

{
	loader(): Promise<T>
	loading: Component<{
		error?: Error
		retry(): any
	}>
	render?(loaded: T, props: P): ReactElement
}

The loading component should accept 2 props:

  • error?: Error: when error is undefined, the component is being loaded. Otherwise, there is an error. If the data is ready, this component will not be rendered.
  • retry(): any: to retry if there is an error.

The loader function should return a promise that resolves to the loaded data.

The render function is optional. If not specified, the default render function is used. The default render function is loaded => <loaded.default {...props}/>.

Other APIs

I recommend use the default option as mentioned in the How section.

  • getBundles(stats, modules, options)
    • returns {assets, preload, prefetch}. Where assets, preload, prefetch are the main assets, preload assets, prefetch assets, respectively.
    • options is an optional parameter with the following keys.
      • entries: string[] (default: ['main']). Name of the entries in webpack.
      • includeHotUpdate: boolean (default: false). Specify whether hot update assets are included.
      • includeSourceMap: boolean (default: false). Specify whether source maps are included.
      • publicPath: string (default: output.publicPath value in the webpack config). Overwrite the output.publicPath config.
      • preserveEntriesOrder: boolean (default: false). If true the javascript assets of the entry chunks will not be moved to the end of the returned arrays.

Note: if preserveEntriesOrder is set (true), to prevent the dynamically imported components (lodable components) from being loaded twice, the entry should be executed after everything is loaded.

The output assets are returned in the following orders unless the preserveEntriesOrder option is set.

  • Highest order (first elements): javascript assets which belong to at least one of the input entries (specified via the options parameter).
  • Lower order (last elements): javascript assets which belong to at least one of the input entries, but are not runtime assets.
  • All other assets' orders are kept unchanged.

Improved features from the original react-loadable

There are several changes in this package compared to the origin.

  • Support webpack 4 and webpack 5.
  • Support newer webpack's structure by loading assets from chunk groups, instead of from chunks.
  • Support preload, prefetch assets.
  • Filter hot update assets by default. This can be changed by options.
  • Simpler stats file format.
  • Rewritten in Typescript.
  • Converted to ES6 module.
  • Assets aer sorted in output.
  • Support short hand definition of loadable component definition.
  • Remove LoadableMap.
  • And many more...