|
1 | | -# react-router-config-loader |
| 1 | +# react-router-config-loader |
| 2 | +[webpack](https://webpack.js.org/) loader transforms plain react router configuration object defined in json/yaml file into [react-router-config](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config) js module. |
| 3 | + |
| 4 | +[](https://travis-ci.org/grayyang/react-router-config-loader) |
| 5 | +[](https://coveralls.io/github/grayyang/react-router-config-loader?branch=master) |
| 6 | +[](https://badge.fury.io/js/react-router-config-loader) |
| 7 | + |
| 8 | +## Motivation |
| 9 | +[react-router-config](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config) provides one centralized routes configuration within react. However, the requirement to reference component classes directly in routes configuration limits its usage. |
| 10 | + |
| 11 | +[react-router-config-loader](https://www.npmjs.com/package/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](https://webpack.js.org/). |
| 12 | + |
| 13 | +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: |
| 14 | +* 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 |
| 15 | + * Ability to check validation of URL path in backend |
| 16 | + * Ability to trace route of URL path in backend |
| 17 | +* Customization to route configuration object, which can adds additional features to react-router-config |
| 18 | + * Support to use relative path in routes configuration |
| 19 | + * Support inherit properties available in child routes automatically |
| 20 | + |
| 21 | +## Installation |
| 22 | +Use [npm](https://www.npmjs.com/) install to add devDependencies: |
| 23 | +```sh |
| 24 | +$ npm install --save-dev react-router-config-loader |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | +[react-router-config-loader](https://www.npmjs.com/package/react-router-config-loader) supports loading react-router configuration defined in json, yaml, and js file. Below configuration samples correspond to the [sample](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config#route-configuration-shape) provided by [react-router-config](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config). |
| 29 | + |
| 30 | +### JSON |
| 31 | +Inline usage of loader is given in below examples. For other ways, refer to webpack [documents](https://webpack.js.org/concepts/loaders/#using-loaders) for detail. |
| 32 | +#### main.js |
| 33 | +```js |
| 34 | +import routes from 'react-router-config-loader!./routes.json'; |
| 35 | +``` |
| 36 | +#### routes.json |
| 37 | +```json |
| 38 | +[ |
| 39 | + { |
| 40 | + "component": "./Root", |
| 41 | + "routes": [ |
| 42 | + { |
| 43 | + "path": "/", |
| 44 | + "exact": true, |
| 45 | + "component": "./Home" |
| 46 | + }, |
| 47 | + { |
| 48 | + "path": "/child/:id", |
| 49 | + "component": "./Child", |
| 50 | + "routes": [ |
| 51 | + { |
| 52 | + "path": "/child/:id/grand-child", |
| 53 | + "component": "./GrandChild" |
| 54 | + } |
| 55 | + ] |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | +] |
| 60 | +``` |
| 61 | + |
| 62 | +### YAML |
| 63 | +Chaining [yaml-loader](https://www.npmjs.com/package/yaml-loader) before [react-router-config-loader](https://www.npmjs.com/package/react-router-config-loader) to transform routes defined in yaml file. |
| 64 | +#### main.js |
| 65 | +```js |
| 66 | +import routes from 'react-router-config-loader!yaml-loader!./routes.yaml'; |
| 67 | +``` |
| 68 | +#### routes.yaml |
| 69 | +```yaml |
| 70 | +- component: ./Root |
| 71 | + routes: |
| 72 | + - component: ./Home |
| 73 | + path: / |
| 74 | + exact: true |
| 75 | + - component: ./Child |
| 76 | + path: /child/:id |
| 77 | + routes: |
| 78 | + - component: ./GrandChild |
| 79 | + path: /child/:id/grand-child |
| 80 | +``` |
| 81 | +
|
| 82 | +### JS |
| 83 | +Routes configuration object can also be defined and exported from an js file (currently only using module.exports are supported). |
| 84 | +#### main.js |
| 85 | +```js |
| 86 | +import routes from 'react-router-config-loader!./routes.js'; |
| 87 | +``` |
| 88 | +#### routes.js |
| 89 | +```js |
| 90 | +module.exports = [ |
| 91 | + { component: './Root', |
| 92 | + routes: [ |
| 93 | + { path: '/', |
| 94 | + exact: true, |
| 95 | + component: './Home' |
| 96 | + }, |
| 97 | + { path: '/child/:id', |
| 98 | + component: './Child', |
| 99 | + routes: [ |
| 100 | + { path: '/child/:id/grand-child', |
| 101 | + component: './GrandChild' |
| 102 | + } |
| 103 | + ] |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | +] |
| 108 | +``` |
| 109 | + |
| 110 | +## Route Configuration |
| 111 | +Both configuration fields defined in [react-router-config](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config), and additional fields including _componentName_, _inheritProps_ are supported. |
| 112 | + |
| 113 | +### component: string |
| 114 | +The core difference between [react-router-config](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config) configuration and [react-router-config-loader](https://www.npmjs.com/package/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. |
| 115 | + |
| 116 | +`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`](#componentsDir) options is set. |
| 117 | + |
| 118 | +If `componentName` field is not specified, the file name (without extention) is used as the name of the component. |
| 119 | + |
| 120 | +### componentName: string |
| 121 | +Once specified, the value of `componentName` will be used as the import name of the component. |
| 122 | + |
| 123 | +### path: string |
| 124 | +Absolute [path-to-regrex](https://www.npmjs.com/package/path-to-regexp) path used to match for the component if [`relativePath`](#relativePath) is not set. Otherwise, relative path should be used. |
| 125 | + |
| 126 | +### exact: boolean |
| 127 | +When `true`, will only match if the path matches the URL exactly. See https://reacttraining.com/react-router/core/api/Route/exact-bool |
| 128 | + |
| 129 | +### strict: bool |
| 130 | +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. |
| 131 | + |
| 132 | +### inheritProps: object |
| 133 | +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, |
| 134 | +```json |
| 135 | +[ |
| 136 | + { |
| 137 | + "component": "./Root", |
| 138 | + "routes": [ |
| 139 | + { |
| 140 | + "path": "/", |
| 141 | + "exact": true, |
| 142 | + "component": "./Home" |
| 143 | + }, |
| 144 | + { |
| 145 | + "path": "/child/:id", |
| 146 | + "component": "./Child", |
| 147 | + "parentProp": "parentProp", |
| 148 | + "inheritProps": { |
| 149 | + "inheritProp": "inheritProp" |
| 150 | + }, |
| 151 | + "routes": [ |
| 152 | + { |
| 153 | + "path": "/child/:id/grand-child", |
| 154 | + "component": "./GrandChild", |
| 155 | + "selfProp": "selfProp" |
| 156 | + } |
| 157 | + ] |
| 158 | + } |
| 159 | + ] |
| 160 | + } |
| 161 | +] |
| 162 | +``` |
| 163 | +will get below `props.route` for `GrandChild` component: |
| 164 | +```js |
| 165 | +{ |
| 166 | + component: GrandChild, |
| 167 | + path: '/child/:id/grand-child', |
| 168 | + selfProp: 'selfProp', |
| 169 | + inheritProp: 'inheritProp', |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### Other fields |
| 174 | +Other fields are free to add into route object, which is also available via `props.route` inside the component. |
| 175 | + |
| 176 | +## Loader Options |
| 177 | +Several options controls the behavior of the loader on how routes configuration object is transformed. |
| 178 | + |
| 179 | +### componentsDir: string |
| 180 | +Once specified, this directory will be used as the context path with with `component` path are resolved. |
| 181 | + |
| 182 | +### relativePath: boolean |
| 183 | +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`. |
| 184 | +```json |
| 185 | +[ |
| 186 | + { |
| 187 | + "component": "./Root", |
| 188 | + "routes": [ |
| 189 | + { |
| 190 | + "path": "/", |
| 191 | + "exact": true, |
| 192 | + "component": "./Home" |
| 193 | + }, |
| 194 | + { |
| 195 | + "path": "/child/:id", |
| 196 | + "component": "./Child", |
| 197 | + "routes": [ |
| 198 | + { |
| 199 | + "path": "grand-child", |
| 200 | + "component": "./GrandChild" |
| 201 | + } |
| 202 | + ] |
| 203 | + } |
| 204 | + ] |
| 205 | + } |
| 206 | +] |
| 207 | +``` |
0 commit comments