@modern-js/utils 2.31.2 → 2.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +1 -1
  3. package/dist/cjs/runtime/nestedRoutes.js +21 -8
  4. package/dist/cjs/runtime-node/index.js +1 -0
  5. package/dist/cjs/runtime-node/loaderContext/createLoaderCtx.js +34 -0
  6. package/dist/cjs/runtime-node/loaderContext/createRequestCtx.js +38 -0
  7. package/dist/cjs/runtime-node/loaderContext/index.js +22 -0
  8. package/dist/cjs/universal/constants.js +4 -0
  9. package/dist/compiled/webpack-chain/index.js +1 -1
  10. package/dist/compiled/webpack-chain/package.json +1 -1
  11. package/dist/compiled/webpack-chain/types/index.d.ts +381 -170
  12. package/dist/esm/runtime/nestedRoutes.js +47 -13
  13. package/dist/esm/runtime-node/index.js +1 -0
  14. package/dist/esm/runtime-node/loaderContext/createLoaderCtx.js +20 -0
  15. package/dist/esm/runtime-node/loaderContext/createRequestCtx.js +24 -0
  16. package/dist/esm/runtime-node/loaderContext/index.js +3 -0
  17. package/dist/esm/universal/constants.js +1 -0
  18. package/dist/esm-node/runtime/nestedRoutes.js +22 -9
  19. package/dist/esm-node/runtime-node/index.js +1 -0
  20. package/dist/esm-node/runtime-node/loaderContext/createLoaderCtx.js +15 -0
  21. package/dist/esm-node/runtime-node/loaderContext/createRequestCtx.js +19 -0
  22. package/dist/esm-node/runtime-node/loaderContext/index.js +3 -0
  23. package/dist/esm-node/universal/constants.js +1 -0
  24. package/dist/types/runtime/nestedRoutes.d.ts +3 -2
  25. package/dist/types/runtime-node/index.d.ts +2 -1
  26. package/dist/types/runtime-node/loaderContext/createLoaderCtx.d.ts +7 -0
  27. package/dist/types/runtime-node/loaderContext/createRequestCtx.d.ts +7 -0
  28. package/dist/types/runtime-node/loaderContext/index.d.ts +4 -0
  29. package/dist/types/runtime-node/nestedRoutes.d.ts +1 -1
  30. package/dist/types/universal/constants.d.ts +5 -1
  31. package/package.json +6 -6
@@ -1,15 +1,21 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
1
2
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
5
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
4
6
  import { Suspense } from "react";
