@modern-js/app-tools 2.53.1-alpha.4 → 2.54.1-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. package/bin/modern.js +0 -2
  2. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +2 -1
  3. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +2 -2
  4. package/dist/cjs/commands/build.js +2 -0
  5. package/dist/cjs/commands/deploy.js +2 -2
  6. package/dist/cjs/commands/dev.js +19 -5
  7. package/dist/cjs/commands/index.js +113 -7
  8. package/dist/cjs/commands/serve.js +19 -5
  9. package/dist/cjs/hooks.js +5 -0
  10. package/dist/cjs/index.js +14 -83
  11. package/dist/cjs/plugins/analyze/constants.js +56 -0
  12. package/dist/cjs/{analyze → plugins/analyze}/generateCode.js +11 -84
  13. package/dist/cjs/{analyze → plugins/analyze}/getBundleEntry.js +11 -20
  14. package/dist/cjs/{analyze → plugins/analyze}/getFileSystemEntry.js +33 -47
  15. package/dist/cjs/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  16. package/dist/cjs/{analyze → plugins/analyze}/index.js +9 -24
  17. package/dist/cjs/plugins/analyze/templates.js +101 -0
  18. package/dist/cjs/{analyze → plugins/analyze}/utils.js +12 -55
  19. package/dist/cjs/plugins/deploy/platforms/netlify.js +2 -2
  20. package/dist/cjs/plugins/deploy/platforms/node.js +2 -2
  21. package/dist/cjs/plugins/deploy/platforms/vercel.js +2 -2
  22. package/dist/cjs/{initialize → plugins/initialize}/index.js +1 -1
  23. package/dist/cjs/plugins/serverBuild.js +10 -4
  24. package/dist/cjs/utils/createServer.js +1 -1
  25. package/dist/cjs/utils/{getServerInternalPlugins.js → loadPlugins.js} +18 -11
  26. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +4 -1
  27. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +2 -2
  28. package/dist/esm/commands/build.js +16 -9
  29. package/dist/esm/commands/deploy.js +2 -2
  30. package/dist/esm/commands/dev.js +13 -9
  31. package/dist/esm/commands/index.js +411 -3
  32. package/dist/esm/commands/serve.js +12 -8
  33. package/dist/esm/hooks.js +5 -0
  34. package/dist/esm/index.js +12 -394
  35. package/dist/esm/plugins/analyze/constants.js +24 -0
  36. package/dist/esm/{analyze → plugins/analyze}/generateCode.js +27 -169
  37. package/dist/esm/plugins/analyze/getBundleEntry.js +101 -0
  38. package/dist/esm/plugins/analyze/getFileSystemEntry.js +195 -0
  39. package/dist/esm/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  40. package/dist/esm/{analyze → plugins/analyze}/index.js +42 -70
  41. package/dist/esm/plugins/analyze/templates.js +22 -0
  42. package/dist/esm/{analyze → plugins/analyze}/utils.js +13 -135
  43. package/dist/esm/plugins/deploy/platforms/netlify.js +5 -3
  44. package/dist/esm/plugins/deploy/platforms/node.js +5 -3
  45. package/dist/esm/plugins/deploy/platforms/vercel.js +5 -3
  46. package/dist/esm/{initialize → plugins/initialize}/index.js +1 -1
  47. package/dist/esm/plugins/serverBuild.js +13 -6
  48. package/dist/esm/utils/createServer.js +2 -2
  49. package/dist/esm/utils/loadPlugins.js +64 -0
  50. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +2 -1
  51. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +2 -2
  52. package/dist/esm-node/commands/build.js +2 -0
  53. package/dist/esm-node/commands/deploy.js +2 -2
  54. package/dist/esm-node/commands/dev.js +11 -7
  55. package/dist/esm-node/commands/index.js +92 -3
  56. package/dist/esm-node/commands/serve.js +10 -6
  57. package/dist/esm-node/hooks.js +5 -0
  58. package/dist/esm-node/index.js +12 -79
  59. package/dist/esm-node/plugins/analyze/constants.js +24 -0
  60. package/dist/esm-node/{analyze → plugins/analyze}/generateCode.js +14 -87
  61. package/dist/esm-node/{analyze → plugins/analyze}/getBundleEntry.js +11 -20
  62. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +75 -0
  63. package/dist/esm-node/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  64. package/dist/esm-node/{analyze → plugins/analyze}/index.js +10 -25
  65. package/dist/esm-node/plugins/analyze/templates.js +75 -0
  66. package/dist/esm-node/{analyze → plugins/analyze}/utils.js +13 -52
  67. package/dist/esm-node/plugins/deploy/platforms/netlify.js +3 -3
  68. package/dist/esm-node/plugins/deploy/platforms/node.js +3 -3
  69. package/dist/esm-node/plugins/deploy/platforms/vercel.js +3 -3
  70. package/dist/esm-node/{initialize → plugins/initialize}/index.js +1 -1
  71. package/dist/esm-node/plugins/serverBuild.js +11 -5
  72. package/dist/esm-node/utils/createServer.js +2 -2
  73. package/dist/esm-node/utils/loadPlugins.js +21 -0
  74. package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
  75. package/dist/types/commands/index.d.ts +10 -3
  76. package/dist/types/config/initialize/inits.d.ts +1 -1
  77. package/dist/types/exports/server.d.ts +4 -0
  78. package/dist/types/index.d.ts +2 -5
  79. package/dist/types/plugins/analyze/constants.d.ts +9 -0
  80. package/dist/types/{analyze → plugins/analyze}/generateCode.d.ts +1 -1
  81. package/dist/types/plugins/analyze/getBundleEntry.d.ts +4 -0
  82. package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +5 -0
  83. package/dist/types/{analyze → plugins/analyze}/getHtmlTemplate.d.ts +1 -1
  84. package/dist/types/{analyze → plugins/analyze}/getServerRoutes.d.ts +1 -1
  85. package/dist/types/{analyze → plugins/analyze}/index.d.ts +1 -1
  86. package/dist/types/plugins/analyze/templates.d.ts +19 -0
  87. package/dist/types/{analyze → plugins/analyze}/utils.d.ts +4 -6
  88. package/dist/types/{initialize → plugins/initialize}/index.d.ts +1 -1
  89. package/dist/types/types/hooks.d.ts +11 -1
  90. package/dist/types/utils/loadPlugins.d.ts +5 -0
  91. package/package.json +22 -22
  92. package/dist/cjs/analyze/constants.js +0 -122
  93. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +0 -197
  94. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +0 -195
  95. package/dist/cjs/analyze/getClientRoutes/index.js +0 -31
  96. package/dist/cjs/analyze/getClientRoutes/utils.js +0 -59
  97. package/dist/cjs/analyze/makeLegalIdentifier.js +0 -37
  98. package/dist/cjs/analyze/nestedRoutes.js +0 -295
  99. package/dist/cjs/analyze/templates.js +0 -444
  100. package/dist/esm/analyze/constants.js +0 -76
  101. package/dist/esm/analyze/getBundleEntry.js +0 -75
  102. package/dist/esm/analyze/getClientRoutes/getRoutes.js +0 -185
  103. package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +0 -183
  104. package/dist/esm/analyze/getClientRoutes/index.js +0 -6
  105. package/dist/esm/analyze/getClientRoutes/utils.js +0 -28
  106. package/dist/esm/analyze/getFileSystemEntry.js +0 -113
  107. package/dist/esm/analyze/makeLegalIdentifier.js +0 -15
  108. package/dist/esm/analyze/nestedRoutes.js +0 -398
  109. package/dist/esm/analyze/templates.js +0 -435
  110. package/dist/esm/utils/getServerInternalPlugins.js +0 -40
  111. package/dist/esm-node/analyze/constants.js +0 -76
  112. package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +0 -163
  113. package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +0 -161
  114. package/dist/esm-node/analyze/getClientRoutes/index.js +0 -6
  115. package/dist/esm-node/analyze/getClientRoutes/utils.js +0 -22
  116. package/dist/esm-node/analyze/getFileSystemEntry.js +0 -89
  117. package/dist/esm-node/analyze/makeLegalIdentifier.js +0 -13
  118. package/dist/esm-node/analyze/nestedRoutes.js +0 -259
  119. package/dist/esm-node/analyze/templates.js +0 -405
  120. package/dist/esm-node/utils/getServerInternalPlugins.js +0 -15
  121. package/dist/types/analyze/constants.d.ts +0 -42
  122. package/dist/types/analyze/getBundleEntry.d.ts +0 -3
  123. package/dist/types/analyze/getClientRoutes/getRoutes.d.ts +0 -8
  124. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +0 -9
  125. package/dist/types/analyze/getClientRoutes/index.d.ts +0 -2
  126. package/dist/types/analyze/getClientRoutes/utils.d.ts +0 -5
  127. package/dist/types/analyze/getFileSystemEntry.d.ts +0 -4
  128. package/dist/types/analyze/makeLegalIdentifier.d.ts +0 -1
  129. package/dist/types/analyze/nestedRoutes.d.ts +0 -7
  130. package/dist/types/analyze/templates.d.ts +0 -30
  131. package/dist/types/utils/getServerInternalPlugins.d.ts +0 -2
  132. /package/dist/cjs/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  133. /package/dist/cjs/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  134. /package/dist/esm/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  135. /package/dist/esm/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  136. /package/dist/esm-node/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  137. /package/dist/esm-node/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  138. /package/dist/types/{analyze → plugins/analyze}/isDefaultExportFunction.d.ts +0 -0
