@modern-js/utils 2.15.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.
Files changed (118) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/FileSizeReporter.js +69 -92
  3. package/dist/cjs/alias.js +34 -58
  4. package/dist/cjs/analyzeProject.js +58 -41
  5. package/dist/cjs/applyOptionsChain.js +15 -42
  6. package/dist/cjs/babel.js +28 -39
  7. package/dist/cjs/chainId.js +7 -25
  8. package/dist/cjs/clearConsole.js +7 -25
  9. package/dist/cjs/commands.js +14 -29
  10. package/dist/cjs/compatRequire.js +22 -38
  11. package/dist/cjs/compiled.js +120 -139
  12. package/dist/cjs/constants.js +143 -98
  13. package/dist/cjs/debug.js +8 -26
  14. package/dist/cjs/emptyDir.js +10 -28
  15. package/dist/cjs/ensureAbsolutePath.js +13 -36
  16. package/dist/cjs/ensureArray.js +10 -26
  17. package/dist/cjs/findExists.js +14 -37
  18. package/dist/cjs/generateMetaTags.js +12 -31
  19. package/dist/cjs/getBrowserslist.js +20 -27
  20. package/dist/cjs/getCoreJsVersion.js +49 -37
  21. package/dist/cjs/getEntryOptions.js +14 -29
  22. package/dist/cjs/getPackageManager.js +23 -46
  23. package/dist/cjs/getPort.js +26 -61
  24. package/dist/cjs/getServerConfig.js +51 -43
  25. package/dist/cjs/getTargetDir.js +50 -38
  26. package/dist/cjs/import.js +16 -27
  27. package/dist/cjs/index.js +61 -60
  28. package/dist/cjs/is/index.js +53 -68
  29. package/dist/cjs/is/nodeEnv.js +13 -29
  30. package/dist/cjs/is/platform.js +13 -26
  31. package/dist/cjs/is/type.js +14 -33
  32. package/dist/cjs/logger.js +54 -50
  33. package/dist/cjs/monorepo.js +47 -78
  34. package/dist/cjs/nodeEnv.js +29 -31
  35. package/dist/cjs/path.js +42 -63
  36. package/dist/cjs/pathSerializer.js +43 -51
  37. package/dist/cjs/plugin.js +11 -29
  38. package/dist/cjs/prettyInstructions.js +35 -68
  39. package/dist/cjs/printBuildError.js +15 -37
  40. package/dist/cjs/react.js +18 -41
  41. package/dist/cjs/readTsConfig.js +21 -39
  42. package/dist/cjs/removeSlash.js +13 -27
  43. package/dist/cjs/resolve.js +25 -31
  44. package/dist/cjs/routes.js +23 -43
  45. package/dist/cjs/runtime/index.js +18 -17
  46. package/dist/cjs/runtime/parsed.js +8 -26
  47. package/dist/cjs/runtimeExports.js +31 -56
  48. package/dist/cjs/ssr.js +13 -26
  49. package/dist/cjs/storage.js +51 -41
  50. package/dist/cjs/testUtils.js +8 -29
  51. package/dist/cjs/types.js +4 -15
  52. package/dist/cjs/universal/constants.js +13 -26
  53. package/dist/cjs/universal/formatWebpack.js +16 -44
  54. package/dist/cjs/universal/nestedRoutes.js +94 -46
  55. package/dist/cjs/universal/pluginDagSort.js +58 -0
  56. package/dist/cjs/universal/remixRouter.js +18 -17
  57. package/dist/cjs/universal/serialize.js +16 -37
  58. package/dist/cjs/version.js +25 -41
  59. package/dist/cjs/wait.js +7 -25
  60. package/dist/cjs/watch.js +69 -43
  61. package/dist/esm/FileSizeReporter.js +40 -52
  62. package/dist/esm/alias.js +9 -19
  63. package/dist/esm/analyzeProject.js +3 -7
  64. package/dist/esm/applyOptionsChain.js +5 -17
  65. package/dist/esm/babel.js +6 -8
  66. package/dist/esm/chainId.js +1 -4
  67. package/dist/esm/clearConsole.js +1 -4
  68. package/dist/esm/commands.js +6 -12
  69. package/dist/esm/compatRequire.js +10 -17
  70. package/dist/esm/compiled.js +32 -76
  71. package/dist/esm/constants.js +143 -89
  72. package/dist/esm/debug.js +1 -4
  73. package/dist/esm/emptyDir.js +1 -4
  74. package/dist/esm/ensureAbsolutePath.js +1 -4
  75. package/dist/esm/ensureArray.js +4 -5
  76. package/dist/esm/findExists.js +1 -4
  77. package/dist/esm/generateMetaTags.js +6 -10
  78. package/dist/esm/getBrowserslist.js +8 -6
  79. package/dist/esm/getCoreJsVersion.js +1 -4
  80. package/dist/esm/getEntryOptions.js +5 -5
  81. package/dist/esm/getPackageManager.js +1 -4
  82. package/dist/esm/getPort.js +10 -25
  83. package/dist/esm/getServerConfig.js +2 -9
  84. package/dist/esm/getTargetDir.js +1 -4
  85. package/dist/esm/import.js +3 -4
  86. package/dist/esm/is/index.js +18 -30
  87. package/dist/esm/is/nodeEnv.js +5 -12
  88. package/dist/esm/is/platform.js +2 -6
  89. package/dist/esm/is/type.js +8 -18
  90. package/dist/esm/logger.js +37 -17
  91. package/dist/esm/monorepo.js +21 -38
  92. package/dist/esm/nodeEnv.js +18 -11
  93. package/dist/esm/path.js +16 -23
  94. package/dist/esm/pathSerializer.js +21 -21
  95. package/dist/esm/plugin.js +1 -4
  96. package/dist/esm/prettyInstructions.js +13 -32
  97. package/dist/esm/printBuildError.js +4 -11
  98. package/dist/esm/react.js +1 -4
  99. package/dist/esm/readTsConfig.js +2 -6
  100. package/dist/esm/removeSlash.js +3 -8
  101. package/dist/esm/resolve.js +14 -11
  102. package/dist/esm/routes.js +4 -10
  103. package/dist/esm/runtime/parsed.js +2 -5
  104. package/dist/esm/runtimeExports.js +17 -22
  105. package/dist/esm/ssr.js +1 -4
  106. package/dist/esm/storage.js +2 -6
  107. package/dist/esm/testUtils.js +2 -8
  108. package/dist/esm/types.js +1 -0
  109. package/dist/esm/universal/constants.js +2 -6
  110. package/dist/esm/universal/formatWebpack.js +10 -22
  111. package/dist/esm/universal/nestedRoutes.js +41 -27
  112. package/dist/esm/universal/pluginDagSort.js +50 -0
  113. package/dist/esm/universal/serialize.js +4 -5
  114. package/dist/esm/version.js +5 -7
  115. package/dist/esm/wait.js +1 -3
  116. package/dist/esm/watch.js +14 -9
  117. package/dist/types/universal/pluginDagSort.d.ts +1 -0
  118. package/package.json +17 -5
