-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit, get started with router
- Loading branch information
0 parents
commit 7e82cb1
Showing
10 changed files
with
3,748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib/ | ||
node_modules/ | ||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
src/ | ||
test/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "porpoise-router", | ||
"version": "1.0.0", | ||
"description": "An powerful framework-agnostic routing solution.", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc && webpack" | ||
}, | ||
"author": "raghav-misra", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"typescript": "^3.9.7", | ||
"webpack": "^4.44.1", | ||
"webpack-cli": "^3.3.12" | ||
}, | ||
"dependencies": { | ||
"path-to-regexp": "^6.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `porpoise-router` | ||
|
||
A simple client-side routing solution for custom elements. | ||
|
||
```js | ||
/* Initialize the router */ | ||
import { createRouter } from "porpoise-router"; | ||
|
||
const router = createRouter({ | ||
// path "/" renders | ||
// <index-view></index-view> | ||
"/": "index-view", | ||
|
||
// path "/about" renders | ||
// <about-view></about-view> | ||
"/about": "about-view", | ||
|
||
// path "/profile/:userId" renders | ||
// <profile-view></profile-view> | ||
// "userId" access via (router.current.params.userId) | ||
"/profile/:userId": "profile-view", | ||
}); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//@ts-ignore | ||
!function () { "use strict"; !function () { if (void 0 === window.Reflect || void 0 === window.customElements || window.customElements.polyfillWrapFlushCallback) return; const t = HTMLElement; window.HTMLElement = { HTMLElement: function () { return Reflect.construct(t, [], this.constructor) } }.HTMLElement, HTMLElement.prototype = t.prototype, HTMLElement.prototype.constructor = HTMLElement, Object.setPrototypeOf(HTMLElement, t) }() }(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// ES5 interop with custom elements: | ||
import "./es5-adapter.js"; | ||
import { PorpoiseRouter, IRouteDescriptor } from "./porpoise-router.js"; | ||
import { pathToRegexp } from "path-to-regexp"; | ||
|
||
export default function createRouter(target: string, routes: Record<string, string | IRouteDescriptor>) { | ||
const routerNode = document.querySelector(`porpoise-router[name="${target}"]`) as PorpoiseRouter; | ||
|
||
if (routerNode) { | ||
routerNode.configure(routes); | ||
} | ||
else console.error(`Could not find <porpoise-router name="${target}"></porpoise-router> in the DOM.`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface IRouteDescriptor { | ||
element: string, | ||
props?: Record<string, string> | ||
} | ||
|
||
export class PorpoiseRouter extends HTMLElement { | ||
routes: Record<string, string | IRouteDescriptor> = Object.create(null); | ||
|
||
configure(routes: Record<string, string | IRouteDescriptor>) { | ||
this.routes = routes; | ||
window.addEventListener("popstate", this.changeView.bind(this)); | ||
} | ||
|
||
changeView() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "es2020", | ||
"lib": [ | ||
"es2015", | ||
"dom" | ||
], | ||
"declaration": true, | ||
"outDir": "./lib/esm", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"baseUrl": "./", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
entry: { | ||
"index": "./lib/esm/index.js", | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, "lib"), | ||
filename: "[name].js", | ||
libraryTarget: "global", | ||
library: "PorpoiseRouter", | ||
umdNamedDefine: true, | ||
}, | ||
resolve: { | ||
extensions: [".js"], | ||
}, | ||
devtool: "source-map" | ||
}; |