@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
|
@@ -6,33 +6,29 @@ import { makeLegalIdentifier } from "../makeLegalIdentifier";
|
|
|
6
6
|
import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
|
|
7
7
|
import { replaceWithAlias } from "../utils";
|
|
8
8
|
import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
|
|
9
|
-
|
|
10
9
|
var compName = function compName(srcDirectory, filePath) {
|
|
11
10
|
var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
|
|
12
11
|
return "Comp_".concat(legalCompName);
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
var layoutNameAbbr = function layoutNameAbbr(filePath) {
|
|
16
14
|
var prefix = 'L_';
|
|
17
15
|
var dirName = path.dirname(filePath).split('/').pop() || '';
|
|
18
16
|
return "".concat(prefix).concat(makeLegalIdentifier(dirName));
|
|
19
17
|
};
|
|
20
|
-
|
|
21
18
|
var parents = [];
|
|
22
|
-
/* eslint-disable no-param-reassign */
|
|
23
19
|
|
|
20
|
+
/* eslint-disable no-param-reassign */
|
|
24
21
|
var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
25
22
|
var dir = _ref.dir,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
routes = _ref.routes,
|
|
24
|
+
_ref$basePath = _ref.basePath,
|
|
25
|
+
basePath = _ref$basePath === void 0 ? '/' : _ref$basePath,
|
|
26
|
+
srcDirectory = _ref.srcDirectory,
|
|
27
|
+
srcAlias = _ref.srcAlias;
|
|
31
28
|
var hasDynamicRoute = false;
|
|
32
29
|
var resetParent = false;
|
|
33
30
|
var parent = parents[parents.length - 1];
|
|
34
31
|
var layout = findLayout(dir);
|
|
35
|
-
|
|
36
32
|
if (layout) {
|
|
37
33
|
if (basePath === '/') {
|
|
38
34
|
throw new Error("should use _app instead of _layout in ".concat(dir));
|
|
@@ -54,24 +50,17 @@ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
|
54
50
|
routes = route.routes;
|
|
55
51
|
}
|
|
56
52
|
}
|
|
57
|
-
|
|
58
53
|
var _iterator = _createForOfIteratorHelper(fs.readdirSync(dir)),
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
_step;
|
|
61
55
|
try {
|
|
62
56
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
63
57
|
var relative = _step.value;
|
|
64
58
|
var filePath = path.join(dir, relative);
|
|
65
|
-
|
|
66
59
|
if (!shouldSkip(filePath)) {
|
|
67
60
|
var filename = path.basename(filePath, path.extname(filePath));
|
|
68
|
-
|
|
69
61
|
var _alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
|
|
70
|
-
|
|
71
62
|
var _componentName = compName(srcDirectory, filePath);
|
|
72
|
-
|
|
73
63
|
var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
74
|
-
|
|
75
64
|
if (dynamicRouteMatched) {
|
|
76
65
|
if (hasDynamicRoute) {
|
|
77
66
|
throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
|
|
@@ -79,7 +68,6 @@ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
|
79
68
|
hasDynamicRoute = true;
|
|
80
69
|
}
|
|
81
70
|
}
|
|
82
|
-
|
|
83
71
|
var _route = {
|
|
84
72
|
path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1]).concat(dynamicRouteMatched[2]) : filename),
|
|
85
73
|
_component: _alias,
|
|
@@ -87,7 +75,6 @@ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
|
87
75
|
exact: true,
|
|
88
76
|
parent: parent
|
|
89
77
|
};
|
|
90
|
-
|
|
91
78
|
if (fs.statSync(filePath).isDirectory()) {
|
|
92
79
|
recursiveReadDirLegacy({
|
|
93
80
|
dir: filePath,
|
|
@@ -98,20 +85,16 @@ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
|
98
85
|
});
|
|
99
86
|
continue;
|
|
100
87
|
}
|
|
101
|
-
|
|
102
88
|
if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
103
89
|
continue;
|
|
104
90
|
}
|
|
105
|
-
|
|
106
91
|
if (filename === FILE_SYSTEM_ROUTES_INDEX) {
|
|
107
92
|
_route.path = basePath === '/' ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
|
|
108
93
|
}
|
|
109
|
-
|
|
110
94
|
if (filename === '404' && basePath === '/') {
|
|
111
95
|
_route.path = '*';
|
|
112
96
|
_route.exact = false;
|
|
113
97
|
}
|
|
114
|
-
|
|
115
98
|
routes.push(_route);
|
|
116
99
|
}
|
|
117
100
|
}
|
|
@@ -120,26 +103,24 @@ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
|
|
|
120
103
|
} finally {
|
|
121
104
|
_iterator.f();
|
|
122
105
|
}
|
|
123
|
-
|
|
124
106
|
if (resetParent) {
|
|
125
107
|
parents.pop();
|
|
126
108
|
}
|
|
127
109
|
};
|
|
128
110
|
/* eslint-enable no-param-reassign */
|
|
129
111
|
|
|
130
|
-
|
|
131
112
|
var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
|
|
132
113
|
var flat = function flat(routes) {
|
|
133
114
|
return routes.reduce(function (memo, route) {
|
|
134
115
|
return memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]);
|
|
135
116
|
}, []);
|
|
136
117
|
};
|
|
137
|
-
|
|
138
118
|
var generate = function generate(route) {
|
|
139
119
|
var codes = [];
|
|
140
120
|
var lastComponent = route.component;
|
|
141
|
-
var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")];
|
|
121
|
+
var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")];
|
|
142
122
|
|
|
123
|
+
// eslint-disable-next-line no-param-reassign, no-cond-assign
|
|
143
124
|
while (route = route.parent) {
|
|
144
125
|
var layoutComponent = route.component;
|
|
145
126
|
var layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
@@ -148,7 +129,6 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
|
|
|
148
129
|
codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
|
|
149
130
|
lastComponent = currentComponent;
|
|
150
131
|
}
|
|
151
|
-
|
|
152
132
|
var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
|
|
153
133
|
fs.outputFileSync(file, "".concat(imports.join('\n'), "\n").concat(codes.join('\n'), "\nexport default ").concat(lastComponent));
|
|
154
134
|
return {
|
|
@@ -156,7 +136,6 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
|
|
|
156
136
|
_component: replaceWithAlias(internalDirectory, file, internalDirAlias)
|
|
157
137
|
};
|
|
158
138
|
};
|
|
159
|
-
|
|
160
139
|
var normalized = flat(nested).map(function (route) {
|
|
161
140
|
return route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
|
|
162
141
|
parent: undefined
|
|
@@ -166,24 +145,20 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
|
|
|
166
145
|
});
|
|
167
146
|
return normalized;
|
|
168
147
|
};
|
|
169
|
-
|
|
170
148
|
export var getClientRoutes = function getClientRoutes(_ref2) {
|
|
171
149
|
var entrypoint = _ref2.entrypoint,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
150
|
+
srcDirectory = _ref2.srcDirectory,
|
|
151
|
+
srcAlias = _ref2.srcAlias,
|
|
152
|
+
internalDirectory = _ref2.internalDirectory,
|
|
153
|
+
internalDirAlias = _ref2.internalDirAlias;
|
|
176
154
|
var entry = entrypoint.entry,
|
|
177
|
-
|
|
178
|
-
|
|
155
|
+
entryName = entrypoint.entryName;
|
|
179
156
|
if (!fs.existsSync(entry)) {
|
|
180
157
|
throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
|
|
181
158
|
}
|
|
182
|
-
|
|
183
159
|
if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
|
|
184
160
|
throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
|
|
185
161
|
}
|
|
186
|
-
|
|
187
162
|
var routes = [];
|
|
188
163
|
recursiveReadDirLegacy({
|
|
189
164
|
dir: entry,
|
|
@@ -195,15 +170,14 @@ export var getClientRoutes = function getClientRoutes(_ref2) {
|
|
|
195
170
|
var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
|
|
196
171
|
fs.emptyDirSync(internalComponentsDir);
|
|
197
172
|
routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
|
|
198
|
-
parents.length = 0;
|
|
173
|
+
parents.length = 0;
|
|
199
174
|
|
|
175
|
+
// FIXME: support more situations
|
|
200
176
|
routes.sort(function (a, b) {
|
|
201
177
|
var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
|
|
202
|
-
|
|
203
178
|
if (delta === 0) {
|
|
204
179
|
return a.path.length - b.path.length;
|
|
205
180
|
}
|
|
206
|
-
|
|
207
181
|
return delta;
|
|
208
182
|
});
|
|
209
183
|
debug("fileSystem routes: %o", routes);
|
|
@@ -16,12 +16,9 @@ export var shouldSkip = function shouldSkip(file) {
|
|
|
16
16
|
if (fs.statSync(file).isDirectory()) {
|
|
17
17
|
return false;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
19
|
var ext = path.extname(file);
|
|
21
|
-
|
|
22
20
|
if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
|
|
23
21
|
return true;
|
|
24
22
|
}
|
|
25
|
-
|
|
26
23
|
return false;
|
|
27
24
|
};
|
|
@@ -3,59 +3,50 @@ import path from 'path';
|
|
|
3
3
|
import { findExists, ensureAbsolutePath } from '@modern-js/utils';
|
|
4
4
|
import { isDefaultExportFunction } from "./isDefaultExportFunction";
|
|
5
5
|
import { JS_EXTENSIONS, INDEX_FILE_NAME, APP_FILE_NAME, PAGES_DIR_NAME, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, NESTED_ROUTES_DIR } from "./constants";
|
|
6
|
-
|
|
7
6
|
var hasIndex = function hasIndex(dir) {
|
|
8
7
|
return findExists(JS_EXTENSIONS.map(function (ext) {
|
|
9
8
|
return path.resolve(dir, "".concat(INDEX_FILE_NAME).concat(ext));
|
|
10
9
|
}));
|
|
11
10
|
};
|
|
12
|
-
|
|
13
11
|
var hasApp = function hasApp(dir) {
|
|
14
12
|
return findExists(JS_EXTENSIONS.map(function (ext) {
|
|
15
13
|
return path.resolve(dir, "".concat(APP_FILE_NAME).concat(ext));
|
|
16
14
|
}));
|
|
17
15
|
};
|
|
18
|
-
|
|
19
16
|
var hasPages = function hasPages(dir) {
|
|
20
17
|
return fs.existsSync(path.join(dir, PAGES_DIR_NAME));
|
|
21
18
|
};
|
|
22
|
-
|
|
23
19
|
var hasNestedRoutes = function hasNestedRoutes(dir) {
|
|
24
20
|
return fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
|
|
25
21
|
};
|
|
26
|
-
|
|
27
22
|
var isBundleEntry = function isBundleEntry(dir) {
|
|
28
23
|
return hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
|
|
29
24
|
};
|
|
30
|
-
|
|
31
25
|
var scanDir = function scanDir(dirs) {
|
|
32
26
|
return dirs.map(function (dir) {
|
|
33
27
|
var indexFile = hasIndex(dir);
|
|
34
28
|
var customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
|
|
35
29
|
var entryName = path.basename(dir);
|
|
36
|
-
|
|
37
30
|
if (indexFile && !customBootstrap) {
|
|
38
31
|
return {
|
|
39
32
|
entryName: entryName,
|
|
40
33
|
entry: indexFile,
|
|
34
|
+
absoluteEntryDir: path.resolve(dir),
|
|
41
35
|
isAutoMount: false
|
|
42
36
|
};
|
|
43
37
|
}
|
|
44
|
-
|
|
45
38
|
var isHasApp = hasApp(dir);
|
|
46
|
-
|
|
47
39
|
if (isHasApp) {
|
|
48
40
|
return {
|
|
49
41
|
entryName: entryName,
|
|
50
42
|
entry: path.join(dir, APP_FILE_NAME),
|
|
51
43
|
isAutoMount: true,
|
|
44
|
+
absoluteEntryDir: path.resolve(dir),
|
|
52
45
|
customBootstrap: customBootstrap
|
|
53
46
|
};
|
|
54
47
|
}
|
|
55
|
-
|
|
56
48
|
var isHasNestedRoutes = hasNestedRoutes(dir);
|
|
57
49
|
var isHasPages = hasPages(dir);
|
|
58
|
-
|
|
59
50
|
if (isHasNestedRoutes || isHasPages) {
|
|
60
51
|
var entrypoint = {
|
|
61
52
|
entryName: entryName,
|
|
@@ -66,33 +57,29 @@ var scanDir = function scanDir(dirs) {
|
|
|
66
57
|
}))
|
|
67
58
|
},
|
|
68
59
|
isAutoMount: true,
|
|
60
|
+
absoluteEntryDir: path.resolve(dir),
|
|
69
61
|
customBootstrap: customBootstrap
|
|
70
62
|
};
|
|
71
|
-
|
|
72
63
|
if (isHasPages) {
|
|
73
64
|
entrypoint.entry = path.join(dir, PAGES_DIR_NAME);
|
|
74
65
|
}
|
|
75
|
-
|
|
76
66
|
if (isHasNestedRoutes) {
|
|
77
67
|
entrypoint.nestedRoutesEntry = path.join(dir, NESTED_ROUTES_DIR);
|
|
78
68
|
}
|
|
79
|
-
|
|
80
69
|
return entrypoint;
|
|
81
70
|
}
|
|
82
|
-
|
|
83
71
|
return {
|
|
84
72
|
entryName: entryName,
|
|
85
73
|
entry: indexFile,
|
|
74
|
+
absoluteEntryDir: path.resolve(dir),
|
|
86
75
|
isAutoMount: false
|
|
87
76
|
};
|
|
88
77
|
});
|
|
89
78
|
};
|
|
90
|
-
|
|
91
79
|
export var getFileSystemEntry = function getFileSystemEntry(appContext, config) {
|
|
92
80
|
var appDirectory = appContext.appDirectory;
|
|
93
81
|
var entriesDir = config.source.entriesDir;
|
|
94
82
|
var src = ensureAbsolutePath(appDirectory, entriesDir);
|
|
95
|
-
|
|
96
83
|
if (fs.existsSync(src)) {
|
|
97
84
|
if (fs.statSync(src).isDirectory()) {
|
|
98
85
|
return scanDir(isBundleEntry(src) ? [src] : fs.readdirSync(src).map(function (file) {
|
|
@@ -6,7 +6,6 @@ import { fs, findExists, MAIN_ENTRY_NAME } from '@modern-js/utils';
|
|
|
6
6
|
import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from "./constants";
|
|
7
7
|
import * as templates from "./templates";
|
|
8
8
|
var PartialPosition;
|
|
9
|
-
|
|
10
9
|
(function (PartialPosition) {
|
|
11
10
|
PartialPosition["TOP"] = "top";
|
|
12
11
|
PartialPosition["HEAD"] = "head";
|
|
@@ -14,7 +13,6 @@ var PartialPosition;
|
|
|
14
13
|
PartialPosition["BOTTOM"] = "bottom";
|
|
15
14
|
PartialPosition["INDEX"] = "index";
|
|
16
15
|
})(PartialPosition || (PartialPosition = {}));
|
|
17
|
-
|
|
18
16
|
var findPartials = function findPartials(dir, entryName, position) {
|
|
19
17
|
if (fs.existsSync(dir)) {
|
|
20
18
|
var base = findExists(HTML_PARTIALS_EXTENSIONS.map(function (ext) {
|
|
@@ -28,15 +26,13 @@ var findPartials = function findPartials(dir, entryName, position) {
|
|
|
28
26
|
content: fs.readFileSync(file, 'utf8')
|
|
29
27
|
} : null;
|
|
30
28
|
}
|
|
31
|
-
|
|
32
29
|
return null;
|
|
33
|
-
};
|
|
34
|
-
|
|
30
|
+
};
|
|
35
31
|
|
|
32
|
+
// generate html template for
|
|
36
33
|
export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
37
34
|
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(entrypoints, api, _ref) {
|
|
38
35
|
var appContext, config, appDirectory, internalDirectory, configDir, htmlDir, htmlTemplates, _iterator, _step, _loop;
|
|
39
|
-
|
|
40
36
|
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
41
37
|
while (1) {
|
|
42
38
|
switch (_context2.prev = _context2.next) {
|
|
@@ -50,7 +46,6 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
50
46
|
_context2.prev = 6;
|
|
51
47
|
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
52
48
|
var entrypoint, entryName, name, customIndexTemplate, hookRunners, _yield$hookRunners$ht, partials, templatePath, bottomTemplate;
|
|
53
|
-
|
|
54
49
|
return _regeneratorRuntime().wrap(function _loop$(_context) {
|
|
55
50
|
while (1) {
|
|
56
51
|
switch (_context.prev = _context.next) {
|
|
@@ -59,16 +54,13 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
59
54
|
entryName = entrypoint.entryName;
|
|
60
55
|
name = entrypoints.length === 1 && entryName === MAIN_ENTRY_NAME ? '' : entryName;
|
|
61
56
|
customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
|
|
62
|
-
|
|
63
57
|
if (!customIndexTemplate) {
|
|
64
58
|
_context.next = 8;
|
|
65
59
|
break;
|
|
66
60
|
}
|
|
67
|
-
|
|
68
61
|
htmlTemplates[entryName] = customIndexTemplate.file;
|
|
69
62
|
_context.next = 18;
|
|
70
63
|
break;
|
|
71
|
-
|
|
72
64
|
case 8:
|
|
73
65
|
hookRunners = api.useHookRunners();
|
|
74
66
|
_context.next = 11;
|
|
@@ -84,7 +76,6 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
84
76
|
body: []
|
|
85
77
|
})
|
|
86
78
|
});
|
|
87
|
-
|
|
88
79
|
case 11:
|
|
89
80
|
_yield$hookRunners$ht = _context.sent;
|
|
90
81
|
partials = _yield$hookRunners$ht.partials;
|
|
@@ -92,11 +83,9 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
92
83
|
fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
|
|
93
84
|
htmlTemplates[entryName] = templatePath;
|
|
94
85
|
bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
|
|
95
|
-
|
|
96
86
|
if (bottomTemplate) {
|
|
97
87
|
htmlTemplates["__".concat(entryName, "-bottom__")] = bottomTemplate.content;
|
|
98
88
|
}
|
|
99
|
-
|
|
100
89
|
case 18:
|
|
101
90
|
case "end":
|
|
102
91
|
return _context.stop();
|
|
@@ -104,41 +93,29 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
104
93
|
}
|
|
105
94
|
}, _loop);
|
|
106
95
|
});
|
|
107
|
-
|
|
108
96
|
_iterator.s();
|
|
109
|
-
|
|
110
97
|
case 9:
|
|
111
98
|
if ((_step = _iterator.n()).done) {
|
|
112
99
|
_context2.next = 13;
|
|
113
100
|
break;
|
|
114
101
|
}
|
|
115
|
-
|
|
116
102
|
return _context2.delegateYield(_loop(), "t0", 11);
|
|
117
|
-
|
|
118
103
|
case 11:
|
|
119
104
|
_context2.next = 9;
|
|
120
105
|
break;
|
|
121
|
-
|
|
122
106
|
case 13:
|
|
123
107
|
_context2.next = 18;
|
|
124
108
|
break;
|
|
125
|
-
|
|
126
109
|
case 15:
|
|
127
110
|
_context2.prev = 15;
|
|
128
111
|
_context2.t1 = _context2["catch"](6);
|
|
129
|
-
|
|
130
112
|
_iterator.e(_context2.t1);
|
|
131
|
-
|
|
132
113
|
case 18:
|
|
133
114
|
_context2.prev = 18;
|
|
134
|
-
|
|
135
115
|
_iterator.f();
|
|
136
|
-
|
|
137
116
|
return _context2.finish(18);
|
|
138
|
-
|
|
139
117
|
case 21:
|
|
140
118
|
return _context2.abrupt("return", htmlTemplates);
|
|
141
|
-
|
|
142
119
|
case 22:
|
|
143
120
|
case "end":
|
|
144
121
|
return _context2.stop();
|
|
@@ -146,7 +123,6 @@ export var getHtmlTemplate = /*#__PURE__*/function () {
|
|
|
146
123
|
}
|
|
147
124
|
}, _callee, null, [[6, 15, 18, 21]]);
|
|
148
125
|
}));
|
|
149
|
-
|
|
150
126
|
return function getHtmlTemplate(_x, _x2, _x3) {
|
|
151
127
|
return _ref2.apply(this, arguments);
|
|
152
128
|
};
|
|
@@ -2,18 +2,18 @@ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutPr
|
|
|
2
2
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
3
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
4
4
|
var _excluded = ["path"],
|
|
5
|
-
|
|
5
|
+
_excluded2 = ["path"];
|
|
6
6
|
import path from 'path';
|
|
7
7
|
import fs from 'fs';
|
|
8
8
|
import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY, MAIN_ENTRY_NAME, removeTailSlash } from '@modern-js/utils';
|
|
9
9
|
import { walkDirectory } from "./utils";
|
|
10
|
+
|
|
10
11
|
/**
|
|
11
12
|
* Add base url for each server route.
|
|
12
13
|
* @param baseUrl - Base url from server.baseUrl
|
|
13
14
|
* @param routes - Server routes.
|
|
14
15
|
* @returns Server routes with baseUrl prefixed.
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
17
|
var applyBaseUrl = function applyBaseUrl(baseUrl, routes) {
|
|
18
18
|
if (baseUrl) {
|
|
19
19
|
if (Array.isArray(baseUrl)) {
|
|
@@ -29,33 +29,30 @@ var applyBaseUrl = function applyBaseUrl(baseUrl, routes) {
|
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
|
|
33
32
|
return routes;
|
|
34
33
|
};
|
|
34
|
+
|
|
35
35
|
/**
|
|
36
36
|
*
|
|
37
37
|
* @param original - Original entrypoint route info.
|
|
38
38
|
* @param routeOptions - Custom entrypoint route config from server.routes.
|
|
39
39
|
* @returns
|
|
40
40
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
41
|
var applyRouteOptions = function applyRouteOptions(original, routeOptions) {
|
|
44
42
|
var route = routeOptions.route,
|
|
45
|
-
|
|
46
|
-
original.isSPA = !disableSpa;
|
|
43
|
+
disableSpa = routeOptions.disableSpa;
|
|
44
|
+
original.isSPA = !disableSpa;
|
|
47
45
|
|
|
46
|
+
// set entryPath as dir
|
|
48
47
|
!original.isSPA && (original.entryPath = path.dirname(original.entryPath));
|
|
49
48
|
var routes;
|
|
50
|
-
|
|
51
49
|
if (route) {
|
|
52
50
|
if (Array.isArray(route)) {
|
|
53
51
|
routes = route.map(function (url) {
|
|
54
52
|
if (isPlainObject(url)) {
|
|
55
53
|
var _ref = url,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
urlPath = _ref.path,
|
|
55
|
+
other = _objectWithoutProperties(_ref, _excluded);
|
|
59
56
|
return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
|
60
57
|
urlPath: urlPath
|
|
61
58
|
});
|
|
@@ -67,9 +64,8 @@ var applyRouteOptions = function applyRouteOptions(original, routeOptions) {
|
|
|
67
64
|
});
|
|
68
65
|
} else if (isPlainObject(route)) {
|
|
69
66
|
var _ref2 = route,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
urlPath = _ref2.path,
|
|
68
|
+
other = _objectWithoutProperties(_ref2, _excluded2);
|
|
73
69
|
routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
|
74
70
|
urlPath: urlPath
|
|
75
71
|
})];
|
|
@@ -81,36 +77,32 @@ var applyRouteOptions = function applyRouteOptions(original, routeOptions) {
|
|
|
81
77
|
} else {
|
|
82
78
|
routes = [original];
|
|
83
79
|
}
|
|
84
|
-
|
|
85
80
|
return routes;
|
|
86
81
|
};
|
|
82
|
+
|
|
87
83
|
/**
|
|
88
84
|
* Collect routes from entrypoints.
|
|
89
85
|
* @param entrypoints - Bundle entrypoints.
|
|
90
86
|
* @param config - Normalized user config.
|
|
91
87
|
* @returns entrypoint Routes
|
|
92
88
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
89
|
var collectHtmlRoutes = function collectHtmlRoutes(entrypoints, appContext, config) {
|
|
96
90
|
var _config$output = config.output,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
91
|
+
htmlPath = _config$output.htmlPath,
|
|
92
|
+
disableHtmlFolder = _config$output.disableHtmlFolder,
|
|
93
|
+
enableModernMode = _config$output.enableModernMode,
|
|
94
|
+
_config$server = config.server,
|
|
95
|
+
baseUrl = _config$server.baseUrl,
|
|
96
|
+
routes = _config$server.routes,
|
|
97
|
+
ssr = _config$server.ssr,
|
|
98
|
+
ssrByEntries = _config$server.ssrByEntries;
|
|
105
99
|
var packageName = appContext.packageName;
|
|
106
100
|
var htmlRoutes = entrypoints.reduce(function (previous, _ref3) {
|
|
107
101
|
var entryName = _ref3.entryName;
|
|
108
102
|
var entryOptions = getEntryOptions(entryName, ssr, ssrByEntries, packageName);
|
|
109
103
|
var isSSR = Boolean(entryOptions);
|
|
110
|
-
|
|
111
104
|
var _ref4 = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {},
|
|
112
|
-
|
|
113
|
-
|
|
105
|
+
resHeaders = _ref4.resHeaders;
|
|
114
106
|
var route = {
|
|
115
107
|
urlPath: "/".concat(entryName === MAIN_ENTRY_NAME ? '' : entryName),
|
|
116
108
|
entryName: entryName,
|
|
@@ -121,32 +113,29 @@ var collectHtmlRoutes = function collectHtmlRoutes(entrypoints, appContext, conf
|
|
|
121
113
|
enableModernMode: Boolean(enableModernMode),
|
|
122
114
|
bundle: isSSR ? "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(entryName, ".js") : undefined
|
|
123
115
|
};
|
|
124
|
-
|
|
125
116
|
if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
|
|
126
117
|
var routeOptions = isPlainObject(routes[entryName]) ? routes[entryName] : {
|
|
127
118
|
route: routes[entryName]
|
|
128
119
|
};
|
|
129
120
|
route = applyRouteOptions(route, routeOptions);
|
|
130
121
|
}
|
|
131
|
-
|
|
132
122
|
return Array.isArray(route) ? [].concat(_toConsumableArray(previous), _toConsumableArray(route)) : [].concat(_toConsumableArray(previous), [route]);
|
|
133
123
|
}, []);
|
|
134
124
|
htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
|
|
135
125
|
return htmlRoutes;
|
|
136
126
|
};
|
|
127
|
+
|
|
137
128
|
/**
|
|
138
129
|
* Collect static public file routes from config/public folder.
|
|
139
130
|
* @param appContext - App context info.
|
|
140
131
|
* @param config - normalized user config.
|
|
141
132
|
* @returns Static public file routes.
|
|
142
133
|
*/
|
|
143
|
-
|
|
144
|
-
|
|
145
134
|
var collectStaticRoutes = function collectStaticRoutes(appContext, config) {
|
|
146
135
|
var appDirectory = appContext.appDirectory;
|
|
147
136
|
var configDir = config.source.configDir,
|
|
148
|
-
|
|
149
|
-
|
|
137
|
+
_config$server$public = config.server.publicRoutes,
|
|
138
|
+
publicRoutes = _config$server$public === void 0 ? {} : _config$server$public;
|
|
150
139
|
var publicFolder = path.resolve(appDirectory, configDir, 'public');
|
|
151
140
|
return fs.existsSync(publicFolder) ? walkDirectory(publicFolder).map(function (filePath) {
|
|
152
141
|
var urlPath = "".concat(urlJoin(toPosix(filePath).slice(toPosix(publicFolder).length)));
|
|
@@ -158,13 +147,11 @@ var collectStaticRoutes = function collectStaticRoutes(appContext, config) {
|
|
|
158
147
|
};
|
|
159
148
|
}) : [];
|
|
160
149
|
};
|
|
161
|
-
|
|
162
150
|
export var getServerRoutes = function getServerRoutes(entrypoints, _ref5) {
|
|
163
151
|
var appContext = _ref5.appContext,
|
|
164
|
-
|
|
152
|
+
config = _ref5.config;
|
|
165
153
|
return [].concat(_toConsumableArray(collectHtmlRoutes(entrypoints, appContext, config)), _toConsumableArray(collectStaticRoutes(appContext, config)));
|
|
166
154
|
};
|
|
167
|
-
|
|
168
155
|
var toPosix = function toPosix(pathStr) {
|
|
169
156
|
return pathStr.split(path.sep).join(path.posix.sep);
|
|
170
157
|
};
|