@modern-js/app-tools 1.21.5 → 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.
Files changed (170) hide show
  1. package/CHANGELOG.md +319 -51
  2. package/dist/js/modern/analyze/constants.js +1 -0
  3. package/dist/js/modern/analyze/generateCode.js +126 -32
  4. package/dist/js/modern/analyze/getBundleEntry.js +7 -14
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +173 -0
  6. package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +13 -58
  7. package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
  8. package/dist/js/modern/analyze/getClientRoutes/utils.js +18 -0
  9. package/dist/js/modern/analyze/getFileSystemEntry.js +27 -21
  10. package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
  11. package/dist/js/modern/analyze/getServerRoutes.js +15 -32
  12. package/dist/js/modern/analyze/index.js +81 -23
  13. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
  14. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
  15. package/dist/js/modern/analyze/nestedRoutes.js +102 -0
  16. package/dist/js/modern/analyze/templates.js +151 -12
  17. package/dist/js/modern/analyze/utils.js +2 -8
  18. package/dist/js/modern/builder/builderPlugins/compatModern.js +208 -0
  19. package/dist/js/modern/builder/createHtmlConfig.js +58 -0
  20. package/dist/js/modern/builder/createOutputConfig.js +70 -0
  21. package/dist/js/modern/builder/createSourceConfig.js +74 -0
  22. package/dist/js/modern/builder/createToolsConfig.js +87 -0
  23. package/dist/js/modern/builder/index.js +108 -0
  24. package/dist/js/modern/builder/share.js +44 -0
  25. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +29 -0
  26. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +33 -0
  27. package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
  28. package/dist/js/modern/commands/build.js +16 -110
  29. package/dist/js/modern/commands/dev.js +12 -28
  30. package/dist/js/modern/commands/inspect.js +8 -66
  31. package/dist/js/modern/commands/start.js +2 -3
  32. package/dist/js/modern/index.js +17 -16
  33. package/dist/js/modern/locale/en.js +0 -1
  34. package/dist/js/modern/locale/zh.js +0 -1
  35. package/dist/js/modern/utils/commands.js +5 -0
  36. package/dist/js/modern/utils/config.js +1 -12
  37. package/dist/js/modern/utils/createServer.js +12 -1
  38. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
  39. package/dist/js/modern/utils/printInstructions.js +3 -2
  40. package/dist/js/modern/utils/routes.js +0 -2
  41. package/dist/js/node/analyze/constants.js +3 -1
  42. package/dist/js/node/analyze/generateCode.js +123 -47
  43. package/dist/js/node/analyze/getBundleEntry.js +7 -20
  44. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +181 -0
  45. package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +19 -78
  46. package/dist/js/node/analyze/getClientRoutes/index.js +19 -0
  47. package/dist/js/node/analyze/getClientRoutes/utils.js +28 -0
  48. package/dist/js/node/analyze/getFileSystemEntry.js +26 -28
  49. package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
  50. package/dist/js/node/analyze/getServerRoutes.js +14 -40
  51. package/dist/js/node/analyze/index.js +81 -33
  52. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
  53. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
  54. package/dist/js/node/analyze/nestedRoutes.js +111 -0
  55. package/dist/js/node/analyze/templates.js +153 -21
  56. package/dist/js/node/analyze/utils.js +4 -20
  57. package/dist/js/node/builder/builderPlugins/compatModern.js +216 -0
  58. package/dist/js/node/builder/createHtmlConfig.js +68 -0
  59. package/dist/js/node/builder/createOutputConfig.js +76 -0
  60. package/dist/js/node/builder/createSourceConfig.js +82 -0
  61. package/dist/js/node/builder/createToolsConfig.js +94 -0
  62. package/dist/js/node/builder/index.js +117 -0
  63. package/dist/js/node/builder/share.js +51 -0
  64. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +36 -0
  65. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +40 -0
  66. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
  67. package/dist/js/node/commands/build.js +14 -121
  68. package/dist/js/node/commands/deploy.js +0 -2
  69. package/dist/js/node/commands/dev.js +10 -44
  70. package/dist/js/node/commands/index.js +0 -6
  71. package/dist/js/node/commands/inspect.js +10 -90
  72. package/dist/js/node/commands/start.js +2 -11
  73. package/dist/js/node/exports/server.js +0 -1
  74. package/dist/js/node/hooks.js +0 -2
  75. package/dist/js/node/index.js +22 -32
  76. package/dist/js/node/locale/en.js +0 -1
  77. package/dist/js/node/locale/index.js +0 -4
  78. package/dist/js/node/locale/zh.js +0 -1
  79. package/dist/js/node/utils/commands.js +12 -0
  80. package/dist/js/node/utils/config.js +1 -25
  81. package/dist/js/node/utils/createServer.js +15 -11
  82. package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
  83. package/dist/js/node/utils/language.js +0 -2
  84. package/dist/js/node/utils/printInstructions.js +3 -6
  85. package/dist/js/node/utils/routes.js +0 -5
  86. package/dist/js/treeshaking/analyze/constants.js +16 -0
  87. package/dist/js/treeshaking/analyze/generateCode.js +369 -0
  88. package/dist/js/treeshaking/analyze/getBundleEntry.js +69 -0
  89. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +184 -0
  90. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +185 -0
  91. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
  92. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +24 -0
  93. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +96 -0
  94. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +129 -0
  95. package/dist/js/treeshaking/analyze/getServerRoutes.js +157 -0
  96. package/dist/js/treeshaking/analyze/index.js +334 -0
  97. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +28 -0
  98. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +16 -0
  99. package/dist/js/treeshaking/analyze/nestedRoutes.js +165 -0
  100. package/dist/js/treeshaking/analyze/templates.js +170 -0
  101. package/dist/js/treeshaking/analyze/utils.js +88 -0
  102. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +212 -0
  103. package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
  104. package/dist/js/treeshaking/builder/createOutputConfig.js +70 -0
  105. package/dist/js/treeshaking/builder/createSourceConfig.js +88 -0
  106. package/dist/js/treeshaking/builder/createToolsConfig.js +85 -0
  107. package/dist/js/treeshaking/builder/index.js +161 -0
  108. package/dist/js/treeshaking/builder/share.js +46 -0
  109. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
  110. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +41 -0
  111. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
  112. package/dist/js/treeshaking/commands/build.js +83 -0
  113. package/dist/js/treeshaking/commands/deploy.js +26 -0
  114. package/dist/js/treeshaking/commands/dev.js +124 -0
  115. package/dist/js/treeshaking/commands/index.js +3 -0
  116. package/dist/js/treeshaking/commands/inspect.js +29 -0
  117. package/dist/js/treeshaking/commands/start.js +69 -0
  118. package/dist/js/treeshaking/exports/server.js +1 -0
  119. package/dist/js/treeshaking/hooks.js +21 -0
  120. package/dist/js/treeshaking/index.js +267 -0
  121. package/dist/js/treeshaking/locale/en.js +34 -0
  122. package/dist/js/treeshaking/locale/index.js +9 -0
  123. package/dist/js/treeshaking/locale/zh.js +34 -0
  124. package/dist/js/treeshaking/utils/commands.js +5 -0
  125. package/dist/js/treeshaking/utils/config.js +124 -0
  126. package/dist/js/treeshaking/utils/createServer.js +73 -0
  127. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +58 -0
  128. package/dist/js/treeshaking/utils/language.js +5 -0
  129. package/dist/js/treeshaking/utils/printInstructions.js +30 -0
  130. package/dist/js/treeshaking/utils/routes.js +29 -0
  131. package/dist/js/treeshaking/utils/types.js +0 -0
  132. package/dist/types/analyze/constants.d.ts +1 -0
  133. package/dist/types/analyze/generateCode.d.ts +1 -1
  134. package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
  135. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
  136. package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
  137. package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
  138. package/dist/types/analyze/index.d.ts +4 -5
  139. package/dist/types/analyze/nestedRoutes.d.ts +5 -0
  140. package/dist/types/analyze/templates.d.ts +19 -3
  141. package/dist/types/analyze/utils.d.ts +2 -1
  142. package/dist/types/builder/builderPlugins/compatModern.d.ts +13 -0
  143. package/dist/types/builder/createHtmlConfig.d.ts +6 -0
  144. package/dist/types/builder/createOutputConfig.d.ts +3 -0
  145. package/dist/types/builder/createSourceConfig.d.ts +5 -0
  146. package/dist/types/builder/createToolsConfig.d.ts +13 -0
  147. package/dist/types/builder/index.d.ts +15 -0
  148. package/dist/types/builder/share.d.ts +26 -0
  149. package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
  150. package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
  151. package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
  152. package/dist/types/commands/build.d.ts +2 -1
  153. package/dist/types/commands/deploy.d.ts +2 -1
  154. package/dist/types/commands/dev.d.ts +2 -1
  155. package/dist/types/commands/inspect.d.ts +2 -6
  156. package/dist/types/commands/start.d.ts +2 -1
  157. package/dist/types/hooks.d.ts +15 -10
  158. package/dist/types/index.d.ts +4 -4
  159. package/dist/types/locale/en.d.ts +0 -1
  160. package/dist/types/locale/index.d.ts +0 -2
  161. package/dist/types/locale/zh.d.ts +0 -1
  162. package/dist/types/utils/commands.d.ts +1 -0
  163. package/dist/types/utils/config.d.ts +0 -1
  164. package/dist/types/utils/createServer.d.ts +8 -1
  165. package/dist/types/utils/printInstructions.d.ts +3 -2
  166. package/dist/types/utils/types.d.ts +2 -3
  167. package/package.json +25 -42
  168. package/dist/js/modern/utils/createCompiler.js +0 -61
  169. package/dist/js/node/utils/createCompiler.js +0 -81
  170. package/dist/types/utils/createCompiler.d.ts +0 -13