@@ -1,163 +0,0 @@
1
- import path from "path";
2
- import { fs } from "@modern-js/utils";
3
- import { makeLegalIdentifier } from "../makeLegalIdentifier";
4
- import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
5
- import { replaceWithAlias } from "../utils";
6
- import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
7
- const compName = (srcDirectory, filePath) => {
8
- const legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
9
- return `Comp_${legalCompName}`;
10
- };
11
- const layoutNameAbbr = (filePath) => {
12
- const prefix = "L_";
13
- const dirName = path.dirname(filePath).split("/").pop() || "";
14
- return `${prefix}${makeLegalIdentifier(dirName)}`;
15
- };
16
- const parents = [];
17
- const recursiveReadDir = ({ dir, routes, basePath = "/", srcDirectory, srcAlias }) => {
18
- let hasDynamicRoute = false;
19
- let resetParent = false;
20
- let parent = parents[parents.length - 1];
21
- const layout = findLayout(dir);
22
- if (layout) {
23
- if (basePath === "/") {
24
- throw new Error(`should use _app instead of _layout in ${dir}`);
25
- } else {
26
- const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
27
- const componentName = compName(srcDirectory, layout);
28
- const route = {
29
- path: `${basePath.substring(0, basePath.length - 1)}`,
30
- children: [],
31
- _component: alias,
32
- component: componentName,
33
- parent,
34
- type: "page"
35
- };
36
- parent = route;
37
- resetParent = true;
38
- routes.push(route);
39
- parents.push(route);
40
- routes = route.children;
41
- }
42
- }
43
- for (const relative of fs.readdirSync(dir)) {
44
- const filePath = path.join(dir, relative);
45
- if (!shouldSkip(filePath)) {
46
- const filename = path.basename(filePath, path.extname(filePath));
47
- const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
48
- const componentName = compName(srcDirectory, filePath);
49
- const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
50
- if (dynamicRouteMatched) {
51
- if (hasDynamicRoute) {
52
- throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
53
- } else {
54
- hasDynamicRoute = true;
55
- }
56
- }
57
- const route = {
58
- path: `${basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1].replace(/\$$/, "?")}${dynamicRouteMatched[2]}` : filename}`,
59
- _component: alias,
60
- component: componentName,
61
- parent,
62
- type: "page"
63
- };
64
- if (fs.statSync(filePath).isDirectory()) {
65
- recursiveReadDir({
66
- dir: filePath,
67
- routes,
68
- basePath: `${route.path}/`,
69
- srcDirectory,
70
- srcAlias
71
- });
72
- continue;
73
- }
74
- if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
75
- continue;
76
- }
77
- if (filename === FILE_SYSTEM_ROUTES_INDEX) {
78
- route.path = basePath === "/" ? basePath : `${basePath.substring(0, basePath.length - 1)}`;
79
- }
80
- if (filename === "404" && basePath === "/") {
81
- route.path = "*";
82
- }
83
- routes.push(route);
84
- }
85
- }
86
- if (resetParent) {
87
- parents.pop();
88
- }
89
- };
90
- const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
91
- const flat = (routes) => routes.reduce((memo, route) => memo.concat(Array.isArray(route.children) ? flat(route.children) : [
92
- route
93
- ]), []);
94
- const generate = (route) => {
95
- const codes = [];
96
- let lastComponent = route.component;
97
- const imports = [
98
- `import React from 'react';`,
99
- `import ${lastComponent} from '${route._component}'`
100
- ];
101
- while (route = route.parent) {
102
- const layoutComponent = route.component;
103
- const layoutComponentAbbr = layoutNameAbbr(route._component);
104
- imports.push(`import ${layoutComponent} from '${route._component}';`);
105
- const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
106
- codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
107
- lastComponent = currentComponent;
108
- }
109
- const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
110
- fs.outputFileSync(file, `${imports.join("\n")}
111
- ${codes.join("\n")}
112
- export default ${lastComponent}`);
113
- return {
114
- component: lastComponent,
115
- _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
116
- };
117
- };
118
- const normalized = flat(nested).map((route) => route.parent ? {
119
- ...route,
120
- ...generate(route),
121
- parent: void 0
122
- } : {
123
- ...route,
124
- parent: void 0
125
- });
126
- return normalized;
127
- };
128
- const getClientRoutes = ({ entrypoint, srcDirectory, srcAlias, internalDirectory, internalDirAlias }) => {
129
- const { entryName, pageRoutesEntry } = entrypoint;
130
- if (!pageRoutesEntry) {
131
- return [];
132
- }
133
- if (!fs.existsSync(pageRoutesEntry)) {
134
- throw new Error(`generate file system routes error, ${pageRoutesEntry} directory not found.`);
135
- }
136
- if (!(fs.existsSync(pageRoutesEntry) && fs.statSync(pageRoutesEntry).isDirectory())) {
137
- throw new Error(`generate file system routes error, ${pageRoutesEntry} should be directory.`);
138
- }
139
- let routes = [];
140
- recursiveReadDir({
141
- dir: pageRoutesEntry,
142
- routes,
143
- basePath: "/",
144
- srcDirectory,
145
- srcAlias
146
- });
147
- const internalComponentsDir = path.resolve(internalDirectory, `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
148
- fs.emptyDirSync(internalComponentsDir);
149
- routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
150
- parents.length = 0;
151
- routes.sort((a, b) => {
152
- const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
153
- if (delta === 0) {
154
- return a.path.length - b.path.length;
155
- }
156
- return delta;
157
- });
158
- debug(`fileSystem routes: %o`, routes);
159
- return routes;
160
- };
161
- export {
162
- getClientRoutes
163
- };
@@ -1,161 +0,0 @@
1
- import path from "path";
2
- import { fs } from "@modern-js/utils";
3
- import { makeLegalIdentifier } from "../makeLegalIdentifier";
4
- import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
5
- import { replaceWithAlias } from "../utils";
6
- import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
7
- const compName = (srcDirectory, filePath) => {
8
- const legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
9
- return `Comp_${legalCompName}`;
10
- };
11
- const layoutNameAbbr = (filePath) => {
12
- const prefix = "L_";
13
- const dirName = path.dirname(filePath).split("/").pop() || "";
14
- return `${prefix}${makeLegalIdentifier(dirName)}`;
15
- };
16
- const parents = [];
17
- const recursiveReadDirLegacy = ({ dir, routes, basePath = "/", srcDirectory, srcAlias }) => {
18
- let hasDynamicRoute = false;
19
- let resetParent = false;
20
- let parent = parents[parents.length - 1];
21
- const layout = findLayout(dir);
22
- if (layout) {
23
- if (basePath === "/") {
24
- throw new Error(`should use _app instead of _layout in ${dir}`);
25
- } else {
26
- const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
27
- const componentName = compName(srcDirectory, layout);
28
- const route = {
29
- path: `${basePath.substring(0, basePath.length - 1)}`,
30
- exact: false,
31
- routes: [],
32
- _component: alias,
33
- component: componentName,
34
- parent
35
- };
36
- parent = route;
37
- resetParent = true;
38
- routes.push(route);
39
- parents.push(route);
40
- routes = route.routes;
41
- }
42
- }
43
- for (const relative of fs.readdirSync(dir)) {
44
- const filePath = path.join(dir, relative);
45
- if (!shouldSkip(filePath)) {
46
- const filename = path.basename(filePath, path.extname(filePath));
47
- const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
48
- const componentName = compName(srcDirectory, filePath);
49
- const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
50
- if (dynamicRouteMatched) {
51
- if (hasDynamicRoute) {
52
- throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
53
- } else {
54
- hasDynamicRoute = true;
55
- }
56
- }
57
- const route = {
58
- path: `${basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1].replace(/\$$/, "?")}${dynamicRouteMatched[2]}` : filename}`,
59
- _component: alias,
60
- component: componentName,
61
- exact: true,
62
- parent
63
- };
64
- if (fs.statSync(filePath).isDirectory()) {
65
- recursiveReadDirLegacy({
66
- dir: filePath,
67
- routes,
68
- basePath: `${route.path}/`,
69
- srcDirectory,
70
- srcAlias
71
- });
72
- continue;
73
- }
74
- if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
75
- continue;
76
- }
77
- if (filename === FILE_SYSTEM_ROUTES_INDEX) {
78
- route.path = basePath === "/" ? basePath : `${basePath.substring(0, basePath.length - 1)}`;
79
- }
80
- if (filename === "404" && basePath === "/") {
81
- route.path = "*";
82
- route.exact = false;
83
- }
84
- routes.push(route);
85
- }
86
- }
87
- if (resetParent) {
88
- parents.pop();
89
- }
90
- };
91
- const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
92
- const flat = (routes) => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [
93
- route
94
- ]), []);
95
- const generate = (route) => {
96
- const codes = [];
97
- let lastComponent = route.component;
98
- const imports = [
99
- `import React from 'react';`,
100
- `import ${lastComponent} from '${route._component}'`
101
- ];
102
- while (route = route.parent) {
103
- const layoutComponent = route.component;
104
- const layoutComponentAbbr = layoutNameAbbr(route._component);
105
- imports.push(`import ${layoutComponent} from '${route._component}';`);
106
- const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
107
- codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
108
- lastComponent = currentComponent;
109
- }
110
- const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
111
- fs.outputFileSync(file, `${imports.join("\n")}
112
- ${codes.join("\n")}
113
- export default ${lastComponent}`);
114
- return {
115
- component: lastComponent,
116
- _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
117
- };
118
- };
119
- const normalized = flat(nested).map((route) => route.parent ? {
120
- ...route,
121
- ...generate(route),
122
- parent: void 0
123
- } : {
124
- ...route,
125
- parent: void 0
126
- });
127
- return normalized;
128
- };
129
- const getClientRoutes = ({ entrypoint, srcDirectory, srcAlias, internalDirectory, internalDirAlias }) => {
130
- const { entry, entryName } = entrypoint;
131
- if (!fs.existsSync(entry)) {
132
- throw new Error(`generate file system routes error, ${entry} directory not found.`);
133
- }
134
- if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
135
- throw new Error(`generate file system routes error, ${entry} should be directory.`);
136
- }
137
- let routes = [];
138
- recursiveReadDirLegacy({
139
- dir: entry,
140
- routes,
141
- basePath: "/",
142
- srcDirectory,
143
- srcAlias
144
- });
145
- const internalComponentsDir = path.resolve(internalDirectory, `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
146
- fs.emptyDirSync(internalComponentsDir);
147
- routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
148
- parents.length = 0;
149
- routes.sort((a, b) => {
150
- const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
151
- if (delta === 0) {
152
- return a.path.length - b.path.length;
153
- }
154
- return delta;
155
- });
156
- debug(`fileSystem routes: %o`, routes);
157
- return routes;
158
- };
159
- export {
160
- getClientRoutes
161
- };
@@ -1,6 +0,0 @@
1
- import { getClientRoutes } from "./getRoutes";
2
- import { getClientRoutes as getClientRoutes2 } from "./getRoutesLegacy";
3
- export {
4
- getClientRoutes,
5
- getClientRoutes2 as getClientRoutesLegacy
6
- };
@@ -1,22 +0,0 @@
1
- import path from "path";
2
- import { createDebugger, findExists, fs } from "@modern-js/utils";
3
- import { JS_EXTENSIONS, FILE_SYSTEM_ROUTES_LAYOUT, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, FILE_SYSTEM_ROUTES_IGNORED_REGEX } from "../constants";
4
- const debug = createDebugger("get-client-routes");
5
- const findLayout = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
6
- const getRouteWeight = (route) => route === "*" ? 999 : route.split(":").length - 1;
7
- const shouldSkip = (file) => {
8
- if (fs.statSync(file).isDirectory()) {
9
- return false;
10
- }
11
- const ext = path.extname(file);
12
- if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
13
- return true;
14
- }
15
- return false;
16
- };
17
- export {
18
- debug,
19
- findLayout,
20
- getRouteWeight,
21
- shouldSkip
22
- };
@@ -1,89 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { findExists, ensureAbsolutePath } from "@modern-js/utils";
4
- import { isDefaultExportFunction } from "./isDefaultExportFunction";
5
- import { JS_EXTENSIONS, INDEX_FILE_NAME, APP_FILE_NAME, PAGES_DIR_NAME, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, NESTED_ROUTES_DIR } from "./constants";
6
- const hasIndex = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${INDEX_FILE_NAME}${ext}`)));
7
- const hasApp = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${APP_FILE_NAME}${ext}`)));
8
- const hasPages = (dir) => fs.existsSync(path.join(dir, PAGES_DIR_NAME));
9
- const hasNestedRoutes = (dir) => fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
10
- const isBundleEntry = (dir) => hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
11
- const scanDir = (dirs) => dirs.map((dir) => {
12
- const indexFile = hasIndex(dir);
13
- const customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
14
- const entryName = path.basename(dir);
15
- if (indexFile && !customBootstrap) {
16
- return {
17
- entryName,
18
- isMainEntry: false,
19
- entry: indexFile,
20
- absoluteEntryDir: path.resolve(dir),
21
- isAutoMount: false
22
- };
23
- }
24
- const isHasApp = hasApp(dir);
25
- if (isHasApp) {
26
- return {
27
- entryName,
28
- isMainEntry: false,
29
- entry: path.join(dir, APP_FILE_NAME),
30
- isAutoMount: true,
31
- absoluteEntryDir: path.resolve(dir),
32
- customBootstrap
33
- };
34
- }
35
- const isHasNestedRoutes = hasNestedRoutes(dir);
36
- const isHasPages = hasPages(dir);
37
- if (isHasNestedRoutes || isHasPages) {
38
- const entrypoint = {
39
- entryName,
40
- isMainEntry: false,
41
- entry: "",
42
- fileSystemRoutes: {
43
- globalApp: findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `./${PAGES_DIR_NAME}/${FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`)))
44
- },
45
- isAutoMount: true,
46
- absoluteEntryDir: path.resolve(dir),
47
- customBootstrap
48
- };
49
- if (isHasPages) {
50
- entrypoint.entry = path.join(dir, PAGES_DIR_NAME);
51
- entrypoint.pageRoutesEntry = entrypoint.entry;
52
- }
53
- if (isHasNestedRoutes) {
54
- entrypoint.entry = path.join(dir, NESTED_ROUTES_DIR);
55
- entrypoint.nestedRoutesEntry = entrypoint.entry;
56
- }
57
- return entrypoint;
58
- }
59
- return {
60
- entryName,
61
- isMainEntry: false,
62
- entry: indexFile,
63
- absoluteEntryDir: path.resolve(dir),
64
- isAutoMount: false
65
- };
66
- });
67
- const getFileSystemEntry = (appContext, config) => {
68
- const { appDirectory } = appContext;
69
- const { source: { entriesDir, disableEntryDirs } } = config;
70
- let disabledDirs = [];
71
- if (disableEntryDirs && Array.isArray(disableEntryDirs)) {
72
- disabledDirs = disableEntryDirs === null || disableEntryDirs === void 0 ? void 0 : disableEntryDirs.map((dir) => ensureAbsolutePath(appDirectory, dir));
73
- }
74
- const src = ensureAbsolutePath(appDirectory, entriesDir || "");
75
- if (fs.existsSync(src)) {
76
- if (fs.statSync(src).isDirectory()) {
77
- return scanDir(isBundleEntry(src) ? [
78
- src
79
- ] : fs.readdirSync(src).map((file) => path.join(src, file)).filter((file) => fs.statSync(file).isDirectory() && isBundleEntry(file) && !disabledDirs.includes(file)));
80
- } else {
81
- throw Error(`source.entriesDir accept a directory.`);
82
- }
83
- } else {
84
- throw Error(`src dir ${entriesDir} not found.`);
85
- }
86
- };
87
- export {
88
- getFileSystemEntry
89
- };
@@ -1,13 +0,0 @@
1
- const reservedWords = "break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public";
2
- const builtins = "arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl";
3
- const forbidList = new Set(`${reservedWords} ${builtins}`.split(" "));
4
- function makeLegalIdentifier(str) {
5
- const identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, "_");
6
- if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
7
- return `_${identifier}`;
8
- }
9
- return identifier || "_";
10
- }
11
- export {
12
- makeLegalIdentifier
13
- };