react-wildcat-ensure
v6.2.0
Published
An asynchronous System.import helper.
Downloads
47
Keywords
Readme
react-wildcat-ensure
A wrapper for System.import
that behaves like Webpack's require.ensure
:
- initial import calls are asynchronous
- subsequent import calls returns a synchronous cached import response
Designed for compatibility with React Router's asynchronous route loading.
Installation
jspm:
jspm install react-wildcat-ensure
npm:
npm install react-wildcat-ensure
Usage
Importing a single module:
import ensure from "react-wildcat-ensure";
// Lazy loaded component
export function getComponent(location, cb) {
ensure("./AsyncComponent.js", module, (err, module) => {
return cb(err, module);
});
}
Importing a key/value hash of modules:
import ensure from "react-wildcat-ensure";
// Lazy loaded index route
export function getIndexRoute(location, cb) {
ensure({
header: "./AsyncHeader.js",
component: "./AsyncComponent.js"
}, module, (err, modules) => {
return cb(err, modules);
});
}
Importing an array of modules:
import ensure from "react-wildcat-ensure";
// Lazy loaded child routes
export function getChildRoutes(location, cb) {
ensure([
"./AsyncChildRouteOne.js",
"./AsyncChildRouteTwo.js"
], module, (err, modules) => {
return cb(err, modules);
});
}
License
MIT