@modern-js/plugin-router-v5 0.0.0-next-20221220061243
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/CHANGELOG.md +45 -0
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/js/modern/cli/index.js +127 -0
- package/dist/js/modern/index.js +5 -0
- package/dist/js/modern/modern-app-env.d.js +0 -0
- package/dist/js/modern/runtime/DefaultNotFound.js +14 -0
- package/dist/js/modern/runtime/index.js +7 -0
- package/dist/js/modern/runtime/plugin.js +94 -0
- package/dist/js/modern/runtime/utils.js +108 -0
- package/dist/js/node/cli/index.js +142 -0
- package/dist/js/node/index.js +33 -0
- package/dist/js/node/modern-app-env.d.js +0 -0
- package/dist/js/node/runtime/DefaultNotFound.js +37 -0
- package/dist/js/node/runtime/index.js +29 -0
- package/dist/js/node/runtime/plugin.js +118 -0
- package/dist/js/node/runtime/utils.js +132 -0
- package/dist/js/treeshaking/cli/index.js +150 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/dist/js/treeshaking/modern-app-env.d.js +1 -0
- package/dist/js/treeshaking/runtime/DefaultNotFound.js +14 -0
- package/dist/js/treeshaking/runtime/index.js +5 -0
- package/dist/js/treeshaking/runtime/plugin.js +124 -0
- package/dist/js/treeshaking/runtime/utils.js +138 -0
- package/dist/types/cli/index.d.ts +14 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/runtime/DefaultNotFound.d.ts +2 -0
- package/dist/types/runtime/index.d.ts +6 -0
- package/dist/types/runtime/plugin.d.ts +52 -0
- package/dist/types/runtime/utils.d.ts +6 -0
- package/package.json +97 -0
- package/types/index.d.ts +18 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function _objectSpread(target) {
|
|
15
|
+
for(var i = 1; i < arguments.length; i++){
|
|
16
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
17
|
+
var ownKeys = Object.keys(source);
|
|
18
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
19
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
20
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
ownKeys.forEach(function(key) {
|
|
24
|
+
_defineProperty(target, key, source[key]);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
29
|
+
function _objectWithoutProperties(source, excluded) {
|
|
30
|
+
if (source == null) return {};
|
|
31
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
32
|
+
var key, i;
|
|
33
|
+
if (Object.getOwnPropertySymbols) {
|
|
34
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
35
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
36
|
+
key = sourceSymbolKeys[i];
|
|
37
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
38
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
39
|
+
target[key] = source[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return target;
|
|
43
|
+
}
|
|
44
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
45
|
+
if (source == null) return {};
|
|
46
|
+
var target = {};
|
|
47
|
+
var sourceKeys = Object.keys(source);
|
|
48
|
+
var key, i;
|
|
49
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
50
|
+
key = sourceKeys[i];
|
|
51
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
52
|
+
target[key] = source[key];
|
|
53
|
+
}
|
|
54
|
+
return target;
|
|
55
|
+
}
|
|
56
|
+
import { jsx } from "react/jsx-runtime";
|
|
57
|
+
import { Route, matchPath } from "react-router-dom";
|
|
58
|
+
import { DefaultNotFound } from "./DefaultNotFound";
|
|
59
|
+
function renderRoutes(routesConfig) {
|
|
60
|
+
var extraProps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
61
|
+
var Layout = function(_param) {
|
|
62
|
+
var Component = _param.Component, props = _objectWithoutProperties(_param, [
|
|
63
|
+
"Component"
|
|
64
|
+
]);
|
|
65
|
+
var GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
|
|
66
|
+
if (!GlobalLayout) {
|
|
67
|
+
return /* @__PURE__ */ jsx(Component, _objectSpread({}, props));
|
|
68
|
+
}
|
|
69
|
+
return /* @__PURE__ */ jsx(GlobalLayout, _objectSpread({
|
|
70
|
+
Component: Component
|
|
71
|
+
}, props));
|
|
72
|
+
};
|
|
73
|
+
var findMatchedRoute = function(pathname) {
|
|
74
|
+
var ref;
|
|
75
|
+
return routesConfig === null || routesConfig === void 0 ? void 0 : (ref = routesConfig.routes) === null || ref === void 0 ? void 0 : ref.find(function(route) {
|
|
76
|
+
var info = matchPath(pathname, {
|
|
77
|
+
path: route.path,
|
|
78
|
+
exact: route.exact,
|
|
79
|
+
sensitive: route.sensitive
|
|
80
|
+
});
|
|
81
|
+
return Boolean(info);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return /* @__PURE__ */ jsx(Route, {
|
|
85
|
+
path: "/",
|
|
86
|
+
render: function(props) {
|
|
87
|
+
var matchedRoute = findMatchedRoute(props.location.pathname);
|
|
88
|
+
if (!matchedRoute) {
|
|
89
|
+
return /* @__PURE__ */ jsx(DefaultNotFound, {});
|
|
90
|
+
}
|
|
91
|
+
return /* @__PURE__ */ jsx(Route, {
|
|
92
|
+
path: matchedRoute.path,
|
|
93
|
+
exact: matchedRoute.exact,
|
|
94
|
+
sensitive: matchedRoute.sensitive,
|
|
95
|
+
render: function(routeProps) {
|
|
96
|
+
return /* @__PURE__ */ jsx(Layout, _objectSpread({
|
|
97
|
+
Component: matchedRoute.component
|
|
98
|
+
}, routeProps, extraProps));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function getLocation(serverContext) {
|
|
105
|
+
var ref;
|
|
106
|
+
var ref1 = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {}, pathname = ref1.pathname, url = ref1.url;
|
|
107
|
+
var cleanUrl = (ref = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || ref === void 0 ? void 0 : ref.replace("https://", "");
|
|
108
|
+
var index = (cleanUrl || "").indexOf(pathname);
|
|
109
|
+
if (index === -1) {
|
|
110
|
+
return pathname;
|
|
111
|
+
}
|
|
112
|
+
return cleanUrl.substring(index);
|
|
113
|
+
}
|
|
114
|
+
var urlJoin = function() {
|
|
115
|
+
for(var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
116
|
+
parts[_key] = arguments[_key];
|
|
117
|
+
}
|
|
118
|
+
var separator = "/";
|
|
119
|
+
var replace = new RegExp("".concat(separator, "{1,}"), "g");
|
|
120
|
+
return standardSlash(parts.join(separator).replace(replace, separator));
|
|
121
|
+
};
|
|
122
|
+
function standardSlash(str) {
|
|
123
|
+
var addr = str;
|
|
124
|
+
if (!addr || typeof addr !== "string") {
|
|
125
|
+
return addr;
|
|
126
|
+
}
|
|
127
|
+
if (addr.startsWith(".")) {
|
|
128
|
+
addr = addr.slice(1);
|
|
129
|
+
}
|
|
130
|
+
if (!addr.startsWith("/")) {
|
|
131
|
+
addr = "/".concat(addr);
|
|
132
|
+
}
|
|
133
|
+
if (addr.endsWith("/") && addr !== "/") {
|
|
134
|
+
addr = addr.slice(0, addr.length - 1);
|
|
135
|
+
}
|
|
136
|
+
return addr;
|
|
137
|
+
}
|
|
138
|
+
export { getLocation, renderRoutes, standardSlash, urlJoin };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
2
|
+
import { RouterConfig } from '../runtime/plugin';
|
|
3
|
+
interface UserConfig {
|
|
4
|
+
runtime?: {
|
|
5
|
+
router?: RouterConfig;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const _default: () => CliPlugin<AppTools & {
|
|
10
|
+
userConfig: UserConfig;
|
|
11
|
+
normalizedConfig: Required<UserConfig>;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { routerPlugin } from './plugin';
|
|
2
|
+
import type { SingleRouteConfig, HistoryConfig, RouterConfig } from './plugin';
|
|
3
|
+
export type { SingleRouteConfig, HistoryConfig, RouterConfig };
|
|
4
|
+
export default routerPlugin;
|
|
5
|
+
export * from 'react-router-dom';
|
|
6
|
+
export * from 'history';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { History, BrowserHistoryBuildOptions, HashHistoryBuildOptions } from 'history';
|
|
3
|
+
import { RouteProps } from 'react-router-dom';
|
|
4
|
+
import type { Plugin } from '@modern-js/runtime';
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
_SERVER_DATA?: {
|
|
8
|
+
router: {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export type SingleRouteConfig = RouteProps & {
|
|
16
|
+
redirect?: string;
|
|
17
|
+
routes?: SingleRouteConfig[];
|
|
18
|
+
key?: string;
|
|
19
|
+
/**
|
|
20
|
+
* layout component
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
layout?: React.ComponentType;
|
|
24
|
+
/**
|
|
25
|
+
* component would be rendered when route macthed.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
component?: React.ComponentType;
|
|
29
|
+
};
|
|
30
|
+
export type HistoryConfig = {
|
|
31
|
+
supportHtml5History: true;
|
|
32
|
+
historyOptions: BrowserHistoryBuildOptions;
|
|
33
|
+
} | {
|
|
34
|
+
supportHtml5History: false;
|
|
35
|
+
historyOptions: HashHistoryBuildOptions;
|
|
36
|
+
};
|
|
37
|
+
export type RouterConfig = Partial<HistoryConfig> & {
|
|
38
|
+
mode?: 'react-router-5';
|
|
39
|
+
routesConfig?: {
|
|
40
|
+
globalApp?: React.ComponentType<any>;
|
|
41
|
+
routes?: SingleRouteConfig[];
|
|
42
|
+
};
|
|
43
|
+
history?: History;
|
|
44
|
+
serverBase?: string[];
|
|
45
|
+
};
|
|
46
|
+
export declare const routerPlugin: ({
|
|
47
|
+
serverBase,
|
|
48
|
+
history: customHistory,
|
|
49
|
+
supportHtml5History,
|
|
50
|
+
routesConfig,
|
|
51
|
+
historyOptions
|
|
52
|
+
}: RouterConfig) => Plugin;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { RouterConfig } from './plugin';
|
|
3
|
+
export declare function renderRoutes(routesConfig: RouterConfig['routesConfig'], extraProps?: any): JSX.Element;
|
|
4
|
+
export declare function getLocation(serverContext: any): string;
|
|
5
|
+
export declare const urlJoin: (...parts: string[]) => string;
|
|
6
|
+
export declare function standardSlash(str: string): string;
|
package/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@modern-js/plugin-router-v5",
|
|
3
|
+
"description": "The meta-framework suite designed from scratch for frontend-focused modern web development.",
|
|
4
|
+
"homepage": "https://modernjs.dev",
|
|
5
|
+
"bugs": "https://github.com/modern-js-dev/modern.js/issues",
|
|
6
|
+
"repository": "modern-js-dev/modern.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"react",
|
|
10
|
+
"framework",
|
|
11
|
+
"modern",
|
|
12
|
+
"modern.js"
|
|
13
|
+
],
|
|
14
|
+
"version": "0.0.0-next-20221220061243",
|
|
15
|
+
"jsnext:source": "./src/index.ts",
|
|
16
|
+
"types": "./dist/types/cli/index.d.ts",
|
|
17
|
+
"main": "./dist/js/node/cli/index.js",
|
|
18
|
+
"module": "./dist/js/treeshaking/index.js",
|
|
19
|
+
"jsnext:modern": "./dist/js/modern/cli/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"jsnext:source": "./src/cli/index.ts",
|
|
23
|
+
"default": "./dist/js/node/cli/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./runtime": {
|
|
26
|
+
"jsnext:source": "./src/runtime/index.ts",
|
|
27
|
+
"node": "./dist/js/node/runtime/index.js",
|
|
28
|
+
"default": "./dist/js/treeshaking/runtime/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./cli": {
|
|
31
|
+
"jsnext:source": "./src/cli/index.ts",
|
|
32
|
+
"default": "./dist/js/node/cli/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"typesVersions": {
|
|
36
|
+
"*": {
|
|
37
|
+
".": [
|
|
38
|
+
"./dist/types/cli/index.d.ts"
|
|
39
|
+
],
|
|
40
|
+
"types": [
|
|
41
|
+
"./types/index.d.ts"
|
|
42
|
+
],
|
|
43
|
+
"cli": [
|
|
44
|
+
"./dist/types/cli/index.d.ts"
|
|
45
|
+
],
|
|
46
|
+
"runtime": [
|
|
47
|
+
"./dist/types/runtime/index.d.ts"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"history": "^4.7.9",
|
|
53
|
+
"react-router-dom": "^5.1.2",
|
|
54
|
+
"hoist-non-react-statics": "^3.3.2",
|
|
55
|
+
"@types/history": "^4.7.9",
|
|
56
|
+
"@types/react-router-dom": "^5.1.8",
|
|
57
|
+
"@modern-js/plugin": "0.0.0-next-20221220061243",
|
|
58
|
+
"@modern-js/utils": "0.0.0-next-20221220061243",
|
|
59
|
+
"@modern-js/types": "0.0.0-next-20221220061243"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"react": ">=17",
|
|
63
|
+
"react-dom": ">=17"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"react": "^18",
|
|
67
|
+
"react-dom": "^18",
|
|
68
|
+
"ts-jest": "^27.0.4",
|
|
69
|
+
"typescript": "^4",
|
|
70
|
+
"jest": "^27",
|
|
71
|
+
"@babel/core": "^7.18.0",
|
|
72
|
+
"@babel/runtime": "^7.18.0",
|
|
73
|
+
"@types/jest": "^27",
|
|
74
|
+
"@types/node": "^14",
|
|
75
|
+
"@testing-library/react": "^13.4.0",
|
|
76
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
77
|
+
"@types/invariant": "^2.2.30",
|
|
78
|
+
"@modern-js/core": "0.0.0-next-20221220061243",
|
|
79
|
+
"@modern-js/runtime": "0.0.0-next-20221220061243",
|
|
80
|
+
"@modern-js/utils": "0.0.0-next-20221220061243",
|
|
81
|
+
"@scripts/build": "0.0.0-next-20221220061243",
|
|
82
|
+
"@modern-js/app-tools": "0.0.0-next-20221220061243",
|
|
83
|
+
"@scripts/jest-config": "0.0.0-next-20221220061243"
|
|
84
|
+
},
|
|
85
|
+
"sideEffects": false,
|
|
86
|
+
"modernConfig": {},
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"registry": "https://registry.npmjs.org/",
|
|
89
|
+
"access": "public"
|
|
90
|
+
},
|
|
91
|
+
"scripts": {
|
|
92
|
+
"dev": "modern-lib build --watch",
|
|
93
|
+
"new": "modern-lib new",
|
|
94
|
+
"build": "modern-lib build",
|
|
95
|
+
"test": "jest --passWithNoTests"
|
|
96
|
+
}
|
|
97
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RouterConfig } from '../dist/types';
|
|
2
|
+
import '../dist/types/runtime';
|
|
3
|
+
|
|
4
|
+
declare module '@modern-js/app-tools' {
|
|
5
|
+
export interface RuntimeUserConfig {
|
|
6
|
+
router?: RouterConfig | boolean;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare module '@modern-js/runtime/router-v5' {
|
|
11
|
+
export * from '../dist/types/runtime';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare module '@modern-js/runtime' {
|
|
15
|
+
interface AppConfig {
|
|
16
|
+
router?: RouterConfig | boolean;
|
|
17
|
+
}
|
|
18
|
+
}
|