@@ -0,0 +1,69 @@
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
+ var ensureExtensions = function ensureExtensions(file) {
6
+ if (!path.extname(file)) {
7
+ return findExists(JS_EXTENSIONS.map(function (ext) {
8
+ return "".concat(file).concat(ext);
9
+ })) || file;
10
+ }
11
+ return file;
12
+ };
13
+ var ifAlreadyExists = function ifAlreadyExists(entrypoints, checked) {
14
+ return entrypoints.some(function (entrypoint) {
15
+ if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
16
+ // reset entryName
17
+ checked.entryName = entrypoint.entryName;
18
+ return true;
19
+ }
20
+ // filesystem routes entrypoint conflict with normal entrypoint.
21
+ if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
22
+ throw new Error("Entry configuration conflicts\n Your configuration: ".concat(checked.entry, ".\n Default entrypoint: ").concat(entrypoint.entry, "\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules."));
23
+ }
24
+ return false;
25
+ });
26
+ };
27
+ export var getBundleEntry = function getBundleEntry(appContext, config) {
28
+ var appDirectory = appContext.appDirectory,
29
+ packageName = appContext.packageName;
30
+ var _config$source = config.source,
31
+ disableDefaultEntries = _config$source.disableDefaultEntries,
32
+ entries = _config$source.entries,
33
+ entriesDir = _config$source.entriesDir;
34
+ var defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config);
35
+
36
+ // merge entrypoints from user config with directory convention.
37
+ if (entries) {
38
+ Object.keys(entries).forEach(function (name) {
39
+ var value = entries[name];
40
+ var entrypoint = typeof value === 'string' ? {
41
+ entryName: name,
42
+ entry: ensureAbsolutePath(appDirectory, value),
43
+ isAutoMount: true,
44
+ fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value)).isDirectory() ? {} : undefined
45
+ } : {
46
+ entryName: name,
47
+ entry: ensureAbsolutePath(appDirectory, value.entry),
48
+ isAutoMount: !value.disableMount,
49
+ fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
50
+ };
51
+ if (!ifAlreadyExists(defaults, entrypoint)) {
52
+ defaults.push(entrypoint);
53
+ }
54
+ });
55
+ }
56
+ if (!disableDefaultEntries) {
57
+ // find main entry point which server route is '/'.
58
+ var entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir);
59
+ var found = defaults.find(function (_ref) {
60
+ var entryName = _ref.entryName,
61
+ entry = _ref.entry,
62
+ _ref$nestedRoutesEntr = _ref.nestedRoutesEntry,
63
+ nestedRoutesEntry = _ref$nestedRoutesEntr === void 0 ? '' : _ref$nestedRoutesEntr;
64
+ return entryName === packageName || path.dirname(entry) === entriesDirAbs || path.dirname(nestedRoutesEntry) === entriesDirAbs;
65
+ });
66
+ found && (found.entryName = MAIN_ENTRY_NAME);
67
+ }
68
+ return defaults;
69
+ };
@@ -0,0 +1,184 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
+ import path from 'path';
4
+ import { fs } from '@modern-js/utils';
5
+ import { makeLegalIdentifier } from "../makeLegalIdentifier";
6
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
7
+ import { replaceWithAlias } from "../utils";
8
+ import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
9
+ var compName = function compName(srcDirectory, filePath) {
10
+ var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
11
+ return "Comp_".concat(legalCompName);
12
+ };
13
+ var layoutNameAbbr = function layoutNameAbbr(filePath) {
14
+ var prefix = 'L_';
15
+ var dirName = path.dirname(filePath).split('/').pop() || '';
16
+ return "".concat(prefix).concat(makeLegalIdentifier(dirName));
17
+ };
18
+ var parents = [];
19
+
20
+ /* eslint-disable no-param-reassign */
21
+ var recursiveReadDir = function recursiveReadDir(_ref) {
22
+ var dir = _ref.dir,
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;
28
+ var hasDynamicRoute = false;
29
+ var resetParent = false;
30
+ var parent = parents[parents.length - 1];
31
+ var layout = findLayout(dir);
32
+ if (layout) {
33
+ if (basePath === '/') {
34
+ throw new Error("should use _app instead of _layout in ".concat(dir));
35
+ } else {
36
+ var alias = replaceWithAlias(srcDirectory, layout, srcAlias);
37
+ var componentName = compName(srcDirectory, layout);
38
+ var route = {
39
+ path: "".concat(basePath.substring(0, basePath.length - 1)),
40
+ children: [],
41
+ _component: alias,
42
+ component: componentName,
43
+ parent: parent,
44
+ type: 'page'
45
+ };
46
+ parent = route;
47
+ resetParent = true;
48
+ routes.push(route);
49
+ parents.push(route);
50
+ routes = route.children;
51
+ }
52
+ }
53
+ var _iterator = _createForOfIteratorHelper(fs.readdirSync(dir)),
54
+ _step;
55
+ try {
56
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
57
+ var relative = _step.value;
58
+ var filePath = path.join(dir, relative);
59
+ if (!shouldSkip(filePath)) {
60
+ var filename = path.basename(filePath, path.extname(filePath));
61
+ var _alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
62
+ var _componentName = compName(srcDirectory, filePath);
63
+ var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
64
+ if (dynamicRouteMatched) {
65
+ if (hasDynamicRoute) {
66
+ throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
67
+ } else {
68
+ hasDynamicRoute = true;
69
+ }
70
+ }
71
+ var _route = {
72
+ path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1]).concat(dynamicRouteMatched[2]) : filename),
73
+ _component: _alias,
74
+ component: _componentName,
75
+ parent: parent,
76
+ type: 'page'
77
+ };
78
+ if (fs.statSync(filePath).isDirectory()) {
79
+ recursiveReadDir({
80
+ dir: filePath,
81
+ routes: routes,
82
+ basePath: "".concat(_route.path, "/"),
83
+ srcDirectory: srcDirectory,
84
+ srcAlias: srcAlias
85
+ });
86
+ continue;
87
+ }
88
+ if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
89
+ continue;
90
+ }
91
+ if (filename === FILE_SYSTEM_ROUTES_INDEX) {
92
+ _route.path = basePath === '/' ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
93
+ }
94
+ if (filename === '404' && basePath === '/') {
95
+ _route.path = '*';
96
+ }
97
+ routes.push(_route);
98
+ }
99
+ }
100
+ } catch (err) {
101
+ _iterator.e(err);
102
+ } finally {
103
+ _iterator.f();
104
+ }
105
+ if (resetParent) {
106
+ parents.pop();
107
+ }
108
+ };
109
+ /* eslint-enable no-param-reassign */
110
+
111
+ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
112
+ var flat = function flat(routes) {
113
+ return routes.reduce(function (memo, route) {
114
+ return memo.concat(Array.isArray(route.children) ? flat(route.children) : [route]);
115
+ }, []);
116
+ };
117
+ var generate = function generate(route) {
118
+ var codes = [];
119
+ var lastComponent = route.component;
120
+ var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")];
121
+
122
+ // eslint-disable-next-line no-param-reassign, no-cond-assign
123
+ while (route = route.parent) {
124
+ var layoutComponent = route.component;
125
+ var layoutComponentAbbr = layoutNameAbbr(route._component);
126
+ imports.push("import ".concat(layoutComponent, " from '").concat(route._component, "';"));
127
+ var currentComponent = "".concat(layoutComponentAbbr, "_").concat(lastComponent);
128
+ codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
129
+ lastComponent = currentComponent;
130
+ }
131
+ var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
132
+ fs.outputFileSync(file, "".concat(imports.join('\n'), "\n").concat(codes.join('\n'), "\nexport default ").concat(lastComponent));
133
+ return {
134
+ component: lastComponent,
135
+ _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
136
+ };
137
+ };
138
+ var normalized = flat(nested).map(function (route) {
139
+ return route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
140
+ parent: undefined
141
+ }) : _objectSpread(_objectSpread({}, route), {}, {
142
+ parent: undefined
143
+ });
144
+ });
145
+ return normalized;
146
+ };
147
+ export var getClientRoutes = function getClientRoutes(_ref2) {
148
+ var entrypoint = _ref2.entrypoint,
149
+ srcDirectory = _ref2.srcDirectory,
150
+ srcAlias = _ref2.srcAlias,
151
+ internalDirectory = _ref2.internalDirectory,
152
+ internalDirAlias = _ref2.internalDirAlias;
153
+ var entry = entrypoint.entry,
154
+ entryName = entrypoint.entryName;
155
+ if (!fs.existsSync(entry)) {
156
+ throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
157
+ }
158
+ if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
159
+ throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
160
+ }
161
+ var routes = [];
162
+ recursiveReadDir({
163
+ dir: entry,
164
+ routes: routes,
165
+ basePath: '/',
166
+ srcDirectory: srcDirectory,
167
+ srcAlias: srcAlias
168
+ });
169
+ var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
170
+ fs.emptyDirSync(internalComponentsDir);
171
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
172
+ parents.length = 0;
173
+
174
+ // FIXME: support more situations
175
+ routes.sort(function (a, b) {
176
+ var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
177
+ if (delta === 0) {
178
+ return a.path.length - b.path.length;
179
+ }
180
+ return delta;
181
+ });
182
+ debug("fileSystem routes: %o", routes);
183
+ return routes;
184
+ };
@@ -0,0 +1,185 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
+ import path from 'path';
4
+ import { fs } from '@modern-js/utils';
5
+ import { makeLegalIdentifier } from "../makeLegalIdentifier";
6
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
7
+ import { replaceWithAlias } from "../utils";
8
+ import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
9
+ var compName = function compName(srcDirectory, filePath) {
10
+ var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
11
+ return "Comp_".concat(legalCompName);
12
+ };
13
+ var layoutNameAbbr = function layoutNameAbbr(filePath) {
14
+ var prefix = 'L_';
15
+ var dirName = path.dirname(filePath).split('/').pop() || '';
16
+ return "".concat(prefix).concat(makeLegalIdentifier(dirName));
17
+ };
18
+ var parents = [];
19
+
20
+ /* eslint-disable no-param-reassign */
21
+ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
22
+ var dir = _ref.dir,
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;
28
+ var hasDynamicRoute = false;
29
+ var resetParent = false;
30
+ var parent = parents[parents.length - 1];
31
+ var layout = findLayout(dir);
32
+ if (layout) {
33
+ if (basePath === '/') {
34
+ throw new Error("should use _app instead of _layout in ".concat(dir));
35
+ } else {
36
+ var alias = replaceWithAlias(srcDirectory, layout, srcAlias);
37
+ var componentName = compName(srcDirectory, layout);
38
+ var route = {
39
+ path: "".concat(basePath.substring(0, basePath.length - 1)),
40
+ exact: false,
41
+ routes: [],
42
+ _component: alias,
43
+ component: componentName,
44
+ parent: parent
45
+ };
46
+ parent = route;
47
+ resetParent = true;
48
+ routes.push(route);
49
+ parents.push(route);
50
+ routes = route.routes;
51
+ }
52
+ }
53
+ var _iterator = _createForOfIteratorHelper(fs.readdirSync(dir)),
54
+ _step;
55
+ try {
56
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
57
+ var relative = _step.value;
58
+ var filePath = path.join(dir, relative);
59
+ if (!shouldSkip(filePath)) {
60
+ var filename = path.basename(filePath, path.extname(filePath));
61
+ var _alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
62
+ var _componentName = compName(srcDirectory, filePath);
63
+ var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
64
+ if (dynamicRouteMatched) {
65
+ if (hasDynamicRoute) {
66
+ throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
67
+ } else {
68
+ hasDynamicRoute = true;
69
+ }
70
+ }
71
+ var _route = {
72
+ path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1]).concat(dynamicRouteMatched[2]) : filename),
73
+ _component: _alias,
74
+ component: _componentName,
75
+ exact: true,
76
+ parent: parent
77
+ };
78
+ if (fs.statSync(filePath).isDirectory()) {
79
+ recursiveReadDirLegacy({
80
+ dir: filePath,
81
+ routes: routes,
82
+ basePath: "".concat(_route.path, "/"),
83
+ srcDirectory: srcDirectory,
84
+ srcAlias: srcAlias
85
+ });
86
+ continue;
87
+ }
88
+ if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
89
+ continue;
90
+ }
91
+ if (filename === FILE_SYSTEM_ROUTES_INDEX) {
92
+ _route.path = basePath === '/' ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
93
+ }
94
+ if (filename === '404' && basePath === '/') {
95
+ _route.path = '*';
96
+ _route.exact = false;
97
+ }
98
+ routes.push(_route);
99
+ }
100
+ }
101
+ } catch (err) {
102
+ _iterator.e(err);
103
+ } finally {
104
+ _iterator.f();
105
+ }
106
+ if (resetParent) {
107
+ parents.pop();
108
+ }
109
+ };
110
+ /* eslint-enable no-param-reassign */
111
+
112
+ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
113
+ var flat = function flat(routes) {
114
+ return routes.reduce(function (memo, route) {
115
+ return memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]);
116
+ }, []);
117
+ };
118
+ var generate = function generate(route) {
119
+ var codes = [];
120
+ var lastComponent = route.component;
121
+ var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")];
122
+
123
+ // eslint-disable-next-line no-param-reassign, no-cond-assign
124
+ while (route = route.parent) {
125
+ var layoutComponent = route.component;
126
+ var layoutComponentAbbr = layoutNameAbbr(route._component);
127
+ imports.push("import ".concat(layoutComponent, " from '").concat(route._component, "';"));
128
+ var currentComponent = "".concat(layoutComponentAbbr, "_").concat(lastComponent);
129
+ codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
130
+ lastComponent = currentComponent;
131
+ }
132
+ var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
133
+ fs.outputFileSync(file, "".concat(imports.join('\n'), "\n").concat(codes.join('\n'), "\nexport default ").concat(lastComponent));
134
+ return {
135
+ component: lastComponent,
136
+ _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
137
+ };
138
+ };
139
+ var normalized = flat(nested).map(function (route) {
140
+ return route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
141
+ parent: undefined
142
+ }) : _objectSpread(_objectSpread({}, route), {}, {
143
+ parent: undefined
144
+ });
145
+ });
146
+ return normalized;
147
+ };
148
+ export var getClientRoutes = function getClientRoutes(_ref2) {
149
+ var entrypoint = _ref2.entrypoint,
150
+ srcDirectory = _ref2.srcDirectory,
151
+ srcAlias = _ref2.srcAlias,
152
+ internalDirectory = _ref2.internalDirectory,
153
+ internalDirAlias = _ref2.internalDirAlias;
154
+ var entry = entrypoint.entry,
155
+ entryName = entrypoint.entryName;
156
+ if (!fs.existsSync(entry)) {
157
+ throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
158
+ }
159
+ if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
160
+ throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
161
+ }
162
+ var routes = [];
163
+ recursiveReadDirLegacy({
164
+ dir: entry,
165
+ routes: routes,
166
+ basePath: '/',
167
+ srcDirectory: srcDirectory,
168
+ srcAlias: srcAlias
169
+ });
170
+ var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
171
+ fs.emptyDirSync(internalComponentsDir);
172
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
173
+ parents.length = 0;
174
+
175
+ // FIXME: support more situations
176
+ routes.sort(function (a, b) {
177
+ var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
178
+ if (delta === 0) {
179
+ return a.path.length - b.path.length;
180
+ }
181
+ return delta;
182
+ });
183
+ debug("fileSystem routes: %o", routes);
184
+ return routes;
185
+ };
@@ -0,0 +1,2 @@
1
+ export { getClientRoutes } from "./getRoutes";
2
+ export { getClientRoutes as getClientRoutesLegacy } from "./getRoutesLegacy";
@@ -0,0 +1,24 @@
1
+ import path from 'path';
2
+ import { createDebugger, findExists, fs } from '@modern-js/utils';
3
+ import { JS_EXTENSIONS, FILE_SYSTEM_ROUTES_LAYOUT, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, FILE_SYSTEM_ROUTES_IGNORED_REGEX } from "../constants";
4
+ var debug = createDebugger('get-client-routes');
5
+ export { debug };
6
+ export var findLayout = function findLayout(dir) {
7
+ return findExists(JS_EXTENSIONS.map(function (ext) {
8
+ return path.resolve(dir, "".concat(FILE_SYSTEM_ROUTES_LAYOUT).concat(ext));
9
+ }));
10
+ };
11
+ export var getRouteWeight = function getRouteWeight(route) {
12
+ return route === '*' ? 999 : route.split(':').length - 1;
13
+ };
14
+ export var shouldSkip = function shouldSkip(file) {
15
+ // should not skip directory.
16
+ if (fs.statSync(file).isDirectory()) {
17
+ return false;
18
+ }
19
+ var ext = path.extname(file);
20
+ if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
21
+ return true;
22
+ }
23
+ return false;
24
+ };
@@ -0,0 +1,96 @@
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, NESTED_ROUTES_DIR } from "./constants";
6
+ var hasIndex = function hasIndex(dir) {
7
+ return findExists(JS_EXTENSIONS.map(function (ext) {
8
+ return path.resolve(dir, "".concat(INDEX_FILE_NAME).concat(ext));
9
+ }));
10
+ };
11
+ var hasApp = function hasApp(dir) {
12
+ return findExists(JS_EXTENSIONS.map(function (ext) {
13
+ return path.resolve(dir, "".concat(APP_FILE_NAME).concat(ext));
14
+ }));
15
+ };
16
+ var hasPages = function hasPages(dir) {
17
+ return fs.existsSync(path.join(dir, PAGES_DIR_NAME));
18
+ };
19
+ var hasNestedRoutes = function hasNestedRoutes(dir) {
20
+ return fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
21
+ };
22
+ var isBundleEntry = function isBundleEntry(dir) {
23
+ return hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
24
+ };
25
+ var scanDir = function scanDir(dirs) {
26
+ return dirs.map(function (dir) {
27
+ var indexFile = hasIndex(dir);
28
+ var customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
29
+ var entryName = path.basename(dir);
30
+ if (indexFile && !customBootstrap) {
31
+ return {
32
+ entryName: entryName,
33
+ entry: indexFile,
34
+ absoluteEntryDir: path.resolve(dir),
35
+ isAutoMount: false
36
+ };
37
+ }
38
+ var isHasApp = hasApp(dir);
39
+ if (isHasApp) {
40
+ return {
41
+ entryName: entryName,
42
+ entry: path.join(dir, APP_FILE_NAME),
43
+ isAutoMount: true,
44
+ absoluteEntryDir: path.resolve(dir),
45
+ customBootstrap: customBootstrap
46
+ };
47
+ }
48
+ var isHasNestedRoutes = hasNestedRoutes(dir);
49
+ var isHasPages = hasPages(dir);
50
+ if (isHasNestedRoutes || isHasPages) {
51
+ var entrypoint = {
52
+ entryName: entryName,
53
+ entry: '',
54
+ fileSystemRoutes: {
55
+ globalApp: findExists(JS_EXTENSIONS.map(function (ext) {
56
+ return path.resolve(dir, "./".concat(PAGES_DIR_NAME, "/").concat(FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT).concat(ext));
57
+ }))
58
+ },
59
+ isAutoMount: true,
60
+ absoluteEntryDir: path.resolve(dir),
61
+ customBootstrap: customBootstrap
62
+ };
63
+ if (isHasPages) {
64
+ entrypoint.entry = path.join(dir, PAGES_DIR_NAME);
65
+ }
66
+ if (isHasNestedRoutes) {
67
+ entrypoint.nestedRoutesEntry = path.join(dir, NESTED_ROUTES_DIR);
68
+ }
69
+ return entrypoint;
70
+ }
71
+ return {
72
+ entryName: entryName,
73
+ entry: indexFile,
74
+ absoluteEntryDir: path.resolve(dir),
75
+ isAutoMount: false
76
+ };
77
+ });
78
+ };
79
+ export var getFileSystemEntry = function getFileSystemEntry(appContext, config) {
80
+ var appDirectory = appContext.appDirectory;
81
+ var entriesDir = config.source.entriesDir;
82
+ var src = ensureAbsolutePath(appDirectory, entriesDir);
83
+ if (fs.existsSync(src)) {
84
+ if (fs.statSync(src).isDirectory()) {
85
+ return scanDir(isBundleEntry(src) ? [src] : fs.readdirSync(src).map(function (file) {
86
+ return path.join(src, file);
87
+ }).filter(function (file) {
88
+ return fs.statSync(file).isDirectory() && isBundleEntry(file);
89
+ }));
90
+ } else {
91
+ throw Error("source.entriesDir accept a directory.");
92
+ }
93
+ } else {
94
+ throw Error("src dir ".concat(entriesDir, " not found."));
95
+ }
96
+ };