@modern-js/app-tools 2.54.3-alpha.4 → 2.54.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. package/dist/cjs/plugins/deploy/dependencies/index.js +17 -26
  2. package/dist/cjs/plugins/deploy/dependencies/utils.js +21 -14
  3. package/dist/cjs/plugins/deploy/platforms/netlify.js +8 -10
  4. package/dist/cjs/plugins/deploy/platforms/netlifyEntry.js +1 -0
  5. package/dist/cjs/plugins/deploy/platforms/node.js +8 -11
  6. package/dist/cjs/plugins/deploy/platforms/nodeEntry.js +1 -0
  7. package/dist/cjs/plugins/deploy/platforms/vercel.js +8 -10
  8. package/dist/cjs/plugins/deploy/platforms/vercelEntry.js +1 -0
  9. package/dist/esm/plugins/deploy/dependencies/index.js +69 -94
  10. package/dist/esm/plugins/deploy/dependencies/utils.js +40 -19
  11. package/dist/esm/plugins/deploy/platforms/netlify.js +9 -11
  12. package/dist/esm/plugins/deploy/platforms/netlifyEntry.js +2 -1
  13. package/dist/esm/plugins/deploy/platforms/node.js +9 -12
  14. package/dist/esm/plugins/deploy/platforms/nodeEntry.js +2 -1
  15. package/dist/esm/plugins/deploy/platforms/vercel.js +9 -11
  16. package/dist/esm/plugins/deploy/platforms/vercelEntry.js +2 -1
  17. package/dist/esm-node/plugins/deploy/dependencies/index.js +18 -27
  18. package/dist/esm-node/plugins/deploy/dependencies/utils.js +22 -15
  19. package/dist/esm-node/plugins/deploy/platforms/netlify.js +8 -10
  20. package/dist/esm-node/plugins/deploy/platforms/netlifyEntry.js +1 -0
  21. package/dist/esm-node/plugins/deploy/platforms/node.js +8 -11
  22. package/dist/esm-node/plugins/deploy/platforms/nodeEntry.js +1 -0
  23. package/dist/esm-node/plugins/deploy/platforms/vercel.js +8 -10
  24. package/dist/esm-node/plugins/deploy/platforms/vercelEntry.js +1 -0
  25. package/dist/types/plugins/deploy/dependencies/index.d.ts +1 -11
  26. package/dist/types/plugins/deploy/dependencies/utils.d.ts +1 -7
  27. package/package.json +17 -25
  28. package/dist/cjs/plugins/deploy/exports.js +0 -28
  29. package/dist/esm/plugins/deploy/exports.js +0 -4
  30. package/dist/esm-node/plugins/deploy/exports.js +0 -4
  31. package/dist/js/modern/analyze/constants.js +0 -15
  32. package/dist/js/modern/analyze/generateCode.js +0 -179
  33. package/dist/js/modern/analyze/getBundleEntry.js +0 -75
  34. package/dist/js/modern/analyze/getClientRoutes.js +0 -219
  35. package/dist/js/modern/analyze/getFileSystemEntry.js +0 -74
  36. package/dist/js/modern/analyze/getHtmlTemplate.js +0 -82
  37. package/dist/js/modern/analyze/getServerRoutes.js +0 -192
  38. package/dist/js/modern/analyze/index.js +0 -148
  39. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -32
  40. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -16
  41. package/dist/js/modern/analyze/templates.js +0 -88
  42. package/dist/js/modern/analyze/utils.js +0 -92
  43. package/dist/js/modern/commands/build.js +0 -154
  44. package/dist/js/modern/commands/deploy.js +0 -5
  45. package/dist/js/modern/commands/dev.js +0 -95
  46. package/dist/js/modern/commands/index.js +0 -3
  47. package/dist/js/modern/commands/inspect.js +0 -69
  48. package/dist/js/modern/commands/start.js +0 -31
  49. package/dist/js/modern/exports/server.js +0 -1
  50. package/dist/js/modern/hooks.js +0 -21
  51. package/dist/js/modern/index.js +0 -109
  52. package/dist/js/modern/locale/en.js +0 -35
  53. package/dist/js/modern/locale/index.js +0 -9
  54. package/dist/js/modern/locale/zh.js +0 -35
  55. package/dist/js/modern/utils/config.js +0 -78
  56. package/dist/js/modern/utils/createCompiler.js +0 -61
  57. package/dist/js/modern/utils/createServer.js +0 -18
  58. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -36
  59. package/dist/js/modern/utils/language.js +0 -5
  60. package/dist/js/modern/utils/printInstructions.js +0 -11
  61. package/dist/js/modern/utils/routes.js +0 -15
  62. package/dist/js/modern/utils/types.js +0 -0
  63. package/dist/js/node/analyze/constants.js +0 -36
  64. package/dist/js/node/analyze/generateCode.js +0 -208
  65. package/dist/js/node/analyze/getBundleEntry.js +0 -89
  66. package/dist/js/node/analyze/getClientRoutes.js +0 -241
  67. package/dist/js/node/analyze/getFileSystemEntry.js +0 -90
  68. package/dist/js/node/analyze/getHtmlTemplate.js +0 -106
  69. package/dist/js/node/analyze/getServerRoutes.js +0 -208
  70. package/dist/js/node/analyze/index.js +0 -178
  71. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -50
  72. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -24
  73. package/dist/js/node/analyze/templates.js +0 -106
  74. package/dist/js/node/analyze/utils.js +0 -113
  75. package/dist/js/node/commands/build.js +0 -174
  76. package/dist/js/node/commands/deploy.js +0 -14
  77. package/dist/js/node/commands/dev.js +0 -120
  78. package/dist/js/node/commands/index.js +0 -44
  79. package/dist/js/node/commands/inspect.js +0 -98
  80. package/dist/js/node/commands/start.js +0 -47
  81. package/dist/js/node/exports/server.js +0 -13
  82. package/dist/js/node/hooks.js +0 -39
  83. package/dist/js/node/index.js +0 -141
  84. package/dist/js/node/locale/en.js +0 -42
  85. package/dist/js/node/locale/index.js +0 -20
  86. package/dist/js/node/locale/zh.js +0 -42
  87. package/dist/js/node/utils/config.js +0 -103
  88. package/dist/js/node/utils/createCompiler.js +0 -81
  89. package/dist/js/node/utils/createServer.js +0 -35
  90. package/dist/js/node/utils/getSpecifiedEntries.js +0 -46
  91. package/dist/js/node/utils/language.js +0 -13
  92. package/dist/js/node/utils/printInstructions.js +0 -22
  93. package/dist/js/node/utils/routes.js +0 -25
  94. package/dist/js/node/utils/types.js +0 -0
  95. package/dist/types/plugins/deploy/exports.d.ts +0 -1
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.54.3-alpha.4",
18
+ "version": "2.54.3",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -43,11 +43,6 @@
43
43
  "types": "./dist/types/exports/server.d.ts",
