From 9e2169a980ece227f33181e081bcf2ed1acda6bc Mon Sep 17 00:00:00 2001 From: syy11cn Date: Fri, 22 Oct 2021 15:35:01 +0800 Subject: [PATCH] fix: remove unused code --- LICENSE | 21 +++++++++++++++++++++ dist/src/index.js | 38 ++++++++++++++++++++++++++++++++++++++ dist/typings/routeType.js | 1 + package.json | 10 +++++++++- src/index.tsx | 15 +++++---------- tsconfig.json | 25 +++++++++++++------------ typings/routeType.ts | 2 +- 7 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 LICENSE create mode 100644 dist/src/index.js create mode 100644 dist/typings/routeType.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3464ce7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [2021] [Yiyang Sun] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/dist/src/index.js b/dist/src/index.js new file mode 100644 index 0000000..a3a06d9 --- /dev/null +++ b/dist/src/index.js @@ -0,0 +1,38 @@ +import { jsx as _jsx } from "react/jsx-runtime"; +import { Switch, useLocation, useRouteMatch, Route, Redirect, } from 'react-router-dom'; +const RouterView = ({ routes, onEnter, }) => { + const { pathname } = useLocation(); + const match = useRouteMatch(); + console.log('当前页面路径:' + pathname); + console.log('路径匹配', match); + let redirectPath = ''; + let agreeToGo = true; + function next(path) { + agreeToGo = true; + if (path) { + redirectPath = path; + } + } + onEnter(pathname, next); + return (_jsx(Switch, { children: routes.map((route, index) => { + // If auth needed. + if (route.auth) { + // If `next()` called. + if (agreeToGo) { + // Need to redirect. + if (redirectPath) { + return _jsx(Redirect, { to: redirectPath }, index); + } + else { + // OK to render. + return (_jsx(Route, { path: route.path, render: (props) => (_jsx(route.component, Object.assign({}, props, { routes: route.routes }), void 0)) }, index)); + } + } + } + else { + // No need for auth. + return (_jsx(Route, { path: route.path, render: (props) => (_jsx(route.component, Object.assign({}, props, { routes: route.routes }), void 0)) }, index)); + } + }) }, void 0)); +}; +export { RouterView }; diff --git a/dist/typings/routeType.js b/dist/typings/routeType.js new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/typings/routeType.js @@ -0,0 +1 @@ +export {}; diff --git a/package.json b/package.json index c351057..bbef0dc 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,16 @@ { "name": "config-router", "version": "1.0.0", - "main": "index.js", + "author": { + "name": "Yiyang Sun", + "email": "syy11cn@outlook.com" + }, + "main": "dist/src/index.js", "license": "MIT", + "repository": { + "type": "git", + "url": "git@github.com:syy11cn/config-router.git" + }, "dependencies": { "react-router-dom": "^5.3.0" }, diff --git a/src/index.tsx b/src/index.tsx index 8411aab..733971d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,11 +1,5 @@ import { FunctionComponent } from 'react'; -import { - Switch, - useLocation, - useRouteMatch, - Route, - Redirect, -} from 'react-router-dom'; +import { Switch, useLocation, Route, Redirect } from 'react-router-dom'; import routeType from '../typings/routeType'; interface RouterViewProps { @@ -17,14 +11,14 @@ const RouterView: FunctionComponent = ({ routes, onEnter, }) => { + // Pathname of current page. const { pathname } = useLocation(); - const match = useRouteMatch(); - console.log('当前页面路径:' + pathname); - console.log('路径匹配', match); + // No redirect and no agree to go by default. let redirectPath: string = ''; let agreeToGo = true; + // Change state of `redirectPath` and `agreeToGo` function next(path?: string) { agreeToGo = true; if (path) { @@ -32,6 +26,7 @@ const RouterView: FunctionComponent = ({ } } + // Use hook. onEnter(pathname, next); return ( diff --git a/tsconfig.json b/tsconfig.json index 3670c0c..f93523e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,20 +1,21 @@ { "compilerOptions": { "target": "ESNext", - "useDefineForClassFields": true, - "lib": ["DOM", "DOM.Iterable", "ESNext"], - "allowJs": false, - "skipLibCheck": false, - "esModuleInterop": false, - "allowSyntheticDefaultImports": true, + // "useDefineForClassFields": true, + // "lib": ["DOM", "DOM.Iterable", "ESNext"], + // "allowJs": false, + // "skipLibCheck": false, + // "esModuleInterop": false, + // "allowSyntheticDefaultImports": true, "strict": true, - "forceConsistentCasingInFileNames": true, + // "forceConsistentCasingInFileNames": true, "module": "ESNext", "moduleResolution": "Node", - "resolveJsonModule": true, - "isolatedModules": true, - "noEmit": true, - "jsx": "react-jsx" + // "resolveJsonModule": true, + // "isolatedModules": true, + // "noEmit": true, + "jsx": "react-jsx", + "outDir": "dist" }, - "include": ["./src/**/*"] + "include": ["src/**/*"] } diff --git a/typings/routeType.ts b/typings/routeType.ts index 3ca771b..e96171b 100644 --- a/typings/routeType.ts +++ b/typings/routeType.ts @@ -1,4 +1,4 @@ -import React from 'react'; +import * as React from 'react'; interface routeType { path: string;