Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
pegasus-loader
Advanced tools
Project Structure based Route declarations for React Router and Webpack
Project Structure based Route declarations for React Router using Webpack
The purpose of pegasus-loader is to remove the need for writing route configuration by hand.
Many years ago, web developers simply put files in folders which described the structure of their applications. Web browsers would request each URL and the web server would translate this URL into a path on the file system.
Since then, web applications have gotten more complex and the need to support multiple HTTP verbs for a single URL became necessary. We had to start using a separate routing table to figure out where to send each request.
However these days, that type of complexity can be kept on the server where REST and GraphQL are the entry point. The entry point for client side applications are always based on a simple HTTP GET request.
Today with tools such as Webpack to enable asynchronous payload chunking and React Router to provide declarative component-based routing, it's possible to go back to simply putting files in folders and letting tools generate your routing configuration.
This assumes that you have a Webpack project with React Router set up. Please use torontojs.com's Repo as an example.
Install it and its dependencies from npm:
npm install --save pegasus-loader
Use pegasus-loader with a given base path to generate routing configuration:
// will generate routing configuration for the current directory
var routingConfiguration = require("pegasus!.");
// an example of routing configuration for a nested directory
var nestedRouteConfig = require("pegasus!./nestedDir");
// example of routing configuration for a node_module
var moduleRouteConfig = require("pegasus!./node_modules/exampleModule");
Use the routingConfiguration
in React Router's Router
:
<Router>
{routingConfiguration}
</Router>
Sit back and let pegasus-loader maintain your routes!
An example of this is available on torontojs.com's Repo.
In essence, for a given URL in your application, take the path and add /index.js
to determine the routable file.
For example, given the URL http://myapp.com/about/the_team
, the React component that will render is located at ./about/the_team/index.js
.
Dynamic segments are handled by using a @
instead of the :
in React Router's configuration.
Unfortunately the file system doesn't like the :
character. So I've opted to use a @
instead.
pegasus-loader will simply replace the @
with a :
in the path to your React component in order to handle dynamic segments.
For example, given the URL http://myapp.com/users/12345
, the React component located at ./users/@userID/index.js
will render with this.props.params.userID
set to "12345"
.
By having a declarative project-structure based routing system, the tooling can take over and do a lot for you.
pegasus-loader will automatically into React Router's Dynamic Routing hooks to enable lazy loading of Routable Components via Webpack asynchronous split points.
This means that just like in the early days when each page in our application was loaded on-demand by the browser, we can similarily load each page and construct your application incrementally.
The best part is that you don't have to do anything! Just put a file in a folder and the tooling will do the rest.
TorontoJS Website - A promotional website for TorontoJS to pull in JavaScript events from Toronto and the surrounding area. It also displays videos and general information about TorontoJS while serving as a base on which to expand upon. Uses pegasus-loader, Griffin.js, WebPack, React Router, Babel.js, and React Transform.
Join the TorontoJS Slack chat in the #help channel.
MIT
FAQs
Project Structure based Route declarations for React Router and Webpack
We found that pegasus-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.