@modern-js/app-tools 1.21.3 → 2.0.0-beta.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/CHANGELOG.md +138 -16
- package/dist/js/modern/analyze/constants.js +1 -0
- package/dist/js/modern/analyze/generateCode.js +118 -16
- package/dist/js/modern/analyze/getBundleEntry.js +3 -2
- package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +198 -0
- package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +8 -28
- package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
- package/dist/js/modern/analyze/getClientRoutes/utils.js +21 -0
- package/dist/js/modern/analyze/getFileSystemEntry.js +31 -12
- package/dist/js/modern/analyze/index.js +2 -1
- package/dist/js/modern/analyze/nestedRoutes.js +127 -0
- package/dist/js/modern/analyze/templates.js +164 -10
- package/dist/js/modern/analyze/utils.js +2 -1
- package/dist/js/modern/builder/builderPlugins/compatModern.js +226 -0
- package/dist/js/modern/builder/createHtmlConfig.js +58 -0
- package/dist/js/modern/builder/createOutputConfig.js +67 -0
- package/dist/js/modern/builder/createSourceConfig.js +85 -0
- package/dist/js/modern/builder/createToolsConfig.js +94 -0
- package/dist/js/modern/builder/index.js +106 -0
- package/dist/js/modern/builder/share.js +48 -0
- package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +37 -0
- package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +44 -0
- package/dist/js/modern/commands/build.js +41 -95
- package/dist/js/modern/commands/dev.js +15 -19
- package/dist/js/modern/commands/inspect.js +17 -58
- package/dist/js/modern/commands/start.js +2 -1
- package/dist/js/modern/index.js +14 -4
- package/dist/js/modern/locale/en.js +0 -1
- package/dist/js/modern/locale/zh.js +0 -1
- package/dist/js/modern/utils/commands.js +5 -0
- package/dist/js/modern/utils/createCompiler.js +37 -40
- package/dist/js/modern/utils/createServer.js +16 -0
- package/dist/js/modern/utils/printInstructions.js +1 -1
- package/dist/js/node/analyze/constants.js +3 -1
- package/dist/js/node/analyze/generateCode.js +121 -14
- package/dist/js/node/analyze/getBundleEntry.js +3 -2
- package/dist/js/node/analyze/getClientRoutes/getRoutes.js +221 -0
- package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +16 -35
- package/dist/js/node/analyze/getClientRoutes/index.js +21 -0
- package/dist/js/node/analyze/getClientRoutes/utils.js +42 -0
- package/dist/js/node/analyze/getFileSystemEntry.js +30 -11
- package/dist/js/node/analyze/index.js +2 -1
- package/dist/js/node/analyze/nestedRoutes.js +144 -0
- package/dist/js/node/analyze/templates.js +167 -10
- package/dist/js/node/analyze/utils.js +6 -2
- package/dist/js/node/builder/builderPlugins/compatModern.js +245 -0
- package/dist/js/node/builder/createHtmlConfig.js +76 -0
- package/dist/js/node/builder/createOutputConfig.js +75 -0
- package/dist/js/node/builder/createSourceConfig.js +99 -0
- package/dist/js/node/builder/createToolsConfig.js +103 -0
- package/dist/js/node/builder/index.js +129 -0
- package/dist/js/node/builder/share.js +63 -0
- package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
- package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
- package/dist/js/node/commands/build.js +44 -98
- package/dist/js/node/commands/dev.js +13 -23
- package/dist/js/node/commands/inspect.js +18 -74
- package/dist/js/node/commands/start.js +3 -1
- package/dist/js/node/index.js +19 -2
- package/dist/js/node/locale/en.js +0 -1
- package/dist/js/node/locale/zh.js +0 -1
- package/dist/js/node/utils/commands.js +14 -0
- package/dist/js/node/utils/createCompiler.js +43 -49
- package/dist/js/node/utils/createServer.js +21 -2
- package/dist/js/node/utils/printInstructions.js +1 -1
- package/dist/js/treeshaking/analyze/constants.js +16 -0
- package/dist/js/treeshaking/analyze/generateCode.js +412 -0
- package/dist/js/treeshaking/analyze/getBundleEntry.js +77 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +210 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +211 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +27 -0
- package/dist/js/treeshaking/analyze/getFileSystemEntry.js +109 -0
- package/dist/js/treeshaking/analyze/getHtmlTemplate.js +153 -0
- package/dist/js/treeshaking/analyze/getServerRoutes.js +170 -0
- package/dist/js/treeshaking/analyze/index.js +214 -0
- package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +32 -0
- package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +18 -0
- package/dist/js/treeshaking/analyze/nestedRoutes.js +207 -0
- package/dist/js/treeshaking/analyze/templates.js +189 -0
- package/dist/js/treeshaking/analyze/utils.js +95 -0
- package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +231 -0
- package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
- package/dist/js/treeshaking/builder/createOutputConfig.js +67 -0
- package/dist/js/treeshaking/builder/createSourceConfig.js +100 -0
- package/dist/js/treeshaking/builder/createToolsConfig.js +90 -0
- package/dist/js/treeshaking/builder/index.js +172 -0
- package/dist/js/treeshaking/builder/share.js +50 -0
- package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +58 -0
- package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
- package/dist/js/treeshaking/commands/build.js +168 -0
- package/dist/js/treeshaking/commands/deploy.js +29 -0
- package/dist/js/treeshaking/commands/dev.js +146 -0
- package/dist/js/treeshaking/commands/index.js +3 -0
- package/dist/js/treeshaking/commands/inspect.js +52 -0
- package/dist/js/treeshaking/commands/start.js +78 -0
- package/dist/js/treeshaking/exports/server.js +1 -0
- package/dist/js/treeshaking/hooks.js +21 -0
- package/dist/js/treeshaking/index.js +304 -0
- package/dist/js/treeshaking/locale/en.js +34 -0
- package/dist/js/treeshaking/locale/index.js +9 -0
- package/dist/js/treeshaking/locale/zh.js +34 -0
- package/dist/js/treeshaking/utils/commands.js +5 -0
- package/dist/js/treeshaking/utils/config.js +140 -0
- package/dist/js/treeshaking/utils/createCompiler.js +117 -0
- package/dist/js/treeshaking/utils/createServer.js +83 -0
- package/dist/js/treeshaking/utils/getSpecifiedEntries.js +68 -0
- package/dist/js/treeshaking/utils/language.js +5 -0
- package/dist/js/treeshaking/utils/printInstructions.js +35 -0
- package/dist/js/treeshaking/utils/routes.js +33 -0
- package/dist/js/treeshaking/utils/types.js +0 -0
- package/dist/types/analyze/constants.d.ts +1 -0
- package/dist/types/analyze/generateCode.d.ts +1 -1
- package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
- package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
- package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
- package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
- package/dist/types/analyze/index.d.ts +2 -2
- package/dist/types/analyze/nestedRoutes.d.ts +5 -0
- package/dist/types/analyze/templates.d.ts +17 -3
- package/dist/types/analyze/utils.d.ts +2 -1
- package/dist/types/builder/builderPlugins/compatModern.d.ts +14 -0
- package/dist/types/builder/createHtmlConfig.d.ts +6 -0
- package/dist/types/builder/createOutputConfig.d.ts +3 -0
- package/dist/types/builder/createSourceConfig.d.ts +5 -0
- package/dist/types/builder/createToolsConfig.d.ts +13 -0
- package/dist/types/builder/index.d.ts +19 -0
- package/dist/types/builder/share.d.ts +26 -0
- package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
- package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
- package/dist/types/commands/build.d.ts +2 -1
- package/dist/types/commands/deploy.d.ts +2 -1
- package/dist/types/commands/dev.d.ts +2 -1
- package/dist/types/commands/inspect.d.ts +5 -6
- package/dist/types/commands/start.d.ts +2 -1
- package/dist/types/hooks.d.ts +15 -10
- package/dist/types/index.d.ts +4 -2
- package/dist/types/locale/en.d.ts +0 -1
- package/dist/types/locale/index.d.ts +0 -2
- package/dist/types/locale/zh.d.ts +0 -1
- package/dist/types/utils/commands.d.ts +1 -0
- package/dist/types/utils/createCompiler.d.ts +9 -10
- package/dist/types/utils/createServer.d.ts +8 -1
- package/dist/types/utils/printInstructions.d.ts +3 -2
- package/dist/types/utils/types.d.ts +2 -3
- package/package.json +25 -43
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getClientRoutes = void 0;
|
|
7
|
+
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _makeLegalIdentifier = require("../makeLegalIdentifier");
|
|
13
|
+
|
|
14
|
+
var _constants = require("../constants");
|
|
15
|
+
|
|
16
|
+
var _utils2 = require("../utils");
|
|
17
|
+
|
|
18
|
+
var _utils3 = require("./utils");
|
|
19
|
+
|
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
+
|
|
22
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
23
|
+
|
|
24
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
25
|
+
|
|
26
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
27
|
+
|
|
28
|
+
const compName = (srcDirectory, filePath) => {
|
|
29
|
+
const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
|
|
30
|
+
return `Comp_${legalCompName}`;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const layoutNameAbbr = filePath => {
|
|
34
|
+
const prefix = 'L_';
|
|
35
|
+
const dirName = _path.default.dirname(filePath).split('/').pop() || '';
|
|
36
|
+
return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const parents = [];
|
|
40
|
+
/* eslint-disable no-param-reassign */
|
|
41
|
+
|
|
42
|
+
const recursiveReadDir = ({
|
|
43
|
+
dir,
|
|
44
|
+
routes,
|
|
45
|
+
basePath: _basePath = '/',
|
|
46
|
+
srcDirectory,
|
|
47
|
+
srcAlias
|
|
48
|
+
}) => {
|
|
49
|
+
let hasDynamicRoute = false;
|
|
50
|
+
let resetParent = false;
|
|
51
|
+
let parent = parents[parents.length - 1];
|
|
52
|
+
const layout = (0, _utils3.findLayout)(dir);
|
|
53
|
+
|
|
54
|
+
if (layout) {
|
|
55
|
+
if (_basePath === '/') {
|
|
56
|
+
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
57
|
+
} else {
|
|
58
|
+
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, layout, srcAlias);
|
|
59
|
+
const componentName = compName(srcDirectory, layout);
|
|
60
|
+
const route = {
|
|
61
|
+
path: `${_basePath.substring(0, _basePath.length - 1)}`,
|
|
62
|
+
children: [],
|
|
63
|
+
_component: alias,
|
|
64
|
+
component: componentName,
|
|
65
|
+
parent,
|
|
66
|
+
type: 'page'
|
|
67
|
+
};
|
|
68
|
+
parent = route;
|
|
69
|
+
resetParent = true;
|
|
70
|
+
routes.push(route);
|
|
71
|
+
parents.push(route);
|
|
72
|
+
routes = route.children;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const relative of _utils.fs.readdirSync(dir)) {
|
|
77
|
+
const filePath = _path.default.join(dir, relative);
|
|
78
|
+
|
|
79
|
+
if (!(0, _utils3.shouldSkip)(filePath)) {
|
|
80
|
+
const filename = _path.default.basename(filePath, _path.default.extname(filePath));
|
|
81
|
+
|
|
82
|
+
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
|
|
83
|
+
const componentName = compName(srcDirectory, filePath);
|
|
84
|
+
|
|
85
|
+
const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
86
|
+
|
|
87
|
+
if (dynamicRouteMatched) {
|
|
88
|
+
if (hasDynamicRoute) {
|
|
89
|
+
throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
|
|
90
|
+
} else {
|
|
91
|
+
hasDynamicRoute = true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const route = {
|
|
96
|
+
path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
|
97
|
+
_component: alias,
|
|
98
|
+
component: componentName,
|
|
99
|
+
parent,
|
|
100
|
+
type: 'page'
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
if (_utils.fs.statSync(filePath).isDirectory()) {
|
|
104
|
+
recursiveReadDir({
|
|
105
|
+
dir: filePath,
|
|
106
|
+
routes,
|
|
107
|
+
basePath: `${route.path}/`,
|
|
108
|
+
srcDirectory,
|
|
109
|
+
srcAlias
|
|
110
|
+
});
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
|
|
119
|
+
route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (filename === '404' && _basePath === '/') {
|
|
123
|
+
route.path = '*';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
routes.push(route);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (resetParent) {
|
|
131
|
+
parents.pop();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
/* eslint-enable no-param-reassign */
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
|
138
|
+
const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.children) ? flat(route.children) : [route]), []);
|
|
139
|
+
|
|
140
|
+
const generate = route => {
|
|
141
|
+
const codes = [];
|
|
142
|
+
let lastComponent = route.component;
|
|
143
|
+
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`]; // eslint-disable-next-line no-param-reassign, no-cond-assign
|
|
144
|
+
|
|
145
|
+
while (route = route.parent) {
|
|
146
|
+
const layoutComponent = route.component;
|
|
147
|
+
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
148
|
+
imports.push(`import ${layoutComponent} from '${route._component}';`);
|
|
149
|
+
const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
|
|
150
|
+
codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
|
|
151
|
+
lastComponent = currentComponent;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
|
155
|
+
|
|
156
|
+
_utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
component: lastComponent,
|
|
160
|
+
_component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
|
|
165
|
+
parent: undefined
|
|
166
|
+
}) : _objectSpread(_objectSpread({}, route), {}, {
|
|
167
|
+
parent: undefined
|
|
168
|
+
}));
|
|
169
|
+
return normalized;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const getClientRoutes = ({
|
|
173
|
+
entrypoint,
|
|
174
|
+
srcDirectory,
|
|
175
|
+
srcAlias,
|
|
176
|
+
internalDirectory,
|
|
177
|
+
internalDirAlias
|
|
178
|
+
}) => {
|
|
179
|
+
const {
|
|
180
|
+
entry,
|
|
181
|
+
entryName
|
|
182
|
+
} = entrypoint;
|
|
183
|
+
|
|
184
|
+
if (!_utils.fs.existsSync(entry)) {
|
|
185
|
+
throw new Error(`generate file system routes error, ${entry} directory not found.`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
|
|
189
|
+
throw new Error(`generate file system routes error, ${entry} should be directory.`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let routes = [];
|
|
193
|
+
recursiveReadDir({
|
|
194
|
+
dir: entry,
|
|
195
|
+
routes,
|
|
196
|
+
basePath: '/',
|
|
197
|
+
srcDirectory,
|
|
198
|
+
srcAlias
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
|
|
202
|
+
|
|
203
|
+
_utils.fs.emptyDirSync(internalComponentsDir);
|
|
204
|
+
|
|
205
|
+
routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
|
|
206
|
+
parents.length = 0; // FIXME: support more situations
|
|
207
|
+
|
|
208
|
+
routes.sort((a, b) => {
|
|
209
|
+
const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
|
|
210
|
+
|
|
211
|
+
if (delta === 0) {
|
|
212
|
+
return a.path.length - b.path.length;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return delta;
|
|
216
|
+
});
|
|
217
|
+
(0, _utils3.debug)(`fileSystem routes: %o`, routes);
|
|
218
|
+
return routes;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
exports.getClientRoutes = getClientRoutes;
|
|
@@ -9,9 +9,13 @@ var _path = _interopRequireDefault(require("path"));
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("@modern-js/utils");
|
|
11
11
|
|
|
12
|
-
var _makeLegalIdentifier = require("
|
|
12
|
+
var _makeLegalIdentifier = require("../makeLegalIdentifier");
|
|
13
13
|
|
|
14
|
-
var _constants = require("
|
|
14
|
+
var _constants = require("../constants");
|
|
15
|
+
|
|
16
|
+
var _utils2 = require("../utils");
|
|
17
|
+
|
|
18
|
+
var _utils3 = require("./utils");
|
|
15
19
|
|
|
16
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
21
|
|
|
@@ -21,27 +25,6 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
21
25
|
|
|
22
26
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
23
27
|
|
|
24
|
-
const debug = (0, _utils.createDebugger)('get-client-routes');
|
|
25
|
-
|
|
26
|
-
const findLayout = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
|
|
27
|
-
|
|
28
|
-
const shouldSkip = file => {
|
|
29
|
-
// should not skip directory.
|
|
30
|
-
if (_utils.fs.statSync(file).isDirectory()) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const ext = _path.default.extname(file);
|
|
35
|
-
|
|
36
|
-
if (_constants.FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !_constants.JS_EXTENSIONS.includes(ext) || _constants.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === _path.default.basename(file, ext)) {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return false;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const replaceWithAlias = (base, filePath, alias) => (0, _utils.normalizeToPosixPath)(_path.default.join(alias, _path.default.relative(base, filePath)));
|
|
44
|
-
|
|
45
28
|
const compName = (srcDirectory, filePath) => {
|
|
46
29
|
const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
|
|
47
30
|
return `Comp_${legalCompName}`;
|
|
@@ -56,7 +39,7 @@ const layoutNameAbbr = filePath => {
|
|
|
56
39
|
const parents = [];
|
|
57
40
|
/* eslint-disable no-param-reassign */
|
|
58
41
|
|
|
59
|
-
const
|
|
42
|
+
const recursiveReadDirLegacy = ({
|
|
60
43
|
dir,
|
|
61
44
|
routes,
|
|
62
45
|
basePath: _basePath = '/',
|
|
@@ -66,13 +49,13 @@ const recursiveReadDir = ({
|
|
|
66
49
|
let hasDynamicRoute = false;
|
|
67
50
|
let resetParent = false;
|
|
68
51
|
let parent = parents[parents.length - 1];
|
|
69
|
-
const layout = findLayout(dir);
|
|
52
|
+
const layout = (0, _utils3.findLayout)(dir);
|
|
70
53
|
|
|
71
54
|
if (layout) {
|
|
72
55
|
if (_basePath === '/') {
|
|
73
56
|
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
74
57
|
} else {
|
|
75
|
-
const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
|
|
58
|
+
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, layout, srcAlias);
|
|
76
59
|
const componentName = compName(srcDirectory, layout);
|
|
77
60
|
const route = {
|
|
78
61
|
path: `${_basePath.substring(0, _basePath.length - 1)}`,
|
|
@@ -93,10 +76,10 @@ const recursiveReadDir = ({
|
|
|
93
76
|
for (const relative of _utils.fs.readdirSync(dir)) {
|
|
94
77
|
const filePath = _path.default.join(dir, relative);
|
|
95
78
|
|
|
96
|
-
if (!shouldSkip(filePath)) {
|
|
79
|
+
if (!(0, _utils3.shouldSkip)(filePath)) {
|
|
97
80
|
const filename = _path.default.basename(filePath, _path.default.extname(filePath));
|
|
98
81
|
|
|
99
|
-
const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
|
|
82
|
+
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
|
|
100
83
|
const componentName = compName(srcDirectory, filePath);
|
|
101
84
|
|
|
102
85
|
const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
@@ -118,7 +101,7 @@ const recursiveReadDir = ({
|
|
|
118
101
|
};
|
|
119
102
|
|
|
120
103
|
if (_utils.fs.statSync(filePath).isDirectory()) {
|
|
121
|
-
|
|
104
|
+
recursiveReadDirLegacy({
|
|
122
105
|
dir: filePath,
|
|
123
106
|
routes,
|
|
124
107
|
basePath: `${route.path}/`,
|
|
@@ -175,7 +158,7 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
175
158
|
|
|
176
159
|
return {
|
|
177
160
|
component: lastComponent,
|
|
178
|
-
_component: replaceWithAlias(internalDirectory, file, internalDirAlias)
|
|
161
|
+
_component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
|
|
179
162
|
};
|
|
180
163
|
};
|
|
181
164
|
|
|
@@ -187,8 +170,6 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
187
170
|
return normalized;
|
|
188
171
|
};
|
|
189
172
|
|
|
190
|
-
const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
|
|
191
|
-
|
|
192
173
|
const getClientRoutes = ({
|
|
193
174
|
entrypoint,
|
|
194
175
|
srcDirectory,
|
|
@@ -210,7 +191,7 @@ const getClientRoutes = ({
|
|
|
210
191
|
}
|
|
211
192
|
|
|
212
193
|
let routes = [];
|
|
213
|
-
|
|
194
|
+
recursiveReadDirLegacy({
|
|
214
195
|
dir: entry,
|
|
215
196
|
routes,
|
|
216
197
|
basePath: '/',
|
|
@@ -226,7 +207,7 @@ const getClientRoutes = ({
|
|
|
226
207
|
parents.length = 0; // FIXME: support more situations
|
|
227
208
|
|
|
228
209
|
routes.sort((a, b) => {
|
|
229
|
-
const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
|
|
210
|
+
const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
|
|
230
211
|
|
|
231
212
|
if (delta === 0) {
|
|
232
213
|
return a.path.length - b.path.length;
|
|
@@ -234,7 +215,7 @@ const getClientRoutes = ({
|
|
|
234
215
|
|
|
235
216
|
return delta;
|
|
236
217
|
});
|
|
237
|
-
debug(`fileSystem routes: %o`, routes);
|
|
218
|
+
(0, _utils3.debug)(`fileSystem routes: %o`, routes);
|
|
238
219
|
return routes;
|
|
239
220
|
};
|
|
240
221
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "getClientRoutes", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _getRoutes.getClientRoutes;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "getClientRoutesLegacy", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _getRoutesLegacy.getClientRoutes;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
var _getRoutes = require("./getRoutes");
|
|
20
|
+
|
|
21
|
+
var _getRoutesLegacy = require("./getRoutesLegacy");
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.shouldSkip = exports.getRouteWeight = exports.findLayout = exports.debug = void 0;
|
|
7
|
+
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _constants = require("../constants");
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
const debug = (0, _utils.createDebugger)('get-client-routes');
|
|
17
|
+
exports.debug = debug;
|
|
18
|
+
|
|
19
|
+
const findLayout = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
|
|
20
|
+
|
|
21
|
+
exports.findLayout = findLayout;
|
|
22
|
+
|
|
23
|
+
const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
|
|
24
|
+
|
|
25
|
+
exports.getRouteWeight = getRouteWeight;
|
|
26
|
+
|
|
27
|
+
const shouldSkip = file => {
|
|
28
|
+
// should not skip directory.
|
|
29
|
+
if (_utils.fs.statSync(file).isDirectory()) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const ext = _path.default.extname(file);
|
|
34
|
+
|
|
35
|
+
if (_constants.FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !_constants.JS_EXTENSIONS.includes(ext) || _constants.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === _path.default.basename(file, ext)) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return false;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
exports.shouldSkip = shouldSkip;
|
|
@@ -23,7 +23,9 @@ const hasApp = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext =>
|
|
|
23
23
|
|
|
24
24
|
const hasPages = dir => _fs.default.existsSync(_path.default.join(dir, _constants.PAGES_DIR_NAME));
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const hasNestedRoutes = dir => _fs.default.existsSync(_path.default.join(dir, _constants.NESTED_ROUTES_DIR));
|
|
27
|
+
|
|
28
|
+
const isBundleEntry = dir => hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
|
|
27
29
|
|
|
28
30
|
const scanDir = dirs => dirs.map(dir => {
|
|
29
31
|
const indexFile = hasIndex(dir);
|
|
@@ -39,30 +41,47 @@ const scanDir = dirs => dirs.map(dir => {
|
|
|
39
41
|
};
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
|
|
44
|
+
const isHasApp = hasApp(dir);
|
|
45
|
+
|
|
46
|
+
if (isHasApp) {
|
|
43
47
|
return {
|
|
44
48
|
entryName,
|
|
45
49
|
entry: _path.default.join(dir, _constants.APP_FILE_NAME),
|
|
46
50
|
isAutoMount: true,
|
|
47
51
|
customBootstrap
|
|
48
52
|
};
|
|
49
|
-
}
|
|
50
|
-
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const isHasNestedRoutes = hasNestedRoutes(dir);
|
|
56
|
+
const isHasPages = hasPages(dir);
|
|
57
|
+
|
|
58
|
+
if (isHasNestedRoutes || isHasPages) {
|
|
59
|
+
const entrypoint = {
|
|
51
60
|
entryName,
|
|
52
|
-
entry:
|
|
61
|
+
entry: '',
|
|
53
62
|
fileSystemRoutes: {
|
|
54
63
|
globalApp: (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `./${_constants.PAGES_DIR_NAME}/${_constants.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`)))
|
|
55
64
|
},
|
|
56
65
|
isAutoMount: true,
|
|
57
66
|
customBootstrap
|
|
58
67
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
|
|
69
|
+
if (isHasPages) {
|
|
70
|
+
entrypoint.entry = _path.default.join(dir, _constants.PAGES_DIR_NAME);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (isHasNestedRoutes) {
|
|
74
|
+
entrypoint.nestedRoutesEntry = _path.default.join(dir, _constants.NESTED_ROUTES_DIR);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return entrypoint;
|
|
65
78
|
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
entryName,
|
|
82
|
+
entry: indexFile,
|
|
83
|
+
isAutoMount: false
|
|
84
|
+
};
|
|
66
85
|
});
|
|
67
86
|
|
|
68
87
|
const getFileSystemEntry = (appContext, config) => {
|
|
@@ -123,7 +123,8 @@ var _default = () => ({
|
|
|
123
123
|
entrypoints,
|
|
124
124
|
serverRoutes: routes
|
|
125
125
|
}));
|
|
126
|
-
|
|
126
|
+
const nestedRouteEntries = entrypoints.map(point => point.nestedRoutesEntry).filter(Boolean);
|
|
127
|
+
pagesDir = entrypoints.map(point => point.entry).filter(Boolean).concat(nestedRouteEntries);
|
|
127
128
|
originEntrypoints = (0, _lodash.cloneDeep)(entrypoints);
|
|
128
129
|
await generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
129
130
|
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.walk = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _constants = require("./constants");
|
|
13
|
+
|
|
14
|
+
var _utils2 = require("./utils");
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
21
|
+
|
|
22
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
23
|
+
|
|
24
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
25
|
+
|
|
26
|
+
const LAYOUT_FILE = 'layout';
|
|
27
|
+
const PAGE_FILE = 'page';
|
|
28
|
+
const LOADING_FILE = 'loading';
|
|
29
|
+
const ERROR_FILE = 'error';
|
|
30
|
+
const LOADER_FILE = 'loader';
|
|
31
|
+
const conventionNames = [LAYOUT_FILE, PAGE_FILE, LOADING_FILE, ERROR_FILE, LOADER_FILE];
|
|
32
|
+
|
|
33
|
+
const replaceDynamicPath = routePath => {
|
|
34
|
+
return routePath.replace(/\[(.*?)\]/g, ':$1');
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const createIndexRoute = (routeInfo, rootDir, filename) => {
|
|
38
|
+
return createRoute(_objectSpread(_objectSpread({}, routeInfo), {}, {
|
|
39
|
+
index: true,
|
|
40
|
+
children: undefined
|
|
41
|
+
}), rootDir, filename);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const createRoute = (routeInfo, rootDir, filename) => {
|
|
45
|
+
const id = (0, _utils.getRouteId)(filename, rootDir);
|
|
46
|
+
return _objectSpread(_objectSpread({}, routeInfo), {}, {
|
|
47
|
+
id,
|
|
48
|
+
type: 'nested'
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const walk = async (dirname, rootDir, alias) => {
|
|
53
|
+
if (!(await _utils.fs.pathExists(dirname))) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const isDirectory = (await _utils.fs.stat(dirname)).isDirectory();
|
|
58
|
+
|
|
59
|
+
if (!isDirectory) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const relativeDir = path.relative(rootDir, dirname);
|
|
64
|
+
const pathSegments = relativeDir.split(path.sep);
|
|
65
|
+
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
66
|
+
const isRoot = lastSegment === '';
|
|
67
|
+
const isPathlessLayout = lastSegment.startsWith('__');
|
|
68
|
+
const isWithoutLayoutPath = lastSegment.includes('.');
|
|
69
|
+
let routePath = isRoot || isPathlessLayout ? '/' : `${lastSegment}`;
|
|
70
|
+
|
|
71
|
+
if (isWithoutLayoutPath) {
|
|
72
|
+
routePath = lastSegment.split('.').join('/');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
routePath = replaceDynamicPath(routePath);
|
|
76
|
+
const route = {
|
|
77
|
+
path: routePath,
|
|
78
|
+
children: []
|
|
79
|
+
};
|
|
80
|
+
const items = await _utils.fs.readdir(dirname);
|
|
81
|
+
|
|
82
|
+
for (const item of items) {
|
|
83
|
+
const itemPath = path.join(dirname, item);
|
|
84
|
+
const extname = path.extname(item);
|
|
85
|
+
const itemWithoutExt = item.slice(0, -extname.length);
|
|
86
|
+
const isDirectory = (await _utils.fs.stat(itemPath)).isDirectory();
|
|
87
|
+
|
|
88
|
+
if (isDirectory) {
|
|
89
|
+
const childRoute = await walk(itemPath, rootDir, alias);
|
|
90
|
+
|
|
91
|
+
if (childRoute) {
|
|
92
|
+
var _route$children;
|
|
93
|
+
|
|
94
|
+
(_route$children = route.children) === null || _route$children === void 0 ? void 0 : _route$children.push(childRoute);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (extname && (!_constants.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (itemWithoutExt === LAYOUT_FILE) {
|
|
103
|
+
route._component = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (itemWithoutExt === PAGE_FILE) {
|
|
107
|
+
var _route$children2;
|
|
108
|
+
|
|
109
|
+
const childRoute = createIndexRoute({
|
|
110
|
+
_component: (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name)
|
|
111
|
+
}, rootDir, itemPath);
|
|
112
|
+
(_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.push(childRoute);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (itemWithoutExt === LOADER_FILE) {
|
|
116
|
+
route.loader = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (itemWithoutExt === LOADING_FILE) {
|
|
120
|
+
route.loading = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (itemWithoutExt === ERROR_FILE) {
|
|
124
|
+
route.error = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const finalRoute = createRoute(route, rootDir, path.join(dirname, `${LAYOUT_FILE}.ts`));
|
|
129
|
+
/**
|
|
130
|
+
* when the url is /, the __auth/layout.tsx component should not be rendered
|
|
131
|
+
* - routes
|
|
132
|
+
* - __auth
|
|
133
|
+
* - layout.tsx
|
|
134
|
+
* - layout.tsx
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
if (isPathlessLayout) {
|
|
138
|
+
delete finalRoute.path;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return finalRoute;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
exports.walk = walk;
|