@modern-js/app-tools 2.39.1 → 2.39.2-alpha.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/dist/cjs/analyze/getBundleEntry.js +9 -1
- package/dist/cjs/config/initialize/inits.js +39 -43
- package/dist/esm/analyze/getBundleEntry.js +9 -1
- package/dist/esm/config/initialize/inits.js +44 -49
- package/dist/esm-node/analyze/getBundleEntry.js +9 -1
- package/dist/esm-node/config/initialize/inits.js +38 -43
- package/dist/js/modern/analyze/constants.js +15 -0
- package/dist/js/modern/analyze/generateCode.js +179 -0
- package/dist/js/modern/analyze/getBundleEntry.js +75 -0
- package/dist/js/modern/analyze/getClientRoutes.js +219 -0
- package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
- package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
- package/dist/js/modern/analyze/getServerRoutes.js +192 -0
- package/dist/js/modern/analyze/index.js +148 -0
- package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
- package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
- package/dist/js/modern/analyze/templates.js +88 -0
- package/dist/js/modern/analyze/utils.js +92 -0
- package/dist/js/modern/commands/build.js +154 -0
- package/dist/js/modern/commands/deploy.js +5 -0
- package/dist/js/modern/commands/dev.js +95 -0
- package/dist/js/modern/commands/index.js +3 -0
- package/dist/js/modern/commands/inspect.js +69 -0
- package/dist/js/modern/commands/start.js +31 -0
- package/dist/js/modern/exports/server.js +1 -0
- package/dist/js/modern/hooks.js +21 -0
- package/dist/js/modern/index.js +109 -0
- package/dist/js/modern/locale/en.js +35 -0
- package/dist/js/modern/locale/index.js +9 -0
- package/dist/js/modern/locale/zh.js +35 -0
- package/dist/js/modern/utils/config.js +78 -0
- package/dist/js/modern/utils/createCompiler.js +61 -0
- package/dist/js/modern/utils/createServer.js +18 -0
- package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
- package/dist/js/modern/utils/language.js +5 -0
- package/dist/js/modern/utils/printInstructions.js +11 -0
- package/dist/js/modern/utils/routes.js +15 -0
- package/dist/js/modern/utils/types.js +0 -0
- package/dist/js/node/analyze/constants.js +36 -0
- package/dist/js/node/analyze/generateCode.js +208 -0
- package/dist/js/node/analyze/getBundleEntry.js +89 -0
- package/dist/js/node/analyze/getClientRoutes.js +241 -0
- package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
- package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
- package/dist/js/node/analyze/getServerRoutes.js +208 -0
- package/dist/js/node/analyze/index.js +178 -0
- package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
- package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
- package/dist/js/node/analyze/templates.js +106 -0
- package/dist/js/node/analyze/utils.js +113 -0
- package/dist/js/node/commands/build.js +174 -0
- package/dist/js/node/commands/deploy.js +14 -0
- package/dist/js/node/commands/dev.js +120 -0
- package/dist/js/node/commands/index.js +44 -0
- package/dist/js/node/commands/inspect.js +98 -0
- package/dist/js/node/commands/start.js +47 -0
- package/dist/js/node/exports/server.js +13 -0
- package/dist/js/node/hooks.js +39 -0
- package/dist/js/node/index.js +141 -0
- package/dist/js/node/locale/en.js +42 -0
- package/dist/js/node/locale/index.js +20 -0
- package/dist/js/node/locale/zh.js +42 -0
- package/dist/js/node/utils/config.js +103 -0
- package/dist/js/node/utils/createCompiler.js +81 -0
- package/dist/js/node/utils/createServer.js +35 -0
- package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
- package/dist/js/node/utils/language.js +13 -0
- package/dist/js/node/utils/printInstructions.js +22 -0
- package/dist/js/node/utils/routes.js +25 -0
- package/dist/js/node/utils/types.js +0 -0
- package/dist/types/config/initialize/inits.d.ts +2 -1
- package/package.json +6 -6
@@ -0,0 +1,208 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.generateCode = exports.createImportStatements = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var templates = _interopRequireWildcard(require("./templates"));
|
13
|
+
|
14
|
+
var _getClientRoutes = require("./getClientRoutes");
|
15
|
+
|
16
|
+
var _constants = require("./constants");
|
17
|
+
|
18
|
+
var _utils2 = require("./utils");
|
19
|
+
|
20
|
+
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); }
|
21
|
+
|
22
|
+
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; }
|
23
|
+
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25
|
+
|
26
|
+
const createImportSpecifier = specifiers => {
|
27
|
+
let defaults = '';
|
28
|
+
const named = [];
|
29
|
+
|
30
|
+
for (const {
|
31
|
+
local,
|
32
|
+
imported
|
33
|
+
} of specifiers) {
|
34
|
+
if (local && imported) {
|
35
|
+
named.push(`${imported} as ${local}`);
|
36
|
+
} else if (local) {
|
37
|
+
defaults = local;
|
38
|
+
} else {
|
39
|
+
named.push(imported);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
if (defaults && named.length) {
|
44
|
+
return `${defaults}, { ${named.join(', ')} }`;
|
45
|
+
} else if (defaults) {
|
46
|
+
return defaults;
|
47
|
+
} else {
|
48
|
+
return `{ ${named.join(', ')} }`;
|
49
|
+
}
|
50
|
+
};
|
51
|
+
|
52
|
+
const createImportStatements = statements => {
|
53
|
+
// merge import statements with the same value.
|
54
|
+
const deDuplicated = [];
|
55
|
+
const seen = new Map();
|
56
|
+
|
57
|
+
for (const {
|
58
|
+
value,
|
59
|
+
specifiers,
|
60
|
+
initialize
|
61
|
+
} of statements) {
|
62
|
+
if (!seen.has(value)) {
|
63
|
+
deDuplicated.push({
|
64
|
+
value,
|
65
|
+
specifiers,
|
66
|
+
initialize
|
67
|
+
});
|
68
|
+
seen.set(value, specifiers);
|
69
|
+
} else {
|
70
|
+
var _deDuplicated$modifyI, _deDuplicated$modifyI2;
|
71
|
+
|
72
|
+
seen.get(value).push(...specifiers); // make "initialize" param can be connected when multiple plugins were imported from same package
|
73
|
+
|
74
|
+
const modifyIndex = deDuplicated.findIndex(v => v.value === value);
|
75
|
+
const originInitialize = (_deDuplicated$modifyI = (_deDuplicated$modifyI2 = deDuplicated[modifyIndex]) === null || _deDuplicated$modifyI2 === void 0 ? void 0 : _deDuplicated$modifyI2.initialize) !== null && _deDuplicated$modifyI !== void 0 ? _deDuplicated$modifyI : '';
|
76
|
+
deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
return deDuplicated.map(({
|
81
|
+
value,
|
82
|
+
specifiers,
|
83
|
+
initialize
|
84
|
+
}) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
|
85
|
+
};
|
86
|
+
|
87
|
+
exports.createImportStatements = createImportStatements;
|
88
|
+
|
89
|
+
const generateCode = async (appContext, config, entrypoints, api) => {
|
90
|
+
const {
|
91
|
+
internalDirectory,
|
92
|
+
srcDirectory,
|
93
|
+
internalDirAlias,
|
94
|
+
internalSrcAlias
|
95
|
+
} = appContext;
|
96
|
+
const hookRunners = api.useHookRunners();
|
97
|
+
const {
|
98
|
+
output: {
|
99
|
+
mountId
|
100
|
+
}
|
101
|
+
} = config;
|
102
|
+
|
103
|
+
for (const entrypoint of entrypoints) {
|
104
|
+
const {
|
105
|
+
entryName,
|
106
|
+
isAutoMount,
|
107
|
+
customBootstrap,
|
108
|
+
fileSystemRoutes
|
109
|
+
} = entrypoint;
|
110
|
+
|
111
|
+
if (isAutoMount) {
|
112
|
+
// generate routes file for file system routes entrypoint.
|
113
|
+
if (fileSystemRoutes) {
|
114
|
+
const initialRoutes = (0, _getClientRoutes.getClientRoutes)({
|
115
|
+
entrypoint,
|
116
|
+
srcDirectory,
|
117
|
+
srcAlias: internalSrcAlias,
|
118
|
+
internalDirectory,
|
119
|
+
internalDirAlias
|
120
|
+
});
|
121
|
+
const {
|
122
|
+
routes
|
123
|
+
} = await hookRunners.modifyFileSystemRoutes({
|
124
|
+
entrypoint,
|
125
|
+
routes: initialRoutes
|
126
|
+
});
|
127
|
+
const {
|
128
|
+
code
|
129
|
+
} = await hookRunners.beforeGenerateRoutes({
|
130
|
+
entrypoint,
|
131
|
+
code: templates.fileSystemRoutes({
|
132
|
+
routes
|
133
|
+
})
|
134
|
+
});
|
135
|
+
|
136
|
+
_utils.fs.outputFileSync(_path.default.resolve(internalDirectory, `./${entryName}/${_constants.FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
|
137
|
+
} // call modifyEntryImports hook
|
138
|
+
|
139
|
+
|
140
|
+
const {
|
141
|
+
imports: importStatements
|
142
|
+
} = await hookRunners.modifyEntryImports({
|
143
|
+
entrypoint,
|
144
|
+
imports: (0, _utils2.getDefaultImports)({
|
145
|
+
entrypoint,
|
146
|
+
srcDirectory,
|
147
|
+
internalSrcAlias,
|
148
|
+
internalDirAlias,
|
149
|
+
internalDirectory
|
150
|
+
})
|
151
|
+
}); // call modifyEntryRuntimePlugins hook
|
152
|
+
|
153
|
+
const {
|
154
|
+
plugins
|
155
|
+
} = await hookRunners.modifyEntryRuntimePlugins({
|
156
|
+
entrypoint,
|
157
|
+
plugins: []
|
158
|
+
}); // call modifyEntryRenderFunction hook
|
159
|
+
|
160
|
+
const {
|
161
|
+
code: renderFunction
|
162
|
+
} = await hookRunners.modifyEntryRenderFunction({
|
163
|
+
entrypoint,
|
164
|
+
code: templates.renderFunction({
|
165
|
+
plugins,
|
166
|
+
customBootstrap,
|
167
|
+
fileSystemRoutes
|
168
|
+
})
|
169
|
+
}); // call modifyEntryExport hook
|
170
|
+
|
171
|
+
const {
|
172
|
+
exportStatement
|
173
|
+
} = await hookRunners.modifyEntryExport({
|
174
|
+
entrypoint,
|
175
|
+
exportStatement: 'export default AppWrapper;'
|
176
|
+
});
|
177
|
+
const code = templates.index({
|
178
|
+
mountId: mountId,
|
179
|
+
imports: createImportStatements(importStatements),
|
180
|
+
renderFunction,
|
181
|
+
exportStatement
|
182
|
+
});
|
183
|
+
|
184
|
+
const entryFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_POINT_FILE_NAME}`);
|
185
|
+
|
186
|
+
entrypoint.entry = entryFile; // generate entry file.
|
187
|
+
|
188
|
+
if (config.source.enableAsyncEntry) {
|
189
|
+
const {
|
190
|
+
code: asyncEntryCode
|
191
|
+
} = await hookRunners.modifyAsyncEntry({
|
192
|
+
entrypoint,
|
193
|
+
code: `import('./${_constants.ENTRY_BOOTSTRAP_FILE_NAME}');`
|
194
|
+
});
|
195
|
+
|
196
|
+
_utils.fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
|
197
|
+
|
198
|
+
const bootstrapFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_BOOTSTRAP_FILE_NAME}`);
|
199
|
+
|
200
|
+
_utils.fs.outputFileSync(bootstrapFile, code, 'utf8');
|
201
|
+
} else {
|
202
|
+
_utils.fs.outputFileSync(entryFile, code, 'utf8');
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
};
|
207
|
+
|
208
|
+
exports.generateCode = generateCode;
|
@@ -0,0 +1,89 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.getBundleEntry = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var _getFileSystemEntry = require("./getFileSystemEntry");
|
13
|
+
|
14
|
+
var _constants = require("./constants");
|
15
|
+
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
+
|
18
|
+
const ensureExtensions = file => {
|
19
|
+
if (!_path.default.extname(file)) {
|
20
|
+
return (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => `${file}${ext}`)) || file;
|
21
|
+
}
|
22
|
+
|
23
|
+
return file;
|
24
|
+
};
|
25
|
+
|
26
|
+
const ifAlreadyExists = (entrypoints, checked) => entrypoints.some(entrypoint => {
|
27
|
+
if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
|
28
|
+
// reset entryName
|
29
|
+
checked.entryName = entrypoint.entryName;
|
30
|
+
return true;
|
31
|
+
} // filesystem routes entrypoint conflict with normal entrypoint.
|
32
|
+
|
33
|
+
|
34
|
+
if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
|
35
|
+
throw new Error(`Entry configuration conflicts\n Your configuration: ${checked.entry}.\n Default entrypoint: ${entrypoint.entry}\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`);
|
36
|
+
}
|
37
|
+
|
38
|
+
return false;
|
39
|
+
});
|
40
|
+
|
41
|
+
const getBundleEntry = (appContext, config) => {
|
42
|
+
const {
|
43
|
+
appDirectory,
|
44
|
+
packageName
|
45
|
+
} = appContext;
|
46
|
+
const {
|
47
|
+
source: {
|
48
|
+
disableDefaultEntries,
|
49
|
+
entries,
|
50
|
+
entriesDir
|
51
|
+
}
|
52
|
+
} = config;
|
53
|
+
const defaults = disableDefaultEntries ? [] : (0, _getFileSystemEntry.getFileSystemEntry)(appContext, config); // merge entrypoints from user config with directory convention.
|
54
|
+
|
55
|
+
if (entries) {
|
56
|
+
Object.keys(entries).forEach(name => {
|
57
|
+
const value = entries[name];
|
58
|
+
const entrypoint = typeof value === 'string' ? {
|
59
|
+
entryName: name,
|
60
|
+
entry: (0, _utils.ensureAbsolutePath)(appDirectory, value),
|
61
|
+
isAutoMount: true,
|
62
|
+
fileSystemRoutes: _utils.fs.statSync((0, _utils.ensureAbsolutePath)(appDirectory, value)).isDirectory() ? {} : undefined
|
63
|
+
} : {
|
64
|
+
entryName: name,
|
65
|
+
entry: (0, _utils.ensureAbsolutePath)(appDirectory, value.entry),
|
66
|
+
isAutoMount: !value.disableMount,
|
67
|
+
fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
|
68
|
+
};
|
69
|
+
|
70
|
+
if (!ifAlreadyExists(defaults, entrypoint)) {
|
71
|
+
defaults.push(entrypoint);
|
72
|
+
}
|
73
|
+
});
|
74
|
+
}
|
75
|
+
|
76
|
+
if (!disableDefaultEntries) {
|
77
|
+
// find main entry point which server route is '/'.
|
78
|
+
const entriesDirAbs = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
|
79
|
+
const found = defaults.find(({
|
80
|
+
entryName,
|
81
|
+
entry
|
82
|
+
}) => entryName === packageName || _path.default.dirname(entry) === entriesDirAbs);
|
83
|
+
found && (found.entryName = _utils.MAIN_ENTRY_NAME);
|
84
|
+
}
|
85
|
+
|
86
|
+
return defaults;
|
87
|
+
};
|
88
|
+
|
89
|
+
exports.getBundleEntry = getBundleEntry;
|
@@ -0,0 +1,241 @@
|
|
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;
|
@@ -0,0 +1,90 @@
|
|
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;
|