@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,181 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getClientRoutes = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _utils = require("@modern-js/utils");
9
+ var _makeLegalIdentifier = require("../makeLegalIdentifier");
10
+ var _constants = require("../constants");
11
+ var _utils2 = require("../utils");
12
+ var _utils3 = require("./utils");
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+ 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; }
15
+ 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; }
16
+ 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; }
17
+ const compName = (srcDirectory, filePath) => {
18
+ const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
19
+ return `Comp_${legalCompName}`;
20
+ };
21
+ const layoutNameAbbr = filePath => {
22
+ const prefix = 'L_';
23
+ const dirName = _path.default.dirname(filePath).split('/').pop() || '';
24
+ return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
25
+ };
26
+ const parents = [];
27
+
28
+ /* eslint-disable no-param-reassign */
29
+ const recursiveReadDir = ({
30
+ dir,
31
+ routes,
32
+ basePath: _basePath = '/',
33
+ srcDirectory,
34
+ srcAlias
35
+ }) => {
36
+ let hasDynamicRoute = false;
37
+ let resetParent = false;
38
+ let parent = parents[parents.length - 1];
39
+ const layout = (0, _utils3.findLayout)(dir);
40
+ if (layout) {
41
+ if (_basePath === '/') {
42
+ throw new Error(`should use _app instead of _layout in ${dir}`);
43
+ } else {
44
+ const alias = (0, _utils2.replaceWithAlias)(srcDirectory, layout, srcAlias);
45
+ const componentName = compName(srcDirectory, layout);
46
+ const route = {
47
+ path: `${_basePath.substring(0, _basePath.length - 1)}`,
48
+ children: [],
49
+ _component: alias,
50
+ component: componentName,
51
+ parent,
52
+ type: 'page'
53
+ };
54
+ parent = route;
55
+ resetParent = true;
56
+ routes.push(route);
57
+ parents.push(route);
58
+ routes = route.children;
59
+ }
60
+ }
61
+ for (const relative of _utils.fs.readdirSync(dir)) {
62
+ const filePath = _path.default.join(dir, relative);
63
+ if (!(0, _utils3.shouldSkip)(filePath)) {
64
+ const filename = _path.default.basename(filePath, _path.default.extname(filePath));
65
+ const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
66
+ const componentName = compName(srcDirectory, filePath);
67
+ const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
68
+ if (dynamicRouteMatched) {
69
+ if (hasDynamicRoute) {
70
+ throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
71
+ } else {
72
+ hasDynamicRoute = true;
73
+ }
74
+ }
75
+ const route = {
76
+ path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
77
+ _component: alias,
78
+ component: componentName,
79
+ parent,
80
+ type: 'page'
81
+ };
82
+ if (_utils.fs.statSync(filePath).isDirectory()) {
83
+ recursiveReadDir({
84
+ dir: filePath,
85
+ routes,
86
+ basePath: `${route.path}/`,
87
+ srcDirectory,
88
+ srcAlias
89
+ });
90
+ continue;
91
+ }
92
+ if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
93
+ continue;
94
+ }
95
+ if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
96
+ route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
97
+ }
98
+ if (filename === '404' && _basePath === '/') {
99
+ route.path = '*';
100
+ }
101
+ routes.push(route);
102
+ }
103
+ }
104
+ if (resetParent) {
105
+ parents.pop();
106
+ }
107
+ };
108
+ /* eslint-enable no-param-reassign */
109
+
110
+ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
111
+ const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.children) ? flat(route.children) : [route]), []);
112
+ const generate = route => {
113
+ const codes = [];
114
+ let lastComponent = route.component;
115
+ const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
116
+
117
+ // eslint-disable-next-line no-param-reassign, no-cond-assign
118
+ while (route = route.parent) {
119
+ const layoutComponent = route.component;
120
+ const layoutComponentAbbr = layoutNameAbbr(route._component);
121
+ imports.push(`import ${layoutComponent} from '${route._component}';`);
122
+ const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
123
+ codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
124
+ lastComponent = currentComponent;
125
+ }
126
+ const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
127
+ _utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
128
+ return {
129
+ component: lastComponent,
130
+ _component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
131
+ };
132
+ };
133
+ const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
134
+ parent: undefined
135
+ }) : _objectSpread(_objectSpread({}, route), {}, {
136
+ parent: undefined
137
+ }));
138
+ return normalized;
139
+ };
140
+ const getClientRoutes = ({
141
+ entrypoint,
142
+ srcDirectory,
143
+ srcAlias,
144
+ internalDirectory,
145
+ internalDirAlias
146
+ }) => {
147
+ const {
148
+ entry,
149
+ entryName
150
+ } = entrypoint;
151
+ if (!_utils.fs.existsSync(entry)) {
152
+ throw new Error(`generate file system routes error, ${entry} directory not found.`);
153
+ }
154
+ if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
155
+ throw new Error(`generate file system routes error, ${entry} should be directory.`);
156
+ }
157
+ let routes = [];
158
+ recursiveReadDir({
159
+ dir: entry,
160
+ routes,
161
+ basePath: '/',
162
+ srcDirectory,
163
+ srcAlias
164
+ });
165
+ const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
166
+ _utils.fs.emptyDirSync(internalComponentsDir);
167
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
168
+ parents.length = 0;
169
+
170
+ // FIXME: support more situations
171
+ routes.sort((a, b) => {
172
+ const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
173
+ if (delta === 0) {
174
+ return a.path.length - b.path.length;
175
+ }
176
+ return delta;
177
+ });
178
+ (0, _utils3.debug)(`fileSystem routes: %o`, routes);
179
+ return routes;
180
+ };
181
+ exports.getClientRoutes = getClientRoutes;
@@ -4,59 +4,29 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getClientRoutes = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
- var _makeLegalIdentifier = require("./makeLegalIdentifier");
13
-
14
- var _constants = require("./constants");
15
-
9
+ var _makeLegalIdentifier = require("../makeLegalIdentifier");
10
+ var _constants = require("../constants");
11
+ var _utils2 = require("../utils");
12
+ var _utils3 = require("./utils");
16
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
14
  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
