@modern-js/app-tools 1.21.3 → 2.0.0-beta.0

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 (146) hide show
  1. package/CHANGELOG.md +138 -16
  2. package/dist/js/modern/analyze/constants.js +1 -0
  3. package/dist/js/modern/analyze/generateCode.js +118 -16
  4. package/dist/js/modern/analyze/getBundleEntry.js +3 -2
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +198 -0
  6. package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +8 -28
  7. package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
  8. package/dist/js/modern/analyze/getClientRoutes/utils.js +21 -0
  9. package/dist/js/modern/analyze/getFileSystemEntry.js +31 -12
  10. package/dist/js/modern/analyze/index.js +2 -1
  11. package/dist/js/modern/analyze/nestedRoutes.js +127 -0
  12. package/dist/js/modern/analyze/templates.js +164 -10
  13. package/dist/js/modern/analyze/utils.js +2 -1
  14. package/dist/js/modern/builder/builderPlugins/compatModern.js +226 -0
  15. package/dist/js/modern/builder/createHtmlConfig.js +58 -0
  16. package/dist/js/modern/builder/createOutputConfig.js +67 -0
  17. package/dist/js/modern/builder/createSourceConfig.js +85 -0
  18. package/dist/js/modern/builder/createToolsConfig.js +94 -0
  19. package/dist/js/modern/builder/index.js +106 -0
  20. package/dist/js/modern/builder/share.js +48 -0
  21. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +37 -0
  22. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +44 -0
  23. package/dist/js/modern/commands/build.js +41 -95
  24. package/dist/js/modern/commands/dev.js +15 -19
  25. package/dist/js/modern/commands/inspect.js +17 -58
  26. package/dist/js/modern/commands/start.js +2 -1
  27. package/dist/js/modern/index.js +14 -4
  28. package/dist/js/modern/locale/en.js +0 -1
  29. package/dist/js/modern/locale/zh.js +0 -1
  30. package/dist/js/modern/utils/commands.js +5 -0
  31. package/dist/js/modern/utils/createCompiler.js +37 -40
  32. package/dist/js/modern/utils/createServer.js +16 -0
  33. package/dist/js/modern/utils/printInstructions.js +1 -1
  34. package/dist/js/node/analyze/constants.js +3 -1
  35. package/dist/js/node/analyze/generateCode.js +121 -14
  36. package/dist/js/node/analyze/getBundleEntry.js +3 -2
  37. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +221 -0
  38. package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +16 -35
  39. package/dist/js/node/analyze/getClientRoutes/index.js +21 -0
  40. package/dist/js/node/analyze/getClientRoutes/utils.js +42 -0
  41. package/dist/js/node/analyze/getFileSystemEntry.js +30 -11
  42. package/dist/js/node/analyze/index.js +2 -1
  43. package/dist/js/node/analyze/nestedRoutes.js +144 -0
  44. package/dist/js/node/analyze/templates.js +167 -10
  45. package/dist/js/node/analyze/utils.js +6 -2
  46. package/dist/js/node/builder/builderPlugins/compatModern.js +245 -0
  47. package/dist/js/node/builder/createHtmlConfig.js +76 -0
  48. package/dist/js/node/builder/createOutputConfig.js +75 -0
  49. package/dist/js/node/builder/createSourceConfig.js +99 -0
  50. package/dist/js/node/builder/createToolsConfig.js +103 -0
  51. package/dist/js/node/builder/index.js +129 -0
  52. package/dist/js/node/builder/share.js +63 -0
  53. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
  54. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
  55. package/dist/js/node/commands/build.js +44 -98
  56. package/dist/js/node/commands/dev.js +13 -23
  57. package/dist/js/node/commands/inspect.js +18 -74
  58. package/dist/js/node/commands/start.js +3 -1
  59. package/dist/js/node/index.js +19 -2
  60. package/dist/js/node/locale/en.js +0 -1
  61. package/dist/js/node/locale/zh.js +0 -1
  62. package/dist/js/node/utils/commands.js +14 -0
  63. package/dist/js/node/utils/createCompiler.js +43 -49
  64. package/dist/js/node/utils/createServer.js +21 -2
  65. package/dist/js/node/utils/printInstructions.js +1 -1
  66. package/dist/js/treeshaking/analyze/constants.js +16 -0
  67. package/dist/js/treeshaking/analyze/generateCode.js +412 -0
  68. package/dist/js/treeshaking/analyze/getBundleEntry.js +77 -0
  69. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +210 -0
  70. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +211 -0
  71. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
  72. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +27 -0
  73. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +109 -0
  74. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +153 -0
  75. package/dist/js/treeshaking/analyze/getServerRoutes.js +170 -0
  76. package/dist/js/treeshaking/analyze/index.js +214 -0
  77. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +32 -0
  78. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +18 -0
  79. package/dist/js/treeshaking/analyze/nestedRoutes.js +207 -0
  80. package/dist/js/treeshaking/analyze/templates.js +189 -0
  81. package/dist/js/treeshaking/analyze/utils.js +95 -0
  82. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +231 -0
  83. package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
  84. package/dist/js/treeshaking/builder/createOutputConfig.js +67 -0
  85. package/dist/js/treeshaking/builder/createSourceConfig.js +100 -0
  86. package/dist/js/treeshaking/builder/createToolsConfig.js +90 -0
  87. package/dist/js/treeshaking/builder/index.js +172 -0
  88. package/dist/js/treeshaking/builder/share.js +50 -0
  89. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +58 -0
  90. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
  91. package/dist/js/treeshaking/commands/build.js +168 -0
  92. package/dist/js/treeshaking/commands/deploy.js +29 -0
  93. package/dist/js/treeshaking/commands/dev.js +146 -0
  94. package/dist/js/treeshaking/commands/index.js +3 -0
  95. package/dist/js/treeshaking/commands/inspect.js +52 -0
  96. package/dist/js/treeshaking/commands/start.js +78 -0
  97. package/dist/js/treeshaking/exports/server.js +1 -0
  98. package/dist/js/treeshaking/hooks.js +21 -0
  99. package/dist/js/treeshaking/index.js +304 -0
  100. package/dist/js/treeshaking/locale/en.js +34 -0
  101. package/dist/js/treeshaking/locale/index.js +9 -0
  102. package/dist/js/treeshaking/locale/zh.js +34 -0
  103. package/dist/js/treeshaking/utils/commands.js +5 -0
  104. package/dist/js/treeshaking/utils/config.js +140 -0
  105. package/dist/js/treeshaking/utils/createCompiler.js +117 -0
  106. package/dist/js/treeshaking/utils/createServer.js +83 -0
  107. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +68 -0
  108. package/dist/js/treeshaking/utils/language.js +5 -0
  109. package/dist/js/treeshaking/utils/printInstructions.js +35 -0
  110. package/dist/js/treeshaking/utils/routes.js +33 -0
  111. package/dist/js/treeshaking/utils/types.js +0 -0
  112. package/dist/types/analyze/constants.d.ts +1 -0
  113. package/dist/types/analyze/generateCode.d.ts +1 -1
  114. package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
  115. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
  116. package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
  117. package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
  118. package/dist/types/analyze/index.d.ts +2 -2
  119. package/dist/types/analyze/nestedRoutes.d.ts +5 -0
  120. package/dist/types/analyze/templates.d.ts +17 -3
  121. package/dist/types/analyze/utils.d.ts +2 -1
  122. package/dist/types/builder/builderPlugins/compatModern.d.ts +14 -0
  123. package/dist/types/builder/createHtmlConfig.d.ts +6 -0
  124. package/dist/types/builder/createOutputConfig.d.ts +3 -0
  125. package/dist/types/builder/createSourceConfig.d.ts +5 -0
  126. package/dist/types/builder/createToolsConfig.d.ts +13 -0
  127. package/dist/types/builder/index.d.ts +19 -0
  128. package/dist/types/builder/share.d.ts +26 -0
  129. package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
  130. package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
  131. package/dist/types/commands/build.d.ts +2 -1
  132. package/dist/types/commands/deploy.d.ts +2 -1
  133. package/dist/types/commands/dev.d.ts +2 -1
  134. package/dist/types/commands/inspect.d.ts +5 -6
  135. package/dist/types/commands/start.d.ts +2 -1
  136. package/dist/types/hooks.d.ts +15 -10
  137. package/dist/types/index.d.ts +4 -2
  138. package/dist/types/locale/en.d.ts +0 -1
  139. package/dist/types/locale/index.d.ts +0 -2
  140. package/dist/types/locale/zh.d.ts +0 -1
  141. package/dist/types/utils/commands.d.ts +1 -0
  142. package/dist/types/utils/createCompiler.d.ts +9 -10
  143. package/dist/types/utils/createServer.d.ts +8 -1
  144. package/dist/types/utils/printInstructions.d.ts +3 -2
  145. package/dist/types/utils/types.d.ts +2 -3
  146. package/package.json +25 -43