@@ -1,10 +1,7 @@
1
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { Suspense } from "react";
3
- import {
4
- createRoutesFromElements,
5
- Route
6
- } from "react-router-dom";
7
- const transformNestedRoutes = (routes) => {
3
+ import { createRoutesFromElements, Route } from "react-router-dom";
4
+ export const transformNestedRoutes = (routes) => {
8
5
  const routeElements = [];
9
6
  for (const route of routes) {
10
7
  const routeElement = renderNestedRoute(route);
@@ -12,7 +9,7 @@ const transformNestedRoutes = (routes) => {
12
9
  }
13
10
  return createRoutesFromElements(routeElements);
14
11
  };
15
- const renderNestedRoute = (nestedRoute, options = {}) => {
12
+ export const renderNestedRoute = (nestedRoute, options = {}) => {
16
13
  const { children, index, id, component, isRoot, lazyImport, config } = nestedRoute;
17
14
  const Component = component;
18
15
  const { parent, DeferredDataComponent, props = {} } = options;
@@ -24,46 +21,67 @@ const renderNestedRoute = (nestedRoute, options = {}) => {
24
21
  action: nestedRoute.action,
25
22
  hasErrorBoundary: nestedRoute.hasErrorBoundary,
26
23
  shouldRevalidate: nestedRoute.shouldRevalidate,
27
- handle: typeof config === "object" ? config == null ? void 0 : config.handle : {},
24
+ handle: typeof config === "object" ? config === null || config === void 0 ? void 0 : config.handle : {},
28
25
  index: nestedRoute.index,
29
26
  element: nestedRoute.element,
30
27
  errorElement: nestedRoute.errorElement
31
28
  };
32
29
  if (nestedRoute.error) {
33
- const errorElement = /* @__PURE__ */ jsx(nestedRoute.error, {});
30
+ const errorElement = /* @__PURE__ */ _jsx(nestedRoute.error, {});
34
31
  routeProps.errorElement = errorElement;
35
32
  }
36
33
  let element;
37
34
  if (Component) {
38
- if ((parent == null ? void 0 : parent.loading) && lazyImport) {
35
+ if ((parent === null || parent === void 0 ? void 0 : parent.loading) && lazyImport) {
39
36
  const Loading = parent.loading;
40
37
  if (isLoadableComponent(Component)) {
41
- element = /* @__PURE__ */ jsx(Component, { fallback: /* @__PURE__ */ jsx(Loading, {}) });
38
+ element = /* @__PURE__ */ _jsx(Component, {
39
+ fallback: /* @__PURE__ */ _jsx(Loading, {})
40
+ });
42
41
  } else {
43
- element = /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(Loading, {}), children: /* @__PURE__ */ jsx(Component, {}) });
42
+ element = /* @__PURE__ */ _jsx(Suspense, {
43
+ fallback: /* @__PURE__ */ _jsx(Loading, {}),
44
+ children: /* @__PURE__ */ _jsx(Component, {})
45
+ });
44
46
  }
45
47
  } else if (isLoadableComponent(Component) && lazyImport) {
46
- element = /* @__PURE__ */ jsx(Component, {});
48
+ element = /* @__PURE__ */ _jsx(Component, {});
47
49
  } else if (isRoot) {
48
- element = /* @__PURE__ */ jsxs(Fragment, { children: [
49
- /* @__PURE__ */ jsx(Component, { ...props }),
50
- typeof document === "undefined" && DeferredDataComponent && /* @__PURE__ */ jsx(DeferredDataComponent, {})
51
- ] });
50
+ element = /* @__PURE__ */ _jsxs(_Fragment, {
51
+ children: [
52
+ /* @__PURE__ */ _jsx(Component, {
53
+ ...props
54
+ }),
55
+ typeof document === "undefined" && DeferredDataComponent && /* @__PURE__ */ _jsx(DeferredDataComponent, {})
56
+ ]
57
+ });
52
58
  } else if (lazyImport) {
53
- element = /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(Component, {}) });
59
+ element = /* @__PURE__ */ _jsx(Suspense, {
60
+ fallback: null,
61
+ children: /* @__PURE__ */ _jsx(Component, {})
62
+ });
54
63
  } else {
55
- element = /* @__PURE__ */ jsx(Component, {});
64
+ element = /* @__PURE__ */ _jsx(Component, {});
56
65
  }
57
66
  } else {
58
- nestedRoute.loading = parent == null ? void 0 : parent.loading;
67
+ nestedRoute.loading = parent === null || parent === void 0 ? void 0 : parent.loading;
59
68
  }
60
69
  if (element) {
61
70
  routeProps.element = element;
62
71
  }
63
- const childElements = children == null ? void 0 : children.map((childRoute) => {
64
- return renderNestedRoute(childRoute, { parent: nestedRoute });
72
+ const childElements = children === null || children === void 0 ? void 0 : children.map((childRoute) => {
73
+ return renderNestedRoute(childRoute, {
74
+ parent: nestedRoute
75
+ });
65
76
  });
66
- const routeElement = index ? /* @__PURE__ */ jsx(Route, { ...routeProps, index: true }, id) : /* @__PURE__ */ jsx(Route, { ...routeProps, index: false, children: childElements }, id);
77
+ const routeElement = index ? /* @__PURE__ */ _jsx(Route, {
78
+ ...routeProps,
79
+ index: true
80
+ }, id) : /* @__PURE__ */ _jsx(Route, {
81
+ ...routeProps,
82
+ index: false,
83
+ children: childElements
84
+ }, id);
67
85
  return routeElement;
68
86
  };
69
87
  function createLoader(route) {
@@ -87,7 +105,3 @@ function createLoader(route) {
87
105
  function isLoadableComponent(component) {
88
106
  return component && component.displayName === "Loadable" && component.preload && typeof component.preload === "function";
89
107
  }
90
- export {
91
- renderNestedRoute,
92
- transformNestedRoutes
93
- };
@@ -0,0 +1,50 @@
1
+ export const pluginDagSort = (plugins, key = "name", preKey = "pre", postKey = "post") => {
2
+ let allLines = [];
3
+ function getPluginByAny(q) {
4
+ const target = plugins.find((item) => typeof q === "string" ? item[key] === q : item[key] === q[key]);
5
+ if (!target) {
6
+ throw new Error(`plugin ${q} not existed`);
7
+ }
8
+ return target;
9
+ }
10
+ plugins.forEach((item) => {
11
+ var _item_preKey, _item_postKey;
12
+ (_item_preKey = item[preKey]) === null || _item_preKey === void 0 ? void 0 : _item_preKey.forEach((p) => {
13
+ if (plugins.find((ap) => ap.name === p)) {
14
+ allLines.push([
15
+ getPluginByAny(p)[key],
16
+ getPluginByAny(item)[key]
17
+ ]);
18
+ }
19
+ });
20
+ (_item_postKey = item[postKey]) === null || _item_postKey === void 0 ? void 0 : _item_postKey.forEach((pt) => {
21
+ if (plugins.find((ap) => ap.name === pt)) {
22
+ allLines.push([
23
+ getPluginByAny(item)[key],
24
+ getPluginByAny(pt)[key]
25
+ ]);
26
+ }
27
+ });
28
+ });
29
+ let zeroEndPoints = plugins.filter((item) => !allLines.find((l) => l[1] === item[key]));
30
+ const sortedPoint = [];
31
+ while (zeroEndPoints.length) {
32
+ const zep = zeroEndPoints.shift();
33
+ sortedPoint.push(getPluginByAny(zep));
34
+ allLines = allLines.filter((l) => l[0] !== getPluginByAny(zep)[key]);
35
+ const restPoints = plugins.filter((item) => !sortedPoint.find((sp) => sp[key] === item[key]));
36
+ zeroEndPoints = restPoints.filter(
37
+ // eslint-disable-next-line @typescript-eslint/no-loop-func
38
+ (item) => !allLines.find((l) => l[1] === item[key])
39
+ );
40
+ }
41
+ if (allLines.length) {
42
+ const restInRingPoints = {};
43
+ allLines.forEach((l) => {
44
+ restInRingPoints[l[0]] = true;
45
+ restInRingPoints[l[1]] = true;
46
+ });
47
+ throw new Error(`plugins dependences has loop: ${Object.keys(restInRingPoints).join(",")}`);
48
+ }
49
+ return sortedPoint;
50
+ };
@@ -1,7 +1,6 @@
1
1
  import serialize from "serialize-javascript";
2
- const serializeJson = (data) => {
3
- return serialize(data, { isJSON: true });
4
- };
5
- export {
6
- serializeJson
2
+ export const serializeJson = (data) => {
3
+ return serialize(data, {
4
+ isJSON: true
5
+ });
7
6
  };
@@ -1,10 +1,12 @@
1
1
  import path from "path";
2
2
  import { fs, execa, semver } from "./compiled";
3
- async function getPnpmVersion() {
4
- const { stdout } = await execa("pnpm", ["--version"]);
3
+ export async function getPnpmVersion() {
4
+ const { stdout } = await execa("pnpm", [
5
+ "--version"
6
+ ]);
5
7
  return stdout;
6
8
  }
7
- const isReact18 = (cwd) => {
9
+ export const isReact18 = (cwd) => {
8
10
  const pkgPath = path.join(cwd, "package.json");
9
11
  if (!fs.existsSync(pkgPath)) {
10
12
  return false;
@@ -19,7 +21,3 @@ const isReact18 = (cwd) => {
19
21
  }
20
22
  return semver.satisfies(semver.minVersion(deps.react), ">=18.0.0");
21
23
  };
22
- export {
23
- getPnpmVersion,
24
- isReact18
25
- };
package/dist/esm/wait.js CHANGED
@@ -1,6 +1,4 @@
1
1
  const wait = (time = 0) => new Promise((resolve) => {
2
2
  setTimeout(resolve, time);
3
3
  });
4
- export {
5
- wait
6
- };
4
+ export { wait };
package/dist/esm/watch.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as path from "path";
2
2
  import { chokidar } from "./compiled";
3
- const WatchChangeType = {
3
+ export const WatchChangeType = {
4
4
  ADD: "add",
5
5
  UNLINK: "unlink",
6
6
  CHANGE: "change"
7
7
  };
8
- const watch = (watchDir, runTask, ignored = []) => {
8
+ export const watch = (watchDir, runTask, ignored = []) => {
9
9
  let ready = false;
10
10
  const watcher = chokidar.watch(watchDir, {
11
11
  ignored
@@ -13,24 +13,29 @@ const watch = (watchDir, runTask, ignored = []) => {
13
13
  watcher.on("ready", () => ready = true);
14
14
  watcher.on("change", async (filePath) => {
15
15
  const changedFilePath = path.resolve(filePath);
16
- await runTask({ changedFilePath, changeType: WatchChangeType.CHANGE });
16
+ await runTask({
17
+ changedFilePath,
18
+ changeType: WatchChangeType.CHANGE
19
+ });
17
20
  });
18
21
  watcher.on("add", async (filePath) => {
19
22
  const changedFilePath = path.resolve(filePath);
20
23
  if (ready) {
21
- await runTask({ changedFilePath, changeType: WatchChangeType.ADD });
24
+ await runTask({
25
+ changedFilePath,
26
+ changeType: WatchChangeType.ADD
27
+ });
22
28
  }
23
29
  });
24
30
  watcher.on("unlink", async (filePath) => {
25
31
  const changedFilePath = path.resolve(filePath);
26
- await runTask({ changedFilePath, changeType: WatchChangeType.UNLINK });
32
+ await runTask({
33
+ changedFilePath,
34
+ changeType: WatchChangeType.UNLINK
35
+ });
27
36
  });
28
37
  watcher.on("error", (err) => {
29
38
  throw err;
30
39
  });
31
40
  return watcher;
32
41
  };
33
- export {
34
- WatchChangeType,
35
- watch
36
- };
@@ -0,0 +1 @@
1
+ export declare const pluginDagSort: <P extends Record<string, any>>(plugins: P[], key?: string, preKey?: string, postKey?: string) => P[];
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/toolkit/utils"
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.15.0",
18
+ "version": "2.16.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/index.d.ts",
17
21
  "main": "./dist/index.js",
@@ -95,6 +99,11 @@
95
99
  "import": "./dist/esm/universal/nestedRoutes.js",
96
100
  "types": "./dist/types/universal/nestedRoutes.d.ts",
97
101
  "default": "./dist/cjs/universal/nestedRoutes.js"
102
+ },
103
+ "./universal/pluginDagSort": {
104
+ "import": "./dist/esm/universal/pluginDagSort.js",
105
+ "types": "./dist/types/universal/pluginDagSort.d.ts",
106
+ "default": "./dist/cjs/universal/pluginDagSort.js"
98
107
  }
99
108
  },
100
109
  "publishConfig": {
@@ -195,6 +204,9 @@
195
204
  ],
196
205
  "universal/nestedRoutes": [
197
206
  "./dist/types/universal/nestedRoutes.d.ts"
207
+ ],
208
+ "universal/pluginDagSort": [
209
+ "./dist/types/universal/pluginDagSort.d.ts"
198
210
  ]
199
211
  }
200
212
  },
@@ -230,9 +242,9 @@
230
242
  "typescript": "^4",
231
243
  "webpack": "^5.76.2",
232
244
  "@types/serialize-javascript": "^5.0.1",
233
- "@modern-js/types": "2.15.0",
234
- "@scripts/build": "2.15.0",
235
- "@scripts/jest-config": "2.15.0"
245
+ "@modern-js/types": "2.16.0",
246
+ "@scripts/build": "2.16.0",
247
+ "@scripts/jest-config": "2.16.0"
236
248
  },
237
249
  "sideEffects": false,
238
250
  "scripts": {