@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,129 @@
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
+ (function (PartialPosition) {
10
+ PartialPosition["TOP"] = "top";
11
+ PartialPosition["HEAD"] = "head";
12
+ PartialPosition["BODY"] = "body";
13
+ PartialPosition["BOTTOM"] = "bottom";
14
+ PartialPosition["INDEX"] = "index";
15
+ })(PartialPosition || (PartialPosition = {}));
16
+ var findPartials = function findPartials(dir, entryName, position) {
17
+ if (fs.existsSync(dir)) {
18
+ var base = findExists(HTML_PARTIALS_EXTENSIONS.map(function (ext) {
19
+ return path.resolve(dir, "".concat(position).concat(ext));
20
+ }));
21
+ var file = entryName ? findExists(HTML_PARTIALS_EXTENSIONS.map(function (ext) {
22
+ return path.resolve(dir, entryName, "".concat(position).concat(ext));
23
+ })) || base : base;
24
+ return file ? {
25
+ file: file,
26
+ content: fs.readFileSync(file, 'utf8')
27
+ } : null;
28
+ }
29
+ return null;
30
+ };
31
+
32
+ // generate html template for
33
+ export var getHtmlTemplate = /*#__PURE__*/function () {
34
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(entrypoints, api, _ref) {
35
+ var appContext, config, appDirectory, internalDirectory, configDir, htmlDir, htmlTemplates, _iterator, _step, _loop;
36
+ return _regeneratorRuntime().wrap(function _callee$(_context2) {
37
+ while (1) {
38
+ switch (_context2.prev = _context2.next) {
39
+ case 0:
40
+ appContext = _ref.appContext, config = _ref.config;
41
+ appDirectory = appContext.appDirectory, internalDirectory = appContext.internalDirectory;
42
+ configDir = config.source.configDir;
43
+ htmlDir = path.resolve(appDirectory, configDir, HTML_PARTIALS_FOLDER);
44
+ htmlTemplates = {};
45
+ _iterator = _createForOfIteratorHelper(entrypoints);
46
+ _context2.prev = 6;
47
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
48
+ var entrypoint, entryName, name, customIndexTemplate, hookRunners, _yield$hookRunners$ht, partials, templatePath, bottomTemplate;
49
+ return _regeneratorRuntime().wrap(function _loop$(_context) {
50
+ while (1) {
51
+ switch (_context.prev = _context.next) {
52
+ case 0:
53
+ entrypoint = _step.value;
54
+ entryName = entrypoint.entryName;
55
+ name = entrypoints.length === 1 && entryName === MAIN_ENTRY_NAME ? '' : entryName;
56
+ customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
57
+ if (!customIndexTemplate) {
58
+ _context.next = 8;
59
+ break;
60
+ }
61
+ htmlTemplates[entryName] = customIndexTemplate.file;
62
+ _context.next = 18;
63
+ break;
64
+ case 8:
65
+ hookRunners = api.useHookRunners();
66
+ _context.next = 11;
67
+ return hookRunners.htmlPartials({
68
+ entrypoint: entrypoint,
69
+ partials: [PartialPosition.TOP, PartialPosition.HEAD, PartialPosition.BODY].reduce(function (previous, position) {
70
+ var found = findPartials(htmlDir, name, position);
71
+ previous[position] = found ? [found.content] : [];
72
+ return previous;
73
+ }, {
74
+ top: [],
75
+ head: [],
76
+ body: []
77
+ })
78
+ });
79
+ case 11:
80
+ _yield$hookRunners$ht = _context.sent;
81
+ partials = _yield$hookRunners$ht.partials;
82
+ templatePath = path.resolve(internalDirectory, entryName, 'index.html');
83
+ fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
84
+ htmlTemplates[entryName] = templatePath;
85
+ bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
86
+ if (bottomTemplate) {
87
+ htmlTemplates["__".concat(entryName, "-bottom__")] = bottomTemplate.content;
88
+ }
89
+ case 18:
90
+ case "end":
91
+ return _context.stop();
92
+ }
93
+ }
94
+ }, _loop);
95
+ });
96
+ _iterator.s();
97
+ case 9:
98
+ if ((_step = _iterator.n()).done) {
99
+ _context2.next = 13;
100
+ break;
101
+ }
102
+ return _context2.delegateYield(_loop(), "t0", 11);
103
+ case 11:
104
+ _context2.next = 9;
105
+ break;
106
+ case 13:
107
+ _context2.next = 18;
108
+ break;
109
+ case 15:
110
+ _context2.prev = 15;
111
+ _context2.t1 = _context2["catch"](6);
112
+ _iterator.e(_context2.t1);
113
+ case 18:
114
+ _context2.prev = 18;
115
+ _iterator.f();
116
+ return _context2.finish(18);
117
+ case 21:
118
+ return _context2.abrupt("return", htmlTemplates);
119
+ case 22:
120
+ case "end":
121
+ return _context2.stop();
122
+ }
123
+ }
124
+ }, _callee, null, [[6, 15, 18, 21]]);
125
+ }));
126
+ return function getHtmlTemplate(_x, _x2, _x3) {
127
+ return _ref2.apply(this, arguments);
128
+ };
129
+ }();
@@ -0,0 +1,157 @@
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
+ /**
12
+ * Add base url for each server route.
13
+ * @param baseUrl - Base url from server.baseUrl
14
+ * @param routes - Server routes.
15
+ * @returns Server routes with baseUrl prefixed.
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
+ return routes;
33
+ };
34
+
35
+ /**
36
+ *
37
+ * @param original - Original entrypoint route info.
38
+ * @param routeOptions - Custom entrypoint route config from server.routes.
39
+ * @returns
40
+ */
41
+ var applyRouteOptions = function applyRouteOptions(original, routeOptions) {
42
+ var route = routeOptions.route,
43
+ disableSpa = routeOptions.disableSpa;
44
+ original.isSPA = !disableSpa;
45
+
46
+ // set entryPath as dir
47
+ !original.isSPA && (original.entryPath = path.dirname(original.entryPath));
48
+ var routes;
49
+ if (route) {
50
+ if (Array.isArray(route)) {
51
+ routes = route.map(function (url) {
52
+ if (isPlainObject(url)) {
53
+ var _ref = url,
54
+ urlPath = _ref.path,
55
+ other = _objectWithoutProperties(_ref, _excluded);
56
+ return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
57
+ urlPath: urlPath
58
+ });
59
+ } else {
60
+ return _objectSpread(_objectSpread({}, original), {}, {
61
+ urlPath: url
62
+ });
63
+ }
64
+ });
65
+ } else if (isPlainObject(route)) {
66
+ var _ref2 = route,
67
+ urlPath = _ref2.path,
68
+ other = _objectWithoutProperties(_ref2, _excluded2);
69
+ routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
70
+ urlPath: urlPath
71
+ })];
72
+ } else {
73
+ routes = [_objectSpread(_objectSpread({}, original), {}, {
74
+ urlPath: route
75
+ })];
76
+ }
77
+ } else {
78
+ routes = [original];
79
+ }
80
+ return routes;
81
+ };
82
+
83
+ /**
84
+ * Collect routes from entrypoints.
85
+ * @param entrypoints - Bundle entrypoints.
86
+ * @param config - Normalized user config.
87
+ * @returns entrypoint Routes
88
+ */
89
+ var collectHtmlRoutes = function collectHtmlRoutes(entrypoints, appContext, config) {
90
+ var _config$output = config.output,
91
+ htmlPath = _config$output.htmlPath,
92
+ disableHtmlFolder = _config$output.disableHtmlFolder,
93
+ enableModernMode = _config$output.enableModernMode,
94
+ _config$server = config.server,
95
+ baseUrl = _config$server.baseUrl,
96
+ routes = _config$server.routes,
97
+ ssr = _config$server.ssr,
98
+ ssrByEntries = _config$server.ssrByEntries;
99
+ var packageName = appContext.packageName;
100
+ var htmlRoutes = entrypoints.reduce(function (previous, _ref3) {
101
+ var entryName = _ref3.entryName;
102
+ var entryOptions = getEntryOptions(entryName, ssr, ssrByEntries, packageName);
103
+ var isSSR = Boolean(entryOptions);
104
+ var _ref4 = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {},
105
+ resHeaders = _ref4.resHeaders;
106
+ var route = {
107
+ urlPath: "/".concat(entryName === MAIN_ENTRY_NAME ? '' : entryName),
108
+ entryName: entryName,
109
+ entryPath: removeLeadingSlash(path.posix.normalize("".concat(htmlPath, "/").concat(entryName).concat(disableHtmlFolder ? '.html' : '/index.html'))),
110
+ isSPA: true,
111
+ isSSR: isSSR,
112
+ responseHeaders: resHeaders,
113
+ enableModernMode: Boolean(enableModernMode),
114
+ bundle: isSSR ? "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(entryName, ".js") : undefined
115
+ };
116
+ if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
117
+ var routeOptions = isPlainObject(routes[entryName]) ? routes[entryName] : {
118
+ route: routes[entryName]
119
+ };
120
+ route = applyRouteOptions(route, routeOptions);
121
+ }
122
+ return Array.isArray(route) ? [].concat(_toConsumableArray(previous), _toConsumableArray(route)) : [].concat(_toConsumableArray(previous), [route]);
123
+ }, []);
124
+ htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
125
+ return htmlRoutes;
126
+ };
127
+
128
+ /**
129
+ * Collect static public file routes from config/public folder.
130
+ * @param appContext - App context info.
131
+ * @param config - normalized user config.
132
+ * @returns Static public file routes.
133
+ */
134
+ var collectStaticRoutes = function collectStaticRoutes(appContext, config) {
135
+ var appDirectory = appContext.appDirectory;
136
+ var configDir = config.source.configDir,
137
+ _config$server$public = config.server.publicRoutes,
138
+ publicRoutes = _config$server$public === void 0 ? {} : _config$server$public;
139
+ var publicFolder = path.resolve(appDirectory, configDir, 'public');
140
+ return fs.existsSync(publicFolder) ? walkDirectory(publicFolder).map(function (filePath) {
141
+ var urlPath = "".concat(urlJoin(toPosix(filePath).slice(toPosix(publicFolder).length)));
142
+ return {
143
+ urlPath: publicRoutes[removeLeadingSlash(urlPath)] || urlPath,
144
+ isSPA: true,
145
+ isSSR: false,
146
+ entryPath: toPosix(path.relative(path.resolve(appDirectory, configDir), filePath))
147
+ };
148
+ }) : [];
149
+ };
150
+ export var getServerRoutes = function getServerRoutes(entrypoints, _ref5) {
151
+ var appContext = _ref5.appContext,
152
+ config = _ref5.config;
153
+ return [].concat(_toConsumableArray(collectHtmlRoutes(entrypoints, appContext, config)), _toConsumableArray(collectStaticRoutes(appContext, config)));
154
+ };
155
+ var toPosix = function toPosix(pathStr) {
156
+ return pathStr.split(path.sep).join(path.posix.sep);
157
+ };
@@ -0,0 +1,334 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
+ import * as path from 'path';
6
+ import { createAsyncWaterfall } from '@modern-js/plugin';
7
+ import { createDebugger, fs, isApiOnly } from '@modern-js/utils';
8
+ import { cloneDeep } from '@modern-js/utils/lodash';
9
+ import { createBuilderForEdenX } from "../builder";
10
+ import { printInstructions } from "../utils/printInstructions";
11
+ import { generateRoutes } from "../utils/routes";
12
+ import { emitResolvedConfig } from "../utils/config";
13
+ import { getCommand } from "../utils/commands";
14
+ import { isRouteComponentFile } from "./utils";
15
+ var debug = createDebugger('plugin-analyze');
16
+ export var modifyEntryImports = createAsyncWaterfall();
17
+ export var modifyEntryExport = createAsyncWaterfall();
18
+ export var addRuntimeExports = createAsyncWaterfall();
19
+ export var modifyEntryRuntimePlugins = createAsyncWaterfall();
20
+ export var modifyEntryRenderFunction = createAsyncWaterfall();
21
+ export var modifyAsyncEntry = createAsyncWaterfall();
22
+ export var modifyFileSystemRoutes = createAsyncWaterfall();
23
+ export var modifyServerRoutes = createAsyncWaterfall();
24
+ export var htmlPartials = createAsyncWaterfall();
25
+ export var beforeGenerateRoutes = createAsyncWaterfall();
26
+ export var addDefineTypes = createAsyncWaterfall();
27
+ export default (function () {
28
+ return {
29
+ name: '@modern-js/plugin-analyze',
30
+ registerHook: {
31
+ modifyAsyncEntry: modifyAsyncEntry,
32
+ modifyEntryImports: modifyEntryImports,
33
+ modifyEntryExport: modifyEntryExport,
34
+ modifyEntryRuntimePlugins: modifyEntryRuntimePlugins,
35
+ modifyEntryRenderFunction: modifyEntryRenderFunction,
36
+ modifyFileSystemRoutes: modifyFileSystemRoutes,
37
+ modifyServerRoutes: modifyServerRoutes,
38
+ htmlPartials: htmlPartials,
39
+ addRuntimeExports: addRuntimeExports,
40
+ beforeGenerateRoutes: beforeGenerateRoutes,
41
+ addDefineTypes: addDefineTypes
42
+ },
43
+ setup: function setup(api) {
44
+ var pagesDir = [];
45
+ var originEntrypoints = [];
46
+ return {
47
+ prepare: function prepare() {
48
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
49
+ var _resolvedConfig$sourc;
50
+ var appContext, resolvedConfig, hookRunners, apiOnly, _yield$hookRunners$mo, _routes, _yield$Promise$all, _yield$Promise$all2, getBundleEntry, getServerRoutes, generateCode, getHtmlTemplate, entrypoints, defaultChecked, initialRoutes, _yield$hookRunners$mo2, routes, nestedRouteEntries, htmlTemplates, command, buildCommands, normalizedConfig, builder;
51
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
52
+ while (1) {
53
+ switch (_context6.prev = _context6.next) {
54
+ case 0:
55
+ appContext = api.useAppContext();
56
+ resolvedConfig = api.useResolvedConfigContext();
57
+ hookRunners = api.useHookRunners();
58
+ try {
59
+ fs.emptydirSync(appContext.internalDirectory);
60
+ } catch (_unused) {
61
+ // FIXME:
62
+ }
63
+ _context6.next = 6;
64
+ return isApiOnly(appContext.appDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$sourc = resolvedConfig.source) === null || _resolvedConfig$sourc === void 0 ? void 0 : _resolvedConfig$sourc.entriesDir);
65
+ case 6:
66
+ apiOnly = _context6.sent;
67
+ _context6.next = 9;
68
+ return hookRunners.addRuntimeExports();
69
+ case 9:
70
+ if (!apiOnly) {
71
+ _context6.next = 18;
72
+ break;
73
+ }
74
+ _context6.next = 12;
75
+ return hookRunners.modifyServerRoutes({
76
+ routes: []
77
+ });
78
+ case 12:
79
+ _yield$hookRunners$mo = _context6.sent;
80
+ _routes = _yield$hookRunners$mo.routes;
81
+ debug("server routes: %o", _routes);
82
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
83
+ apiOnly: apiOnly,
84
+ serverRoutes: _routes
85
+ });
86
+ api.setAppContext(appContext);
87
+ return _context6.abrupt("return");
88
+ case 18:
89
+ _context6.next = 20;
90
+ return Promise.all([import("./getBundleEntry"), import("./getServerRoutes"), import("./generateCode"), import("./getHtmlTemplate")]);
91
+ case 20:
92
+ _yield$Promise$all = _context6.sent;
93
+ _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 4);
94
+ getBundleEntry = _yield$Promise$all2[0].getBundleEntry;
95
+ getServerRoutes = _yield$Promise$all2[1].getServerRoutes;
96
+ generateCode = _yield$Promise$all2[2].generateCode;
97
+ getHtmlTemplate = _yield$Promise$all2[3].getHtmlTemplate;
98
+ entrypoints = getBundleEntry(appContext, resolvedConfig);
99
+ defaultChecked = entrypoints.map(function (point) {
100
+ return point.entryName;
101
+ });
102
+ debug("entrypoints: %o", entrypoints);
103
+ initialRoutes = getServerRoutes(entrypoints, {
104
+ appContext: appContext,
105
+ config: resolvedConfig
106
+ });
107
+ _context6.next = 32;
108
+ return hookRunners.modifyServerRoutes({
109
+ routes: initialRoutes
110
+ });
111
+ case 32:
112
+ _yield$hookRunners$mo2 = _context6.sent;
113
+ routes = _yield$hookRunners$mo2.routes;
114
+ debug("server routes: %o", routes);
115
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
116
+ entrypoints: entrypoints,
117
+ serverRoutes: routes
118
+ });
119
+ api.setAppContext(appContext);
120
+ nestedRouteEntries = entrypoints.map(function (point) {
121
+ return point.nestedRoutesEntry;
122
+ }).filter(Boolean);
123
+ pagesDir = entrypoints.map(function (point) {
124
+ return point.entry;
125
+ }).filter(Boolean).concat(nestedRouteEntries);
126
+ originEntrypoints = cloneDeep(entrypoints);
127
+ _context6.next = 42;
128
+ return generateCode(appContext, resolvedConfig, entrypoints, api);
129
+ case 42:
130
+ _context6.next = 44;
131
+ return getHtmlTemplate(entrypoints, api, {
132
+ appContext: appContext,
133
+ config: resolvedConfig
134
+ });
135
+ case 44:
136
+ htmlTemplates = _context6.sent;
137
+ debug("html templates: %o", htmlTemplates);
138
+ _context6.next = 48;
139
+ return hookRunners.addDefineTypes();
140
+ case 48:
141
+ debug("add Define Types");
142
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
143
+ entrypoints: entrypoints,
144
+ checkedEntries: defaultChecked,
145
+ apiOnly: apiOnly,
146
+ serverRoutes: routes,
147
+ htmlTemplates: htmlTemplates
148
+ });
149
+ api.setAppContext(appContext);
150
+ command = getCommand();
151
+ buildCommands = ['dev', 'build', 'inspect', 'deploy'];
152
+ if (!buildCommands.includes(command)) {
153
+ _context6.next = 60;
154
+ break;
155
+ }
156
+ normalizedConfig = api.useResolvedConfigContext();
157
+ _context6.next = 57;
158
+ return createBuilderForEdenX({
159
+ normalizedConfig: normalizedConfig,
160
+ appContext: appContext,
161
+ compatPluginConfig: {
162
+ onBeforeBuild: function onBeforeBuild(_ref) {
163
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
164
+ var bundlerConfigs, hookRunners;
165
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
166
+ while (1) {
167
+ switch (_context.prev = _context.next) {
168
+ case 0:
169
+ bundlerConfigs = _ref.bundlerConfigs;
170
+ hookRunners = api.useHookRunners();
171
+ _context.next = 4;
172
+ return generateRoutes(appContext);
173
+ case 4:
174
+ _context.next = 6;
175
+ return hookRunners.beforeBuild({
176
+ bundlerConfigs: bundlerConfigs
177
+ });
178
+ case 6:
179
+ case "end":
180
+ return _context.stop();
181
+ }
182
+ }
183
+ }, _callee);
184
+ }))();
185
+ },
186
+ onAfterBuild: function onAfterBuild(_ref2) {
187
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
188
+ var stats, hookRunners;
189
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
190
+ while (1) {
191
+ switch (_context2.prev = _context2.next) {
192
+ case 0:
193
+ stats = _ref2.stats;
194
+ hookRunners = api.useHookRunners();
195
+ _context2.next = 4;
196
+ return hookRunners.afterBuild({
197
+ stats: stats
198
+ });
199
+ case 4:
200
+ _context2.next = 6;
201
+ return emitResolvedConfig(appContext.appDirectory, normalizedConfig);
202
+ case 6:
203
+ case "end":
204
+ return _context2.stop();
205
+ }
206
+ }
207
+ }, _callee2);
208
+ }))();
209
+ },
210
+ onDevCompileDone: function onDevCompileDone(_ref3) {
211
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
212
+ var isFirstCompile, hookRunners;
213
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
214
+ while (1) {
215
+ switch (_context3.prev = _context3.next) {
216
+ case 0:
217
+ isFirstCompile = _ref3.isFirstCompile;
218
+ hookRunners = api.useHookRunners();
219
+ if (process.stdout.isTTY || isFirstCompile) {
220
+ hookRunners.afterDev();
221
+ if (isFirstCompile) {
222
+ printInstructions(hookRunners, appContext, normalizedConfig);
223
+ }
224
+ }
225
+ case 3:
226
+ case "end":
227
+ return _context3.stop();
228
+ }
229
+ }
230
+ }, _callee3);
231
+ }))();
232
+ },
233
+ onBeforeCreateCompiler: function onBeforeCreateCompiler(_ref4) {
234
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
235
+ var bundlerConfigs, hookRunners;
236
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
237
+ while (1) {
238
+ switch (_context4.prev = _context4.next) {
239
+ case 0:
240
+ bundlerConfigs = _ref4.bundlerConfigs;
241
+ hookRunners = api.useHookRunners(); // run modernjs framework `beforeCreateCompiler` hook
242
+ _context4.next = 4;
243
+ return hookRunners.beforeCreateCompiler({
244
+ bundlerConfigs: bundlerConfigs
245
+ });
246
+ case 4:
247
+ case "end":
248
+ return _context4.stop();
249
+ }
250
+ }
251
+ }, _callee4);
252
+ }))();
253
+ },
254
+ onAfterCreateCompiler: function onAfterCreateCompiler(_ref5) {
255
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
256
+ var compiler, hookRunners;
257
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
258
+ while (1) {
259
+ switch (_context5.prev = _context5.next) {
260
+ case 0:
261
+ compiler = _ref5.compiler;
262
+ hookRunners = api.useHookRunners(); // run modernjs framework afterCreateCompiler hooks
263
+ _context5.next = 4;
264
+ return hookRunners.afterCreateCompiler({
265
+ compiler: compiler
266
+ });
267
+ case 4:
268
+ case "end":
269
+ return _context5.stop();
270
+ }
271
+ }
272
+ }, _callee5);
273
+ }))();
274
+ }
275
+ }
276
+ });
277
+ case 57:
278
+ builder = _context6.sent;
279
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
280
+ builder: builder
281
+ });
282
+ api.setAppContext(appContext);
283
+ case 60:
284
+ case "end":
285
+ return _context6.stop();
286
+ }
287
+ }
288
+ }, _callee6);
289
+ }))();
290
+ },
291
+ watchFiles: function watchFiles() {
292
+ return pagesDir;
293
+ },
294
+ fileChange: function fileChange(e) {
295
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
296
+ var appContext, appDirectory, filename, eventType, isPageFile, absoluteFilePath, isRouteComponent, resolvedConfig, _yield$import, generateCode, entrypoints;
297
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
298
+ while (1) {
299
+ switch (_context7.prev = _context7.next) {
300
+ case 0:
301
+ appContext = api.useAppContext();
302
+ appDirectory = appContext.appDirectory;
303
+ filename = e.filename, eventType = e.eventType;
304
+ isPageFile = function isPageFile(name) {
305
+ return pagesDir.some(function (pageDir) {
306
+ return name.includes(pageDir);
307
+ });
308
+ };
309
+ absoluteFilePath = path.resolve(appDirectory, filename);
310
+ isRouteComponent = isPageFile(absoluteFilePath) && isRouteComponentFile(absoluteFilePath);
311
+ if (!(isRouteComponent && (eventType === 'add' || eventType === 'unlink'))) {
312
+ _context7.next = 14;
313
+ break;
314
+ }
315
+ resolvedConfig = api.useResolvedConfigContext();
316
+ _context7.next = 10;
317
+ return import("./generateCode");
318
+ case 10:
319
+ _yield$import = _context7.sent;
320
+ generateCode = _yield$import.generateCode;
321
+ entrypoints = cloneDeep(originEntrypoints);
322
+ generateCode(appContext, resolvedConfig, entrypoints, api);
323
+ case 14:
324
+ case "end":
325
+ return _context7.stop();
326
+ }
327
+ }
328
+ }, _callee7);
329
+ }))();
330
+ }
331
+ };
332
+ }
333
+ };
334
+ });
@@ -0,0 +1,28 @@
1
+ import fs from 'fs';
2
+ import { parse } from '@babel/parser';
3
+ import traverse from '@babel/traverse';
4
+ import * as t from '@babel/types';
5
+ var isFunction = function isFunction(node) {
6
+ return t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
7
+ };
8
+ export var isDefaultExportFunction = function isDefaultExportFunction(file) {
9
+ if (!file || !fs.existsSync(file)) {
10
+ return false;
11
+ }
12
+ var ast = parse(fs.readFileSync(file, 'utf8'), {
13
+ sourceType: 'unambiguous',
14
+ plugins: ['jsx', 'typescript', 'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'decorators-legacy', 'functionBind', 'classPrivateMethods', ['pipelineOperator', {
15
+ proposal: 'minimal'
16
+ }], 'optionalChaining', 'optionalCatchBinding', 'objectRestSpread', 'numericSeparator']
17
+ });
18
+ var isExportFunction = false;
19
+ traverse(ast, {
20
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(path) {
21
+ var declaration = path.node.declaration;
22
+ if (isFunction(declaration)) {
23
+ isExportFunction = true;
24
+ }
25
+ }
26
+ });
27
+ return isExportFunction;
28
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * modified from https://github.com/rollup/plugins/blob/master/packages/pluginutils
3
+ * license at https://github.com/rollup/plugins/blob/master/LICENSE
4
+ */
5
+ var reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
6
+ var builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
7
+ var forbidList = new Set("".concat(reservedWords, " ").concat(builtins).split(' '));
8
+ export function makeLegalIdentifier(str) {
9
+ var identifier = str.replace(/-(\w)/g, function (_, letter) {
10
+ return letter.toUpperCase();
11
+ }).replace(/[^$_a-zA-Z0-9]/g, '_');
12
+ if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
13
+ return "_".concat(identifier);
14
+ }
15
+ return identifier || '_';
16
+ }