@modern-js/app-tools 2.39.2-alpha.0 → 2.39.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/analyze/nestedRoutes.js +1 -2
- package/dist/esm/analyze/nestedRoutes.js +1 -2
- package/dist/esm-node/analyze/nestedRoutes.js +1 -2
- package/package.json +24 -24
- package/dist/js/modern/analyze/constants.js +0 -15
- package/dist/js/modern/analyze/generateCode.js +0 -179
- package/dist/js/modern/analyze/getBundleEntry.js +0 -75
- package/dist/js/modern/analyze/getClientRoutes.js +0 -219
- package/dist/js/modern/analyze/getFileSystemEntry.js +0 -74
- package/dist/js/modern/analyze/getHtmlTemplate.js +0 -82
- package/dist/js/modern/analyze/getServerRoutes.js +0 -192
- package/dist/js/modern/analyze/index.js +0 -148
- package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -32
- package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -16
- package/dist/js/modern/analyze/templates.js +0 -88
- package/dist/js/modern/analyze/utils.js +0 -92
- package/dist/js/modern/commands/build.js +0 -154
- package/dist/js/modern/commands/deploy.js +0 -5
- package/dist/js/modern/commands/dev.js +0 -95
- package/dist/js/modern/commands/index.js +0 -3
- package/dist/js/modern/commands/inspect.js +0 -69
- package/dist/js/modern/commands/start.js +0 -31
- package/dist/js/modern/exports/server.js +0 -1
- package/dist/js/modern/hooks.js +0 -21
- package/dist/js/modern/index.js +0 -109
- package/dist/js/modern/locale/en.js +0 -35
- package/dist/js/modern/locale/index.js +0 -9
- package/dist/js/modern/locale/zh.js +0 -35
- package/dist/js/modern/utils/config.js +0 -78
- package/dist/js/modern/utils/createCompiler.js +0 -61
- package/dist/js/modern/utils/createServer.js +0 -18
- package/dist/js/modern/utils/getSpecifiedEntries.js +0 -36
- package/dist/js/modern/utils/language.js +0 -5
- package/dist/js/modern/utils/printInstructions.js +0 -11
- package/dist/js/modern/utils/routes.js +0 -15
- package/dist/js/modern/utils/types.js +0 -0
- package/dist/js/node/analyze/constants.js +0 -36
- package/dist/js/node/analyze/generateCode.js +0 -208
- package/dist/js/node/analyze/getBundleEntry.js +0 -89
- package/dist/js/node/analyze/getClientRoutes.js +0 -241
- package/dist/js/node/analyze/getFileSystemEntry.js +0 -90
- package/dist/js/node/analyze/getHtmlTemplate.js +0 -106
- package/dist/js/node/analyze/getServerRoutes.js +0 -208
- package/dist/js/node/analyze/index.js +0 -178
- package/dist/js/node/analyze/isDefaultExportFunction.js +0 -50
- package/dist/js/node/analyze/makeLegalIdentifier.js +0 -24
- package/dist/js/node/analyze/templates.js +0 -106
- package/dist/js/node/analyze/utils.js +0 -113
- package/dist/js/node/commands/build.js +0 -174
- package/dist/js/node/commands/deploy.js +0 -14
- package/dist/js/node/commands/dev.js +0 -120
- package/dist/js/node/commands/index.js +0 -44
- package/dist/js/node/commands/inspect.js +0 -98
- package/dist/js/node/commands/start.js +0 -47
- package/dist/js/node/exports/server.js +0 -13
- package/dist/js/node/hooks.js +0 -39
- package/dist/js/node/index.js +0 -141
- package/dist/js/node/locale/en.js +0 -42
- package/dist/js/node/locale/index.js +0 -20
- package/dist/js/node/locale/zh.js +0 -42
- package/dist/js/node/utils/config.js +0 -103
- package/dist/js/node/utils/createCompiler.js +0 -81
- package/dist/js/node/utils/createServer.js +0 -35
- package/dist/js/node/utils/getSpecifiedEntries.js +0 -46
- package/dist/js/node/utils/language.js +0 -13
- package/dist/js/node/utils/printInstructions.js +0 -22
- package/dist/js/node/utils/routes.js +0 -25
- package/dist/js/node/utils/types.js +0 -0
@@ -1,241 +0,0 @@
|
|
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
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
-
|
18
|
-
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; }
|
19
|
-
|
20
|
-
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; }
|
21
|
-
|
22
|
-
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
|
-
|
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
|
-
const compName = (srcDirectory, filePath) => {
|
46
|
-
const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
|
47
|
-
return `Comp_${legalCompName}`;
|
48
|
-
};
|
49
|
-
|
50
|
-
const layoutNameAbbr = filePath => {
|
51
|
-
const prefix = 'L_';
|
52
|
-
const dirName = _path.default.dirname(filePath).split('/').pop() || '';
|
53
|
-
return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
|
54
|
-
};
|
55
|
-
|
56
|
-
const parents = [];
|
57
|
-
/* eslint-disable no-param-reassign */
|
58
|
-
|
59
|
-
const recursiveReadDir = ({
|
60
|
-
dir,
|
61
|
-
routes,
|
62
|
-
basePath: _basePath = '/',
|
63
|
-
srcDirectory,
|
64
|
-
srcAlias
|
65
|
-
}) => {
|
66
|
-
let hasDynamicRoute = false;
|
67
|
-
let resetParent = false;
|
68
|
-
let parent = parents[parents.length - 1];
|
69
|
-
const layout = findLayout(dir);
|
70
|
-
|
71
|
-
if (layout) {
|
72
|
-
if (_basePath === '/') {
|
73
|
-
throw new Error(`should use _app instead of _layout in ${dir}`);
|
74
|
-
} else {
|
75
|
-
const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
|
76
|
-
const componentName = compName(srcDirectory, layout);
|
77
|
-
const route = {
|
78
|
-
path: `${_basePath.substring(0, _basePath.length - 1)}`,
|
79
|
-
exact: false,
|
80
|
-
routes: [],
|
81
|
-
_component: alias,
|
82
|
-
component: componentName,
|
83
|
-
parent
|
84
|
-
};
|
85
|
-
parent = route;
|
86
|
-
resetParent = true;
|
87
|
-
routes.push(route);
|
88
|
-
parents.push(route);
|
89
|
-
routes = route.routes;
|
90
|
-
}
|
91
|
-
}
|
92
|
-
|
93
|
-
for (const relative of _utils.fs.readdirSync(dir)) {
|
94
|
-
const filePath = _path.default.join(dir, relative);
|
95
|
-
|
96
|
-
if (!shouldSkip(filePath)) {
|
97
|
-
const filename = _path.default.basename(filePath, _path.default.extname(filePath));
|
98
|
-
|
99
|
-
const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
|
100
|
-
const componentName = compName(srcDirectory, filePath);
|
101
|
-
|
102
|
-
const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
103
|
-
|
104
|
-
if (dynamicRouteMatched) {
|
105
|
-
if (hasDynamicRoute) {
|
106
|
-
throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
|
107
|
-
} else {
|
108
|
-
hasDynamicRoute = true;
|
109
|
-
}
|
110
|
-
}
|
111
|
-
|
112
|
-
const route = {
|
113
|
-
path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
114
|
-
_component: alias,
|
115
|
-
component: componentName,
|
116
|
-
exact: true,
|
117
|
-
parent
|
118
|
-
};
|
119
|
-
|
120
|
-
if (_utils.fs.statSync(filePath).isDirectory()) {
|
121
|
-
recursiveReadDir({
|
122
|
-
dir: filePath,
|
123
|
-
routes,
|
124
|
-
basePath: `${route.path}/`,
|
125
|
-
srcDirectory,
|
126
|
-
srcAlias
|
127
|
-
});
|
128
|
-
continue;
|
129
|
-
}
|
130
|
-
|
131
|
-
if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
|
132
|
-
continue;
|
133
|
-
}
|
134
|
-
|
135
|
-
if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
|
136
|
-
route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
|
137
|
-
}
|
138
|
-
|
139
|
-
if (filename === '404' && _basePath === '/') {
|
140
|
-
route.path = '*';
|
141
|
-
route.exact = false;
|
142
|
-
}
|
143
|
-
|
144
|
-
routes.push(route);
|
145
|
-
}
|
146
|
-
}
|
147
|
-
|
148
|
-
if (resetParent) {
|
149
|
-
parents.pop();
|
150
|
-
}
|
151
|
-
};
|
152
|
-
/* eslint-enable no-param-reassign */
|
153
|
-
|
154
|
-
|
155
|
-
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
156
|
-
const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]), []);
|
157
|
-
|
158
|
-
const generate = route => {
|
159
|
-
const codes = [];
|
160
|
-
let lastComponent = route.component;
|
161
|
-
const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`]; // eslint-disable-next-line no-param-reassign, no-cond-assign
|
162
|
-
|
163
|
-
while (route = route.parent) {
|
164
|
-
const layoutComponent = route.component;
|
165
|
-
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
166
|
-
imports.push(`import ${layoutComponent} from '${route._component}';`);
|
167
|
-
const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
|
168
|
-
codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
|
169
|
-
lastComponent = currentComponent;
|
170
|
-
}
|
171
|
-
|
172
|
-
const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
173
|
-
|
174
|
-
_utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
|
175
|
-
|
176
|
-
return {
|
177
|
-
component: lastComponent,
|
178
|
-
_component: replaceWithAlias(internalDirectory, file, internalDirAlias)
|
179
|
-
};
|
180
|
-
};
|
181
|
-
|
182
|
-
const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
|
183
|
-
parent: undefined
|
184
|
-
}) : _objectSpread(_objectSpread({}, route), {}, {
|
185
|
-
parent: undefined
|
186
|
-
}));
|
187
|
-
return normalized;
|
188
|
-
};
|
189
|
-
|
190
|
-
const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
|
191
|
-
|
192
|
-
const getClientRoutes = ({
|
193
|
-
entrypoint,
|
194
|
-
srcDirectory,
|
195
|
-
srcAlias,
|
196
|
-
internalDirectory,
|
197
|
-
internalDirAlias
|
198
|
-
}) => {
|
199
|
-
const {
|
200
|
-
entry,
|
201
|
-
entryName
|
202
|
-
} = entrypoint;
|
203
|
-
|
204
|
-
if (!_utils.fs.existsSync(entry)) {
|
205
|
-
throw new Error(`generate file system routes error, ${entry} directory not found.`);
|
206
|
-
}
|
207
|
-
|
208
|
-
if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
|
209
|
-
throw new Error(`generate file system routes error, ${entry} should be directory.`);
|
210
|
-
}
|
211
|
-
|
212
|
-
let routes = [];
|
213
|
-
recursiveReadDir({
|
214
|
-
dir: entry,
|
215
|
-
routes,
|
216
|
-
basePath: '/',
|
217
|
-
srcDirectory,
|
218
|
-
srcAlias
|
219
|
-
});
|
220
|
-
|
221
|
-
const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
|
222
|
-
|
223
|
-
_utils.fs.emptyDirSync(internalComponentsDir);
|
224
|
-
|
225
|
-
routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
|
226
|
-
parents.length = 0; // FIXME: support more situations
|
227
|
-
|
228
|
-
routes.sort((a, b) => {
|
229
|
-
const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
|
230
|
-
|
231
|
-
if (delta === 0) {
|
232
|
-
return a.path.length - b.path.length;
|
233
|
-
}
|
234
|
-
|
235
|
-
return delta;
|
236
|
-
});
|
237
|
-
debug(`fileSystem routes: %o`, routes);
|
238
|
-
return routes;
|
239
|
-
};
|
240
|
-
|
241
|
-
exports.getClientRoutes = getClientRoutes;
|
@@ -1,90 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.getFileSystemEntry = void 0;
|
7
|
-
|
8
|
-
var _fs = _interopRequireDefault(require("fs"));
|
9
|
-
|
10
|
-
var _path = _interopRequireDefault(require("path"));
|
11
|
-
|
12
|
-
var _utils = require("@modern-js/utils");
|
13
|
-
|
14
|
-
var _isDefaultExportFunction = require("./isDefaultExportFunction");
|
15
|
-
|
16
|
-
var _constants = require("./constants");
|
17
|
-
|
18
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19
|
-
|
20
|
-
const hasIndex = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.INDEX_FILE_NAME}${ext}`)));
|
21
|
-
|
22
|
-
const hasApp = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.APP_FILE_NAME}${ext}`)));
|
23
|
-
|
24
|
-
const hasPages = dir => _fs.default.existsSync(_path.default.join(dir, _constants.PAGES_DIR_NAME));
|
25
|
-
|
26
|
-
const isBundleEntry = dir => hasApp(dir) || hasPages(dir) || hasIndex(dir);
|
27
|
-
|
28
|
-
const scanDir = dirs => dirs.map(dir => {
|
29
|
-
const indexFile = hasIndex(dir);
|
30
|
-
const customBootstrap = (0, _isDefaultExportFunction.isDefaultExportFunction)(indexFile) ? indexFile : false;
|
31
|
-
|
32
|
-
const entryName = _path.default.basename(dir);
|
33
|
-
|
34
|
-
if (indexFile && !customBootstrap) {
|
35
|
-
return {
|
36
|
-
entryName,
|
37
|
-
entry: indexFile,
|
38
|
-
isAutoMount: false
|
39
|
-
};
|
40
|
-
}
|
41
|
-
|
42
|
-
if (hasApp(dir)) {
|
43
|
-
return {
|
44
|
-
entryName,
|
45
|
-
entry: _path.default.join(dir, _constants.APP_FILE_NAME),
|
46
|
-
isAutoMount: true,
|
47
|
-
customBootstrap
|
48
|
-
};
|
49
|
-
} else if (hasPages(dir)) {
|
50
|
-
return {
|
51
|
-
entryName,
|
52
|
-
entry: _path.default.join(dir, _constants.PAGES_DIR_NAME),
|
53
|
-
fileSystemRoutes: {
|
54
|
-
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
|
-
},
|
56
|
-
isAutoMount: true,
|
57
|
-
customBootstrap
|
58
|
-
};
|
59
|
-
} else {
|
60
|
-
return {
|
61
|
-
entryName,
|
62
|
-
entry: indexFile,
|
63
|
-
isAutoMount: false
|
64
|
-
};
|
65
|
-
}
|
66
|
-
});
|
67
|
-
|
68
|
-
const getFileSystemEntry = (appContext, config) => {
|
69
|
-
const {
|
70
|
-
appDirectory
|
71
|
-
} = appContext;
|
72
|
-
const {
|
73
|
-
source: {
|
74
|
-
entriesDir
|
75
|
-
}
|
76
|
-
} = config;
|
77
|
-
const src = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
|
78
|
-
|
79
|
-
if (_fs.default.existsSync(src)) {
|
80
|
-
if (_fs.default.statSync(src).isDirectory()) {
|
81
|
-
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)));
|
82
|
-
} else {
|
83
|
-
throw Error(`source.entriesDir accept a directory.`);
|
84
|
-
}
|
85
|
-
} else {
|
86
|
-
throw Error(`src dir ${entriesDir} not found.`);
|
87
|
-
}
|
88
|
-
};
|
89
|
-
|
90
|
-
exports.getFileSystemEntry = getFileSystemEntry;
|
@@ -1,106 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.getHtmlTemplate = 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
|
-
var templates = _interopRequireWildcard(require("./templates"));
|
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
-
|
22
|
-
var PartialPosition;
|
23
|
-
|
24
|
-
(function (PartialPosition) {
|
25
|
-
PartialPosition["TOP"] = "top";
|
26
|
-
PartialPosition["HEAD"] = "head";
|
27
|
-
PartialPosition["BODY"] = "body";
|
28
|
-
PartialPosition["BOTTOM"] = "bottom";
|
29
|
-
PartialPosition["INDEX"] = "index";
|
30
|
-
})(PartialPosition || (PartialPosition = {}));
|
31
|
-
|
32
|
-
const findPartials = (dir, entryName, position) => {
|
33
|
-
if (_utils.fs.existsSync(dir)) {
|
34
|
-
const base = (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${position}${ext}`)));
|
35
|
-
const file = entryName ? (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, entryName, `${position}${ext}`))) || base : base;
|
36
|
-
return file ? {
|
37
|
-
file,
|
38
|
-
content: _utils.fs.readFileSync(file, 'utf8')
|
39
|
-
} : null;
|
40
|
-
}
|
41
|
-
|
42
|
-
return null;
|
43
|
-
}; // generate html template for
|
44
|
-
|
45
|
-
|
46
|
-
const getHtmlTemplate = async (entrypoints, api, {
|
47
|
-
appContext,
|
48
|
-
config
|
49
|
-
}) => {
|
50
|
-
const {
|
51
|
-
appDirectory,
|
52
|
-
internalDirectory
|
53
|
-
} = appContext;
|
54
|
-
const {
|
55
|
-
source: {
|
56
|
-
configDir
|
57
|
-
}
|
58
|
-
} = config;
|
59
|
-
|
60
|
-
const htmlDir = _path.default.resolve(appDirectory, configDir, _constants.HTML_PARTIALS_FOLDER);
|
61
|
-
|
62
|
-
const htmlTemplates = {};
|
63
|
-
|
64
|
-
for (const entrypoint of entrypoints) {
|
65
|
-
const {
|
66
|
-
entryName
|
67
|
-
} = entrypoint;
|
68
|
-
const name = entrypoints.length === 1 && entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName;
|
69
|
-
const customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
|
70
|
-
|
71
|
-
if (customIndexTemplate) {
|
72
|
-
htmlTemplates[entryName] = customIndexTemplate.file;
|
73
|
-
} else {
|
74
|
-
const hookRunners = api.useHookRunners();
|
75
|
-
const {
|
76
|
-
partials
|
77
|
-
} = await hookRunners.htmlPartials({
|
78
|
-
entrypoint,
|
79
|
-
partials: [PartialPosition.TOP, PartialPosition.HEAD, PartialPosition.BODY].reduce((previous, position) => {
|
80
|
-
const found = findPartials(htmlDir, name, position);
|
81
|
-
previous[position] = found ? [found.content] : [];
|
82
|
-
return previous;
|
83
|
-
}, {
|
84
|
-
top: [],
|
85
|
-
head: [],
|
86
|
-
body: []
|
87
|
-
})
|
88
|
-
});
|
89
|
-
|
90
|
-
const templatePath = _path.default.resolve(internalDirectory, entryName, 'index.html');
|
91
|
-
|
92
|
-
_utils.fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
|
93
|
-
|
94
|
-
htmlTemplates[entryName] = templatePath;
|
95
|
-
const bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
|
96
|
-
|
97
|
-
if (bottomTemplate) {
|
98
|
-
htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
|
99
|
-
}
|
100
|
-
}
|
101
|
-
}
|
102
|
-
|
103
|
-
return htmlTemplates;
|
104
|
-
};
|
105
|
-
|
106
|
-
exports.getHtmlTemplate = getHtmlTemplate;
|
@@ -1,208 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.getServerRoutes = void 0;
|
7
|
-
|
8
|
-
var _path = _interopRequireDefault(require("path"));
|
9
|
-
|
10
|
-
var _fs = _interopRequireDefault(require("fs"));
|
11
|
-
|
12
|
-
var _utils = require("@modern-js/utils");
|
13
|
-
|
14
|
-
var _utils2 = require("./utils");
|
15
|
-
|
16
|
-
const _excluded = ["path"],
|
17
|
-
_excluded2 = ["path"];
|
18
|
-
|
19
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20
|
-
|
21
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
22
|
-
|
23
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
24
|
-
|
25
|
-
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; }
|
26
|
-
|
27
|
-
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; }
|
28
|
-
|
29
|
-
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; }
|
30
|
-
|
31
|
-
/**
|
32
|
-
* Add base url for each server route.
|
33
|
-
* @param baseUrl - Base url from server.baseUrl
|
34
|
-
* @param routes - Server routes.
|
35
|
-
* @returns Server routes with baseUrl prefixed.
|
36
|
-
*/
|
37
|
-
const applyBaseUrl = (baseUrl, routes) => {
|
38
|
-
if (baseUrl) {
|
39
|
-
if (Array.isArray(baseUrl)) {
|
40
|
-
return baseUrl.reduce((previous, current) => [...previous, ...applyBaseUrl(current, routes)], []);
|
41
|
-
} else {
|
42
|
-
return routes.map(route => {
|
43
|
-
const urlPath = (0, _utils.urlJoin)(baseUrl, route.urlPath);
|
44
|
-
return _objectSpread(_objectSpread({}, route), {}, {
|
45
|
-
urlPath: urlPath === '/' ? urlPath : (0, _utils.removeTailSlash)(urlPath)
|
46
|
-
});
|
47
|
-
});
|
48
|
-
}
|
49
|
-
}
|
50
|
-
|
51
|
-
return routes;
|
52
|
-
};
|
53
|
-
/**
|
54
|
-
*
|
55
|
-
* @param original - Original entrypoint route info.
|
56
|
-
* @param routeOptions - Custom entrypoint route config from server.routes.
|
57
|
-
* @returns
|
58
|
-
*/
|
59
|
-
|
60
|
-
|
61
|
-
const applyRouteOptions = (original, routeOptions) => {
|
62
|
-
const {
|
63
|
-
route,
|
64
|
-
disableSpa
|
65
|
-
} = routeOptions;
|
66
|
-
original.isSPA = !disableSpa; // set entryPath as dir
|
67
|
-
|
68
|
-
!original.isSPA && (original.entryPath = _path.default.dirname(original.entryPath));
|
69
|
-
let routes;
|
70
|
-
|
71
|
-
if (route) {
|
72
|
-
if (Array.isArray(route)) {
|
73
|
-
routes = route.map(url => {
|
74
|
-
if ((0, _utils.isPlainObject)(url)) {
|
75
|
-
const _ref = url,
|
76
|
-
{
|
77
|
-
path: urlPath
|
78
|
-
} = _ref,
|
79
|
-
other = _objectWithoutProperties(_ref, _excluded);
|
80
|
-
|
81
|
-
return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
82
|
-
urlPath
|
83
|
-
});
|
84
|
-
} else {
|
85
|
-
return _objectSpread(_objectSpread({}, original), {}, {
|
86
|
-
urlPath: url
|
87
|
-
});
|
88
|
-
}
|
89
|
-
});
|
90
|
-
} else if ((0, _utils.isPlainObject)(route)) {
|
91
|
-
const _ref2 = route,
|
92
|
-
{
|
93
|
-
path: urlPath
|
94
|
-
} = _ref2,
|
95
|
-
other = _objectWithoutProperties(_ref2, _excluded2);
|
96
|
-
|
97
|
-
routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
98
|
-
urlPath
|
99
|
-
})];
|
100
|
-
} else {
|
101
|
-
routes = [_objectSpread(_objectSpread({}, original), {}, {
|
102
|
-
urlPath: route
|
103
|
-
})];
|
104
|
-
}
|
105
|
-
} else {
|
106
|
-
routes = [original];
|
107
|
-
}
|
108
|
-
|
109
|
-
return routes;
|
110
|
-
};
|
111
|
-
/**
|
112
|
-
* Collect routes from entrypoints.
|
113
|
-
* @param entrypoints - Bundle entrypoints.
|
114
|
-
* @param config - Normalized user config.
|
115
|
-
* @returns entrypoint Routes
|
116
|
-
*/
|
117
|
-
|
118
|
-
|
119
|
-
const collectHtmlRoutes = (entrypoints, appContext, config) => {
|
120
|
-
const {
|
121
|
-
output: {
|
122
|
-
htmlPath,
|
123
|
-
disableHtmlFolder,
|
124
|
-
enableModernMode
|
125
|
-
},
|
126
|
-
server: {
|
127
|
-
baseUrl,
|
128
|
-
routes,
|
129
|
-
ssr,
|
130
|
-
ssrByEntries
|
131
|
-
}
|
132
|
-
} = config;
|
133
|
-
const {
|
134
|
-
packageName
|
135
|
-
} = appContext;
|
136
|
-
let htmlRoutes = entrypoints.reduce((previous, {
|
137
|
-
entryName
|
138
|
-
}) => {
|
139
|
-
const entryOptions = (0, _utils.getEntryOptions)(entryName, ssr, ssrByEntries, packageName);
|
140
|
-
const isSSR = Boolean(entryOptions);
|
141
|
-
const {
|
142
|
-
resHeaders
|
143
|
-
} = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {};
|
144
|
-
let route = {
|
145
|
-
urlPath: `/${entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName}`,
|
146
|
-
entryName,
|
147
|
-
entryPath: (0, _utils.removeLeadingSlash)(_path.default.posix.normalize(`${htmlPath}/${entryName}${disableHtmlFolder ? '.html' : '/index.html'}`)),
|
148
|
-
isSPA: true,
|
149
|
-
isSSR,
|
150
|
-
responseHeaders: resHeaders,
|
151
|
-
enableModernMode: Boolean(enableModernMode),
|
152
|
-
bundle: isSSR ? `${_utils.SERVER_BUNDLE_DIRECTORY}/${entryName}.js` : undefined
|
153
|
-
};
|
154
|
-
|
155
|
-
if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
|
156
|
-
const routeOptions = (0, _utils.isPlainObject)(routes[entryName]) ? routes[entryName] : {
|
157
|
-
route: routes[entryName]
|
158
|
-
};
|
159
|
-
route = applyRouteOptions(route, routeOptions);
|
160
|
-
}
|
161
|
-
|
162
|
-
return Array.isArray(route) ? [...previous, ...route] : [...previous, route];
|
163
|
-
}, []);
|
164
|
-
htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
|
165
|
-
return htmlRoutes;
|
166
|
-
};
|
167
|
-
/**
|
168
|
-
* Collect static public file routes from config/public folder.
|
169
|
-
* @param appContext - App context info.
|
170
|
-
* @param config - normalized user config.
|
171
|
-
* @returns Static public file routes.
|
172
|
-
*/
|
173
|
-
|
174
|
-
|
175
|
-
const collectStaticRoutes = (appContext, config) => {
|
176
|
-
const {
|
177
|
-
appDirectory
|
178
|
-
} = appContext;
|
179
|
-
const {
|
180
|
-
source: {
|
181
|
-
configDir
|
182
|
-
},
|
183
|
-
server: {
|
184
|
-
publicRoutes = {}
|
185
|
-
}
|
186
|
-
} = config;
|
187
|
-
|
188
|
-
const publicFolder = _path.default.resolve(appDirectory, configDir, 'public');
|
189
|
-
|
190
|
-
return _fs.default.existsSync(publicFolder) ? (0, _utils2.walkDirectory)(publicFolder).map(filePath => {
|
191
|
-
const urlPath = `${(0, _utils.urlJoin)(toPosix(filePath).slice(toPosix(publicFolder).length))}`;
|
192
|
-
return {
|
193
|
-
urlPath: publicRoutes[(0, _utils.removeLeadingSlash)(urlPath)] || urlPath,
|
194
|
-
isSPA: true,
|
195
|
-
isSSR: false,
|
196
|
-
entryPath: toPosix(_path.default.relative(_path.default.resolve(appDirectory, configDir), filePath))
|
197
|
-
};
|
198
|
-
}) : [];
|
199
|
-
};
|
200
|
-
|
201
|
-
const getServerRoutes = (entrypoints, {
|
202
|
-
appContext,
|
203
|
-
config
|
204
|
-
}) => [...collectHtmlRoutes(entrypoints, appContext, config), ...collectStaticRoutes(appContext, config)];
|
205
|
-
|
206
|
-
exports.getServerRoutes = getServerRoutes;
|
207
|
-
|
208
|
-
const toPosix = pathStr => pathStr.split(_path.default.sep).join(_path.default.posix.sep);
|