@rpcbase/router 0.0.0
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/package.json +28 -0
- package/src/index.ts +2 -0
- package/src/loadRoute.tsx +30 -0
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rpcbase/router",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc --watch",
|
|
8
|
+
"release": "wireit"
|
|
9
|
+
},
|
|
10
|
+
"wireit": {
|
|
11
|
+
"release": {
|
|
12
|
+
"command": "../../scripts/publish.js",
|
|
13
|
+
"dependencies": [],
|
|
14
|
+
"files": [
|
|
15
|
+
"package.json"
|
|
16
|
+
],
|
|
17
|
+
"output": [],
|
|
18
|
+
"env": {
|
|
19
|
+
"NPM_RELEASE_CHANNEL": {
|
|
20
|
+
"external": true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { lazy } from "react"
|
|
2
|
+
import { LoaderFunction } from "react-router"
|
|
3
|
+
|
|
4
|
+
import { Loader, LoaderArgs } from "@rpcbase/client"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
type RouteModule = {
|
|
8
|
+
default: React.ComponentType<unknown>
|
|
9
|
+
loader?: Loader
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type RouteWithLoader = {
|
|
13
|
+
Component: React.LazyExoticComponent<React.ComponentType<unknown>>
|
|
14
|
+
loader?: LoaderFunction
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const loadRoute = (importPromise: Promise<RouteModule>): RouteWithLoader => {
|
|
18
|
+
const Component = lazy(async () => {
|
|
19
|
+
const module = await importPromise
|
|
20
|
+
return { default: module.default }
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const loader = async (args: LoaderArgs) => {
|
|
24
|
+
const module = await importPromise
|
|
25
|
+
if (!module.loader) return null
|
|
26
|
+
return module.loader(args)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return { Component, loader: loader as unknown as LoaderFunction }
|
|
30
|
+
}
|