UNPKG

react-router-dom

Version:

Declarative routing for React web applications

8 lines (7 loc) 2.68 kB
'use strict';Object.defineProperty(exports,"__esModule",{value:!0});function _interopDefault(e){return e&&"object"===typeof e&&"default"in e?e["default"]:e}var fs=_interopDefault(require("fs")),path=_interopDefault(require("path")),React=_interopDefault(require("react")),reactRouterDom=require("react-router-dom"),history=require("history"),PropTypes=_interopDefault(require("prop-types")); function StaticRouter({children:e,context:d={},location:b="/"}){function c(a,c){return{...g,..."string"===typeof a?history.parsePath(a):a,state:c}}"string"===typeof b&&(b=history.parsePath(b));let g={pathname:b.pathname||"/",search:b.search||"",hash:b.hash||"",state:b.state||null,key:b.key||"default"};return React.createElement(reactRouterDom.Router,{children:e,history:{get action(){return"POP"},get location(){return g},createHref(a){return"string"===typeof a?a:history.createPath(a)},push(a,b){a= c(a,b);"production"!==process.env.NODE_ENV&&(b=history.createPath(a),console.warn("You cannot perform a PUSH with a <StaticRouter>. You probably want a REPLACE instead."+`\n\nTo avoid this warning, find the element that is calling \`navigate("${b}")\``+` and change it to \`navigate("${b}", { replace: true })\`. This could also be`+` caused by rendering a \`<Navigate to={"${b}"} />\`. In that case, just add a `+"`replace={true}` prop to do a redirect instead."));d.url=history.createPath(a);d.state= a.state},replace(a,b){a=c(a,b);d.url=history.createPath(a);d.state=a.state},go(a){throw Error(`You cannot perform ${-1===a?"GO BACK":`GO(${a})`} on the `+"server because it is a stateless environment. This error was probably "+`triggered when you did a \`navigate(${a})\` somewhere in your app.`);},listen(){},block(){}}})} "production"!==process.env.NODE_ENV&&(StaticRouter.displayName="StaticRouter",StaticRouter.propTypes={children:PropTypes.node,location:PropTypes.oneOfType([PropTypes.shape({pathname:PropTypes.string,search:PropTypes.string,hash:PropTypes.string,state:PropTypes.object,key:PropTypes.string}),PropTypes.string])});function byFilesFirst(e,d){return e.isFile()?d.isFile()?0:-1:d.isFile()?1:0} function createRoutesFromFiles(e){let d=fs.readdirSync(e,{withFileTypes:!0});d.sort(byFilesFirst);let b=[];d.forEach(c=>{var d=c.name;let a=path.join(e,d),f=path.basename(d,path.extname(d)).replace(/\./g,"/").replace(/\$(\w+)/g,":$1");"index"===f&&(f="/");c.isFile()?(c=require(a),c.default&&(c=c.default),c=React.createElement(c),b.push({path:f,element:c})):c.isDirectory()&&(c=createRoutesFromFiles(a),(d=b.find(a=>a.path===f))?d.children=c:b.push({path:f,children:c}))});return b} exports.StaticRouter=StaticRouter;exports.createRoutesFromFiles=createRoutesFromFiles;