Giter Site home page Giter Site logo

grayyang / react-router-config-loader Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 73 KB

webpack loader transforms plain react router configuration object defined in json/yaml file into react-router-config js module.

License: MIT License

JavaScript 100.00%
react-router-v4 react-router-config webpack-loader react

react-router-config-loader's Introduction

react-router-config-loader

webpack loader transforms plain react router configuration object defined in json/yaml file into react-router-config js module.

npm version Build Status Coverage Status Greenkeeper badge

Motivation

react-router-config provides one centralized routes configuration within react. However, the requirement to reference component classes directly in routes configuration limits its usage.

react-router-config-loader provides the option to define plain react routes configuration object in json/yaml file, by tramsforming path of component class into class reference and module import with the help of webpack.

By removing direct reference to component classes and code transformation, it opens much more possibile usage of the routes configuration, including but no limited to:

  • Routes definition with no dependency, which can provides single source of truth of the routes to multiple modules, e.g. react SPA and express.js backend
    • Ability to check validation of URL path in backend
    • Ability to trace route of URL path in backend
  • Customization to route configuration object, which can adds additional features to react-router-config
    • Support to use relative path in routes configuration
    • Support inherit properties available in child routes automatically

Installation

Use npm install to add devDependencies:

$ npm install --save-dev react-router-config-loader

Usage

react-router-config-loader supports loading react-router configuration defined in json, yaml, and js file. Below configuration samples correspond to the sample provided by react-router-config.

JSON

Inline usage of loader is given in below examples. For other ways, refer to webpack documents for detail.

main.js

import routes from 'react-router-config-loader!./routes.json';

routes.json

[
  { 
    "component": "./Root",
    "routes": [
      { 
        "path": "/",
        "exact": true,
        "component": "./Home"
      },
      { 
        "path": "/child/:id",
        "component": "./Child",
        "routes": [
          { 
            "path": "/child/:id/grand-child",
            "component": "./GrandChild"
          }
        ]
      }
    ]
  }
]

YAML

Chaining yaml-loader before react-router-config-loader to transform routes defined in yaml file.

main.js

import routes from 'react-router-config-loader!yaml-loader!./routes.yaml';

routes.yaml

- component: ./Root
  routes:
    - component: ./Home
      path: /
      exact: true
    - component: ./Child
      path: /child/:id
      routes:
        - component: ./GrandChild
          path: /child/:id/grand-child

JS

Routes configuration object can also be defined and exported from an js file (currently only using module.exports are supported).

main.js

import routes from 'react-router-config-loader!./routes.js';

routes.js

module.exports = [
  { component: './Root',
    routes: [
      { path: '/',
        exact: true,
        component: './Home'
      },
      { path: '/child/:id',
        component: './Child',
        routes: [
          { path: '/child/:id/grand-child',
            component: './GrandChild'
          }
        ]
      }
    ]
  }
]

Route Configuration

Both configuration fields defined in react-router-config, and additional fields including componentName, inheritProps are supported.

component: string

The core difference between react-router-config configuration and react-router-config-loader configuration is that, the component field is path from which to resolve the react component class instead of the component class.

component supports relative path (and absolute path) to resolve local components, and module path to resolve components from node modules. By default, relative path will be resolved using the context path unless componentsDir options is set.

If componentName field is not specified, the file name (without extention) is used as the name of the component.

componentName: string

Once specified, the value of componentName will be used as the import name of the component.

path: string

Absolute path-to-regrex path used to match for the component if relativePath is not set. Otherwise, relative path should be used.

exact: boolean

When true, will only match if the path matches the URL exactly. See https://reacttraining.com/react-router/core/api/Route/exact-bool

strict: bool

When true, a path that has a trailing slash will only match a URL with a trailing slash. See https://reacttraining.com/react-router/core/api/Route/strict-bool.

inheritProps: object

Object whose properties will be assigned to both the current route, and all its children. Which will be available through the props.route. For example,

[
  { 
    "component": "./Root",
    "routes": [
      { 
        "path": "/",
        "exact": true,
        "component": "./Home"
      },
      { 
        "path": "/child/:id",
        "component": "./Child",
        "parentProp": "parentProp",
        "inheritProps": {
          "inheritProp": "inheritProp"
        },
        "routes": [
          { 
            "path": "/child/:id/grand-child",
            "component": "./GrandChild",
            "selfProp": "selfProp"
          }
        ]
      }
    ]
  }
]

