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

pegasus-loader

v1.1.0

Published

Project Structure based Route declarations for React Router and Webpack

Downloads

338

Readme

pegasus-loader

Project Structure based Route declarations for React Router using Webpack

Purpose

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.

How to use

This assumes that you have a Webpack project with React Router set up. Please use torontojs.com's Repo as an example.

  1. Install it and its dependencies from npm:

    npm install --save pegasus-loader

  2. 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");
  3. Use the routingConfiguration in React Router's Router:

    <Router>
      {routingConfiguration}
    </Router>
  4. Sit back and let pegasus-loader maintain your routes!

Ok, so how do I need to arrange my files and folders?

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.

Hmm but what about dynamic segments?

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".

Advanced features

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.

Example App

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.

Disussion

Join the TorontoJS Slack chat in the #help channel.

Licence

MIT