5
- import { createRoutesFromElements, Route } from "react-router-dom";
6
- export var transformNestedRoutes = function(routes) {
7
+ import { createRoutesFromElements, Outlet, Route } from "react-router-dom";
8
+ import { LOADER_REPORTER_NAME } from "../universal/constants";
9
+ import { time } from "../universal/time";
10
+ export var transformNestedRoutes = function(routes, reporter) {
7
11
  var routeElements = [];
8
12
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
9
13
  try {
10
14
  for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
11
15
  var route = _step.value;
12
- var routeElement = renderNestedRoute(route);
16
+ var routeElement = renderNestedRoute(route, {
17
+ reporter: reporter
18
+ });
13
19
  routeElements.push(routeElement);
14
20
  }
15
21
  } catch (err) {
@@ -33,12 +39,12 @@ export var renderNestedRoute = function(nestedRoute) {
33
39
  var _config, _children;
34
40
  var children = nestedRoute.children, index = nestedRoute.index, id = nestedRoute.id, component = nestedRoute.component, isRoot = nestedRoute.isRoot, lazyImport = nestedRoute.lazyImport, config = nestedRoute.config, handle = nestedRoute.handle;
35
41
  var Component = component;
36
- var parent = options.parent, DeferredDataComponent = options.DeferredDataComponent, _options_props = options.props, props = _options_props === void 0 ? {} : _options_props;
42
+ var parent = options.parent, DeferredDataComponent = options.DeferredDataComponent, _options_props = options.props, props = _options_props === void 0 ? {} : _options_props, reporter = options.reporter;
37
43
  var routeProps = {
38
44
  caseSensitive: nestedRoute.caseSensitive,
39
45
  path: nestedRoute.path,
40
46
  id: nestedRoute.id,
41
- loader: createLoader(nestedRoute),
47
+ loader: createLoader(nestedRoute, reporter),
42
48
  action: nestedRoute.action,
43
49
  hasErrorBoundary: nestedRoute.hasErrorBoundary,
44
50
  shouldRevalidate: nestedRoute.shouldRevalidate,
@@ -89,13 +95,15 @@ export var renderNestedRoute = function(nestedRoute) {
89
95
  } else {
90
96
  var _parent1;
91
97
  nestedRoute.loading = (_parent1 = parent) === null || _parent1 === void 0 ? void 0 : _parent1.loading;
98
+ routeProps.element = /* @__PURE__ */ _jsx(Outlet, {});
92
99
  }
93
100
  if (element) {
94
101
  routeProps.element = element;
95
102
  }
96
103
  var childElements = (_children = children) === null || _children === void 0 ? void 0 : _children.map(function(childRoute) {
97
104
  return renderNestedRoute(childRoute, {
98
- parent: nestedRoute
105
+ parent: nestedRoute,
106
+ reporter: reporter
99
107
  });
100
108
  });
101
109
  var routeElement = index ? /* @__PURE__ */ _jsx(Route, _object_spread_props(_object_spread({}, routeProps), {
@@ -106,15 +114,41 @@ export var renderNestedRoute = function(nestedRoute) {
106
114
  }), id);
107
115
  return routeElement;
108
116
  };
109
- function createLoader(route) {
117
+ function createLoader(route, reporter) {
110
118
  var loader = route.loader;
111
119
  if (loader) {
112
- return function(args) {
113
- if (typeof route.lazyImport === "function") {
114
- route.lazyImport();
115
- }
116
- return loader(args);
117
- };
120
+ return function() {
121
+ var _ref = _async_to_generator(function(args) {
122
+ var end, res, cost, _reporter;
123
+ return _ts_generator(this, function(_state) {
124
+ switch (_state.label) {
125
+ case 0:
126
+ if (typeof route.lazyImport === "function") {
127
+ route.lazyImport();
128
+ }
129
+ end = time();
130
+ return [
131
+ 4,
132
+ loader(args)
133
+ ];
134
+ case 1:
135
+ res = _state.sent();
136
+ cost = end();
137
+ if (typeof document === "undefined" && reporter) {
138
+ ;
139
+ (_reporter = reporter) === null || _reporter === void 0 ? void 0 : _reporter.reportTiming("".concat(LOADER_REPORTER_NAME, "-").concat(route.id), cost);
140
+ }
141
+ return [
142
+ 2,
143
+ res
144
+ ];
145
+ }
146
+ });
147
+ });
148
+ return function(args) {
149
+ return _ref.apply(this, arguments);
150
+ };
151
+ }();
118
152
  } else {
119
153
  return function() {
120
154
  if (typeof route.lazyImport === "function") {
@@ -3,3 +3,4 @@ var _createStorage = createStorage(), run = _createStorage.run, useHeaders = _cr
3
3
  export { run, useHeaders };
4
4
  export { serializeJson } from "./serialize";
5
5
  export * from "./nestedRoutes";
6
+ export * from "./loaderContext";
@@ -0,0 +1,20 @@
1
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
+ export var LoaderContext = /* @__PURE__ */ function() {
3
+ "use strict";
4
+ function LoaderContext2(defaultValue) {
5
+ _class_call_check(this, LoaderContext2);
6
+ this.defaultValue = defaultValue;
7
+ this.symbol = Symbol("loaderContext");
8
+ }
9
+ var _proto = LoaderContext2.prototype;
10
+ _proto.getDefaultValue = function getDefaultValue() {
11
+ if (!this.defaultValue) {
12
+ throw new Error("Can't get defaultValue before initialed");
13
+ }
14
+ return this.defaultValue;
15
+ };
16
+ return LoaderContext2;
17
+ }();
18
+ export function createLoaderContext(defaultValue) {
19
+ return new LoaderContext(defaultValue);
20
+ }
@@ -0,0 +1,24 @@
1
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
+ export var RequestContext = /* @__PURE__ */ function() {
3
+ "use strict";
4
+ function RequestContext2() {
5
+ _class_call_check(this, RequestContext2);
6
+ this.store = /* @__PURE__ */ new Map();
7
+ }
8
+ var _proto = RequestContext2.prototype;
9
+ _proto.get = function get(loaderCtx) {
10
+ var symbol = loaderCtx.symbol;
11
+ if (this.store.get(symbol)) {
12
+ return this.store.get(symbol);
13
+ }
14
+ return loaderCtx.getDefaultValue();
15
+ };
16
+ _proto.set = function set(loaderCtx, value) {
17
+ var symbol = loaderCtx.symbol;
18
+ this.store.set(symbol, value);
19
+ };
20
+ return RequestContext2;
21
+ }();
22
+ export function createRequestContext() {
23
+ return new RequestContext();
24
+ }
@@ -0,0 +1,3 @@
1
+ import { createLoaderContext } from "./createLoaderCtx";
2
+ export { createRequestContext } from "./createRequestCtx";
3
+ export var reporterCtx = createLoaderContext();
@@ -1,3 +1,4 @@
1
1
  export var ROUTE_MANIFEST = "_MODERNJS_ROUTE_MANIFEST";
2
2
  export var HMR_SOCK_PATH = "/webpack-hmr";
3
3
  export var HTML_CHUNKSMAP_SEPARATOR = "<!--<?- chunksMap.js ?>-->";
4
+ export var LOADER_REPORTER_NAME = "server-loader";
@@ -1,10 +1,14 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { Suspense } from "react";
3
- import { createRoutesFromElements, Route } from "react-router-dom";
4
- export const transformNestedRoutes = (routes) => {
3
+ import { createRoutesFromElements, Outlet, Route } from "react-router-dom";
4
+ import { LOADER_REPORTER_NAME } from "../universal/constants";
5
+ import { time } from "../universal/time";
6
+ export const transformNestedRoutes = (routes, reporter) => {
5
7
  const routeElements = [];
6
8
  for (const route of routes) {
7
- const routeElement = renderNestedRoute(route);
9
+ const routeElement = renderNestedRoute(route, {
10
+ reporter
11
+ });
8
12
  routeElements.push(routeElement);
9
13
  }
10
14
  return createRoutesFromElements(routeElements);
@@ -13,12 +17,12 @@ export const renderNestedRoute = (nestedRoute, options = {}) => {
13
17
  var _config, _children;
14
18
  const { children, index, id, component, isRoot, lazyImport, config, handle } = nestedRoute;
15
19
  const Component = component;
16
- const { parent, DeferredDataComponent, props = {} } = options;
20
+ const { parent, DeferredDataComponent, props = {}, reporter } = options;
17
21
  const routeProps = {
18
22
  caseSensitive: nestedRoute.caseSensitive,
19
23
  path: nestedRoute.path,
20
24
  id: nestedRoute.id,
21
- loader: createLoader(nestedRoute),
25
+ loader: createLoader(nestedRoute, reporter),
22
26
  action: nestedRoute.action,
23
27
  hasErrorBoundary: nestedRoute.hasErrorBoundary,
24
28
  shouldRevalidate: nestedRoute.shouldRevalidate,
@@ -74,13 +78,15 @@ export const renderNestedRoute = (nestedRoute, options = {}) => {
74
78
  } else {
75
79
  var _parent1;
76
80
  nestedRoute.loading = (_parent1 = parent) === null || _parent1 === void 0 ? void 0 : _parent1.loading;
81
+ routeProps.element = /* @__PURE__ */ _jsx(Outlet, {});
77
82
  }
78
83
  if (element) {
79
84
  routeProps.element = element;
80
85
  }
81
86
  const childElements = (_children = children) === null || _children === void 0 ? void 0 : _children.map((childRoute) => {
82
87
  return renderNestedRoute(childRoute, {
83
- parent: nestedRoute
88
+ parent: nestedRoute,
89
+ reporter
84
90
  });
85
91
  });
86
92
  const routeElement = index ? /* @__PURE__ */ _jsx(Route, {
@@ -93,14 +99,21 @@ export const renderNestedRoute = (nestedRoute, options = {}) => {
93
99
  }, id);
94
100
  return routeElement;
95
101
  };
96
- function createLoader(route) {
102
+ function createLoader(route, reporter) {
97
103
  const { loader } = route;
98
104
  if (loader) {
99
- return (args) => {
105
+ return async (args) => {
100
106
  if (typeof route.lazyImport === "function") {
101
107
  route.lazyImport();
102
108
  }
103
- return loader(args);
109
+ const end = time();
110
+ const res = await loader(args);
111
+ const cost = end();
112
+ if (typeof document === "undefined" && reporter) {
113
+ var _reporter;
114
+ (_reporter = reporter) === null || _reporter === void 0 ? void 0 : _reporter.reportTiming(`${LOADER_REPORTER_NAME}-${route.id}`, cost);
115
+ }
116
+ return res;
104
117
  };
105
118
  } else {
106
119
  return () => {
@@ -3,3 +3,4 @@ const { run, useContext: useHeaders } = createStorage();
3
3
  export { run, useHeaders };
4
4
  export { serializeJson } from "./serialize";
5
5
  export * from "./nestedRoutes";
6
+ export * from "./loaderContext";
@@ -0,0 +1,15 @@
1
+ export class LoaderContext {
2
+ getDefaultValue() {
3
+ if (!this.defaultValue) {
4
+ throw new Error("Can't get defaultValue before initialed");
5
+ }
6
+ return this.defaultValue;
7
+ }
8
+ constructor(defaultValue) {
9
+ this.defaultValue = defaultValue;
10
+ this.symbol = Symbol("loaderContext");
11
+ }
12
+ }
13
+ export function createLoaderContext(defaultValue) {
14
+ return new LoaderContext(defaultValue);
15
+ }
@@ -0,0 +1,19 @@
1
+ export class RequestContext {
2
+ get(loaderCtx) {
3
+ const { symbol } = loaderCtx;
4
+ if (this.store.get(symbol)) {
5
+ return this.store.get(symbol);
6
+ }
7
+ return loaderCtx.getDefaultValue();
8
+ }
9
+ set(loaderCtx, value) {
10
+ const { symbol } = loaderCtx;
11
+ this.store.set(symbol, value);
12
+ }
13
+ constructor() {
14
+ this.store = /* @__PURE__ */ new Map();
15
+ }
16
+ }
17
+ export function createRequestContext() {
18
+ return new RequestContext();
19
+ }
@@ -0,0 +1,3 @@
1
+ import { createLoaderContext } from "./createLoaderCtx";
2
+ export { createRequestContext } from "./createRequestCtx";
3
+ export const reporterCtx = createLoaderContext();
@@ -1,3 +1,4 @@
1
1
  export const ROUTE_MANIFEST = `_MODERNJS_ROUTE_MANIFEST`;
2
2
  export const HMR_SOCK_PATH = "/webpack-hmr";
3
3
  export const HTML_CHUNKSMAP_SEPARATOR = "<!--<?- chunksMap.js ?>-->";
4
+ export const LOADER_REPORTER_NAME = `server-loader`;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
- import type { NestedRoute } from '@modern-js/types';
3
- export declare const transformNestedRoutes: (routes: NestedRoute[]) => import("react-router-dom").RouteObject[];
2
+ import type { NestedRoute, Reporter } from '@modern-js/types';
3
+ export declare const transformNestedRoutes: (routes: NestedRoute[], reporter: Reporter) => import("react-router-dom").RouteObject[];
4
4
  type DeferredDataComponentType = (props?: {
5
5
  nonce?: string;
6
6
  }) => JSX.Element | null;
@@ -8,5 +8,6 @@ export declare const renderNestedRoute: (nestedRoute: NestedRoute, options?: {
8
8
  parent?: NestedRoute;
9
9
  DeferredDataComponent?: DeferredDataComponentType;
10
10
  props?: Record<string, any>;
11
+ reporter?: Reporter;
11
12
  }) => JSX.Element;
12
13
  export {};
@@ -6,4 +6,5 @@ import type { IncomingHttpHeaders } from 'http';
6
6
  declare const run: <O>(context: IncomingHttpHeaders, cb: () => O | Promise<O>) => Promise<O>, useHeaders: () => IncomingHttpHeaders;
7
7
  export { run, useHeaders };
8
8
  export { serializeJson } from './serialize';
9
- export * from './nestedRoutes';
9
+ export * from './nestedRoutes';
10
+ export * from './loaderContext';
@@ -0,0 +1,7 @@
1
+ export declare class LoaderContext<T = unknown> {
2
+ symbol: symbol;
3
+ private defaultValue?;
4
+ constructor(defaultValue?: T);
5
+ getDefaultValue(): T;
6
+ }
7
+ export declare function createLoaderContext<T = unknown>(defaultValue?: T): LoaderContext<T>;
@@ -0,0 +1,7 @@
1
+ import { LoaderContext } from './createLoaderCtx';
2
+ export declare class RequestContext {
3
+ private store;
4
+ get<T>(loaderCtx: LoaderContext<T>): T;
5
+ set<T = unknown>(loaderCtx: LoaderContext, value: T): void;
6
+ }
7
+ export declare function createRequestContext(): RequestContext;
@@ -0,0 +1,4 @@
1
+ import type { Reporter } from '@modern-js/types';
2
+ export { createRequestContext, type RequestContext } from './createRequestCtx';
3
+ export type { LoaderContext } from './createLoaderCtx';
4
+ export declare const reporterCtx: import("./createLoaderCtx").LoaderContext<Reporter>;
@@ -1,3 +1,3 @@
1
1
  import type { ModernServerContext, ServerRoute } from '@modern-js/types';
2
- export type ServerContext = Pick<ModernServerContext, 'logger' | 'req' | 'res' | 'params' | 'headers' | 'method' | 'url' | 'host' | 'protocol' | 'origin' | 'href' | 'path' | 'query'>;
2
+ export type ServerContext = Pick<ModernServerContext, 'logger' | 'req' | 'reporter' | 'res' | 'params' | 'headers' | 'method' | 'url' | 'host' | 'protocol' | 'origin' | 'href' | 'path' | 'query'>;
3
3
  export declare const matchEntry: (pathname: string, entries: ServerRoute[]) => ServerRoute | undefined;
@@ -9,4 +9,8 @@ export declare const HMR_SOCK_PATH = "/webpack-hmr";
9
9
  /**
10
10
  * html placeholder
11
11
  */
12
- export declare const HTML_CHUNKSMAP_SEPARATOR = "<!--<?- chunksMap.js ?>-->";
12
+ export declare const HTML_CHUNKSMAP_SEPARATOR = "<!--<?- chunksMap.js ?>-->";
13
+ /**
14
+ * reporter name for server loader
15
+ */
16
+ export declare const LOADER_REPORTER_NAME = "server-loader";
package/package.json CHANGED
@@ -15,11 +15,11 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.31.2",
18
+ "version": "2.32.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
22
- "module": "./dist/index.js",
22
+ "module": "./dist/esm/index.js",
23
23
  "_comment": "Provide ESM and CJS exports, ESM is used by runtime package, for treeshaking",
24
24
  "exports": {
25
25
  ".": {
@@ -67,7 +67,7 @@
67
67
  "default": "./dist/cjs/universal/constants.js"
68
68
  },
69
69
  "./universal/format-webpack": {
70
- "types": "./dist/types/universal/format-webpack.d.ts",
70
+ "types": "./dist/types/universal/formatWebpack.d.ts",
71
71
  "import": "./dist/esm/universal/formatWebpack.js",
72
72
  "default": "./dist/cjs/universal/formatWebpack.js"
73
73
  },
@@ -243,9 +243,9 @@
243
243
  "typescript": "^5",
244
244
  "webpack": "^5.88.1",
245
245
  "@types/serialize-javascript": "^5.0.1",
246
- "@modern-js/types": "2.31.2",
247
- "@scripts/build": "2.31.1",
248
- "@scripts/jest-config": "2.31.1"
246
+ "@scripts/build": "2.32.0",
247
+ "@scripts/jest-config": "2.32.0",
248
+ "@modern-js/types": "2.32.0"
249
249
  },
250
250
  "sideEffects": false,
251
251
  "scripts": {