will get below props.route for GrandChild component:

{
  component: GrandChild,
  path: '/child/:id/grand-child',
  selfProp: 'selfProp',
  inheritProp: 'inheritProp',
}

Other fields

Other fields are free to add into route object, which is also available via props.route inside the component.

Loader Options

Several options controls the behavior of the loader on how routes configuration object is transformed.

componentsDir: string

Once specified, this directory will be used as the context path with with component path are resolved.

relativePath: boolean

Once set to true, all path in route are treated as relative path to its parents. For example, below is the corresponding configuration when relativePath equals true.

[
  { 
    "component": "./Root",
    "routes": [
      { 
        "path": "/",
        "exact": true,
        "component": "./Home"
      },
      { 
        "path": "/child/:id",
        "component": "./Child",
        "routes": [
          { 
            "path": "grand-child",
            "component": "./GrandChild"
          }
        ]
      }
    ]
  }
]

react-router-config-loader's People

Contributors

grayyang avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

peet86

react-router-config-loader's Issues

路由嵌套问题

新版的react-router-config是不支持二级路由嵌套了吗~routes这个参数貌似设置了没用

An in-range update of js-promisify is breaking the build 🚨

Version 1.3.0 of js-promisify was just published.

Branch Build failing 🚨
Dependency js-promisify
Current Version 1.2.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

js-promisify is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v1.3.0

1.3.0 (2018-05-23)

Features

Commits

The new version differs by 1 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of eslint is breaking the build 🚨

Version 4.15.0 of eslint was just published.

Branch Build failing 🚨
Dependency eslint
Current Version 4.14.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

eslint is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • coverage/coveralls Coverage pending from Coveralls.io Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v4.15.0
  • 6ab04b5 New: Add context.report({ messageId }) (fixes #6740) (#9165) (Jed Fox)
  • fc7f404 Docs: add url to each of the rules (refs #6582) (#9788) (Patrick McElhaney)
  • fc44da9 Docs: fix sort-imports rule block language (#9805) (ferhat elmas)
  • 65f0176 New: CLIEngine#getRules() (refs #6582) (#9782) (Patrick McElhaney)
  • c64195f Update: More detailed assert message for rule-tester (#9769) (Weijia Wang)
  • 9fcfabf Fix: no-extra-parens false positive (fixes: #9755) (#9795) (Erin)
  • 61e5fa0 Docs: Add table of contents to Node.js API docs (#9785) (Patrick McElhaney)
  • 4c87f42 Fix: incorrect error messages of no-unused-vars (fixes #9774) (#9791) (akouryy)
  • bbabf34 Update: add ignoreComments option to indent rule (fixes #9018) (#9752) (Kevin Partington)
  • db431cb Docs: HTTP -> HTTPS (fixes #9768) (#9768) (Ronald Eddy Jr)
  • cbf0fb9 Docs: describe how to feature-detect scopeManager/visitorKeys support (#9764) (Teddy Katz)
  • f7dcb70 Docs: Add note about "patch release pending" label to maintainer guide (#9763) (Teddy Katz)
Commits

The new version differs by 14 commits.

  • e14ceb0 4.15.0
  • 2dfc3bd Build: changelog update for 4.15.0
  • 6ab04b5 New: Add context.report({ messageId }) (fixes #6740) (#9165)
  • fc7f404 Docs: add url to each of the rules (refs #6582) (#9788)
  • fc44da9 Docs: fix sort-imports rule block language (#9805)
  • 65f0176 New: CLIEngine#getRules() (refs #6582) (#9782)
  • c64195f Update: More detailed assert message for rule-tester (#9769)
  • 9fcfabf Fix: no-extra-parens false positive (fixes: #9755) (#9795)
  • 61e5fa0 Docs: Add table of contents to Node.js API docs (#9785)
  • 4c87f42 Fix: incorrect error messages of no-unused-vars (fixes #9774) (#9791)
  • bbabf34 Update: add ignoreComments option to indent rule (fixes #9018) (#9752)
  • db431cb Docs: HTTP -> HTTPS (fixes #9768) (#9768)
  • cbf0fb9 Docs: describe how to feature-detect scopeManager/visitorKeys support (#9764)
  • f7dcb70 Docs: Add note about "patch release pending" label to maintainer guide (#9763)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.