44
44
  "jsnext:source": "./src/exports/server.ts",
45
45
  "default": "./dist/cjs/exports/server.js"
46
- },
47
- "./deploy": {
48
- "types": "./dist/types/plugins/deploy/exports.d.ts",
49
- "jsnext:source": "./src/plugins/deploy/exports.ts",
50
- "default": "./dist/cjs/plugins/deploy/exports.js"
51
46
  }
52
47
  },
53
48
  "engines": {
@@ -63,9 +58,6 @@
63
58
  ],
64
59
  "server": [
65
60
  "./dist/types/exports/server.d.ts"
66
- ],
67
- "deploy": [
68
- "./dist/types/plugins/deploy/exports.d.ts"
69
61
  ]
70
62
  }
71
63
  },
@@ -88,20 +80,20 @@
88
80
  "mlly": "^1.6.1",
89
81
  "pkg-types": "^1.1.0",
90
82
  "std-env": "^3.7.0",
91
- "@modern-js/node-bundle-require": "2.54.2",
92
- "@modern-js/core": "2.54.2",
93
- "@modern-js/plugin": "2.54.2",
94
- "@modern-js/plugin-data-loader": "2.54.2",
95
- "@modern-js/plugin-i18n": "2.54.2",
96
- "@modern-js/plugin-lint": "2.54.2",
97
- "@modern-js/prod-server": "2.54.2",
98
- "@modern-js/server": "2.54.2",
99
- "@modern-js/server-utils": "2.54.2",
100
- "@modern-js/rsbuild-plugin-esbuild": "2.54.2",
101
- "@modern-js/server-core": "2.54.2",
102
- "@modern-js/uni-builder": "2.54.2",
103
- "@modern-js/types": "2.54.2",
104
- "@modern-js/utils": "2.54.2"
83
+ "@modern-js/core": "2.54.3",
84
+ "@modern-js/node-bundle-require": "2.54.3",
85
+ "@modern-js/plugin-i18n": "2.54.3",
86
+ "@modern-js/plugin-data-loader": "2.54.3",
87
+ "@modern-js/plugin": "2.54.3",
88
+ "@modern-js/plugin-lint": "2.54.3",
89
+ "@modern-js/prod-server": "2.54.3",
90
+ "@modern-js/rsbuild-plugin-esbuild": "2.54.3",
91
+ "@modern-js/server": "2.54.3",
92
+ "@modern-js/types": "2.54.3",
93
+ "@modern-js/server-core": "2.54.3",
94
+ "@modern-js/uni-builder": "2.54.3",
95
+ "@modern-js/server-utils": "2.54.3",
96
+ "@modern-js/utils": "2.54.3"
105
97
  },
