@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.
- package/dist/cjs/{runtimeContext.js → cli/constants.js} +6 -10
- package/dist/cjs/cli/entry.js +42 -0
- package/dist/cjs/cli/index.js +10 -0
- package/dist/cjs/core/compatible.js +13 -29
- package/dist/cjs/core/{appConfig.js → config.js} +6 -4
- package/dist/cjs/core/context/index.js +28 -0
- package/dist/cjs/core/context/runtime.js +42 -0
- package/dist/cjs/core/index.js +8 -12
- package/dist/cjs/core/loader/useLoader.js +2 -2
- package/dist/cjs/core/{plugin.js → plugin/base.js} +5 -25
- package/dist/cjs/core/plugin/index.js +39 -0
- package/dist/cjs/core/plugin/runner.js +42 -0
- package/dist/cjs/index.js +2 -4
- package/dist/cjs/router/cli/code/getClientRoutes/getRoutes.js +197 -0
- package/dist/cjs/router/cli/code/getClientRoutes/getRoutesLegacy.js +195 -0
- package/dist/cjs/router/cli/code/getClientRoutes/index.js +31 -0
- package/dist/cjs/router/cli/code/getClientRoutes/utils.js +59 -0
- package/dist/cjs/router/cli/code/index.js +134 -0
- package/dist/cjs/router/cli/code/makeLegalIdentifier.js +37 -0
- package/dist/cjs/router/cli/code/nestedRoutes.js +294 -0
- package/dist/cjs/router/cli/code/templates.js +371 -0
- package/dist/cjs/router/cli/code/utils.js +143 -0
- package/dist/cjs/router/cli/constants.js +83 -0
- package/dist/cjs/router/cli/entry.js +87 -0
- package/dist/cjs/router/cli/handler.js +71 -0
- package/dist/cjs/router/cli/index.js +25 -0
- package/dist/cjs/router/runtime/plugin.js +7 -4
- package/dist/cjs/router/runtime/plugin.node.js +12 -5
- package/dist/cjs/ssr/index.node.js +1 -0
- package/dist/cjs/state/runtime/plugin.js +3 -2
- package/dist/esm/cli/constants.js +4 -0
- package/dist/esm/cli/entry.js +14 -0
- package/dist/esm/cli/index.js +10 -0
- package/dist/esm/core/compatible.js +11 -30
- package/dist/esm/core/{appConfig.js → config.js} +4 -1
- package/dist/esm/core/context/index.js +4 -0
- package/dist/esm/core/context/runtime.js +18 -0
- package/dist/esm/core/index.js +4 -6
- package/dist/esm/core/loader/useLoader.js +1 -1
- package/dist/esm/core/plugin/base.js +21 -0
- package/dist/esm/core/plugin/index.js +15 -0
- package/dist/esm/core/plugin/runner.js +17 -0
- package/dist/esm/index.js +2 -3
- package/dist/esm/router/cli/code/getClientRoutes/getRoutes.js +185 -0
- package/dist/esm/router/cli/code/getClientRoutes/getRoutesLegacy.js +183 -0
- package/dist/esm/router/cli/code/getClientRoutes/index.js +6 -0
- package/dist/esm/router/cli/code/getClientRoutes/utils.js +28 -0
- package/dist/esm/router/cli/code/index.js +214 -0
- package/dist/esm/router/cli/code/makeLegalIdentifier.js +15 -0
- package/dist/esm/router/cli/code/nestedRoutes.js +397 -0
- package/dist/esm/router/cli/code/templates.js +417 -0
- package/dist/esm/router/cli/code/utils.js +212 -0
- package/dist/esm/router/cli/constants.js +47 -0
- package/dist/esm/router/cli/entry.js +57 -0
- package/dist/esm/router/cli/handler.js +103 -0
- package/dist/esm/router/cli/index.js +57 -0
- package/dist/esm/router/runtime/plugin.js +7 -4
- package/dist/esm/router/runtime/plugin.node.js +13 -6
- package/dist/esm/ssr/index.node.js +1 -0
- package/dist/esm/state/runtime/plugin.js +3 -2
- package/dist/esm-node/cli/constants.js +4 -0
- package/dist/esm-node/cli/entry.js +8 -0
- package/dist/esm-node/cli/index.js +9 -0
- package/dist/esm-node/core/compatible.js +12 -28
- package/dist/esm-node/core/{appConfig.js → config.js} +2 -1
- package/dist/esm-node/core/context/index.js +4 -0
- package/dist/esm-node/core/context/runtime.js +16 -0
- package/dist/esm-node/core/index.js +4 -6
- package/dist/esm-node/core/loader/useLoader.js +1 -1
- package/dist/esm-node/core/plugin/base.js +19 -0
- package/dist/esm-node/core/plugin/index.js +13 -0
- package/dist/esm-node/core/plugin/runner.js +17 -0
- package/dist/esm-node/index.js +2 -3
- package/dist/esm-node/router/cli/code/getClientRoutes/getRoutes.js +163 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/getRoutesLegacy.js +161 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/index.js +6 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/utils.js +22 -0
- package/dist/esm-node/router/cli/code/index.js +100 -0
- package/dist/esm-node/router/cli/code/makeLegalIdentifier.js +13 -0
- package/dist/esm-node/router/cli/code/nestedRoutes.js +258 -0
- package/dist/esm-node/router/cli/code/templates.js +335 -0
- package/dist/esm-node/router/cli/code/utils.js +101 -0
- package/dist/esm-node/router/cli/constants.js +47 -0
- package/dist/esm-node/router/cli/entry.js +50 -0
- package/dist/esm-node/router/cli/handler.js +36 -0
- package/dist/esm-node/router/cli/index.js +22 -0
- package/dist/esm-node/router/runtime/plugin.js +7 -4
- package/dist/esm-node/router/runtime/plugin.node.js +12 -5
- package/dist/esm-node/ssr/index.node.js +1 -0
- package/dist/esm-node/state/runtime/plugin.js +3 -2
- package/dist/types/cli/constants.d.ts +1 -0
- package/dist/types/cli/entry.d.ts +1 -0
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/core/compatible.d.ts +7 -3
- package/dist/types/core/config.d.ts +14 -0
- package/dist/types/core/context/index.d.ts +1 -0
- package/dist/types/{runtimeContext.d.ts → core/context/runtime.d.ts} +5 -4
- package/dist/types/core/index.d.ts +4 -4
- package/dist/types/core/plugin/base.d.ts +80 -0
- package/dist/types/core/plugin/index.d.ts +29 -0
- package/dist/types/core/plugin/runner.d.ts +26 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/router/cli/code/getClientRoutes/getRoutes.d.ts +8 -0
- package/dist/types/router/cli/code/getClientRoutes/getRoutesLegacy.d.ts +9 -0
- package/dist/types/router/cli/code/getClientRoutes/index.d.ts +2 -0
- package/dist/types/router/cli/code/getClientRoutes/utils.d.ts +5 -0
- package/dist/types/router/cli/code/index.d.ts +6 -0
- package/dist/types/router/cli/code/makeLegalIdentifier.d.ts +1 -0
- package/dist/types/router/cli/code/nestedRoutes.d.ts +7 -0
- package/dist/types/router/cli/code/templates.d.ts +14 -0
- package/dist/types/router/cli/code/utils.d.ts +12 -0
- package/dist/types/router/cli/constants.d.ts +32 -0
- package/dist/types/router/cli/entry.d.ts +5 -0
- package/dist/types/router/cli/handler.d.ts +5 -0
- package/dist/types/router/cli/index.d.ts +3 -1
- package/dist/types/ssr/serverRender/types.d.ts +3 -0
- package/package.json +37 -11
- package/dist/esm/core/plugin.js +0 -59
- package/dist/esm/runtimeContext.js +0 -7
- package/dist/esm-node/core/plugin.js +0 -37
- package/dist/esm-node/runtimeContext.js +0 -7
- package/dist/types/core/appConfig.d.ts +0 -4
- package/dist/types/core/plugin.d.ts +0 -204
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../../constants";
|
|
4
|
+
import { replaceWithAlias } from "../utils";
|
|
5
|
+
import { makeLegalIdentifier } from "../makeLegalIdentifier";
|
|
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
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
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
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, getEntryOptions, isRouterV5, isSSGEntry, isUseSSRBundle, logger } from "@modern-js/utils";
|
|
3
|
+
import { FILE_SYSTEM_ROUTES_FILE_NAME } from "../constants";
|
|
4
|
+
import * as templates from "./templates";
|
|
5
|
+
import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
|
|
6
|
+
import { getServerLoadersFile, getServerCombinedModueFile } from "./utils";
|
|
7
|
+
import { walk } from "./nestedRoutes";
|
|
8
|
+
const generateCode = async (appContext, config, entrypoints, api) => {
|
|
9
|
+
const { internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName } = appContext;
|
|
10
|
+
const hookRunners = api.useHookRunners();
|
|
11
|
+
const isV5 = isRouterV5(config);
|
|
12
|
+
const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
|
|
13
|
+
const importsStatemets = /* @__PURE__ */ new Map();
|
|
14
|
+
const oldVersion = typeof (config === null || config === void 0 ? void 0 : config.runtime.router) === "object" ? Boolean((config === null || config === void 0 ? void 0 : config.runtime.router).oldVersion) : false;
|
|
15
|
+
await Promise.all(entrypoints.map(generateEntryCode));
|
|
16
|
+
return {
|
|
17
|
+
importsStatemets
|
|
18
|
+
};
|
|
19
|
+
async function generateEntryCode(entrypoint) {
|
|
20
|
+
const { entryName, isMainEntry, isAutoMount, fileSystemRoutes } = entrypoint;
|
|
21
|
+
if (isAutoMount) {
|
|
22
|
+
if (fileSystemRoutes) {
|
|
23
|
+
var _config_output;
|
|
24
|
+
let initialRoutes = [];
|
|
25
|
+
let nestedRoutes = null;
|
|
26
|
+
if (entrypoint.entry) {
|
|
27
|
+
initialRoutes = getRoutes({
|
|
28
|
+
entrypoint,
|
|
29
|
+
srcDirectory,
|
|
30
|
+
srcAlias: internalSrcAlias,
|
|
31
|
+
internalDirectory,
|
|
32
|
+
internalDirAlias
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (!isV5 && entrypoint.nestedRoutesEntry) {
|
|
36
|
+
nestedRoutes = await walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
|
|
37
|
+
name: internalSrcAlias,
|
|
38
|
+
basename: srcDirectory
|
|
39
|
+
}, entrypoint.entryName, entrypoint.isMainEntry, oldVersion);
|
|
40
|
+
if (nestedRoutes) {
|
|
41
|
+
if (!Array.isArray(nestedRoutes)) {
|
|
42
|
+
nestedRoutes = [
|
|
43
|
+
nestedRoutes
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
for (const route of nestedRoutes) {
|
|
47
|
+
initialRoutes.unshift(route);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const { routes } = await hookRunners.modifyFileSystemRoutes({
|
|
52
|
+
entrypoint,
|
|
53
|
+
routes: initialRoutes
|
|
54
|
+
});
|
|
55
|
+
const config2 = api.useResolvedConfigContext();
|
|
56
|
+
const ssr = getEntryOptions(entryName, isMainEntry, config2.server.ssr, config2.server.ssrByEntries, packageName);
|
|
57
|
+
const useSSG = isSSGEntry(config2, entryName, entrypoints);
|
|
58
|
+
let mode;
|
|
59
|
+
if (ssr) {
|
|
60
|
+
mode = typeof ssr === "object" ? ssr.mode || "string" : "string";
|
|
61
|
+
}
|
|
62
|
+
if (mode === "stream") {
|
|
63
|
+
const hasPageRoute = routes.some((route) => "type" in route && route.type === "page");
|
|
64
|
+
if (hasPageRoute) {
|
|
65
|
+
logger.error("Streaming ssr is not supported when pages dir exists");
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const { code } = await hookRunners.beforeGenerateRoutes({
|
|
70
|
+
entrypoint,
|
|
71
|
+
code: await templates.fileSystemRoutes({
|
|
72
|
+
routes,
|
|
73
|
+
ssrMode: useSSG ? "string" : mode,
|
|
74
|
+
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
75
|
+
entryName: entrypoint.entryName,
|
|
76
|
+
internalDirectory,
|
|
77
|
+
splitRouteChunks: config2 === null || config2 === void 0 ? void 0 : (_config_output = config2.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
|
|
78
|
+
})
|
|
79
|
+
});
|
|
80
|
+
if (entrypoint.nestedRoutesEntry && isUseSSRBundle(config2)) {
|
|
81
|
+
const routesServerFile = getServerLoadersFile(internalDirectory, entryName);
|
|
82
|
+
const code2 = templates.routesForServer({
|
|
83
|
+
routes
|
|
84
|
+
});
|
|
85
|
+
await fs.ensureFile(routesServerFile);
|
|
86
|
+
await fs.writeFile(routesServerFile, code2);
|
|
87
|
+
}
|
|
88
|
+
const serverLoaderCombined = templates.ssrLoaderCombinedModule(entrypoints, entrypoint, config2, appContext);
|
|
89
|
+
if (serverLoaderCombined) {
|
|
90
|
+
const serverLoaderFile = getServerCombinedModueFile(internalDirectory, entryName);
|
|
91
|
+
await fs.outputFile(serverLoaderFile, serverLoaderCombined);
|
|
92
|
+
}
|
|
93
|
+
fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, "utf8");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export {
|
|
99
|
+
generateCode
|
|
100
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
};
|