15
  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
16
  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
17
  const compName = (srcDirectory, filePath) => {
46
18
  const legalCompName = (0, _makeLegalIdentifier.makeLegalIdentifier)(_path.default.relative(srcDirectory, filePath));
47
19
  return `Comp_${legalCompName}`;
48
20
  };
49
-
50
21
  const layoutNameAbbr = filePath => {
51
22
  const prefix = 'L_';
52
23
  const dirName = _path.default.dirname(filePath).split('/').pop() || '';
53
24
  return `${prefix}${(0, _makeLegalIdentifier.makeLegalIdentifier)(dirName)}`;
54
25
  };
55
-
56
26
  const parents = [];
57
- /* eslint-disable no-param-reassign */
58
27
 
59
- const recursiveReadDir = ({
28
+ /* eslint-disable no-param-reassign */
29
+ const recursiveReadDirLegacy = ({
60
30
  dir,
61
31
  routes,
62
32
  basePath: _basePath = '/',
@@ -66,13 +36,12 @@ const recursiveReadDir = ({
66
36
  let hasDynamicRoute = false;
67
37
  let resetParent = false;
68
38
  let parent = parents[parents.length - 1];
69
- const layout = findLayout(dir);
70
-
39
+ const layout = (0, _utils3.findLayout)(dir);
71
40
  if (layout) {
72
41
  if (_basePath === '/') {
73
42
  throw new Error(`should use _app instead of _layout in ${dir}`);
74
43
  } else {
75
- const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
44
+ const alias = (0, _utils2.replaceWithAlias)(srcDirectory, layout, srcAlias);
76
45
  const componentName = compName(srcDirectory, layout);
77
46
  const route = {
78
47
  path: `${_basePath.substring(0, _basePath.length - 1)}`,
@@ -89,18 +58,13 @@ const recursiveReadDir = ({
89
58
  routes = route.routes;
90
59
  }
91
60
  }
92
-
93
61
  for (const relative of _utils.fs.readdirSync(dir)) {
94
62
  const filePath = _path.default.join(dir, relative);
95
-
96
- if (!shouldSkip(filePath)) {
63
+ if (!(0, _utils3.shouldSkip)(filePath)) {
97
64
  const filename = _path.default.basename(filePath, _path.default.extname(filePath));
98
-
99
- const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
65
+ const alias = (0, _utils2.replaceWithAlias)(srcDirectory, filePath, srcAlias);
100
66
  const componentName = compName(srcDirectory, filePath);
101
-
102
67
  const dynamicRouteMatched = _constants.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
103
-
104
68
  if (dynamicRouteMatched) {
105
69
  if (hasDynamicRoute) {
106
70
  throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
@@ -108,7 +72,6 @@ const recursiveReadDir = ({
108
72
  hasDynamicRoute = true;
109
73
  }
110
74
  }
111
-
112
75
  const route = {
113
76
  path: `${_basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
114
77
  _component: alias,
@@ -116,9 +79,8 @@ const recursiveReadDir = ({
116
79
  exact: true,
117
80
  parent
118
81
  };
119
-
120
82
  if (_utils.fs.statSync(filePath).isDirectory()) {
121
- recursiveReadDir({
83
+ recursiveReadDirLegacy({
122
84
  dir: filePath,
123
85
  routes,
124
86
  basePath: `${route.path}/`,
@@ -127,39 +89,33 @@ const recursiveReadDir = ({
127
89
  });
128
90
  continue;
129
91
  }
130
-
131
92
  if (filename === _constants.FILE_SYSTEM_ROUTES_LAYOUT) {
132
93
  continue;
133
94
  }
134
-
135
95
  if (filename === _constants.FILE_SYSTEM_ROUTES_INDEX) {
136
96
  route.path = _basePath === '/' ? _basePath : `${_basePath.substring(0, _basePath.length - 1)}`;
137
97
  }
138
-
139
98
  if (filename === '404' && _basePath === '/') {
140
99
  route.path = '*';
141
100
  route.exact = false;
142
101
  }
143
-
144
102
  routes.push(route);
145
103
  }
146
104
  }
147
-
148
105
  if (resetParent) {
149
106
  parents.pop();
150
107
  }
151
108
  };
152
109
  /* eslint-enable no-param-reassign */
153
110
 
154
-
155
111
  const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
156
112
  const flat = routes => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]), []);
157
-
158
113
  const generate = route => {
159
114
  const codes = [];
160
115
  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
116
+ const imports = [`import React from 'react';`, `import ${lastComponent} from '${route._component}'`];
162
117
 
118
+ // eslint-disable-next-line no-param-reassign, no-cond-assign
163
119
  while (route = route.parent) {
164
120
  const layoutComponent = route.component;
165
121
  const layoutComponentAbbr = layoutNameAbbr(route._component);
@@ -168,17 +124,13 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
168
124
  codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
169
125
  lastComponent = currentComponent;
170
126
  }
171
-
172
127
  const file = _path.default.resolve(internalComponentsDir, `${lastComponent}.jsx`);
173
-
174
128
  _utils.fs.outputFileSync(file, `${imports.join('\n')}\n${codes.join('\n')}\nexport default ${lastComponent}`);
175
-
176
129
  return {
177
130
  component: lastComponent,
178
- _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
131
+ _component: (0, _utils2.replaceWithAlias)(internalDirectory, file, internalDirAlias)
179
132
  };
180
133
  };
181
-
182
134
  const normalized = flat(nested).map(route => route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
183
135
  parent: undefined
184
136
  }) : _objectSpread(_objectSpread({}, route), {}, {
@@ -186,9 +138,6 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
186
138
  }));
187
139
  return normalized;
188
140
  };
189
-
190
- const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
191
-
192
141
  const getClientRoutes = ({
193
142
  entrypoint,
194
143
  srcDirectory,
@@ -200,42 +149,34 @@ const getClientRoutes = ({
200
149
  entry,
201
150
  entryName
202
151
  } = entrypoint;
203
-
204
152
  if (!_utils.fs.existsSync(entry)) {
205
153
  throw new Error(`generate file system routes error, ${entry} directory not found.`);
206
154
  }
207
-
208
155
  if (!(_utils.fs.existsSync(entry) && _utils.fs.statSync(entry).isDirectory())) {
209
156
  throw new Error(`generate file system routes error, ${entry} should be directory.`);
210
157
  }
211
-
212
158
  let routes = [];
213
- recursiveReadDir({
159
+ recursiveReadDirLegacy({
214
160
  dir: entry,
215
161
  routes,
216
162
  basePath: '/',
217
163
  srcDirectory,
218
164
  srcAlias
219
165
  });
220
-
221
166
  const internalComponentsDir = _path.default.resolve(internalDirectory, `${entryName}/${_constants.FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
222
-
223
167
  _utils.fs.emptyDirSync(internalComponentsDir);
224
-
225
168
  routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
226
- parents.length = 0; // FIXME: support more situations
169
+ parents.length = 0;
227
170
 
171
+ // FIXME: support more situations
228
172
  routes.sort((a, b) => {
229
- const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
230
-
173
+ const delta = (0, _utils3.getRouteWeight)(a.path) - (0, _utils3.getRouteWeight)(b.path);
231
174
  if (delta === 0) {
232
175
  return a.path.length - b.path.length;
233
176
  }
234
-
235
177
  return delta;
236
178
  });
237
- debug(`fileSystem routes: %o`, routes);
179
+ (0, _utils3.debug)(`fileSystem routes: %o`, routes);
238
180
  return routes;
239
181
  };
240
-
241
182
  exports.getClientRoutes = getClientRoutes;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "getClientRoutes", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _getRoutes.getClientRoutes;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "getClientRoutesLegacy", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _getRoutesLegacy.getClientRoutes;
16
+ }
17
+ });
18
+ var _getRoutes = require("./getRoutes");
19
+ var _getRoutesLegacy = require("./getRoutesLegacy");
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.shouldSkip = exports.getRouteWeight = exports.findLayout = exports.debug = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _utils = require("@modern-js/utils");
9
+ var _constants = require("../constants");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ const debug = (0, _utils.createDebugger)('get-client-routes');
12
+ exports.debug = debug;
13
+ const findLayout = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
14
+ exports.findLayout = findLayout;
15
+ const getRouteWeight = route => route === '*' ? 999 : route.split(':').length - 1;
16
+ exports.getRouteWeight = getRouteWeight;
17
+ const shouldSkip = file => {
18
+ // should not skip directory.
19
+ if (_utils.fs.statSync(file).isDirectory()) {
20
+ return false;
21
+ }
22
+ const ext = _path.default.extname(file);
23
+ 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)) {
24
+ return true;
25
+ }
26
+ return false;
27
+ };
28
+ exports.shouldSkip = shouldSkip;
@@ -4,67 +4,67 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getFileSystemEntry = void 0;
7
-
8
7
  var _fs = _interopRequireDefault(require("fs"));
9
-
10
8
  var _path = _interopRequireDefault(require("path"));
11
-
12
9
  var _utils = require("@modern-js/utils");
13
-
14
10
  var _isDefaultExportFunction = require("./isDefaultExportFunction");
15
-
16
11
  var _constants = require("./constants");
17
-
18
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
13
  const hasIndex = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.INDEX_FILE_NAME}${ext}`)));
21
-
22
14
  const hasApp = dir => (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${_constants.APP_FILE_NAME}${ext}`)));
23
-
24
15
  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
-
16
+ const hasNestedRoutes = dir => _fs.default.existsSync(_path.default.join(dir, _constants.NESTED_ROUTES_DIR));
17
+ const isBundleEntry = dir => hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
28
18
  const scanDir = dirs => dirs.map(dir => {
29
19
  const indexFile = hasIndex(dir);
30
20
  const customBootstrap = (0, _isDefaultExportFunction.isDefaultExportFunction)(indexFile) ? indexFile : false;
31
-
32
21
  const entryName = _path.default.basename(dir);
33
-
34
22
  if (indexFile && !customBootstrap) {
35
23
  return {
36
24
  entryName,
37
25
  entry: indexFile,
26
+ absoluteEntryDir: _path.default.resolve(dir),
38
27
  isAutoMount: false
39
28
  };
40
29
  }
41
-
42
- if (hasApp(dir)) {
30
+ const isHasApp = hasApp(dir);
31
+ if (isHasApp) {
43
32
  return {
44
33
  entryName,
45
34
  entry: _path.default.join(dir, _constants.APP_FILE_NAME),
46
35
  isAutoMount: true,
36
+ absoluteEntryDir: _path.default.resolve(dir),
47
37
  customBootstrap
48
38
  };
49
- } else if (hasPages(dir)) {
50
- return {
39
+ }
40
+ const isHasNestedRoutes = hasNestedRoutes(dir);
41
+ const isHasPages = hasPages(dir);
42
+ if (isHasNestedRoutes || isHasPages) {
43
+ const entrypoint = {
51
44
  entryName,
52
- entry: _path.default.join(dir, _constants.PAGES_DIR_NAME),
45
+ entry: '',
53
46
  fileSystemRoutes: {
54
47
  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
48
  },
56
49
  isAutoMount: true,
50
+ absoluteEntryDir: _path.default.resolve(dir),
57
51
  customBootstrap
58
52
  };
59
- } else {
60
- return {
61
- entryName,
62
- entry: indexFile,
63
- isAutoMount: false
64
- };
53
+ if (isHasPages) {
54
+ entrypoint.entry = _path.default.join(dir, _constants.PAGES_DIR_NAME);
55
+ }
56
+ if (isHasNestedRoutes) {
57
+ entrypoint.nestedRoutesEntry = _path.default.join(dir, _constants.NESTED_ROUTES_DIR);
58
+ }
59
+ return entrypoint;
65
60
  }
61
+ return {
62
+ entryName,
63
+ entry: indexFile,
64
+ absoluteEntryDir: _path.default.resolve(dir),
65
+ isAutoMount: false
66
+ };
66
67
  });
67
-
68
68
  const getFileSystemEntry = (appContext, config) => {
69
69
  const {
70
70
  appDirectory
@@ -75,7 +75,6 @@ const getFileSystemEntry = (appContext, config) => {
75
75
  }
76
76
  } = config;
77
77
  const src = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
78
-
79
78
  if (_fs.default.existsSync(src)) {
80
79
  if (_fs.default.statSync(src).isDirectory()) {
81
80
  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)));
@@ -86,5 +85,4 @@ const getFileSystemEntry = (appContext, config) => {
86
85
  throw Error(`src dir ${entriesDir} not found.`);
87
86
  }
88
87
  };
89
-
90
88
  exports.getFileSystemEntry = getFileSystemEntry;
@@ -4,23 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getHtmlTemplate = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _constants = require("./constants");
13
-
14
10
  var templates = _interopRequireWildcard(require("./templates"));
15
-
16
11
  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
12
  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
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
14
  var PartialPosition;
23
-
24
15
  (function (PartialPosition) {
25
16
  PartialPosition["TOP"] = "top";
26
17
  PartialPosition["HEAD"] = "head";
@@ -28,7 +19,6 @@ var PartialPosition;
28
19
  PartialPosition["BOTTOM"] = "bottom";
29
20
  PartialPosition["INDEX"] = "index";
30
21
  })(PartialPosition || (PartialPosition = {}));
31
-
32
22
  const findPartials = (dir, entryName, position) => {
33
23
  if (_utils.fs.existsSync(dir)) {
34
24
  const base = (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${position}${ext}`)));
@@ -38,11 +28,10 @@ const findPartials = (dir, entryName, position) => {
38
28
  content: _utils.fs.readFileSync(file, 'utf8')
39
29
  } : null;
40
30
  }
41
-
42
31
  return null;
43
- }; // generate html template for
44
-
32
+ };
45
33
 
34
+ // generate html template for
46
35
  const getHtmlTemplate = async (entrypoints, api, {
47
36
  appContext,
48
37
  config
@@ -56,18 +45,14 @@ const getHtmlTemplate = async (entrypoints, api, {
56
45
  configDir
57
46
  }
58
47
  } = config;
59
-
60
48
  const htmlDir = _path.default.resolve(appDirectory, configDir, _constants.HTML_PARTIALS_FOLDER);
61
-
62
49
  const htmlTemplates = {};
63
-
64
50
  for (const entrypoint of entrypoints) {
65
51
  const {
66
52
  entryName
67
53
  } = entrypoint;
68
54
  const name = entrypoints.length === 1 && entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName;
69
55
  const customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
70
-
71
56
  if (customIndexTemplate) {
72
57
  htmlTemplates[entryName] = customIndexTemplate.file;
73
58
  } else {
@@ -86,21 +71,15 @@ const getHtmlTemplate = async (entrypoints, api, {
86
71
  body: []
87
72
  })
88
73
  });
89
-
90
74
  const templatePath = _path.default.resolve(internalDirectory, entryName, 'index.html');
91
-
92
75
  _utils.fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
93
-
94
76
  htmlTemplates[entryName] = templatePath;
95
77
  const bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
96
-
97
78
  if (bottomTemplate) {
98
79
  htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
99
80
  }
100
81
  }
101
82
  }
102
-
103
83
  return htmlTemplates;
104
84
  };
105
-
106
85
  exports.getHtmlTemplate = getHtmlTemplate;