alejhern__router 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# React + Vite
|
|
2
|
+
|
|
3
|
+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
|
4
|
+
|
|
5
|
+
Currently, two official plugins are available:
|
|
6
|
+
|
|
7
|
+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
|
|
8
|
+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
|
9
|
+
|
|
10
|
+
## React Compiler
|
|
11
|
+
|
|
12
|
+
The React Compiler is currently not compatible with SWC. See [this issue](https://github.com/vitejs/vite-plugin-react/issues/428) for tracking the progress.
|
|
13
|
+
|
|
14
|
+
## Expanding the ESLint configuration
|
|
15
|
+
|
|
16
|
+
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{EVENTS}from"../utils/const.js";function navigate(href){window.history.pushState({},"",href);const navEvent=new PopStateEvent(EVENTS.POPSTATE);window.dispatchEvent(navEvent)}export function Link({target,to,...props}){const handleClick=e=>{const isMainEvent=e.button===0;const isModifiedEvent=e.metaKey||e.ctrlKey||e.shiftKey||e.altKey;if(!isMainEvent||isModifiedEvent){return}const isManageableEvent=target==="_self"&&(to.startsWith("/")||to.startsWith(window.location.origin));if(isManageableEvent){return}e.preventDefault();navigate(to)};return _jsx("a",{href:to,target:target,onClick:handleClick,...props})}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function Route({path,Component}){return null}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useEffect,useState,Children}from"react";import{EVENTS}from"../utils/const.js";import{getCurrentPath}from"../utils/getCurrentPath.js";import{match}from"path-to-regexp";export function Router({children,routes=[],DefaultComponent=null}){const[currentPage,setCurrentPage]=useState(getCurrentPath());useEffect(()=>{const onLocationChange=()=>{setCurrentPage(window.location.pathname||"/")};window.addEventListener(EVENTS.POPSTATE,onLocationChange);return()=>{window.removeEventListener(EVENTS.POPSTATE,onLocationChange)}},[]);let routerParams={};const childRoutes=Children.map(children,({props,type})=>{const{name}=type;const isRoute=name==="Route";return isRoute?props:null});routes=routes.concat(childRoutes).filter(Boolean);const Page=routes.find(({path})=>{if(path===currentPage)return true;const routeMatcher=match(path,{decode:decodeURIComponent});const matched=routeMatcher(currentPage);if(!matched)return false;Object.assign(routerParams,matched.params);return true})?.Component;return Page?_jsx(Page,{params:routerParams}):DefaultComponent?_jsx(DefaultComponent,{}):null}
|
package/lib/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{Router}from"./components/Router.js";export{Route}from"./components/Route.js";export{Link}from"./components/Link.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const EVENTS={POPSTATE:"popstate"};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const getCurrentPath=()=>window.location.pathname;
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "alejhern__router",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "lib/src/index.js",
|
|
6
|
+
"module": "lib/src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./lib/src/index.js",
|
|
10
|
+
"require": "./lib/src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "vite",
|
|
17
|
+
"build": "vite build",
|
|
18
|
+
"lint": "eslint .",
|
|
19
|
+
"preview": "vite preview",
|
|
20
|
+
"test": "vitest",
|
|
21
|
+
"test-ui": "vitest --ui",
|
|
22
|
+
"prepare": "rm -rf lib && swc src/components src/utils src/index.jsx -d lib"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@testing-library/react": "^16.3.2",
|
|
29
|
+
"happy-dom": "^20.4.0",
|
|
30
|
+
"path-to-regexp": "8.3.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": "^19.2.0",
|
|
34
|
+
"react-dom": "^19.2.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@eslint/js": "^9.39.1",
|
|
38
|
+
"@swc/cli": "^0.7.10",
|
|
39
|
+
"@swc/core": "^1.15.11",
|
|
40
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
41
|
+
"@types/react": "^19.2.5",
|
|
42
|
+
"@types/react-dom": "^19.2.3",
|
|
43
|
+
"@vitejs/plugin-react-swc": "^4.2.2",
|
|
44
|
+
"@vitest/ui": "^4.0.18",
|
|
45
|
+
"eslint": "^9.39.1",
|
|
46
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
47
|
+
"eslint-plugin-react-refresh": "^0.4.24",
|
|
48
|
+
"globals": "^16.5.0",
|
|
49
|
+
"vite": "^7.2.4",
|
|
50
|
+
"vitest": "^4.0.18"
|
|
51
|
+
}
|
|
52
|
+
}
|