@modern-js/plugin-router-v5 2.14.0 → 2.16.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.
@@ -1,106 +1,86 @@
1
- import {
2
- getEntryOptions,
3
- createRuntimeExportsUtils,
4
- PLUGIN_SCHEMAS,
5
- isRouterV5 as isV5
6
- } from "@modern-js/utils";
1
+ import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS, isRouterV5 as isV5 } from "@modern-js/utils";
7
2
  import "./types";
8
3
  const PLUGIN_IDENTIFIER = "router";
9
4
  const ROUTES_IDENTIFIER = "routes";
10
- var cli_default = () => ({
11
- name: "@modern-js/plugin-router-v5",
12
- required: ["@modern-js/runtime"],
13
- setup: (api) => {
14
- const runtimeConfigMap = /* @__PURE__ */ new Map();
15
- let pluginsExportsUtils;
16
- let routerExportsUtils;
17
- return {
18
- config() {
19
- const appContext = api.useAppContext();
20
- pluginsExportsUtils = createRuntimeExportsUtils(
21
- appContext.internalDirectory,
22
- "plugins"
23
- );
24
- routerExportsUtils = createRuntimeExportsUtils(
25
- appContext.internalDirectory,
26
- "router"
27
- );
28
- return {
29
- source: {
30
- alias: {
31
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
32
- "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
5
+ export default () => {
6
+ return {
7
+ name: "@modern-js/plugin-router-v5",
8
+ required: [
9
+ "@modern-js/runtime"
10
+ ],
11
+ setup: (api) => {
12
+ const runtimeConfigMap = /* @__PURE__ */ new Map();
13
+ let pluginsExportsUtils;
14
+ let routerExportsUtils;
15
+ return {
16
+ config() {
17
+ const appContext = api.useAppContext();
18
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
19
+ routerExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "router");
20
+ return {
21
+ source: {
22
+ alias: {
23
+ "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
24
+ "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
25
+ }
33
26
  }
27
+ };
28
+ },
29
+ validateSchema() {
30
+ return PLUGIN_SCHEMAS["@modern-js/plugin-router"];
31
+ },
32
+ modifyEntryImports({ entrypoint, imports }) {
33
+ const { entryName } = entrypoint;
34
+ const userConfig = api.useResolvedConfigContext();
35
+ const { packageName } = api.useAppContext();
36
+ const runtimeConfig = getEntryOptions(entryName, userConfig.runtime, userConfig.runtimeByEntries, packageName);
37
+ runtimeConfigMap.set(entryName, runtimeConfig);
38
+ if (isV5(userConfig)) {
39
+ imports.push({
40
+ value: "@modern-js/runtime/plugins",
41
+ specifiers: [
42
+ {
43
+ imported: PLUGIN_IDENTIFIER
44
+ }
45
+ ]
46
+ });
47
+ } else {
48
+ throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
49
+ }
50
+ return {
51
+ entrypoint,
52
+ imports
53
+ };
54
+ },
55
+ modifyEntryRuntimePlugins({ entrypoint, plugins }) {
56
+ const { entryName, fileSystemRoutes } = entrypoint;
57
+ const { serverRoutes } = api.useAppContext();
58
+ const runtimeConfig = runtimeConfigMap.get(entryName);
59
+ const userConfig = api.useResolvedConfigContext();
60
+ if (isV5(userConfig)) {
61
+ const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
62
+ plugins.push({
63
+ name: PLUGIN_IDENTIFIER,
64
+ options: JSON.stringify({
65
+ serverBase,
66
+ ...runtimeConfig.router,
67
+ routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
68
+ }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
69
+ });
70
+ }
71
+ return {
72
+ entrypoint,
73
+ plugins
74
+ };
75
+ },
76
+ addRuntimeExports() {
77
+ const userConfig = api.useResolvedConfigContext();
78
+ if (isV5(userConfig)) {
79
+ pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
80
+ routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
34
81
  }
35
- };
36
- },
37
- validateSchema() {
38
- return PLUGIN_SCHEMAS["@modern-js/plugin-router"];
39
- },
40
- modifyEntryImports({ entrypoint, imports }) {
41
- const { entryName } = entrypoint;
42
- const userConfig = api.useResolvedConfigContext();
43
- const { packageName } = api.useAppContext();
44
- const runtimeConfig = getEntryOptions(
45
- entryName,
46
- userConfig.runtime,
47
- userConfig.runtimeByEntries,
48
- packageName
49
- );
50
- runtimeConfigMap.set(entryName, runtimeConfig);
51
- if (isV5(userConfig)) {
52
- imports.push({
53
- value: "@modern-js/runtime/plugins",
54
- specifiers: [{ imported: PLUGIN_IDENTIFIER }]
55
- });
56
- } else {
57
- throw new Error(
58
- `should enable runtime.router.mode for entry ${entryName}`
59
- );
60
- }
61
- return {
62
- entrypoint,
63
- imports
64
- };
65
- },
66
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
67
- const { entryName, fileSystemRoutes } = entrypoint;
68
- const { serverRoutes } = api.useAppContext();
69
- const runtimeConfig = runtimeConfigMap.get(entryName);
70
- const userConfig = api.useResolvedConfigContext();
71
- if (isV5(userConfig)) {
72
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
73
- plugins.push({
74
- name: PLUGIN_IDENTIFIER,
75
- options: JSON.stringify({
76
- serverBase,
77
- ...runtimeConfig.router,
78
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
79
- }).replace(
80
- /"routesConfig"\s*:\s*"((\S|\s)+)"/g,
81
- '"routesConfig": $1,'
82
- )
83
- });
84
- }
85
- return {
86
- entrypoint,
87
- plugins
88
- };
89
- },
90
- addRuntimeExports() {
91
- const userConfig = api.useResolvedConfigContext();
92
- if (isV5(userConfig)) {
93
- pluginsExportsUtils.addExport(
94
- `export { default as router } from '@modern-js/plugin-router-v5/runtime'`
95
- );
96
- routerExportsUtils == null ? void 0 : routerExportsUtils.addExport(
97
- `export * from '@modern-js/plugin-router-v5/runtime'`
98
- );
99
82
  }
100
- }
101
- };
102
- }
103
- });
104
- export {
105
- cli_default as default
83
+ };
84
+ }
85
+ };
106
86
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,2 @@
1
- import { default as default2 } from "./runtime";
1
+ export { default } from "./runtime";
2
2
  export * from "./runtime";
3
- export {
4
- default2 as default
5
- };
@@ -1,17 +1,11 @@
1
- import { jsx } from "react/jsx-runtime";
2
- const DefaultNotFound = () => /* @__PURE__ */ jsx(
3
- "div",
4
- {
5
- style: {
6
- margin: "150px auto",
7
- textAlign: "center",
8
- display: "flex",
9
- alignItems: "center",
10
- justifyContent: "center"
11
- },
12
- children: "404"
13
- }
14
- );
15
- export {
16
- DefaultNotFound
17
- };
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export const DefaultNotFound = () => /* @__PURE__ */ _jsx("div", {
3
+ style: {
4
+ margin: "150px auto",
5
+ textAlign: "center",
6
+ display: "flex",
7
+ alignItems: "center",
8
+ justifyContent: "center"
9
+ },
10
+ children: "404"
11
+ });
@@ -1,7 +1,4 @@
1
1
  import { routerPlugin } from "./plugin";
2
- var runtime_default = routerPlugin;
2
+ export default routerPlugin;
3
3
  export * from "react-router-dom";
4
4
  export * from "history";
5
- export {
6
- runtime_default as default
7
- };
@@ -1,27 +1,12 @@
1
- import { jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
- import {
4
- createBrowserHistory,
5
- createHashHistory
6
- } from "history";
7
- import {
8
- Router,
9
- StaticRouter,
10
- useRouteMatch,
11
- useLocation
12
- } from "react-router-dom";
3
+ import { createBrowserHistory, createHashHistory } from "history";
4
+ import { Router, StaticRouter, useRouteMatch, useLocation } from "react-router-dom";
13
5
  import hoistNonReactStatics from "hoist-non-react-statics";
14
6
  import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
15
7
  import { parsedJSONFromElement } from "@modern-js/utils/runtime";
16
8
  import { renderRoutes, getLocation, urlJoin } from "./utils";
17
- const routerPlugin = ({
18
- serverBase = [],
19
- history: customHistory,
20
- supportHtml5History = true,
21
- routesConfig,
22
- createRoutes,
23
- historyOptions = {}
24
- }) => {
9
+ export const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
25
10
  const isBrow = isBrowser();
26
11
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
27
12
  if (isBrow) {
@@ -36,41 +21,55 @@ const routerPlugin = ({
36
21
  useRouteMatch,
37
22
  useLocation
38
23
  };
39
- return next({ context });
24
+ return next({
25
+ context
26
+ });
40
27
  },
41
28
  hoc: ({ App }, next) => {
42
29
  const getRouteApp = () => {
43
- var _a;
44
30
  if (isBrow) {
45
- const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
31
+ var _window__SERVER_DATA;
32
+ const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
46
33
  historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
47
34
  const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
48
- return (props) => /* @__PURE__ */ jsx(Router, { history, children: createRoutes ? /* @__PURE__ */ jsx(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ jsx(App, { ...props, children: renderRoutes(routesConfig, props) }) });
35
+ return (props) => /* @__PURE__ */ _jsx(Router, {
36
+ history,
37
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, {
38
+ ...props,
39
+ Component: createRoutes()
40
+ }) : /* @__PURE__ */ _jsx(App, {
41
+ ...props,
42
+ children: renderRoutes(routesConfig, props)
43
+ })
44
+ });
49
45
  }
50
46
  return (props) => {
51
47
  const runtimeContext = useContext(RuntimeReactContext);
52
48
  const { ssrContext } = runtimeContext;
53
- const location2 = getLocation(ssrContext);
54
- const routerContext = (ssrContext == null ? void 0 : ssrContext.redirection) || {};
55
- const request = ssrContext == null ? void 0 : ssrContext.request;
56
- const baseUrl = request == null ? void 0 : request.baseUrl;
49
+ const location1 = getLocation(ssrContext);
50
+ const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
51
+ const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
52
+ const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
57
53
  const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
58
- return /* @__PURE__ */ jsx(
59
- StaticRouter,
60
- {
61
- basename: basename === "/" ? "" : basename,
62
- location: location2,
63
- context: routerContext,
64
- children: createRoutes ? /* @__PURE__ */ jsx(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ jsx(App, { ...props, children: renderRoutes(routesConfig, props) })
65
- }
66
- );
54
+ return /* @__PURE__ */ _jsx(StaticRouter, {
55
+ basename: basename === "/" ? "" : basename,
56
+ location: location1,
57
+ context: routerContext,
58
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, {
59
+ ...props,
60
+ Component: createRoutes()
61
+ }) : /* @__PURE__ */ _jsx(App, {
62
+ ...props,
63
+ children: renderRoutes(routesConfig, props)
64
+ })
65
+ });
67
66
  };
68
67
  };
69
68
  let RouteApp = getRouteApp();
70
69
  if (App) {
71
70
  RouteApp = hoistNonReactStatics(RouteApp, App);
72
71
  }
73
- if (routesConfig == null ? void 0 : routesConfig.globalApp) {
72
+ if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
74
73
  return next({
75
74
  App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
76
75
  });
@@ -83,6 +82,3 @@ const routerPlugin = ({
83
82
  }
84
83
  };
85
84
  };
86
- export {
87
- routerPlugin
88
- };
@@ -1,20 +1,25 @@
1
- import { jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { Route, matchPath } from "react-router-dom";
3
3
  import { DefaultNotFound } from "./DefaultNotFound";
4
- function renderRoutes(routesConfig, extraProps = {}) {
4
+ export function renderRoutes(routesConfig, extraProps = {}) {
5
5
  if (!routesConfig) {
6
6
  return null;
7
7
  }
8
8
  const Layout = ({ Component, ...props }) => {
9
- const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
9
+ const GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
10
10
  if (!GlobalLayout) {
11
- return /* @__PURE__ */ jsx(Component, { ...props });
11
+ return /* @__PURE__ */ _jsx(Component, {
12
+ ...props
13
+ });
12
14
  }
13
- return /* @__PURE__ */ jsx(GlobalLayout, { Component, ...props });
15
+ return /* @__PURE__ */ _jsx(GlobalLayout, {
16
+ Component,
17
+ ...props
18
+ });
14
19
  };
15
20
  const findMatchedRoute = (pathname) => {
16
- var _a;
17
- return (_a = routesConfig == null ? void 0 : routesConfig.routes) == null ? void 0 : _a.find((route) => {
21
+ var _routesConfig_routes;
22
+ return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find((route) => {
18
23
  const info = matchPath(pathname, {
19
24
  path: route.path,
20
25
  exact: route.exact,
@@ -23,51 +28,42 @@ function renderRoutes(routesConfig, extraProps = {}) {
23
28
  return Boolean(info);
24
29
  });
25
30
  };
26
- return /* @__PURE__ */ jsx(
27
- Route,
28
- {
29
- path: "/",
30
- render: (props) => {
31
- const matchedRoute = findMatchedRoute(props.location.pathname);
32
- if (!matchedRoute) {
33
- return /* @__PURE__ */ jsx(DefaultNotFound, {});
34
- }
35
- return /* @__PURE__ */ jsx(
36
- Route,
37
- {
38
- path: matchedRoute.path,
39
- exact: matchedRoute.exact,
40
- sensitive: matchedRoute.sensitive,
41
- render: (routeProps) => /* @__PURE__ */ jsx(
42
- Layout,
43
- {
44
- Component: matchedRoute.component,
45
- ...routeProps,
46
- ...extraProps
47
- }
48
- )
49
- }
50
- );
31
+ return /* @__PURE__ */ _jsx(Route, {
32
+ path: "/",
33
+ render: (props) => {
34
+ const matchedRoute = findMatchedRoute(props.location.pathname);
35
+ if (!matchedRoute) {
36
+ return /* @__PURE__ */ _jsx(DefaultNotFound, {});
51
37
  }
38
+ return /* @__PURE__ */ _jsx(Route, {
39
+ path: matchedRoute.path,
40
+ exact: matchedRoute.exact,
41
+ sensitive: matchedRoute.sensitive,
42
+ render: (routeProps) => /* @__PURE__ */ _jsx(Layout, {
43
+ Component: matchedRoute.component,
44
+ ...routeProps,
45
+ ...extraProps
46
+ })
47
+ });
52
48
  }
53
- );
49
+ });
54
50
  }
55
- function getLocation(serverContext) {
56
- var _a;
57
- const { pathname, url } = (serverContext == null ? void 0 : serverContext.request) || {};
58
- const cleanUrl = (_a = url == null ? void 0 : url.replace("http://", "")) == null ? void 0 : _a.replace("https://", "");
51
+ export function getLocation(serverContext) {
52
+ var _url_replace;
53
+ const { pathname, url } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
54
+ const cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
59
55
  const index = (cleanUrl || "").indexOf(pathname);
60
56
  if (index === -1) {
61
57
  return pathname;
62
58
  }
63
59
  return cleanUrl.substring(index);
64
60
  }
65
- const urlJoin = (...parts) => {
61
+ export const urlJoin = (...parts) => {
66
62
  const separator = "/";
67
63
  const replace = new RegExp(`${separator}{1,}`, "g");
68
64
  return standardSlash(parts.join(separator).replace(replace, separator));
69
65
  };
70
- function standardSlash(str) {
66
+ export function standardSlash(str) {
71
67
  let addr = str;
72
68
  if (!addr || typeof addr !== "string") {
73
69
  return addr;
@@ -83,9 +79,3 @@ function standardSlash(str) {
83
79
  }
84
80
  return addr;
85
81
  }
86
- export {
87
- getLocation,
88
- renderRoutes,
89
- standardSlash,
90
- urlJoin
91
- };
package/package.json CHANGED
@@ -3,7 +3,11 @@
3
3
  "description": "A Progressive React Framework for modern web development.",
4
4
  "homepage": "https://modernjs.dev",
5
5
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
6
- "repository": "web-infra-dev/modern.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/modern.js",
9
+ "directory": "packages/runtime/plugin-router-v5"
10
+ },
7
11
  "license": "MIT",
8
12
  "keywords": [
9
13
  "react",
@@ -11,7 +15,7 @@
11
15
  "modern",
12
16
  "modern.js"
13
17
  ],
14
- "version": "2.14.0",
18
+ "version": "2.16.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/cli/index.d.ts",
17
21
  "main": "./dist/cjs/cli/index.js",
@@ -55,8 +59,8 @@
55
59
  "history": "^4.7.9",
56
60
  "hoist-non-react-statics": "^3.3.2",
57
61
  "react-router-dom": "^5.3.4",
58
- "@modern-js/types": "2.14.0",
59
- "@modern-js/utils": "2.14.0"
62
+ "@modern-js/types": "2.16.0",
63
+ "@modern-js/utils": "2.16.0"
60
64
  },
61
65
  "peerDependencies": {
62
66
  "react": ">=17",
@@ -66,7 +70,6 @@
66
70
  "@babel/core": "^7.18.0",
67
71
  "@babel/runtime": "^7.18.0",
68
72
  "@testing-library/react": "^13.4.0",
69
- "@testing-library/react-hooks": "^8.0.1",
70
73
  "@types/invariant": "^2.2.30",
71
74
  "@types/jest": "^29",
72
75
  "@types/node": "^14",
@@ -75,12 +78,12 @@
75
78
  "react-dom": "^18",
76
79
  "ts-jest": "^29.0.4",
77
80
  "typescript": "^4",
78
- "@modern-js/app-tools": "2.14.0",
79
- "@modern-js/core": "2.14.0",
80
- "@modern-js/runtime": "2.14.0",
81
- "@modern-js/utils": "2.14.0",
82
- "@scripts/build": "2.14.0",
83
- "@scripts/jest-config": "2.14.0"
81
+ "@modern-js/app-tools": "2.16.0",
82
+ "@modern-js/core": "2.16.0",
83
+ "@modern-js/runtime": "2.16.0",
84
+ "@modern-js/utils": "2.16.0",
85
+ "@scripts/build": "2.16.0",
86
+ "@scripts/jest-config": "2.16.0"
84
87
  },
85
88
  "sideEffects": false,
86
89
  "modernConfig": {},