@@ -0,0 +1,211 @@
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
+
10
+ var compName = function compName(srcDirectory, filePath) {
11
+ var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
12
+ return "Comp_".concat(legalCompName);
13
+ };
14
+
15
+ var layoutNameAbbr = function layoutNameAbbr(filePath) {
16
+ var prefix = 'L_';
17
+ var dirName = path.dirname(filePath).split('/').pop() || '';
18
+ return "".concat(prefix).concat(makeLegalIdentifier(dirName));
19
+ };
20
+
21
+ var parents = [];
22
+ /* eslint-disable no-param-reassign */
23
+
24
+ var recursiveReadDirLegacy = function recursiveReadDirLegacy(_ref) {
25
+ var dir = _ref.dir,
26
+ routes = _ref.routes,
27
+ _ref$basePath = _ref.basePath,
28
+ basePath = _ref$basePath === void 0 ? '/' : _ref$basePath,
29
+ srcDirectory = _ref.srcDirectory,
30
+ srcAlias = _ref.srcAlias;
31
+ var hasDynamicRoute = false;
32
+ var resetParent = false;
33
+ var parent = parents[parents.length - 1];
34
+ var layout = findLayout(dir);
35
+
36
+ if (layout) {
37
+ if (basePath === '/') {
38
+ throw new Error("should use _app instead of _layout in ".concat(dir));
39
+ } else {
40
+ var alias = replaceWithAlias(srcDirectory, layout, srcAlias);
41
+ var componentName = compName(srcDirectory, layout);
42
+ var route = {
43
+ path: "".concat(basePath.substring(0, basePath.length - 1)),
44
+ exact: false,
45
+ routes: [],
46
+ _component: alias,
47
+ component: componentName,
48
+ parent: parent
49
+ };
50
+ parent = route;
51
+ resetParent = true;
52
+ routes.push(route);
53
+ parents.push(route);
54
+ routes = route.routes;
55
+ }
56
+ }
57
+
58
+ var _iterator = _createForOfIteratorHelper(fs.readdirSync(dir)),
59
+ _step;
60
+
61
+ try {
62
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
63
+ var relative = _step.value;
64
+ var filePath = path.join(dir, relative);
65
+
66
+ if (!shouldSkip(filePath)) {
67
+ var filename = path.basename(filePath, path.extname(filePath));
68
+
69
+ var _alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
70
+
71
+ var _componentName = compName(srcDirectory, filePath);
72
+
73
+ var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
74
+
75
+ if (dynamicRouteMatched) {
76
+ if (hasDynamicRoute) {
77
+ throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
78
+ } else {
79
+ hasDynamicRoute = true;
80
+ }
81
+ }
82
+
83
+ var _route = {
84
+ path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1]).concat(dynamicRouteMatched[2]) : filename),
85
+ _component: _alias,
86
+ component: _componentName,
87
+ exact: true,
88
+ parent: parent
89
+ };
90
+
91
+ if (fs.statSync(filePath).isDirectory()) {
92
+ recursiveReadDirLegacy({
93
+ dir: filePath,
94
+ routes: routes,
95
+ basePath: "".concat(_route.path, "/"),
96
+ srcDirectory: srcDirectory,
97
+ srcAlias: srcAlias
98
+ });
99
+ continue;
100
+ }
101
+
102
+ if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
103
+ continue;
104
+ }
105
+
106
+ if (filename === FILE_SYSTEM_ROUTES_INDEX) {
107
+ _route.path = basePath === '/' ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
108
+ }
109
+
110
+ if (filename === '404' && basePath === '/') {
111
+ _route.path = '*';
112
+ _route.exact = false;
113
+ }
114
+
115
+ routes.push(_route);
116
+ }
117
+ }
118
+ } catch (err) {
119
+ _iterator.e(err);
120
+ } finally {
121
+ _iterator.f();
122
+ }
123
+
124
+ if (resetParent) {
125
+ parents.pop();
126
+ }
127
+ };
128
+ /* eslint-enable no-param-reassign */
129
+
130
+
131
+ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
132
+ var flat = function flat(routes) {
133
+ return routes.reduce(function (memo, route) {
134
+ return memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]);
135
+ }, []);
136
+ };
137
+
138
+ var generate = function generate(route) {
139
+ var codes = [];
140
+ var lastComponent = route.component;
141
+ var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")]; // eslint-disable-next-line no-param-reassign, no-cond-assign
142
+
143
+ while (route = route.parent) {
144
+ var layoutComponent = route.component;
145
+ var layoutComponentAbbr = layoutNameAbbr(route._component);
146
+ imports.push("import ".concat(layoutComponent, " from '").concat(route._component, "';"));
147
+ var currentComponent = "".concat(layoutComponentAbbr, "_").concat(lastComponent);
148
+ codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
149
+ lastComponent = currentComponent;
150
+ }
151
+
152
+ var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
153
+ fs.outputFileSync(file, "".concat(imports.join('\n'), "\n").concat(codes.join('\n'), "\nexport default ").concat(lastComponent));
154
+ return {
155
+ component: lastComponent,
156
+ _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
157
+ };
158
+ };
159
+
160
+ var normalized = flat(nested).map(function (route) {
161
+ return route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
162
+ parent: undefined
163
+ }) : _objectSpread(_objectSpread({}, route), {}, {
164
+ parent: undefined
165
+ });
166
+ });
167
+ return normalized;
168
+ };
169
+
170
+ export var getClientRoutes = function getClientRoutes(_ref2) {
171
+ var entrypoint = _ref2.entrypoint,
172
+ srcDirectory = _ref2.srcDirectory,
173
+ srcAlias = _ref2.srcAlias,
174
+ internalDirectory = _ref2.internalDirectory,
175
+ internalDirAlias = _ref2.internalDirAlias;
176
+ var entry = entrypoint.entry,
177
+ entryName = entrypoint.entryName;
178
+
179
+ if (!fs.existsSync(entry)) {
180
+ throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
181
+ }
182
+
183
+ if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
184
+ throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
185
+ }
186
+
187
+ var routes = [];
188
+ recursiveReadDirLegacy({
189
+ dir: entry,
190
+ routes: routes,
191
+ basePath: '/',
192
+ srcDirectory: srcDirectory,
193
+ srcAlias: srcAlias
194
+ });
195
+ var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
196
+ fs.emptyDirSync(internalComponentsDir);
197
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
198
+ parents.length = 0; // FIXME: support more situations
199
+
200
+ routes.sort(function (a, b) {
201
+ var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
202
+
203
+ if (delta === 0) {
204
+ return a.path.length - b.path.length;
205
+ }
206
+
207
+ return delta;
208
+ });
209
+ debug("fileSystem routes: %o", routes);
210
+ return routes;
211
+ };
@@ -0,0 +1,2 @@
1
+ export { getClientRoutes } from "./getRoutes";
2
+ export { getClientRoutes as getClientRoutesLegacy } from "./getRoutesLegacy";
@@ -0,0 +1,27 @@
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
+
20
+ var ext = path.extname(file);
21
+
22
+ if (FILE_SYSTEM_ROUTES_IGNORED_REGEX.test(file) || !JS_EXTENSIONS.includes(ext) || FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT === path.basename(file, ext)) {
23
+ return true;
24
+ }
25
+
26
+ return false;
27
+ };
@@ -0,0 +1,109 @@
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
+
7
+ var hasIndex = function hasIndex(dir) {
8
+ return findExists(JS_EXTENSIONS.map(function (ext) {
9
+ return path.resolve(dir, "".concat(INDEX_FILE_NAME).concat(ext));
10
+ }));
11
+ };
12
+
13
+ var hasApp = function hasApp(dir) {
14
+ return findExists(JS_EXTENSIONS.map(function (ext) {
15
+ return path.resolve(dir, "".concat(APP_FILE_NAME).concat(ext));
16
+ }));
17
+ };
18
+
19
+ var hasPages = function hasPages(dir) {
20
+ return fs.existsSync(path.join(dir, PAGES_DIR_NAME));
21
+ };
22
+
23
+ var hasNestedRoutes = function hasNestedRoutes(dir) {
24
+ return fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
25
+ };
26
+
27
+ var isBundleEntry = function isBundleEntry(dir) {
28
+ return hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
29
+ };
30
+
31
+ var scanDir = function scanDir(dirs) {
32
+ return dirs.map(function (dir) {
33
+ var indexFile = hasIndex(dir);
34
+ var customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
35
+ var entryName = path.basename(dir);
36
+
37
+ if (indexFile && !customBootstrap) {
38
+ return {
39
+ entryName: entryName,
40
+ entry: indexFile,
41
+ isAutoMount: false
42
+ };
43
+ }
44
+
45
+ var isHasApp = hasApp(dir);
46
+
47
+ if (isHasApp) {
48
+ return {
49
+ entryName: entryName,
50
+ entry: path.join(dir, APP_FILE_NAME),
51
+ isAutoMount: true,
52
+ customBootstrap: customBootstrap
53
+ };
54
+ }
55
+
56
+ var isHasNestedRoutes = hasNestedRoutes(dir);
57
+ var isHasPages = hasPages(dir);
58
+
59
+ if (isHasNestedRoutes || isHasPages) {
60
+ var entrypoint = {
61
+ entryName: entryName,
62
+ entry: '',
63
+ fileSystemRoutes: {
64
+ globalApp: findExists(JS_EXTENSIONS.map(function (ext) {
65
+ return path.resolve(dir, "./".concat(PAGES_DIR_NAME, "/").concat(FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT).concat(ext));
66
+ }))
67
+ },
68
+ isAutoMount: true,
69
+ customBootstrap: customBootstrap
70
+ };
71
+
72
+ if (isHasPages) {
73
+ entrypoint.entry = path.join(dir, PAGES_DIR_NAME);
74
+ }
75
+
76
+ if (isHasNestedRoutes) {
77
+ entrypoint.nestedRoutesEntry = path.join(dir, NESTED_ROUTES_DIR);
78
+ }
79
+
80
+ return entrypoint;
81
+ }
82
+
83
+ return {
84
+ entryName: entryName,
85
+ entry: indexFile,
86
+ isAutoMount: false
87
+ };
88
+ });
89
+ };
90
+
91
+ export var getFileSystemEntry = function getFileSystemEntry(appContext, config) {
92
+ var appDirectory = appContext.appDirectory;
93
+ var entriesDir = config.source.entriesDir;
94
+ var src = ensureAbsolutePath(appDirectory, entriesDir);
95
+
96
+ if (fs.existsSync(src)) {
97
+ if (fs.statSync(src).isDirectory()) {
98
+ return scanDir(isBundleEntry(src) ? [src] : fs.readdirSync(src).map(function (file) {
99
+ return path.join(src, file);
100
+ }).filter(function (file) {
101
+ return fs.statSync(file).isDirectory() && isBundleEntry(file);
102
+ }));
103
+ } else {
104
+ throw Error("source.entriesDir accept a directory.");
105
+ }
106
+ } else {
107
+ throw Error("src dir ".concat(entriesDir, " not found."));
108
+ }
109
+ };
@@ -0,0 +1,153 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ import path from 'path';
5
+ import { fs, findExists, MAIN_ENTRY_NAME } from '@modern-js/utils';
6
+ import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from "./constants";
7
+ import * as templates from "./templates";
8
+ var PartialPosition;
9
+
10
+ (function (PartialPosition) {
11
+ PartialPosition["TOP"] = "top";
12
+ PartialPosition["HEAD"] = "head";
13
+ PartialPosition["BODY"] = "body";
14
+ PartialPosition["BOTTOM"] = "bottom";
15
+ PartialPosition["INDEX"] = "index";
16
+ })(PartialPosition || (PartialPosition = {}));
17
+
18
+ var findPartials = function findPartials(dir, entryName, position) {
19
+ if (fs.existsSync(dir)) {
20
+ var base = findExists(HTML_PARTIALS_EXTENSIONS.map(function (ext) {
21
+ return path.resolve(dir, "".concat(position).concat(ext));
22
+ }));
23
+ var file = entryName ? findExists(HTML_PARTIALS_EXTENSIONS.map(function (ext) {
24
+ return path.resolve(dir, entryName, "".concat(position).concat(ext));
25
+ })) || base : base;
26
+ return file ? {
27
+ file: file,
28
+ content: fs.readFileSync(file, 'utf8')
29
+ } : null;
30
+ }
31
+
32
+ return null;
33
+ }; // generate html template for
34
+
35
+
36
+ export var getHtmlTemplate = /*#__PURE__*/function () {
37
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(entrypoints, api, _ref) {
38
+ var appContext, config, appDirectory, internalDirectory, configDir, htmlDir, htmlTemplates, _iterator, _step, _loop;
39
+
40
+ return _regeneratorRuntime().wrap(function _callee$(_context2) {
41
+ while (1) {
42
+ switch (_context2.prev = _context2.next) {
43
+ case 0:
44
+ appContext = _ref.appContext, config = _ref.config;
45
+ appDirectory = appContext.appDirectory, internalDirectory = appContext.internalDirectory;
46
+ configDir = config.source.configDir;
47
+ htmlDir = path.resolve(appDirectory, configDir, HTML_PARTIALS_FOLDER);
48
+ htmlTemplates = {};
49
+ _iterator = _createForOfIteratorHelper(entrypoints);
50
+ _context2.prev = 6;
51
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
52
+ var entrypoint, entryName, name, customIndexTemplate, hookRunners, _yield$hookRunners$ht, partials, templatePath, bottomTemplate;
53
+
54
+ return _regeneratorRuntime().wrap(function _loop$(_context) {
55
+ while (1) {
56
+ switch (_context.prev = _context.next) {
57
+ case 0:
58
+ entrypoint = _step.value;
59
+ entryName = entrypoint.entryName;
60
+ name = entrypoints.length === 1 && entryName === MAIN_ENTRY_NAME ? '' : entryName;
61
+ customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
62
+
63
+ if (!customIndexTemplate) {
64
+ _context.next = 8;
65
+ break;
66
+ }
67
+
68
+ htmlTemplates[entryName] = customIndexTemplate.file;
69
+ _context.next = 18;
70
+ break;
71
+
72
+ case 8:
73
+ hookRunners = api.useHookRunners();
74
+ _context.next = 11;
75
+ return hookRunners.htmlPartials({
76
+ entrypoint: entrypoint,
77
+ partials: [PartialPosition.TOP, PartialPosition.HEAD, PartialPosition.BODY].reduce(function (previous, position) {
78
+ var found = findPartials(htmlDir, name, position);
79
+ previous[position] = found ? [found.content] : [];
80
+ return previous;
81
+ }, {
82
+ top: [],
83
+ head: [],
84
+ body: []
85
+ })
86
+ });
87
+
88
+ case 11:
89
+ _yield$hookRunners$ht = _context.sent;
90
+ partials = _yield$hookRunners$ht.partials;
91
+ templatePath = path.resolve(internalDirectory, entryName, 'index.html');
92
+ fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
93
+ htmlTemplates[entryName] = templatePath;
94
+ bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
95
+
96
+ if (bottomTemplate) {
97
+ htmlTemplates["__".concat(entryName, "-bottom__")] = bottomTemplate.content;
98
+ }
99
+
100
+ case 18:
101
+ case "end":
102
+ return _context.stop();
103
+ }
104
+ }
105
+ }, _loop);
106
+ });
107
+
108
+ _iterator.s();
109
+
110
+ case 9:
111
+ if ((_step = _iterator.n()).done) {
112
+ _context2.next = 13;
113
+ break;
114
+ }
115
+
116
+ return _context2.delegateYield(_loop(), "t0", 11);
117
+
118
+ case 11:
119
+ _context2.next = 9;
120
+ break;
121
+
122
+ case 13:
123
+ _context2.next = 18;
124
+ break;
125
+
126
+ case 15:
127
+ _context2.prev = 15;
128
+ _context2.t1 = _context2["catch"](6);
129
+
130
+ _iterator.e(_context2.t1);
131
+
132
+ case 18:
133
+ _context2.prev = 18;
134
+
135
+ _iterator.f();
136
+
137
+ return _context2.finish(18);
138
+
139
+ case 21:
140
+ return _context2.abrupt("return", htmlTemplates);
141
+
142
+ case 22:
143
+ case "end":
144
+ return _context2.stop();
145
+ }
146
+ }
147
+ }, _callee, null, [[6, 15, 18, 21]]);
148
+ }));
149
+
150
+ return function getHtmlTemplate(_x, _x2, _x3) {
151
+ return _ref2.apply(this, arguments);
152
+ };
153
+ }();
@@ -0,0 +1,170 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
+ var _excluded = ["path"],
5
+ _excluded2 = ["path"];
6
+ import path from 'path';
7
+ import fs from 'fs';
8
+ import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY, MAIN_ENTRY_NAME, removeTailSlash } from '@modern-js/utils';
9
+ import { walkDirectory } from "./utils";
10
+ /**
11
+ * Add base url for each server route.
12
+ * @param baseUrl - Base url from server.baseUrl
13
+ * @param routes - Server routes.
14
+ * @returns Server routes with baseUrl prefixed.
15
+ */
16
+
17
+ var applyBaseUrl = function applyBaseUrl(baseUrl, routes) {
18
+ if (baseUrl) {
19
+ if (Array.isArray(baseUrl)) {
20
+ return baseUrl.reduce(function (previous, current) {
21
+ return [].concat(_toConsumableArray(previous), _toConsumableArray(applyBaseUrl(current, routes)));
22
+ }, []);
23
+ } else {
24
+ return routes.map(function (route) {
25
+ var urlPath = urlJoin(baseUrl, route.urlPath);
26
+ return _objectSpread(_objectSpread({}, route), {}, {
27
+ urlPath: urlPath === '/' ? urlPath : removeTailSlash(urlPath)
28
+ });
29
+ });
30
+ }
31
+ }
32
+
33
+ return routes;
34
+ };
35
+ /**
36
+ *
37
+ * @param original - Original entrypoint route info.
38
+ * @param routeOptions - Custom entrypoint route config from server.routes.
39
+ * @returns
40
+ */
41
+
42
+
43
+ var applyRouteOptions = function applyRouteOptions(original, routeOptions) {
44
+ var route = routeOptions.route,
45
+ disableSpa = routeOptions.disableSpa;
46
+ original.isSPA = !disableSpa; // set entryPath as dir
47
+
48
+ !original.isSPA && (original.entryPath = path.dirname(original.entryPath));
49
+ var routes;
50
+
51
+ if (route) {
52
+ if (Array.isArray(route)) {
53
+ routes = route.map(function (url) {
54
+ if (isPlainObject(url)) {
55
+ var _ref = url,
56
+ urlPath = _ref.path,
57
+ other = _objectWithoutProperties(_ref, _excluded);
58
+
59
+ return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
60
+ urlPath: urlPath
61
+ });
62
+ } else {
63
+ return _objectSpread(_objectSpread({}, original), {}, {
64
+ urlPath: url
65
+ });
66
+ }
67
+ });
68
+ } else if (isPlainObject(route)) {
69
+ var _ref2 = route,
70
+ urlPath = _ref2.path,
71
+ other = _objectWithoutProperties(_ref2, _excluded2);
72
+
73
+ routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
74
+ urlPath: urlPath
75
+ })];
76
+ } else {
77
+ routes = [_objectSpread(_objectSpread({}, original), {}, {
78
+ urlPath: route
79
+ })];
80
+ }
81
+ } else {
82
+ routes = [original];
83
+ }
84
+
85
+ return routes;
86
+ };
87
+ /**
88
+ * Collect routes from entrypoints.
89
+ * @param entrypoints - Bundle entrypoints.
90
+ * @param config - Normalized user config.
91
+ * @returns entrypoint Routes
92
+ */
93
+
94
+
95
+ var collectHtmlRoutes = function collectHtmlRoutes(entrypoints, appContext, config) {
96
+ var _config$output = config.output,
97
+ htmlPath = _config$output.htmlPath,
98
+ disableHtmlFolder = _config$output.disableHtmlFolder,
99
+ enableModernMode = _config$output.enableModernMode,
100
+ _config$server = config.server,
101
+ baseUrl = _config$server.baseUrl,
102
+ routes = _config$server.routes,
103
+ ssr = _config$server.ssr,
104
+ ssrByEntries = _config$server.ssrByEntries;
105
+ var packageName = appContext.packageName;
106
+ var htmlRoutes = entrypoints.reduce(function (previous, _ref3) {
107
+ var entryName = _ref3.entryName;
108
+ var entryOptions = getEntryOptions(entryName, ssr, ssrByEntries, packageName);
109
+ var isSSR = Boolean(entryOptions);
110
+
111
+ var _ref4 = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {},
112
+ resHeaders = _ref4.resHeaders;
113
+
114
+ var route = {
115
+ urlPath: "/".concat(entryName === MAIN_ENTRY_NAME ? '' : entryName),
116
+ entryName: entryName,
117
+ entryPath: removeLeadingSlash(path.posix.normalize("".concat(htmlPath, "/").concat(entryName).concat(disableHtmlFolder ? '.html' : '/index.html'))),
118
+ isSPA: true,
119
+ isSSR: isSSR,
120
+ responseHeaders: resHeaders,
121
+ enableModernMode: Boolean(enableModernMode),
122
+ bundle: isSSR ? "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(entryName, ".js") : undefined
123
+ };
124
+
125
+ if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
126
+ var routeOptions = isPlainObject(routes[entryName]) ? routes[entryName] : {
127
+ route: routes[entryName]
128
+ };
129
+ route = applyRouteOptions(route, routeOptions);
130
+ }
131
+
132
+ return Array.isArray(route) ? [].concat(_toConsumableArray(previous), _toConsumableArray(route)) : [].concat(_toConsumableArray(previous), [route]);
133
+ }, []);
134
+ htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
135
+ return htmlRoutes;
136
+ };
137
+ /**
138
+ * Collect static public file routes from config/public folder.
139
+ * @param appContext - App context info.
140
+ * @param config - normalized user config.
141
+ * @returns Static public file routes.
142
+ */
143
+
144
+
145
+ var collectStaticRoutes = function collectStaticRoutes(appContext, config) {
146
+ var appDirectory = appContext.appDirectory;
147
+ var configDir = config.source.configDir,
148
+ _config$server$public = config.server.publicRoutes,
149
+ publicRoutes = _config$server$public === void 0 ? {} : _config$server$public;
150
+ var publicFolder = path.resolve(appDirectory, configDir, 'public');
151
+ return fs.existsSync(publicFolder) ? walkDirectory(publicFolder).map(function (filePath) {
152
+ var urlPath = "".concat(urlJoin(toPosix(filePath).slice(toPosix(publicFolder).length)));
153
+ return {
154
+ urlPath: publicRoutes[removeLeadingSlash(urlPath)] || urlPath,
155
+ isSPA: true,
156
+ isSSR: false,
157
+ entryPath: toPosix(path.relative(path.resolve(appDirectory, configDir), filePath))
158
+ };
159
+ }) : [];
160
+ };
161
+
162
+ export var getServerRoutes = function getServerRoutes(entrypoints, _ref5) {
163
+ var appContext = _ref5.appContext,
164
+ config = _ref5.config;
165
+ return [].concat(_toConsumableArray(collectHtmlRoutes(entrypoints, appContext, config)), _toConsumableArray(collectStaticRoutes(appContext, config)));
166
+ };
167
+
168
+ var toPosix = function toPosix(pathStr) {
169
+ return pathStr.split(path.sep).join(path.posix.sep);
170
+ };