@modern-js/runtime 2.52.0 → 2.54.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 (123) hide show
  1. package/dist/cjs/{runtimeContext.js → cli/constants.js} +6 -10
  2. package/dist/cjs/cli/entry.js +42 -0
  3. package/dist/cjs/cli/index.js +10 -0
  4. package/dist/cjs/core/compatible.js +13 -29
  5. package/dist/cjs/core/{appConfig.js → config.js} +6 -4
  6. package/dist/cjs/core/context/index.js +28 -0
  7. package/dist/cjs/core/context/runtime.js +42 -0
  8. package/dist/cjs/core/index.js +8 -12
  9. package/dist/cjs/core/loader/useLoader.js +2 -2
  10. package/dist/cjs/core/{plugin.js → plugin/base.js} +5 -25
  11. package/dist/cjs/core/plugin/index.js +39 -0
  12. package/dist/cjs/core/plugin/runner.js +42 -0
  13. package/dist/cjs/index.js +2 -4
  14. package/dist/cjs/router/cli/code/getClientRoutes/getRoutes.js +197 -0
  15. package/dist/cjs/router/cli/code/getClientRoutes/getRoutesLegacy.js +195 -0
  16. package/dist/cjs/router/cli/code/getClientRoutes/index.js +31 -0
  17. package/dist/cjs/router/cli/code/getClientRoutes/utils.js +59 -0
  18. package/dist/cjs/router/cli/code/index.js +134 -0
  19. package/dist/cjs/router/cli/code/makeLegalIdentifier.js +37 -0
  20. package/dist/cjs/router/cli/code/nestedRoutes.js +294 -0
  21. package/dist/cjs/router/cli/code/templates.js +371 -0
  22. package/dist/cjs/router/cli/code/utils.js +143 -0
  23. package/dist/cjs/router/cli/constants.js +83 -0
  24. package/dist/cjs/router/cli/entry.js +87 -0
  25. package/dist/cjs/router/cli/handler.js +71 -0
  26. package/dist/cjs/router/cli/index.js +25 -0
  27. package/dist/cjs/router/runtime/plugin.js +7 -4
  28. package/dist/cjs/router/runtime/plugin.node.js +12 -5
  29. package/dist/cjs/ssr/index.node.js +1 -0
  30. package/dist/cjs/state/runtime/plugin.js +3 -2
  31. package/dist/esm/cli/constants.js +4 -0
  32. package/dist/esm/cli/entry.js +14 -0
  33. package/dist/esm/cli/index.js +10 -0
  34. package/dist/esm/core/compatible.js +11 -30
  35. package/dist/esm/core/{appConfig.js → config.js} +4 -1
  36. package/dist/esm/core/context/index.js +4 -0
  37. package/dist/esm/core/context/runtime.js +18 -0
  38. package/dist/esm/core/index.js +4 -6
  39. package/dist/esm/core/loader/useLoader.js +1 -1
  40. package/dist/esm/core/plugin/base.js +21 -0
  41. package/dist/esm/core/plugin/index.js +15 -0
  42. package/dist/esm/core/plugin/runner.js +17 -0
  43. package/dist/esm/index.js +2 -3
  44. package/dist/esm/router/cli/code/getClientRoutes/getRoutes.js +185 -0
  45. package/dist/esm/router/cli/code/getClientRoutes/getRoutesLegacy.js +183 -0
  46. package/dist/esm/router/cli/code/getClientRoutes/index.js +6 -0
  47. package/dist/esm/router/cli/code/getClientRoutes/utils.js +28 -0
  48. package/dist/esm/router/cli/code/index.js +214 -0
  49. package/dist/esm/router/cli/code/makeLegalIdentifier.js +15 -0
  50. package/dist/esm/router/cli/code/nestedRoutes.js +397 -0
  51. package/dist/esm/router/cli/code/templates.js +417 -0
  52. package/dist/esm/router/cli/code/utils.js +212 -0
  53. package/dist/esm/router/cli/constants.js +47 -0
  54. package/dist/esm/router/cli/entry.js +57 -0
  55. package/dist/esm/router/cli/handler.js +103 -0
  56. package/dist/esm/router/cli/index.js +57 -0
  57. package/dist/esm/router/runtime/plugin.js +7 -4
  58. package/dist/esm/router/runtime/plugin.node.js +13 -6
  59. package/dist/esm/ssr/index.node.js +1 -0
  60. package/dist/esm/state/runtime/plugin.js +3 -2
  61. package/dist/esm-node/cli/constants.js +4 -0
  62. package/dist/esm-node/cli/entry.js +8 -0
  63. package/dist/esm-node/cli/index.js +9 -0
  64. package/dist/esm-node/core/compatible.js +12 -28
  65. package/dist/esm-node/core/{appConfig.js → config.js} +2 -1
  66. package/dist/esm-node/core/context/index.js +4 -0
  67. package/dist/esm-node/core/context/runtime.js +16 -0
  68. package/dist/esm-node/core/index.js +4 -6
  69. package/dist/esm-node/core/loader/useLoader.js +1 -1
  70. package/dist/esm-node/core/plugin/base.js +19 -0
  71. package/dist/esm-node/core/plugin/index.js +13 -0
  72. package/dist/esm-node/core/plugin/runner.js +17 -0
  73. package/dist/esm-node/index.js +2 -3
  74. package/dist/esm-node/router/cli/code/getClientRoutes/getRoutes.js +163 -0
  75. package/dist/esm-node/router/cli/code/getClientRoutes/getRoutesLegacy.js +161 -0
  76. package/dist/esm-node/router/cli/code/getClientRoutes/index.js +6 -0
  77. package/dist/esm-node/router/cli/code/getClientRoutes/utils.js +22 -0
  78. package/dist/esm-node/router/cli/code/index.js +100 -0
  79. package/dist/esm-node/router/cli/code/makeLegalIdentifier.js +13 -0
  80. package/dist/esm-node/router/cli/code/nestedRoutes.js +258 -0
  81. package/dist/esm-node/router/cli/code/templates.js +335 -0
  82. package/dist/esm-node/router/cli/code/utils.js +101 -0
  83. package/dist/esm-node/router/cli/constants.js +47 -0
  84. package/dist/esm-node/router/cli/entry.js +50 -0
  85. package/dist/esm-node/router/cli/handler.js +36 -0
  86. package/dist/esm-node/router/cli/index.js +22 -0
  87. package/dist/esm-node/router/runtime/plugin.js +7 -4
  88. package/dist/esm-node/router/runtime/plugin.node.js +12 -5
  89. package/dist/esm-node/ssr/index.node.js +1 -0
  90. package/dist/esm-node/state/runtime/plugin.js +3 -2
  91. package/dist/types/cli/constants.d.ts +1 -0
  92. package/dist/types/cli/entry.d.ts +1 -0
  93. package/dist/types/cli/index.d.ts +1 -0
  94. package/dist/types/core/compatible.d.ts +7 -3
  95. package/dist/types/core/config.d.ts +14 -0
  96. package/dist/types/core/context/index.d.ts +1 -0
  97. package/dist/types/{runtimeContext.d.ts → core/context/runtime.d.ts} +5 -4
  98. package/dist/types/core/index.d.ts +4 -4
  99. package/dist/types/core/plugin/base.d.ts +80 -0
  100. package/dist/types/core/plugin/index.d.ts +29 -0
  101. package/dist/types/core/plugin/runner.d.ts +26 -0
  102. package/dist/types/index.d.ts +2 -2
  103. package/dist/types/router/cli/code/getClientRoutes/getRoutes.d.ts +8 -0
  104. package/dist/types/router/cli/code/getClientRoutes/getRoutesLegacy.d.ts +9 -0
  105. package/dist/types/router/cli/code/getClientRoutes/index.d.ts +2 -0
  106. package/dist/types/router/cli/code/getClientRoutes/utils.d.ts +5 -0
  107. package/dist/types/router/cli/code/index.d.ts +6 -0
  108. package/dist/types/router/cli/code/makeLegalIdentifier.d.ts +1 -0
  109. package/dist/types/router/cli/code/nestedRoutes.d.ts +7 -0
  110. package/dist/types/router/cli/code/templates.d.ts +14 -0
  111. package/dist/types/router/cli/code/utils.d.ts +12 -0
  112. package/dist/types/router/cli/constants.d.ts +32 -0
  113. package/dist/types/router/cli/entry.d.ts +5 -0
  114. package/dist/types/router/cli/handler.d.ts +5 -0
  115. package/dist/types/router/cli/index.d.ts +3 -1
  116. package/dist/types/ssr/serverRender/types.d.ts +3 -0
  117. package/package.json +37 -11
  118. package/dist/esm/core/plugin.js +0 -59
  119. package/dist/esm/runtimeContext.js +0 -7
  120. package/dist/esm-node/core/plugin.js +0 -37
  121. package/dist/esm-node/runtimeContext.js +0 -7
  122. package/dist/types/core/appConfig.d.ts +0 -4
  123. package/dist/types/core/plugin.d.ts +0 -204