106
98
  "devDependencies": {
107
99
  "@rsbuild/plugin-swc": "0.7.9",
@@ -113,8 +105,8 @@
113
105
  "tsconfig-paths": "^4.2.0",
114
106
  "typescript": "^5",
115
107
  "webpack": "^5.92.0",
116
- "@scripts/build": "2.54.2",
117
- "@scripts/jest-config": "2.54.2"
108
+ "@scripts/jest-config": "2.54.3",
109
+ "@scripts/build": "2.54.3"
118
110
  },
119
111
  "sideEffects": false,
120
112
  "publishConfig": {
@@ -1,28 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var exports_exports = {};
20
- __export(exports_exports, {
21
- handleDependencies: () => import_dependencies.handleDependencies
22
- });
23
- module.exports = __toCommonJS(exports_exports);
24
- var import_dependencies = require("./dependencies");
25
- // Annotate the CommonJS export names for ESM import in node:
26
- 0 && (module.exports = {
27
- handleDependencies
28
- });
@@ -1,4 +0,0 @@
1
- import { handleDependencies } from "./dependencies";
2
- export {
3
- handleDependencies
4
- };
@@ -1,4 +0,0 @@
1
- import { handleDependencies } from "./dependencies";
2
- export {
3
- handleDependencies
4
- };
@@ -1,15 +0,0 @@
1
- export const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
2
- export const INDEX_FILE_NAME = 'index';
3
- export const APP_FILE_NAME = 'App';
4
- export const PAGES_DIR_NAME = 'pages';
5
- export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
6
- export const ENTRY_POINT_FILE_NAME = 'index.js';
7
- export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
8
- export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
9
- export const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
10
- export const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
11
- export const FILE_SYSTEM_ROUTES_INDEX = 'index';
12
- export const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
13
- export const HTML_PARTIALS_FOLDER = 'html';
14
- export const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
15
- export const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
@@ -1,179 +0,0 @@
1
- import path from 'path';
2
- import { fs } from '@modern-js/utils';
3
- import * as templates from "./templates";
4
- import { getClientRoutes } from "./getClientRoutes";
5
- import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
6
- import { getDefaultImports } from "./utils";
7
-
8
- const createImportSpecifier = specifiers => {
9
- let defaults = '';
10
- const named = [];
11
-
12
- for (const {
13
- local,
14
- imported
15
- } of specifiers) {
16
- if (local && imported) {
17
- named.push(`${imported} as ${local}`);
18
- } else if (local) {
19
- defaults = local;
20
- } else {
21
- named.push(imported);
22
- }
23
- }
24
-
25
- if (defaults && named.length) {
26
- return `${defaults}, { ${named.join(', ')} }`;
27
- } else if (defaults) {
28
- return defaults;
29
- } else {
30
- return `{ ${named.join(', ')} }`;
31
- }
32
- };
33
-
34
- export const createImportStatements = statements => {
35
- // merge import statements with the same value.
36
- const deDuplicated = [];
37
- const seen = new Map();
38
-
39
- for (const {
40
- value,
41
- specifiers,
42
- initialize
43
- } of statements) {
44
- if (!seen.has(value)) {
45
- deDuplicated.push({
46
- value,
47
- specifiers,
48
- initialize
49
- });
50
- seen.set(value, specifiers);
51
- } else {
52
- var _deDuplicated$modifyI, _deDuplicated$modifyI2;
53
-
54
- seen.get(value).push(...specifiers); // make "initialize" param can be connected when multiple plugins were imported from same package
55
-
56
- const modifyIndex = deDuplicated.findIndex(v => v.value === value);
57
- 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 : '';
58
- deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
59
- }
60
- }
61
-
62
- return deDuplicated.map(({
63
- value,
64
- specifiers,
65
- initialize
66
- }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
67
- };
68
- export const generateCode = async (appContext, config, entrypoints, api) => {
69
- const {
70
- internalDirectory,
71
- srcDirectory,
72
- internalDirAlias,
73
- internalSrcAlias
74
- } = appContext;
75
- const hookRunners = api.useHookRunners();
76
- const {
77
- output: {
78
- mountId
79
- }
80
- } = config;
81
-
82
- for (const entrypoint of entrypoints) {
83
- const {
84
- entryName,
85
- isAutoMount,
86
- customBootstrap,
87
- fileSystemRoutes
88
- } = entrypoint;
89
-
90
- if (isAutoMount) {
91
- // generate routes file for file system routes entrypoint.
92
- if (fileSystemRoutes) {
93
- const initialRoutes = getClientRoutes({
94
- entrypoint,
95
- srcDirectory,
96
- srcAlias: internalSrcAlias,
97
- internalDirectory,
98
- internalDirAlias
99
- });
100
- const {
101
- routes
102
- } = await hookRunners.modifyFileSystemRoutes({
103
- entrypoint,
104
- routes: initialRoutes
105
- });
106
- const {
107
- code
108
- } = await hookRunners.beforeGenerateRoutes({
109
- entrypoint,
110
- code: templates.fileSystemRoutes({
111
- routes
112
- })
113
- });
114
- fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
115
- } // call modifyEntryImports hook
116
-
117
-
118
- const {
119
- imports: importStatements
120
- } = await hookRunners.modifyEntryImports({
121
- entrypoint,
122
- imports: getDefaultImports({
123
- entrypoint,
124
- srcDirectory,
125
- internalSrcAlias,
126
- internalDirAlias,
127
- internalDirectory
128
- })
129
- }); // call modifyEntryRuntimePlugins hook
130
-
131
- const {
132
- plugins
133
- } = await hookRunners.modifyEntryRuntimePlugins({
134
- entrypoint,
135
- plugins: []
136
- }); // call modifyEntryRenderFunction hook
137
-
138
- const {
139
- code: renderFunction
140
- } = await hookRunners.modifyEntryRenderFunction({
141
- entrypoint,
142
- code: templates.renderFunction({
143
- plugins,
144
- customBootstrap,
145
- fileSystemRoutes
146
- })
147
- }); // call modifyEntryExport hook
148
-
149
- const {
150
- exportStatement
151
- } = await hookRunners.modifyEntryExport({
152
- entrypoint,
153
- exportStatement: 'export default AppWrapper;'
154
- });
155
- const code = templates.index({
156
- mountId: mountId,
157
- imports: createImportStatements(importStatements),
158
- renderFunction,
159
- exportStatement
160
- });
161
- const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
162
- entrypoint.entry = entryFile; // generate entry file.
163
-
164
- if (config.source.enableAsyncEntry) {
165
- const {
166
- code: asyncEntryCode
167
- } = await hookRunners.modifyAsyncEntry({
168
- entrypoint,
169
- code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
170
- });
171
- fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
172
- const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
173
- fs.outputFileSync(bootstrapFile, code, 'utf8');
174
- } else {
175
- fs.outputFileSync(entryFile, code, 'utf8');
176
- }
177
- }
178
- }
179
- };
@@ -1,75 +0,0 @@
1
- import path from 'path';
2
- import { ensureAbsolutePath, fs, findExists, MAIN_ENTRY_NAME } from '@modern-js/utils';
3
- import { getFileSystemEntry } from "./getFileSystemEntry";
4
- import { JS_EXTENSIONS } from "./constants";
5
-
6
- const ensureExtensions = file => {
7
- if (!path.extname(file)) {
8
- return findExists(JS_EXTENSIONS.map(ext => `${file}${ext}`)) || file;
9
- }
10
-
11
- return file;
12
- };
13
-
14
- const ifAlreadyExists = (entrypoints, checked) => entrypoints.some(entrypoint => {
15
- if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
16
- // reset entryName
17
- checked.entryName = entrypoint.entryName;
18
- return true;
19
- } // filesystem routes entrypoint conflict with normal entrypoint.
20
-
21
-
22
- if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
23
- 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.`);
24
- }
25
-
26
- return false;
27
- });
28
-
29
- export const getBundleEntry = (appContext, config) => {
30
- const {
31
- appDirectory,
32
- packageName
33
- } = appContext;
34
- const {
35
- source: {
36
- disableDefaultEntries,
37
- entries,
38
- entriesDir
39
- }
40
- } = config;
41
- const defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config); // merge entrypoints from user config with directory convention.
42
-
43
- if (entries) {
44
- Object.keys(entries).forEach(name => {
45
- const value = entries[name];
46
- const entrypoint = typeof value === 'string' ? {
47
- entryName: name,
48
- entry: ensureAbsolutePath(appDirectory, value),
49
- isAutoMount: true,
50
- fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value)).isDirectory() ? {} : undefined
51
- } : {
52
- entryName: name,
53
- entry: ensureAbsolutePath(appDirectory, value.entry),
54
- isAutoMount: !value.disableMount,
55
- fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
56
- };
57
-
58
- if (!ifAlreadyExists(defaults, entrypoint)) {
59
- defaults.push(entrypoint);
60
- }
61
- });
62
- }
63
-
64
- if (!disableDefaultEntries) {
65
- // find main entry point which server route is '/'.
66
- const entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir);
67
- const found = defaults.find(({
68
- entryName,
69
- entry
70
- }) => entryName === packageName || path.dirname(entry) === entriesDirAbs);
71
- found && (found.entryName = MAIN_ENTRY_NAME);
72
- }
73
-
74
- return defaults;
75
- };
@@ -1,219 +0,0 @@
1
- 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; }
2
-
3
- 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; }
4
-
5
- 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; }
6
-
7
- import path from 'path';
8
- import { fs, createDebugger, findExists, normalizeToPosixPath } from '@modern-js/utils';
9
- import { makeLegalIdentifier } from "./makeLegalIdentifier";
10
- import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, FILE_SYSTEM_ROUTES_IGNORED_REGEX, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT, JS_EXTENSIONS } from "./constants";
11
- const debug = createDebugger('get-client-routes');
12
-
13
- const findLayout = dir => findExists(JS_EXTENSIONS.map(ext => path.resolve(dir, `${FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
14
-
15
- const shouldSkip = file => {
16
- // should not skip directory.
17
- if (fs.statSync(file).isDirectory()) {
18
- return false;
19
- }
20
-
21
- const ext = path.extname(file);
22
-
23
- if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
24
- return true;
25
- }
26
-
27
- return false;
28
- };
29
-
30
- const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
31
-
32
- const compName = (srcDirectory, filePath) => {
33
- const legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
34
- return `Comp_${legalCompName}`;
35
- };
36
-
37
- const layoutNameAbbr = filePath => {
38
- const prefix = 'L_';
39
- const dirName = path.dirname(filePath).split('/').pop() || '';
40
- return `${prefix}${makeLegalIdentifier(dirName)}`;
41
- };
42
-
43
- const parents = [];
44
- /* eslint-disable no-param-reassign */
45
-
46
- const recursiveReadDir = ({
47
- dir,
48
- routes,
49
- basePath: _basePath = '/',
50
- srcDirectory,
51
- srcAlias
52
- }) => {
53
- let hasDynamicRoute = false;
54
- let resetParent = false;
55
- let parent = parents[parents.length - 1];
56
- const layout = findLayout(dir);
57
-
58
- if (layout) {
59
- if (_basePath === '/') {
60
- throw new Error(`should use _app instead of _layout in ${dir}`);
61
- } else {
62
- const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
63
- const componentName = compName(srcDirectory, layout);
64
- const route = {
65
- path: `${_basePath.substring(0, _basePath.length - 1)}`,
66
- exact: false,
67
- routes: [],
68
- _component: alias,
69
- component: componentName,
70
- parent
71
- };
72
- parent = route;
73
- resetParent = true;
74
- routes.push(route);
75
- parents.push(route);
76
- routes = route.routes;
77
- }
78
- }
79
-
80
- for (const relative of fs.readdirSync(dir)) {
81
- const filePath = path.join(dir, relative);
82
-
83
- if (!shouldSkip(filePath)) {
84
- const filename = path.basename(filePath, path.extname(filePath));
85
- const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
86
- const componentName = compName(srcDirectory, filePath);
87
- const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
88
-
89
- if (dynamicRouteMatched) {
90
- if (hasDynamicRoute) {
91
- throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
92
- } else {
93
- hasDynamicRoute = true;
94
- }
95
- }
96
-
97
- const route = {
98
- path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
99
- _component: alias,
100
- component: componentName,
101
- exact: true,
102
- parent
103
- };
104
-
105
- if (fs.statSync(filePath).isDirectory()) {
106
- recursiveReadDir({
107
- dir: filePath,
108
- routes,
109
- basePath: `${route.path}/`,
110
- srcDirectory,
111
- srcAlias
112
- });
113
- continue;
114
- }
115
-
116
- if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
117
- continue;
118
- }
119
-
120
- if (filename === FILE_SYSTEM_ROUTES_INDEX) {
121
- route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
122
- }
123
-
124
- if (filename === '404' && _basePath === '/') {
125
- route.path = '*';
126
- route.exact = false;
127
- }
128
-
129
- routes.push(route);
130
- }
131
- }
132
-
133
- if (resetParent) {
134
- parents.pop();
135
- }
136
- };
137
- /* eslint-enable no-param-reassign */
138
-
139
-
140
- const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
141
- const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]), []);
142
-
143
- const generate = route => {
144
- const codes = [];
145
- let lastComponent = route.component;
146
- const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`]; // eslint-disable-next-line no-param-reassign, no-cond-assign
147
-
148
- while (route = route.parent) {
149
- const layoutComponent = route.component;
150
- const layoutComponentAbbr = layoutNameAbbr(route._component);
151
- imports.push(`import ${layoutComponent} from '${route._component}';`);
152
- const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
153
- codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
154
- lastComponent = currentComponent;
155
- }
156
-
157
- const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
158
- fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
159
- return {
160
- component: lastComponent,
161
- _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
162
- };
163
- };
164
-
165
- const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
166
- parent: undefined
167
- }) : _objectSpread(_objectSpread({}, route), {}, {
168
- parent: undefined
169
- }));
170
- return normalized;
171
- };
172
-
173
- const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
174
-
175
- export const getClientRoutes = ({
176
- entrypoint,
177
- srcDirectory,
178
- srcAlias,
179
- internalDirectory,
180
- internalDirAlias
181
- }) => {
182
- const {
183
- entry,
184
- entryName
185
- } = entrypoint;
186
-
187
- if (!fs.existsSync(entry)) {
188
- throw new Error(`generate file system routes error, ${entry} directory not found.`);
189
- }
190
-
191
- if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
192
- throw new Error(`generate file system routes error, ${entry} should be directory.`);
193
- }
194
-
195
- let routes = [];
196
- recursiveReadDir({
197
- dir: entry,
198
- routes,
199
- basePath: '/',
200
- srcDirectory,
201
- srcAlias
202
- });
203
- const internalComponentsDir = path.resolve(internalDirectory, `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
204
- fs.emptyDirSync(internalComponentsDir);
205
- routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
206
- parents.length = 0; // FIXME: support more situations
207
-
208
- routes.sort((a, b) => {
209
- const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
210
-
211
- if (delta === 0) {
212
- return a.path.length - b.path.length;
213
- }
214
-
215
- return delta;
216
- });
217
- debug(`fileSystem routes: %o`, routes);
218
- return routes;
219
- };
@@ -1,74 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { findExists, ensureAbsolutePath } from '@modern-js/utils';
4
- import { isDefaultExportFunction } from "./isDefaultExportFunction";
5
- import { JS_EXTENSIONS, INDEX_FILE_NAME, APP_FILE_NAME, PAGES_DIR_NAME, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT } from "./constants";
6
-
7
- const hasIndex = dir => findExists(JS_EXTENSIONS.map(ext => path.resolve(dir, `${INDEX_FILE_NAME}${ext}`)));
8
-
9
- const hasApp = dir => findExists(JS_EXTENSIONS.map(ext => path.resolve(dir, `${APP_FILE_NAME}${ext}`)));
10
-
11
- const hasPages = dir => fs.existsSync(path.join(dir, PAGES_DIR_NAME));
12
-
13
- const isBundleEntry = dir => hasApp(dir) || hasPages(dir) || hasIndex(dir);
14
-
15
- const scanDir = dirs => dirs.map(dir => {
16
- const indexFile = hasIndex(dir);
17
- const customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
18
- const entryName = path.basename(dir);
19
-
20
- if (indexFile && !customBootstrap) {
21
- return {
22
- entryName,
23
- entry: indexFile,
24
- isAutoMount: false
25
- };
26
- }
27
-
28
- if (hasApp(dir)) {
29
- return {
30
- entryName,
31
- entry: path.join(dir, APP_FILE_NAME),
32
- isAutoMount: true,
33
- customBootstrap
34
- };
35
- } else if (hasPages(dir)) {
36
- return {
37
- entryName,
38
- entry: path.join(dir, PAGES_DIR_NAME),
39
- fileSystemRoutes: {
40
- globalApp: findExists(JS_EXTENSIONS.map(ext => path.resolve(dir, `./${PAGES_DIR_NAME}/${FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`)))
41
- },
42
- isAutoMount: true,
43
- customBootstrap
44
- };
45
- } else {
46
- return {
47
- entryName,
48
- entry: indexFile,
49
- isAutoMount: false
50
- };
51
- }
52
- });
53
-
54
- export const getFileSystemEntry = (appContext, config) => {
55
- const {
56
- appDirectory
57
- } = appContext;
58
- const {
59
- source: {
60
- entriesDir
61
- }
62
- } = config;
63
- const src = ensureAbsolutePath(appDirectory, entriesDir);
64
-
65
- if (fs.existsSync(src)) {
66
- if (fs.statSync(src).isDirectory()) {
67
- return scanDir(isBundleEntry(src) ? [src] : fs.readdirSync(src).map(file => path.join(src, file)).filter(file => fs.statSync(file).isDirectory() && isBundleEntry(file)));
68
- } else {
69
- throw Error(`source.entriesDir accept a directory.`);
70
- }
71
- } else {
72
- throw Error(`src dir ${entriesDir} not found.`);
73
- }
74
- };