UNPKG

react-static-webpack-plugin

Version:

Build full static sites using React, React Router and Webpack

60 lines (49 loc) 1.77 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAllPaths = exports.getNestedPaths = undefined; var _lang = require('lodash/lang'); var _array = require('lodash/array'); /** * This is not a very sophisticated checking method. Assuming we already know * this is either a Route or an IndexRoute under what cases would this break? */ var isIndexRoute = function isIndexRoute(route) { return (0, _lang.isUndefined)(route.props.path); }; /** * NOTE: We could likely use createRoutes to our advantage here. It may simplify * the code we currently use to recurse over the virtual dom tree: * * import { createRoutes } from 'react-router'; * console.log(createRoutes(routes)); => * [ { path: '/', * component: [Function: Layout], * childRoutes: [ [Object], [Object] ] } ] * * Ex: * const routes = ( * <Route component={App} path='/'> * <Route component={About} path='/about' /> * </Route> * ); * * getAllPaths(routes); => ['/', '/about] */ var getNestedPaths = exports.getNestedPaths = function getNestedPaths(route) { var prefix = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; if (!route) return []; if ((0, _lang.isArray)(route)) return route.map(function (x) { return getNestedPaths(x, prefix); }); // Index routes don't represent a distinct path, so we don't count them if (isIndexRoute(route)) return []; var path = prefix + route.props.path; var nextPrefix = path === '/' ? path : path + '/'; return [path].concat(getNestedPaths(route.props.children, nextPrefix)); }; var getAllPaths = exports.getAllPaths = function getAllPaths(routes) { return (0, _array.flattenDeep)(getNestedPaths(routes)); }; exports.default = getAllPaths;