@@ -2,20 +2,19 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
3
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
4
  import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
5
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
6
  import { jsx as _jsx } from "react/jsx-runtime";
8
7
  import React, { useContext, useMemo } from "react";
9
8
  import hoistNonReactStatics from "hoist-non-react-statics";
10
9
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
11
- import { RuntimeReactContext } from "../runtimeContext";
12
- import { runtime } from "./plugin";
10
+ import { RuntimeReactContext } from "./context/runtime";
11
+ import { registerPlugin } from "./plugin";
13
12
  import { createLoaderManager } from "./loader/loaderManager";
13
+ import { getGlobalRunner } from "./plugin/runner";
14
14
  var IS_REACT18 = process.env.IS_REACT18 === "true";
15
15
  function isClientArgs(id) {
16
16
  return typeof id === "string" || typeof HTMLElement !== "undefined" && _instanceof(id, HTMLElement);
17
17
  }
18
- var runnerMap = /* @__PURE__ */ new WeakMap();
19
18
  var getInitialContext = function(runner) {
20
19
  return {
21
20
  loaderManager: createLoaderManager({}),
@@ -25,33 +24,22 @@ var getInitialContext = function(runner) {
25
24
  };
26
25
  };
27
26
  var createApp = function(param) {
28
- var plugins = param.plugins, globalProps = param.props;
29
- var _appRuntime;
30
- var appRuntime = runtime.clone();
31
- (_appRuntime = appRuntime).usePlugin.apply(_appRuntime, _to_consumable_array(plugins));
27
+ var plugins = param.plugins, runtime = param.runtime, globalProps = param.props;
28
+ var runner = registerPlugin(plugins, {
29
+ plugins: []
30
+ }, runtime);
32
31
  return function(App) {
33
- var runner = appRuntime.init();
34
32
  var WrapperComponent = function(props) {
35
- var element = /* @__PURE__ */ React.createElement(App || React.Fragment, App ? _object_spread({}, props) : null, App ? props.children : React.Children.map(props.children, function(child) {
33
+ return /* @__PURE__ */ React.createElement(App || React.Fragment, App ? _object_spread({}, props) : null, App ? props.children : React.Children.map(props.children, function(child) {
36
34
  return /* @__PURE__ */ React.isValidElement(child) ? /* @__PURE__ */ React.cloneElement(child, _object_spread({}, child.props, props)) : child;
37
35
  }));
38
- var context = useContext(RuntimeReactContext);
39
- return runner.provide({
40
- element,
41
- props: _object_spread({}, props),
42
- context
43
- }, {
44
- onLast: function(param2) {
45
- var element2 = param2.element;
46
- return element2;
47
- }
48
- });
49
36
  };
50
37
  if (App) {
51
38
  hoistNonReactStatics(WrapperComponent, App);
52
39
  }
53
40
  var HOCApp = runner.hoc({
54
- App: WrapperComponent
41
+ App: WrapperComponent,
42
+ config: globalProps || {}
55
43
  }, {
56
44
  onLast: function(param2) {
57
45
  var _$App = param2.App;
@@ -81,7 +69,6 @@ var createApp = function(param) {
81
69
  return hoistNonReactStatics(WrapComponent, _$App);
82
70
  }
83
71
  });
84
- runnerMap.set(HOCApp, runner);
85
72
  return HOCApp;
86
73
  };
87
74
  };
@@ -92,13 +79,7 @@ var bootstrap = function() {
92
79
  switch (_state.label) {
93
80
  case 0:
94
81
  App = BootApp;
95
- runner = runnerMap.get(App);
96
- if (!runner) {
97
- App = createApp({
98
- plugins: []
99
- })(App);
100
- runner = runnerMap.get(App);
101
- }
82
+ runner = getGlobalRunner();
102
83
  context = getInitialContext(runner);
103
84
  runInit = function(_context) {
104
85
  return runner.init({
@@ -1,7 +1,6 @@
1
1
  var APP_CONFIG_SYMBOL = "config";
2
2
  var getConfig = function(Component) {
3
3
  return (
4
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
4
  // @ts-expect-error
6
5
  Component[APP_CONFIG_SYMBOL]
7
6
  );
@@ -10,7 +9,11 @@ var defineConfig = function(Component, config) {
10
9
  Component[APP_CONFIG_SYMBOL] = config;
11
10
  return Component;
12
11
  };
12
+ var defineRuntimeConfig = function(config) {
13
+ return config;
14
+ };
13
15
  export {
14
16
  defineConfig,
17
+ defineRuntimeConfig,
15
18
  getConfig
16
19
  };
@@ -0,0 +1,4 @@
1
+ import { RuntimeReactContext } from "./runtime";
2
+ export {
3
+ RuntimeReactContext
4
+ };
@@ -0,0 +1,18 @@
1
+ import { createContext } from "react";
2
+ import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
3
+ import { createLoaderManager } from "../loader/loaderManager";
4
+ var RuntimeReactContext = createContext({});
5
+ var ServerRouterContext = createContext({});
6
+ var getInitialContext = function(runner) {
7
+ return {
8
+ loaderManager: createLoaderManager({}),
9
+ runner,
10
+ isBrowser: true,
11
+ routeManifest: typeof window !== "undefined" && window[ROUTE_MANIFEST]
12
+ };
13
+ };
14
+ export {
15
+ RuntimeReactContext,
16
+ ServerRouterContext,
17
+ getInitialContext
18
+ };
@@ -1,16 +1,14 @@
1
- import { createPlugin, createRuntime, runtime, registerInit } from "./plugin";
2
- import { defineConfig, getConfig } from "./appConfig";
1
+ import { runtime } from "./plugin";
2
+ import { defineConfig, getConfig, defineRuntimeConfig } from "./config";
3
3
  export * from "./compatible";
4
- import { RuntimeReactContext, ServerRouterContext } from "../runtimeContext";
4
+ import { RuntimeReactContext, ServerRouterContext } from "./context/runtime";
5
5
  export * from "./loader";
6
6
  export * from "@modern-js/plugin";
7
7
  export {
8
8
  RuntimeReactContext,
9
9
  ServerRouterContext,
10
- createPlugin,
11
- createRuntime,
12
10
  defineConfig,
11
+ defineRuntimeConfig,
13
12
  getConfig,
14
- registerInit,
15
13
  runtime
16
14
  };
@@ -4,7 +4,7 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
4
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
5
  import { useContext, useRef, useMemo, useState, useCallback, useEffect } from "react";
6
6
  import invariant from "invariant";
7
- import { RuntimeReactContext } from "../../runtimeContext";
7
+ import { RuntimeReactContext } from "../context/runtime";
8
8
  import { LoaderStatus } from "./loaderManager";
9
9
  var useLoader = function(loaderFn) {
10
10
  var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
@@ -0,0 +1,21 @@
1
+ import { createManager, createPipeline, createAsyncPipeline } from "@modern-js/plugin";
2
+ var client = createAsyncPipeline();
3
+ var server = createAsyncPipeline();
4
+ var hoc = createPipeline();
5
+ var init = createAsyncPipeline();
6
+ var pickContext = createPipeline();
7
+ var runtimeHooks = {
8
+ hoc,
9
+ init,
10
+ client,
11
+ server,
12
+ pickContext
13
+ };
14
+ var createRuntime = function() {
15
+ return createManager(runtimeHooks);
16
+ };
17
+ var runtime = createRuntime();
18
+ export {
19
+ createRuntime,
20
+ runtime
21
+ };
@@ -0,0 +1,15 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import { runtime } from "./base";
3
+ import { setGlobalRunner } from "./runner";
4
+ export * from "./base";
5
+ function registerPlugin(internalPlugins, runtimeConfig, customRuntime) {
6
+ var _instance;
7
+ var _ref = runtimeConfig || {}, _ref_plugins = _ref.plugins, plugins = _ref_plugins === void 0 ? [] : _ref_plugins;
8
+ (_instance = customRuntime || runtime).usePlugin.apply(_instance, _to_consumable_array(internalPlugins).concat(_to_consumable_array(plugins)));
9
+ var runner = (customRuntime || runtime).init();
10
+ setGlobalRunner(runner);
11
+ return runner;
12
+ }
13
+ export {
14
+ registerPlugin
15
+ };
@@ -0,0 +1,17 @@
1
+ import { runtime } from "./base";
2
+ var globalRunner;
3
+ function setGlobalRunner(runner) {
4
+ globalRunner = runner;
5
+ }
6
+ function getGlobalRunner() {
7
+ if (globalRunner) {
8
+ return globalRunner;
9
+ }
10
+ var runner = runtime.init();
11
+ setGlobalRunner(runner);
12
+ return runner;
13
+ }
14
+ export {
15
+ getGlobalRunner,
16
+ setGlobalRunner
17
+ };
package/dist/esm/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import { isBrowser } from "./common";
2
- import { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, defineConfig, registerInit, useRuntimeContext } from "./core";
2
+ import { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext } from "./core";
3
3
  export {
4
4
  RuntimeReactContext,
5
5
  bootstrap,
6
6
  createApp,
7
- createPlugin,
8
7
  defineConfig,
8
+ defineRuntimeConfig,
9
9
  isBrowser,
10
- registerInit,
11
10
  useLoader,
12
11
  useRuntimeContext
13
12
  };
@@ -0,0 +1,185 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import path from "path";
4
+ import { fs } from "@modern-js/utils";
5
+ import { makeLegalIdentifier } from "../makeLegalIdentifier";
6
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../../constants";
7
+ import { replaceWithAlias } from "../utils";
8
+ import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
9
+ var compName = function(srcDirectory, filePath) {
10
+ var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
11
+ return "Comp_".concat(legalCompName);
12
+ };
13
+ var layoutNameAbbr = function(filePath) {
14
+ var prefix = "L_";
15
+ var dirName = path.dirname(filePath).split("/").pop() || "";
16
+ return "".concat(prefix).concat(makeLegalIdentifier(dirName));
17
+ };
18
+ var parents = [];
19
+ var recursiveReadDir = function(param) {
20
+ var dir = param.dir, routes = param.routes, _param_basePath = param.basePath, basePath = _param_basePath === void 0 ? "/" : _param_basePath, srcDirectory = param.srcDirectory, srcAlias = param.srcAlias;
21
+ var hasDynamicRoute = false;
22
+ var resetParent = false;
23
+ var parent = parents[parents.length - 1];
24
+ var layout = findLayout(dir);
25
+ if (layout) {
26
+ if (basePath === "/") {
27
+ throw new Error("should use _app instead of _layout in ".concat(dir));
28
+ } else {
29
+ var alias = replaceWithAlias(srcDirectory, layout, srcAlias);
30
+ var componentName = compName(srcDirectory, layout);
31
+ var route = {
32
+ path: "".concat(basePath.substring(0, basePath.length - 1)),
33
+ children: [],
34
+ _component: alias,
35
+ component: componentName,
36
+ parent,
37
+ type: "page"
38
+ };
39
+ parent = route;
40
+ resetParent = true;
41
+ routes.push(route);
42
+ parents.push(route);
43
+ routes = route.children;
44
+ }
45
+ }
46
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
47
+ try {
48
+ for (var _iterator = fs.readdirSync(dir)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
49
+ var relative = _step.value;
50
+ var filePath = path.join(dir, relative);
51
+ if (!shouldSkip(filePath)) {
52
+ var filename = path.basename(filePath, path.extname(filePath));
53
+ var alias1 = replaceWithAlias(srcDirectory, filePath, srcAlias);
54
+ var componentName1 = compName(srcDirectory, filePath);
55
+ var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
56
+ if (dynamicRouteMatched) {
57
+ if (hasDynamicRoute) {
58
+ throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
59
+ } else {
60
+ hasDynamicRoute = true;
61
+ }
62
+ }
63
+ var route1 = {
64
+ path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1].replace(/\$$/, "?")).concat(dynamicRouteMatched[2]) : filename),
65
+ _component: alias1,
66
+ component: componentName1,
67
+ parent,
68
+ type: "page"
69
+ };
70
+ if (fs.statSync(filePath).isDirectory()) {
71
+ recursiveReadDir({
72
+ dir: filePath,
73
+ routes,
74
+ basePath: "".concat(route1.path, "/"),
75
+ srcDirectory,
76
+ srcAlias
77
+ });
78
+ continue;
79
+ }
80
+ if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
81
+ continue;
82
+ }
83
+ if (filename === FILE_SYSTEM_ROUTES_INDEX) {
84
+ route1.path = basePath === "/" ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
85
+ }
86
+ if (filename === "404" && basePath === "/") {
87
+ route1.path = "*";
88
+ }
89
+ routes.push(route1);
90
+ }
91
+ }
92
+ } catch (err) {
93
+ _didIteratorError = true;
94
+ _iteratorError = err;
95
+ } finally {
96
+ try {
97
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
98
+ _iterator.return();
99
+ }
100
+ } finally {
101
+ if (_didIteratorError) {
102
+ throw _iteratorError;
103
+ }
104
+ }
105
+ }
106
+ if (resetParent) {
107
+ parents.pop();
108
+ }
109
+ };
110
+ var normalizeNestedRoutes = function(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
111
+ var flat = function(routes) {
112
+ return routes.reduce(function(memo, route) {
113
+ return memo.concat(Array.isArray(route.children) ? flat(route.children) : [
114
+ route
115
+ ]);
116
+ }, []);
117
+ };
118
+ var generate = function(route) {
119
+ var codes = [];
120
+ var lastComponent = route.component;
121
+ var imports = [
122
+ "import React from 'react';",
123
+ "import ".concat(lastComponent, " from '").concat(route._component, "'")
124
+ ];
125
+ while (route = route.parent) {
126
+ var layoutComponent = route.component;
127
+ var layoutComponentAbbr = layoutNameAbbr(route._component);
128
+ imports.push("import ".concat(layoutComponent, " from '").concat(route._component, "';"));
129
+ var currentComponent = "".concat(layoutComponentAbbr, "_").concat(lastComponent);
130
+ codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
131
+ lastComponent = currentComponent;
132
+ }
133
+ var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
134
+ fs.outputFileSync(file, "".concat(imports.join("\n"), "\n").concat(codes.join("\n"), "\nexport default ").concat(lastComponent));
135
+ return {
136
+ component: lastComponent,
137
+ _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
138
+ };
139
+ };
140
+ var normalized = flat(nested).map(function(route) {
141
+ return route.parent ? _object_spread_props(_object_spread({}, route, generate(route)), {
142
+ parent: void 0
143
+ }) : _object_spread_props(_object_spread({}, route), {
144
+ parent: void 0
145
+ });
146
+ });
147
+ return normalized;
148
+ };
149
+ var getClientRoutes = function(param) {
150
+ var entrypoint = param.entrypoint, srcDirectory = param.srcDirectory, srcAlias = param.srcAlias, internalDirectory = param.internalDirectory, internalDirAlias = param.internalDirAlias;
151
+ var entryName = entrypoint.entryName, pageRoutesEntry = entrypoint.pageRoutesEntry;
152
+ if (!pageRoutesEntry) {
153
+ return [];
154
+ }
155
+ if (!fs.existsSync(pageRoutesEntry)) {
156
+ throw new Error("generate file system routes error, ".concat(pageRoutesEntry, " directory not found."));
157
+ }
158
+ if (!(fs.existsSync(pageRoutesEntry) && fs.statSync(pageRoutesEntry).isDirectory())) {
159
+ throw new Error("generate file system routes error, ".concat(pageRoutesEntry, " should be directory."));
160
+ }
161
+ var routes = [];
162
+ recursiveReadDir({
163
+ dir: pageRoutesEntry,
164
+ routes,
165
+ basePath: "/",
166
+ srcDirectory,
167
+ srcAlias
168
+ });
169
+ var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
170
+ fs.emptyDirSync(internalComponentsDir);
171
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
172
+ parents.length = 0;
173
+ routes.sort(function(a, b) {
174
+ var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
175
+ if (delta === 0) {
176
+ return a.path.length - b.path.length;
177
+ }
178
+ return delta;
179
+ });
180
+ debug("fileSystem routes: %o", routes);
181
+ return routes;
182
+ };
183
+ export {
184
+ getClientRoutes
185
+ };
@@ -0,0 +1,183 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import path from "path";
4
+ import { fs } from "@modern-js/utils";
5
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../../constants";
6
+ import { replaceWithAlias } from "../utils";
7
+ import { makeLegalIdentifier } from "../makeLegalIdentifier";
8
+ import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
9
+ var compName = function(srcDirectory, filePath) {
10
+ var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
11
+ return "Comp_".concat(legalCompName);
12
+ };
13
+ var layoutNameAbbr = function(filePath) {
14
+ var prefix = "L_";
15
+ var dirName = path.dirname(filePath).split("/").pop() || "";
16
+ return "".concat(prefix).concat(makeLegalIdentifier(dirName));
17
+ };
18
+ var parents = [];
19
+ var recursiveReadDirLegacy = function(param) {
20
+ var dir = param.dir, routes = param.routes, _param_basePath = param.basePath, basePath = _param_basePath === void 0 ? "/" : _param_basePath, srcDirectory = param.srcDirectory, srcAlias = param.srcAlias;
21
+ var hasDynamicRoute = false;
22
+ var resetParent = false;
23
+ var parent = parents[parents.length - 1];
24
+ var layout = findLayout(dir);
25
+ if (layout) {
26
+ if (basePath === "/") {
27
+ throw new Error("should use _app instead of _layout in ".concat(dir));
28
+ } else {
29
+ var alias = replaceWithAlias(srcDirectory, layout, srcAlias);
30
+ var componentName = compName(srcDirectory, layout);
31
+ var route = {
32
+ path: "".concat(basePath.substring(0, basePath.length - 1)),
33
+ exact: false,
34
+ routes: [],
35
+ _component: alias,
36
+ component: componentName,
37
+ parent
38
+ };
39
+ parent = route;
40
+ resetParent = true;
41
+ routes.push(route);
42
+ parents.push(route);
43
+ routes = route.routes;
44
+ }
45
+ }
46
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
47
+ try {
48
+ for (var _iterator = fs.readdirSync(dir)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
49
+ var relative = _step.value;
50
+ var filePath = path.join(dir, relative);
51
+ if (!shouldSkip(filePath)) {
52
+ var filename = path.basename(filePath, path.extname(filePath));
53
+ var alias1 = replaceWithAlias(srcDirectory, filePath, srcAlias);
54
+ var componentName1 = compName(srcDirectory, filePath);
55
+ var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
56
+ if (dynamicRouteMatched) {
57
+ if (hasDynamicRoute) {
58
+ throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
59
+ } else {
60
+ hasDynamicRoute = true;
61
+ }
62
+ }
63
+ var route1 = {
64
+ path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1].replace(/\$$/, "?")).concat(dynamicRouteMatched[2]) : filename),
65
+ _component: alias1,
66
+ component: componentName1,
67
+ exact: true,
68
+ parent
69
+ };
70
+ if (fs.statSync(filePath).isDirectory()) {
71
+ recursiveReadDirLegacy({
72
+ dir: filePath,
73
+ routes,
74
+ basePath: "".concat(route1.path, "/"),
75
+ srcDirectory,
76
+ srcAlias
77
+ });
78
+ continue;
79
+ }
80
+ if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
81
+ continue;
82
+ }
83
+ if (filename === FILE_SYSTEM_ROUTES_INDEX) {
84
+ route1.path = basePath === "/" ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
85
+ }
86
+ if (filename === "404" && basePath === "/") {
87
+ route1.path = "*";
88
+ route1.exact = false;
89
+ }
90
+ routes.push(route1);
91
+ }
92
+ }
93
+ } catch (err) {
94
+ _didIteratorError = true;
95
+ _iteratorError = err;
96
+ } finally {
97
+ try {
98
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
99
+ _iterator.return();
100
+ }
101
+ } finally {
102
+ if (_didIteratorError) {
103
+ throw _iteratorError;
104
+ }
105
+ }
106
+ }
107
+ if (resetParent) {
108
+ parents.pop();
109
+ }
110
+ };
111
+ var normalizeNestedRoutes = function(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
112
+ var flat = function(routes) {
113
+ return routes.reduce(function(memo, route) {
114
+ return memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [
115
+ route
116
+ ]);
117
+ }, []);
118
+ };
119
+ var generate = function(route) {
120
+ var codes = [];
121
+ var lastComponent = route.component;
122
+ var imports = [
123
+ "import React from 'react';",
124
+ "import ".concat(lastComponent, " from '").concat(route._component, "'")
125
+ ];
126
+ while (route = route.parent) {
127
+ var layoutComponent = route.component;
128
+ var layoutComponentAbbr = layoutNameAbbr(route._component);
129
+ imports.push("import ".concat(layoutComponent, " from '").concat(route._component, "';"));
130
+ var currentComponent = "".concat(layoutComponentAbbr, "_").concat(lastComponent);
131
+ codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
132
+ lastComponent = currentComponent;
133
+ }
134
+ var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
135
+ fs.outputFileSync(file, "".concat(imports.join("\n"), "\n").concat(codes.join("\n"), "\nexport default ").concat(lastComponent));
136
+ return {
137
+ component: lastComponent,
138
+ _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
139
+ };
140
+ };
141
+ var normalized = flat(nested).map(function(route) {
142
+ return route.parent ? _object_spread_props(_object_spread({}, route, generate(route)), {
143
+ parent: void 0
144
+ }) : _object_spread_props(_object_spread({}, route), {
145
+ parent: void 0
146
+ });
147
+ });
148
+ return normalized;
149
+ };
150
+ var getClientRoutes = function(param) {
151
+ var entrypoint = param.entrypoint, srcDirectory = param.srcDirectory, srcAlias = param.srcAlias, internalDirectory = param.internalDirectory, internalDirAlias = param.internalDirAlias;
152
+ var entry = entrypoint.entry, entryName = entrypoint.entryName;
153
+ if (!fs.existsSync(entry)) {
154
+ throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
155
+ }
156
+ if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
157
+ throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
158
+ }
159
+ var routes = [];
160
+ recursiveReadDirLegacy({
161
+ dir: entry,
162
+ routes,
163
+ basePath: "/",
164
+ srcDirectory,
165
+ srcAlias
166
+ });
167
+ var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
168
+ fs.emptyDirSync(internalComponentsDir);
169
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
170
+ parents.length = 0;
171
+ routes.sort(function(a, b) {
172
+ var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
173
+ if (delta === 0) {
174
+ return a.path.length - b.path.length;
175
+ }
176
+ return delta;
177
+ });
178
+ debug("fileSystem routes: %o", routes);
179
+ return routes;
180
+ };
181
+ export {
182
+ getClientRoutes
183
+ };
@@ -0,0 +1,6 @@
1
+ import { getClientRoutes } from "./getRoutes";
2
+ import { getClientRoutes as getClientRoutes2 } from "./getRoutesLegacy";
3
+ export {
4
+ getClientRoutes,
5
+ getClientRoutes2 as getClientRoutesLegacy
6
+ };
@@ -0,0 +1,28 @@
1
+ import path from "path";
2
+ import { JS_EXTENSIONS, createDebugger, findExists, fs } from "@modern-js/utils";
3
+ import { FILE_SYSTEM_ROUTES_LAYOUT, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, FILE_SYSTEM_ROUTES_IGNORED_REGEX } from "../../constants";
4
+ var debug = createDebugger("get-client-routes");
5
+ var findLayout = function(dir) {
6
+ return findExists(JS_EXTENSIONS.map(function(ext) {
7
+ return path.resolve(dir, "".concat(FILE_SYSTEM_ROUTES_LAYOUT).concat(ext));
8
+ }));
9
+ };
10
+ var getRouteWeight = function(route) {
11
+ return route === "*" ? 999 : route.split(":").length - 1;
12
+ };
13
+ var shouldSkip = function(file) {
14
+ if (fs.statSync(file).isDirectory()) {
15
+ return false;
16
+ }
17
+ var ext = path.extname(file);
18
+ if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
19
+ return true;
20
+ }
21
+ return false;
22
+ };
23
+ export {
24
+ debug,
25
+ findLayout,
26
+ getRouteWeight,
27
+ shouldSkip
28
+ };