@modern-js/app-tools 2.0.0-beta.0 → 2.0.0-beta.1
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 +181 -0
- package/dist/js/modern/analyze/generateCode.js +29 -37
- package/dist/js/modern/analyze/getBundleEntry.js +4 -12
- package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +5 -30
- package/dist/js/modern/analyze/getClientRoutes/getRoutesLegacy.js +5 -30
- package/dist/js/modern/analyze/getClientRoutes/utils.js +0 -3
- package/dist/js/modern/analyze/getFileSystemEntry.js +4 -17
- package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
- package/dist/js/modern/analyze/getServerRoutes.js +15 -32
- package/dist/js/modern/analyze/index.js +79 -22
- package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
- package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
- package/dist/js/modern/analyze/nestedRoutes.js +8 -33
- package/dist/js/modern/analyze/templates.js +22 -37
- package/dist/js/modern/analyze/utils.js +0 -7
- package/dist/js/modern/builder/builderPlugins/compatModern.js +30 -48
- package/dist/js/modern/builder/createHtmlConfig.js +2 -2
- package/dist/js/modern/builder/createOutputConfig.js +7 -4
- package/dist/js/modern/builder/createSourceConfig.js +0 -11
- package/dist/js/modern/builder/createToolsConfig.js +3 -10
- package/dist/js/modern/builder/index.js +24 -22
- package/dist/js/modern/builder/share.js +0 -4
- package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -8
- package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +2 -13
- package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
- package/dist/js/modern/commands/build.js +6 -46
- package/dist/js/modern/commands/dev.js +4 -16
- package/dist/js/modern/commands/inspect.js +3 -20
- package/dist/js/modern/commands/start.js +0 -2
- package/dist/js/modern/index.js +6 -15
- package/dist/js/modern/utils/config.js +1 -12
- package/dist/js/modern/utils/createServer.js +0 -5
- package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
- package/dist/js/modern/utils/printInstructions.js +2 -1
- package/dist/js/modern/utils/routes.js +0 -2
- package/dist/js/node/analyze/generateCode.js +29 -60
- package/dist/js/node/analyze/getBundleEntry.js +4 -18
- package/dist/js/node/analyze/getClientRoutes/getRoutes.js +5 -45
- package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +5 -45
- package/dist/js/node/analyze/getClientRoutes/index.js +0 -2
- package/dist/js/node/analyze/getClientRoutes/utils.js +0 -14
- package/dist/js/node/analyze/getFileSystemEntry.js +4 -25
- package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
- package/dist/js/node/analyze/getServerRoutes.js +14 -40
- package/dist/js/node/analyze/index.js +79 -32
- package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
- package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
- package/dist/js/node/analyze/nestedRoutes.js +8 -41
- package/dist/js/node/analyze/templates.js +22 -47
- package/dist/js/node/analyze/utils.js +0 -20
- package/dist/js/node/builder/builderPlugins/compatModern.js +30 -59
- package/dist/js/node/builder/createHtmlConfig.js +2 -10
- package/dist/js/node/builder/createOutputConfig.js +7 -6
- package/dist/js/node/builder/createSourceConfig.js +0 -17
- package/dist/js/node/builder/createToolsConfig.js +3 -12
- package/dist/js/node/builder/index.js +24 -36
- package/dist/js/node/builder/share.js +0 -12
- package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -10
- package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +2 -15
- package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
- package/dist/js/node/commands/build.js +4 -57
- package/dist/js/node/commands/deploy.js +0 -2
- package/dist/js/node/commands/dev.js +3 -27
- package/dist/js/node/commands/index.js +0 -6
- package/dist/js/node/commands/inspect.js +3 -27
- package/dist/js/node/commands/start.js +0 -11
- package/dist/js/node/exports/server.js +0 -1
- package/dist/js/node/hooks.js +0 -2
- package/dist/js/node/index.js +7 -34
- package/dist/js/node/locale/index.js +0 -4
- package/dist/js/node/utils/commands.js +0 -2
- package/dist/js/node/utils/config.js +1 -25
- package/dist/js/node/utils/createServer.js +0 -15
- package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
- package/dist/js/node/utils/language.js +0 -2
- package/dist/js/node/utils/printInstructions.js +2 -5
- package/dist/js/node/utils/routes.js +0 -5
- package/dist/js/treeshaking/analyze/generateCode.js +57 -100
- package/dist/js/treeshaking/analyze/getBundleEntry.js +11 -19
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +16 -42
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +16 -42
- package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +0 -3
- package/dist/js/treeshaking/analyze/getFileSystemEntry.js +4 -17
- package/dist/js/treeshaking/analyze/getHtmlTemplate.js +2 -26
- package/dist/js/treeshaking/analyze/getServerRoutes.js +24 -37
- package/dist/js/treeshaking/analyze/index.js +185 -65
- package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +0 -4
- package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +0 -2
- package/dist/js/treeshaking/analyze/nestedRoutes.js +9 -51
- package/dist/js/treeshaking/analyze/templates.js +31 -50
- package/dist/js/treeshaking/analyze/utils.js +7 -14
- package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +40 -59
- package/dist/js/treeshaking/builder/createHtmlConfig.js +15 -15
- package/dist/js/treeshaking/builder/createOutputConfig.js +26 -23
- package/dist/js/treeshaking/builder/createSourceConfig.js +7 -19
- package/dist/js/treeshaking/builder/createToolsConfig.js +16 -21
- package/dist/js/treeshaking/builder/index.js +38 -49
- package/dist/js/treeshaking/builder/share.js +0 -4
- package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +1 -13
- package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +2 -14
- package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
- package/dist/js/treeshaking/commands/build.js +26 -111
- package/dist/js/treeshaking/commands/deploy.js +0 -3
- package/dist/js/treeshaking/commands/dev.js +13 -35
- package/dist/js/treeshaking/commands/inspect.js +5 -28
- package/dist/js/treeshaking/commands/start.js +0 -9
- package/dist/js/treeshaking/index.js +2 -39
- package/dist/js/treeshaking/utils/config.js +1 -17
- package/dist/js/treeshaking/utils/createServer.js +0 -10
- package/dist/js/treeshaking/utils/getSpecifiedEntries.js +0 -10
- package/dist/js/treeshaking/utils/printInstructions.js +0 -5
- package/dist/js/treeshaking/utils/routes.js +0 -4
- package/dist/types/analyze/index.d.ts +2 -3
- package/dist/types/analyze/nestedRoutes.d.ts +1 -1
- package/dist/types/analyze/templates.d.ts +3 -1
- package/dist/types/builder/builderPlugins/compatModern.d.ts +0 -1
- package/dist/types/builder/index.d.ts +2 -6
- package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
- package/dist/types/commands/inspect.d.ts +1 -4
- package/dist/types/index.d.ts +0 -2
- package/dist/types/utils/config.d.ts +0 -1
- package/package.json +23 -22
- package/dist/js/modern/utils/createCompiler.js +0 -58
- package/dist/js/node/utils/createCompiler.js +0 -75
- package/dist/js/treeshaking/utils/createCompiler.js +0 -117
- package/dist/types/utils/createCompiler.d.ts +0 -12
|
@@ -4,41 +4,28 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getClientRoutes = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _makeLegalIdentifier = require("../makeLegalIdentifier");
|
|
13
|
-
|
|
14
10
|
var _constants = require("../constants");
|
|
15
|
-
|
|
16
11
|
var _utils2 = require("../utils");
|
|
17
|
-
|
|
18
12
|
var _utils3 = require("./utils");
|
|
19
|
-
|
|
20
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
14
|
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
15
|
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
16
|
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
17
|
const compName = (srcDirectory, filePath) => {
|
|
29
18
|
const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
|
|
30
19
|
return `Comp_${legalCompName}`;
|
|
31
20
|
};
|
|
32
|
-
|
|
33
21
|
const layoutNameAbbr = filePath => {
|
|
34
22
|
const prefix = 'L_';
|
|
35
23
|
const dirName = _path.default.dirname(filePath).split('/').pop() || '';
|
|
36
24
|
return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
|
|
37
25
|
};
|
|
38
|
-
|
|
39
26
|
const parents = [];
|
|
40
|
-
/* eslint-disable no-param-reassign */
|
|
41
27
|
|
|
28
|
+
/* eslint-disable no-param-reassign */
|
|
42
29
|
const recursiveReadDir = ({
|
|
43
30
|
dir,
|
|
44
31
|
routes,
|
|
@@ -50,7 +37,6 @@ const recursiveReadDir = ({
|
|
|
50
37
|
let resetParent = false;
|
|
51
38
|
let parent = parents[parents.length - 1];
|
|
52
39
|
const layout = (0, _utils3.findLayout)(dir);
|
|
53
|
-
|
|
54
40
|
if (layout) {
|
|
55
41
|
if (_basePath === '/') {
|
|
56
42
|
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
@@ -72,18 +58,13 @@ const recursiveReadDir = ({
|
|
|
72
58
|
routes = route.children;
|
|
73
59
|
}
|
|
74
60
|
}
|
|
75
|
-
|
|
76
61
|
for (const relative of _utils.fs.readdirSync(dir)) {
|
|
77
62
|
const filePath = _path.default.join(dir, relative);
|
|
78
|
-
|
|
79
63
|
if (!(0, _utils3.shouldSkip)(filePath)) {
|
|
80
64
|
const filename = _path.default.basename(filePath, _path.default.extname(filePath));
|
|
81
|
-
|
|
82
65
|
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
|
|
83
66
|
const componentName = compName(srcDirectory, filePath);
|
|
84
|
-
|
|
85
67
|
const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
86
|
-
|
|
87
68
|
if (dynamicRouteMatched) {
|
|
88
69
|
if (hasDynamicRoute) {
|
|
89
70
|
throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
|
|
@@ -91,7 +72,6 @@ const recursiveReadDir = ({
|
|
|
91
72
|
hasDynamicRoute = true;
|
|
92
73
|
}
|
|
93
74
|
}
|
|
94
|
-
|
|
95
75
|
const route = {
|
|
96
76
|
path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
|
97
77
|
_component: alias,
|
|
@@ -99,7 +79,6 @@ const recursiveReadDir = ({
|
|
|
99
79
|
parent,
|
|
100
80
|
type: 'page'
|
|
101
81
|
};
|
|
102
|
-
|
|
103
82
|
if (_utils.fs.statSync(filePath).isDirectory()) {
|
|
104
83
|
recursiveReadDir({
|
|
105
84
|
dir: filePath,
|
|
@@ -110,38 +89,32 @@ const recursiveReadDir = ({
|
|
|
110
89
|
});
|
|
111
90
|
continue;
|
|
112
91
|
}
|
|
113
|
-
|
|
114
92
|
if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
115
93
|
continue;
|
|
116
94
|
}
|
|
117
|
-
|
|
118
95
|
if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
|
|
119
96
|
route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
|
|
120
97
|
}
|
|
121
|
-
|
|
122
98
|
if (filename === '404' && _basePath === '/') {
|
|
123
99
|
route.path = '*';
|
|
124
100
|
}
|
|
125
|
-
|
|
126
101
|
routes.push(route);
|
|
127
102
|
}
|
|
128
103
|
}
|
|
129
|
-
|
|
130
104
|
if (resetParent) {
|
|
131
105
|
parents.pop();
|
|
132
106
|
}
|
|
133
107
|
};
|
|
134
108
|
/* eslint-enable no-param-reassign */
|
|
135
109
|
|
|
136
|
-
|
|
137
110
|
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
|
138
111
|
const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.children) ? flat(route.children) : [route]), []);
|
|
139
|
-
|
|
140
112
|
const generate = route => {
|
|
141
113
|
const codes = [];
|
|
142
114
|
let lastComponent = route.component;
|
|
143
|
-
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
|
|
115
|
+
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
|
|
144
116
|
|
|
117
|
+
// eslint-disable-next-line no-param-reassign, no-cond-assign
|
|
145
118
|
while (route = route.parent) {
|
|
146
119
|
const layoutComponent = route.component;
|
|
147
120
|
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
@@ -150,17 +123,13 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
150
123
|
codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
|
|
151
124
|
lastComponent = currentComponent;
|
|
152
125
|
}
|
|
153
|
-
|
|
154
126
|
const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
|
155
|
-
|
|
156
127
|
_utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
|
|
157
|
-
|
|
158
128
|
return {
|
|
159
129
|
component: lastComponent,
|
|
160
130
|
_component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
|
|
161
131
|
};
|
|
162
132
|
};
|
|
163
|
-
|
|
164
133
|
const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
|
|
165
134
|
parent: undefined
|
|
166
135
|
}) : _objectSpread(_objectSpread({}, route), {}, {
|
|
@@ -168,7 +137,6 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
168
137
|
}));
|
|
169
138
|
return normalized;
|
|
170
139
|
};
|
|
171
|
-
|
|
172
140
|
const getClientRoutes = ({
|
|
173
141
|
entrypoint,
|
|
174
142
|
srcDirectory,
|
|
@@ -180,15 +148,12 @@ const getClientRoutes = ({
|
|
|
180
148
|
entry,
|
|
181
149
|
entryName
|
|
182
150
|
} = entrypoint;
|
|
183
|
-
|
|
184
151
|
if (!_utils.fs.existsSync(entry)) {
|
|
185
152
|
throw new Error(`generate file system routes error, ${entry} directory not found.`);
|
|
186
153
|
}
|
|
187
|
-
|
|
188
154
|
if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
|
|
189
155
|
throw new Error(`generate file system routes error, ${entry} should be directory.`);
|
|
190
156
|
}
|
|
191
|
-
|
|
192
157
|
let routes = [];
|
|
193
158
|
recursiveReadDir({
|
|
194
159
|
dir: entry,
|
|
@@ -197,25 +162,20 @@ const getClientRoutes = ({
|
|
|
197
162
|
srcDirectory,
|
|
198
163
|
srcAlias
|
|
199
164
|
});
|
|
200
|
-
|
|
201
165
|
const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
|
|
202
|
-
|
|
203
166
|
_utils.fs.emptyDirSync(internalComponentsDir);
|
|
204
|
-
|
|
205
167
|
routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
|
|
206
|
-
parents.length = 0;
|
|
168
|
+
parents.length = 0;
|
|
207
169
|
|
|
170
|
+
// FIXME: support more situations
|
|
208
171
|
routes.sort((a, b) => {
|
|
209
172
|
const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
|
|
210
|
-
|
|
211
173
|
if (delta === 0) {
|
|
212
174
|
return a.path.length - b.path.length;
|
|
213
175
|
}
|
|
214
|
-
|
|
215
176
|
return delta;
|
|
216
177
|
});
|
|
217
178
|
(0, _utils3.debug)(`fileSystem routes: %o`, routes);
|
|
218
179
|
return routes;
|
|
219
180
|
};
|
|
220
|
-
|
|
221
181
|
exports.getClientRoutes = getClientRoutes;
|
|
@@ -4,41 +4,28 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getClientRoutes = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _makeLegalIdentifier = require("../makeLegalIdentifier");
|
|
13
|
-
|
|
14
10
|
var _constants = require("../constants");
|
|
15
|
-
|
|
16
11
|
var _utils2 = require("../utils");
|
|
17
|
-
|
|
18
12
|
var _utils3 = require("./utils");
|
|
19
|
-
|
|
20
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
14
|
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
15
|
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
16
|
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
17
|
const compName = (srcDirectory, filePath) => {
|
|
29
18
|
const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
|
|
30
19
|
return `Comp_${legalCompName}`;
|
|
31
20
|
};
|
|
32
|
-
|
|
33
21
|
const layoutNameAbbr = filePath => {
|
|
34
22
|
const prefix = 'L_';
|
|
35
23
|
const dirName = _path.default.dirname(filePath).split('/').pop() || '';
|
|
36
24
|
return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
|
|
37
25
|
};
|
|
38
|
-
|
|
39
26
|
const parents = [];
|
|
40
|
-
/* eslint-disable no-param-reassign */
|
|
41
27
|
|
|
28
|
+
/* eslint-disable no-param-reassign */
|
|
42
29
|
const recursiveReadDirLegacy = ({
|
|
43
30
|
dir,
|
|
44
31
|
routes,
|
|
@@ -50,7 +37,6 @@ const recursiveReadDirLegacy = ({
|
|
|
50
37
|
let resetParent = false;
|
|
51
38
|
let parent = parents[parents.length - 1];
|
|
52
39
|
const layout = (0, _utils3.findLayout)(dir);
|
|
53
|
-
|
|
54
40
|
if (layout) {
|
|
55
41
|
if (_basePath === '/') {
|
|
56
42
|
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
@@ -72,18 +58,13 @@ const recursiveReadDirLegacy = ({
|
|
|
72
58
|
routes = route.routes;
|
|
73
59
|
}
|
|
74
60
|
}
|
|
75
|
-
|
|
76
61
|
for (const relative of _utils.fs.readdirSync(dir)) {
|
|
77
62
|
const filePath = _path.default.join(dir, relative);
|
|
78
|
-
|
|
79
63
|
if (!(0, _utils3.shouldSkip)(filePath)) {
|
|
80
64
|
const filename = _path.default.basename(filePath, _path.default.extname(filePath));
|
|
81
|
-
|
|
82
65
|
const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
|
|
83
66
|
const componentName = compName(srcDirectory, filePath);
|
|
84
|
-
|
|
85
67
|
const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
86
|
-
|
|
87
68
|
if (dynamicRouteMatched) {
|
|
88
69
|
if (hasDynamicRoute) {
|
|
89
70
|
throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
|
|
@@ -91,7 +72,6 @@ const recursiveReadDirLegacy = ({
|
|
|
91
72
|
hasDynamicRoute = true;
|
|
92
73
|
}
|
|
93
74
|
}
|
|
94
|
-
|
|
95
75
|
const route = {
|
|
96
76
|
path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
|
97
77
|
_component: alias,
|
|
@@ -99,7 +79,6 @@ const recursiveReadDirLegacy = ({
|
|
|
99
79
|
exact: true,
|
|
100
80
|
parent
|
|
101
81
|
};
|
|
102
|
-
|
|
103
82
|
if (_utils.fs.statSync(filePath).isDirectory()) {
|
|
104
83
|
recursiveReadDirLegacy({
|
|
105
84
|
dir: filePath,
|
|
@@ -110,39 +89,33 @@ const recursiveReadDirLegacy = ({
|
|
|
110
89
|
});
|
|
111
90
|
continue;
|
|
112
91
|
}
|
|
113
|
-
|
|
114
92
|
if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
115
93
|
continue;
|
|
116
94
|
}
|
|
117
|
-
|
|
118
95
|
if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
|
|
119
96
|
route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
|
|
120
97
|
}
|
|
121
|
-
|
|
122
98
|
if (filename === '404' && _basePath === '/') {
|
|
123
99
|
route.path = '*';
|
|
124
100
|
route.exact = false;
|
|
125
101
|
}
|
|
126
|
-
|
|
127
102
|
routes.push(route);
|
|
128
103
|
}
|
|
129
104
|
}
|
|
130
|
-
|
|
131
105
|
if (resetParent) {
|
|
132
106
|
parents.pop();
|
|
133
107
|
}
|
|
134
108
|
};
|
|
135
109
|
/* eslint-enable no-param-reassign */
|
|
136
110
|
|
|
137
|
-
|
|
138
111
|
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
|
139
112
|
const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]), []);
|
|
140
|
-
|
|
141
113
|
const generate = route => {
|
|
142
114
|
const codes = [];
|
|
143
115
|
let lastComponent = route.component;
|
|
144
|
-
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
|
|
116
|
+
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
|
|
145
117
|
|
|
118
|
+
// eslint-disable-next-line no-param-reassign, no-cond-assign
|
|
146
119
|
while (route = route.parent) {
|
|
147
120
|
const layoutComponent = route.component;
|
|
148
121
|
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
@@ -151,17 +124,13 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
151
124
|
codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
|
|
152
125
|
lastComponent = currentComponent;
|
|
153
126
|
}
|
|
154
|
-
|
|
155
127
|
const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
|
156
|
-
|
|
157
128
|
_utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
|
|
158
|
-
|
|
159
129
|
return {
|
|
160
130
|
component: lastComponent,
|
|
161
131
|
_component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
|
|
162
132
|
};
|
|
163
133
|
};
|
|
164
|
-
|
|
165
134
|
const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
|
|
166
135
|
parent: undefined
|
|
167
136
|
}) : _objectSpread(_objectSpread({}, route), {}, {
|
|
@@ -169,7 +138,6 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
|
|
|
169
138
|
}));
|
|
170
139
|
return normalized;
|
|
171
140
|
};
|
|
172
|
-
|
|
173
141
|
const getClientRoutes = ({
|
|
174
142
|
entrypoint,
|
|
175
143
|
srcDirectory,
|
|
@@ -181,15 +149,12 @@ const getClientRoutes = ({
|
|
|
181
149
|
entry,
|
|
182
150
|
entryName
|
|
183
151
|
} = entrypoint;
|
|
184
|
-
|
|
185
152
|
if (!_utils.fs.existsSync(entry)) {
|
|
186
153
|
throw new Error(`generate file system routes error, ${entry} directory not found.`);
|
|
187
154
|
}
|
|
188
|
-
|
|
189
155
|
if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
|
|
190
156
|
throw new Error(`generate file system routes error, ${entry} should be directory.`);
|
|
191
157
|
}
|
|
192
|
-
|
|
193
158
|
let routes = [];
|
|
194
159
|
recursiveReadDirLegacy({
|
|
195
160
|
dir: entry,
|
|
@@ -198,25 +163,20 @@ const getClientRoutes = ({
|
|
|
198
163
|
srcDirectory,
|
|
199
164
|
srcAlias
|
|
200
165
|
});
|
|
201
|
-
|
|
202
166
|
const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
|
|
203
|
-
|
|
204
167
|
_utils.fs.emptyDirSync(internalComponentsDir);
|
|
205
|
-
|
|
206
168
|
routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
|
|
207
|
-
parents.length = 0;
|
|
169
|
+
parents.length = 0;
|
|
208
170
|
|
|
171
|
+
// FIXME: support more situations
|
|
209
172
|
routes.sort((a, b) => {
|
|
210
173
|
const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
|
|
211
|
-
|
|
212
174
|
if (delta === 0) {
|
|
213
175
|
return a.path.length - b.path.length;
|
|
214
176
|
}
|
|
215
|
-
|
|
216
177
|
return delta;
|
|
217
178
|
});
|
|
218
179
|
(0, _utils3.debug)(`fileSystem routes: %o`, routes);
|
|
219
180
|
return routes;
|
|
220
181
|
};
|
|
221
|
-
|
|
222
182
|
exports.getClientRoutes = getClientRoutes;
|
|
@@ -4,39 +4,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.shouldSkip = exports.getRouteWeight = exports.findLayout = exports.debug = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _constants = require("../constants");
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
11
|
const debug = (0, _utils.createDebugger)('get-client-routes');
|
|
17
12
|
exports.debug = debug;
|
|
18
|
-
|
|
19
13
|
const findLayout = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
|
|
20
|
-
|
|
21
14
|
exports.findLayout = findLayout;
|
|
22
|
-
|
|
23
15
|
const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
|
|
24
|
-
|
|
25
16
|
exports.getRouteWeight = getRouteWeight;
|
|
26
|
-
|
|
27
17
|
const shouldSkip = file => {
|
|
28
18
|
// should not skip directory.
|
|
29
19
|
if (_utils.fs.statSync(file).isDirectory()) {
|
|
30
20
|
return false;
|
|
31
21
|
}
|
|
32
|
-
|
|
33
22
|
const ext = _path.default.extname(file);
|
|
34
|
-
|
|
35
23
|
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
24
|
return true;
|
|
37
25
|
}
|
|
38
|
-
|
|
39
26
|
return false;
|
|
40
27
|
};
|
|
41
|
-
|
|
42
28
|
exports.shouldSkip = shouldSkip;
|
|
@@ -4,57 +4,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getFileSystemEntry = void 0;
|
|
7
|
-
|
|
8
7
|
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
8
|
var _path = _interopRequireDefault(require("path"));
|
|
11
|
-
|
|
12
9
|
var _utils = require("@modern-js/utils");
|
|
13
|
-
|
|
14
10
|
var _isDefaultExportFunction = require("./isDefaultExportFunction");
|
|
15
|
-
|
|
16
11
|
var _constants = require("./constants");
|
|
17
|
-
|
|
18
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
13
|
const hasIndex = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.INDEX_FILE_NAME}${ext}`)));
|
|
21
|
-
|
|
22
14
|
const hasApp = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.APP_FILE_NAME}${ext}`)));
|
|
23
|
-
|
|
24
15
|
const hasPages = dir => _fs.default.existsSync(_path.default.join(dir, _constants.PAGES_DIR_NAME));
|
|
25
|
-
|
|
26
16
|
const hasNestedRoutes = dir => _fs.default.existsSync(_path.default.join(dir, _constants.NESTED_ROUTES_DIR));
|
|
27
|
-
|
|
28
17
|
const isBundleEntry = dir => hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
|
|
29
|
-
|
|
30
18
|
const scanDir = dirs => dirs.map(dir => {
|
|
31
19
|
const indexFile = hasIndex(dir);
|
|
32
20
|
const customBootstrap = (0, _isDefaultExportFunction.isDefaultExportFunction)(indexFile) ? indexFile : false;
|
|
33
|
-
|
|
34
21
|
const entryName = _path.default.basename(dir);
|
|
35
|
-
|
|
36
22
|
if (indexFile && !customBootstrap) {
|
|
37
23
|
return {
|
|
38
24
|
entryName,
|
|
39
25
|
entry: indexFile,
|
|
26
|
+
absoluteEntryDir: _path.default.resolve(dir),
|
|
40
27
|
isAutoMount: false
|
|
41
28
|
};
|
|
42
29
|
}
|
|
43
|
-
|
|
44
30
|
const isHasApp = hasApp(dir);
|
|
45
|
-
|
|
46
31
|
if (isHasApp) {
|
|
47
32
|
return {
|
|
48
33
|
entryName,
|
|
49
34
|
entry: _path.default.join(dir, _constants.APP_FILE_NAME),
|
|
50
35
|
isAutoMount: true,
|
|
36
|
+
absoluteEntryDir: _path.default.resolve(dir),
|
|
51
37
|
customBootstrap
|
|
52
38
|
};
|
|
53
39
|
}
|
|
54
|
-
|
|
55
40
|
const isHasNestedRoutes = hasNestedRoutes(dir);
|
|
56
41
|
const isHasPages = hasPages(dir);
|
|
57
|
-
|
|
58
42
|
if (isHasNestedRoutes || isHasPages) {
|
|
59
43
|
const entrypoint = {
|
|
60
44
|
entryName,
|
|
@@ -63,27 +47,24 @@ const scanDir = dirs => dirs.map(dir => {
|
|
|
63
47
|
globalApp: (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `./${_constants.PAGES_DIR_NAME}/${_constants.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`)))
|
|
64
48
|
},
|
|
65
49
|
isAutoMount: true,
|
|
50
|
+
absoluteEntryDir: _path.default.resolve(dir),
|
|
66
51
|
customBootstrap
|
|
67
52
|
};
|
|
68
|
-
|
|
69
53
|
if (isHasPages) {
|
|
70
54
|
entrypoint.entry = _path.default.join(dir, _constants.PAGES_DIR_NAME);
|
|
71
55
|
}
|
|
72
|
-
|
|
73
56
|
if (isHasNestedRoutes) {
|
|
74
57
|
entrypoint.nestedRoutesEntry = _path.default.join(dir, _constants.NESTED_ROUTES_DIR);
|
|
75
58
|
}
|
|
76
|
-
|
|
77
59
|
return entrypoint;
|
|
78
60
|
}
|
|
79
|
-
|
|
80
61
|
return {
|
|
81
62
|
entryName,
|
|
82
63
|
entry: indexFile,
|
|
64
|
+
absoluteEntryDir: _path.default.resolve(dir),
|
|
83
65
|
isAutoMount: false
|
|
84
66
|
};
|
|
85
67
|
});
|
|
86
|
-
|
|
87
68
|
const getFileSystemEntry = (appContext, config) => {
|
|
88
69
|
const {
|
|
89
70
|
appDirectory
|
|
@@ -94,7 +75,6 @@ const getFileSystemEntry = (appContext, config) => {
|
|
|
94
75
|
}
|
|
95
76
|
} = config;
|
|
96
77
|
const src = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
|
|
97
|
-
|
|
98
78
|
if (_fs.default.existsSync(src)) {
|
|
99
79
|
if (_fs.default.statSync(src).isDirectory()) {
|
|
100
80
|
return scanDir(isBundleEntry(src) ? [src] : _fs.default.readdirSync(src).map(file => _path.default.join(src, file)).filter(file => _fs.default.statSync(file).isDirectory() && isBundleEntry(file)));
|
|
@@ -105,5 +85,4 @@ const getFileSystemEntry = (appContext, config) => {
|
|
|
105
85
|
throw Error(`src dir ${entriesDir} not found.`);
|
|
106
86
|
}
|
|
107
87
|
};
|
|
108
|
-
|
|
109
88
|
exports.getFileSystemEntry = getFileSystemEntry;
|
|
@@ -4,23 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getHtmlTemplate = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _constants = require("./constants");
|
|
13
|
-
|
|
14
10
|
var templates = _interopRequireWildcard(require("./templates"));
|
|
15
|
-
|
|
16
11
|
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
12
|
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
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
14
|
var PartialPosition;
|
|
23
|
-
|
|
24
15
|
(function (PartialPosition) {
|
|
25
16
|
PartialPosition["TOP"] = "top";
|
|
26
17
|
PartialPosition["HEAD"] = "head";
|
|
@@ -28,7 +19,6 @@ var PartialPosition;
|
|
|
28
19
|
PartialPosition["BOTTOM"] = "bottom";
|
|
29
20
|
PartialPosition["INDEX"] = "index";
|
|
30
21
|
})(PartialPosition || (PartialPosition = {}));
|
|
31
|
-
|
|
32
22
|
const findPartials = (dir, entryName, position) => {
|
|
33
23
|
if (_utils.fs.existsSync(dir)) {
|
|
34
24
|
const base = (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${position}${ext}`)));
|
|
@@ -38,11 +28,10 @@ const findPartials = (dir, entryName, position) => {
|
|
|
38
28
|
content: _utils.fs.readFileSync(file, 'utf8')
|
|
39
29
|
} : null;
|
|
40
30
|
}
|
|
41
|
-
|
|
42
31
|
return null;
|
|
43
|
-
};
|
|
44
|
-
|
|
32
|
+
};
|
|
45
33
|
|
|
34
|
+
// generate html template for
|
|
46
35
|
const getHtmlTemplate = async (entrypoints, api, {
|
|
47
36
|
appContext,
|
|
48
37
|
config
|
|
@@ -56,18 +45,14 @@ const getHtmlTemplate = async (entrypoints, api, {
|
|
|
56
45
|
configDir
|
|
57
46
|
}
|
|
58
47
|
} = config;
|
|
59
|
-
|
|
60
48
|
const htmlDir = _path.default.resolve(appDirectory, configDir, _constants.HTML_PARTIALS_FOLDER);
|
|
61
|
-
|
|
62
49
|
const htmlTemplates = {};
|
|
63
|
-
|
|
64
50
|
for (const entrypoint of entrypoints) {
|
|
65
51
|
const {
|
|
66
52
|
entryName
|
|
67
53
|
} = entrypoint;
|
|
68
54
|
const name = entrypoints.length === 1 && entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName;
|
|
69
55
|
const customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
|
|
70
|
-
|
|
71
56
|
if (customIndexTemplate) {
|
|
72
57
|
htmlTemplates[entryName] = customIndexTemplate.file;
|
|
73
58
|
} else {
|
|
@@ -86,21 +71,15 @@ const getHtmlTemplate = async (entrypoints, api, {
|
|
|
86
71
|
body: []
|
|
87
72
|
})
|
|
88
73
|
});
|
|
89
|
-
|
|
90
74
|
const templatePath = _path.default.resolve(internalDirectory, entryName, 'index.html');
|
|
91
|
-
|
|
92
75
|
_utils.fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
|
|
93
|
-
|
|
94
76
|
htmlTemplates[entryName] = templatePath;
|
|
95
77
|
const bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
|
|
96
|
-
|
|
97
78
|
if (bottomTemplate) {
|
|
98
79
|
htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
|
|
99
80
|
}
|
|
100
81
|
}
|
|
101
82
|
}
|
|
102
|
-
|
|
103
83
|
return htmlTemplates;
|
|
104
84
|
};
|
|
105
|
-
|
|
106
85
|
exports.getHtmlTemplate = getHtmlTemplate;
|