@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
@@ -4,30 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getServerRoutes = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _fs = _interopRequireDefault(require("fs"));
11
-
12
9
  var _utils = require("@modern-js/utils");
13
-
14
10
  var _utils2 = require("./utils");
15
-
16
11
  const _excluded = ["path"],
17
- _excluded2 = ["path"];
18
-
12
+ _excluded2 = ["path"];
19
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
-
21
14
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
22
-
23
15
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
24
-
25
16
  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; }
26
-
27
17
  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; }
28
-
29
18
  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; }
30
-
31
19
  /**
32
20
  * Add base url for each server route.
33
21
  * @param baseUrl - Base url from server.baseUrl
@@ -47,37 +35,34 @@ const applyBaseUrl = (baseUrl, routes) => {
47
35
  });
48
36
  }
49
37
  }
50
-
51
38
  return routes;
52
39
  };
40
+
53
41
  /**
54
42
  *
55
43
  * @param original - Original entrypoint route info.
56
44
  * @param routeOptions - Custom entrypoint route config from server.routes.
57
45
  * @returns
58
46
  */
59
-
60
-
61
47
  const applyRouteOptions = (original, routeOptions) => {
62
48
  const {
63
49
  route,
64
50
  disableSpa
65
51
  } = routeOptions;
66
- original.isSPA = !disableSpa; // set entryPath as dir
52
+ original.isSPA = !disableSpa;
67
53
 
54
+ // set entryPath as dir
68
55
  !original.isSPA && (original.entryPath = _path.default.dirname(original.entryPath));
69
56
  let routes;
70
-
71
57
  if (route) {
72
58
  if (Array.isArray(route)) {
73
59
  routes = route.map(url => {
74
60
  if ((0, _utils.isPlainObject)(url)) {
75
61
  const _ref = url,
76
- {
77
- path: urlPath
78
- } = _ref,
79
- other = _objectWithoutProperties(_ref, _excluded);
80
-
62
+ {
63
+ path: urlPath
64
+ } = _ref,
65
+ other = _objectWithoutProperties(_ref, _excluded);
81
66
  return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
82
67
  urlPath
83
68
  });
@@ -89,11 +74,10 @@ const applyRouteOptions = (original, routeOptions) => {
89
74
  });
90
75
  } else if ((0, _utils.isPlainObject)(route)) {
91
76
  const _ref2 = route,
92
- {
93
- path: urlPath
94
- } = _ref2,
95
- other = _objectWithoutProperties(_ref2, _excluded2);
96
-
77
+ {
78
+ path: urlPath
79
+ } = _ref2,
80
+ other = _objectWithoutProperties(_ref2, _excluded2);
97
81
  routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
98
82
  urlPath
99
83
  })];
@@ -105,17 +89,15 @@ const applyRouteOptions = (original, routeOptions) => {
105
89
  } else {
106
90
  routes = [original];
107
91
  }
108
-
109
92
  return routes;
110
93
  };
94
+
111
95
  /**
112
96
  * Collect routes from entrypoints.
113
97
  * @param entrypoints - Bundle entrypoints.
114
98
  * @param config - Normalized user config.
115
99
  * @returns entrypoint Routes
116
100
  */
117
-
118
-
119
101
  const collectHtmlRoutes = (entrypoints, appContext, config) => {
120
102
  const {
121
103
  output: {
@@ -151,27 +133,24 @@ const collectHtmlRoutes = (entrypoints, appContext, config) => {
151
133
  enableModernMode: Boolean(enableModernMode),
152
134
  bundle: isSSR ? `${_utils.SERVER_BUNDLE_DIRECTORY}/${entryName}.js` : undefined
153
135
  };
154
-
155
136
  if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
156
137
  const routeOptions = (0, _utils.isPlainObject)(routes[entryName]) ? routes[entryName] : {
157
138
  route: routes[entryName]
158
139
  };
159
140
  route = applyRouteOptions(route, routeOptions);
160
141
  }
161
-
162
142
  return Array.isArray(route) ? [...previous, ...route] : [...previous, route];
163
143
  }, []);
164
144
  htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
165
145
  return htmlRoutes;
166
146
  };
147
+
167
148
  /**
168
149
  * Collect static public file routes from config/public folder.
169
150
  * @param appContext - App context info.
170
151
  * @param config - normalized user config.
171
152
  * @returns Static public file routes.
172
153
  */
173
-
174
-
175
154
  const collectStaticRoutes = (appContext, config) => {
176
155
  const {
177
156
  appDirectory
@@ -184,9 +163,7 @@ const collectStaticRoutes = (appContext, config) => {
184
163
  publicRoutes = {}
185
164
  }
186
165
  } = config;
187
-
188
166
  const publicFolder = _path.default.resolve(appDirectory, configDir, 'public');
189
-
190
167
  return _fs.default.existsSync(publicFolder) ? (0, _utils2.walkDirectory)(publicFolder).map(filePath => {
191
168
  const urlPath = `${(0, _utils.urlJoin)(toPosix(filePath).slice(toPosix(publicFolder).length))}`;
192
169
  return {
@@ -197,12 +174,9 @@ const collectStaticRoutes = (appContext, config) => {
197
174
  };
198
175
  }) : [];
199
176
  };
200
-
201
177
  const getServerRoutes = (entrypoints, {
202
178
  appContext,
203
179
  config
204
180
  }) => [...collectHtmlRoutes(entrypoints, appContext, config), ...collectStaticRoutes(appContext, config)];
205
-
206
181
  exports.getServerRoutes = getServerRoutes;
207
-
208
182
  const toPosix = pathStr => pathStr.split(_path.default.sep).join(_path.default.posix.sep);
@@ -4,27 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.modifyServerRoutes = exports.modifyFileSystemRoutes = exports.modifyEntryRuntimePlugins = exports.modifyEntryRenderFunction = exports.modifyEntryImports = exports.modifyEntryExport = exports.modifyAsyncEntry = exports.htmlPartials = exports.default = exports.beforeGenerateRoutes = exports.addRuntimeExports = exports.addDefineTypes = void 0;
7
-
8
7
  var path = _interopRequireWildcard(require("path"));
9
-
10
8
  var _plugin = require("@modern-js/plugin");
11
-
12
9
  var _utils = require("@modern-js/utils");
13
-
14
10
  var _lodash = require("@modern-js/utils/lodash");
15
-
11
+ var _builder = require("../builder");
12
+ var _printInstructions = require("../utils/printInstructions");
13
+ var _routes = require("../utils/routes");
14
+ var _config = require("../utils/config");
15
+ var _commands = require("../utils/commands");
16
16
  var _utils2 = require("./utils");
17
-
18
17
  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); }
19
-
20
18
  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; }
21
-
22
19
  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; }
23
-
24
20
  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; }
25
-
26
21
  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; }
27
-
28
22
  const debug = (0, _utils.createDebugger)('plugin-analyze');
29
23
  const modifyEntryImports = (0, _plugin.createAsyncWaterfall)();
30
24
  exports.modifyEntryImports = modifyEntryImports;
@@ -48,7 +42,6 @@ const beforeGenerateRoutes = (0, _plugin.createAsyncWaterfall)();
48
42
  exports.beforeGenerateRoutes = beforeGenerateRoutes;
49
43
  const addDefineTypes = (0, _plugin.createAsyncWaterfall)();
50
44
  exports.addDefineTypes = addDefineTypes;
51
-
52
45
  var _default = () => ({
53
46
  name: '@modern-js/plugin-analyze',
54
47
  registerHook: {
@@ -70,19 +63,16 @@ var _default = () => ({
70
63
  return {
71
64
  async prepare() {
72
65
  var _resolvedConfig$sourc;
73
-
74
- const appContext = api.useAppContext();
66
+ let appContext = api.useAppContext();
75
67
  const resolvedConfig = api.useResolvedConfigContext();
76
68
  const hookRunners = api.useHookRunners();
77
-
78
69
  try {
79
70
  _utils.fs.emptydirSync(appContext.internalDirectory);
80
- } catch (_unused) {// FIXME:
71
+ } catch (_unused) {
72
+ // FIXME:
81
73
  }
82
-
83
74
  const apiOnly = await (0, _utils.isApiOnly)(appContext.appDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$sourc = resolvedConfig.source) === null || _resolvedConfig$sourc === void 0 ? void 0 : _resolvedConfig$sourc.entriesDir);
84
75
  await hookRunners.addRuntimeExports();
85
-
86
76
  if (apiOnly) {
87
77
  const {
88
78
  routes
@@ -90,13 +80,13 @@ var _default = () => ({
90
80
  routes: []
91
81
  });
92
82
  debug(`server routes: %o`, routes);
93
- api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
83
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
94
84
  apiOnly,
95
85
  serverRoutes: routes
96
- }));
86
+ });
87
+ api.setAppContext(appContext);
97
88
  return;
98
89
  }
99
-
100
90
  const [{
101
91
  getBundleEntry
102
92
  }, {
@@ -119,11 +109,13 @@ var _default = () => ({
119
109
  routes: initialRoutes
120
110
  });
121
111
  debug(`server routes: %o`, routes);
122
- api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
112
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
123
113
  entrypoints,
124
114
  serverRoutes: routes
125
- }));
126
- pagesDir = entrypoints.map(point => point.entry);
115
+ });
116
+ api.setAppContext(appContext);
117
+ const nestedRouteEntries = entrypoints.map(point => point.nestedRoutesEntry).filter(Boolean);
118
+ pagesDir = entrypoints.map(point => point.entry).filter(Boolean).concat(nestedRouteEntries);
127
119
  originEntrypoints = (0, _lodash.cloneDeep)(entrypoints);
128
120
  await generateCode(appContext, resolvedConfig, entrypoints, api);
129
121
  const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
@@ -133,19 +125,80 @@ var _default = () => ({
133
125
  debug(`html templates: %o`, htmlTemplates);
134
126
  await hookRunners.addDefineTypes();
135
127
  debug(`add Define Types`);
136
- api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
128
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
137
129
  entrypoints,
138
130
  checkedEntries: defaultChecked,
139
131
  apiOnly,
140
132
  serverRoutes: routes,
141
133
  htmlTemplates
142
- }));
134
+ });
135
+ api.setAppContext(appContext);
136
+ const command = (0, _commands.getCommand)();
137
+ const buildCommands = ['dev', 'build', 'inspect', 'deploy'];
138
+ if (buildCommands.includes(command)) {
139
+ const normalizedConfig = api.useResolvedConfigContext();
140
+ const builder = await (0, _builder.createBuilderForEdenX)({
141
+ normalizedConfig,
142
+ appContext,
143
+ compatPluginConfig: {
144
+ async onBeforeBuild({
145
+ bundlerConfigs
146
+ }) {
147
+ const hookRunners = api.useHookRunners();
148
+ await (0, _routes.generateRoutes)(appContext);
149
+ await hookRunners.beforeBuild({
150
+ bundlerConfigs
151
+ });
152
+ },
153
+ async onAfterBuild({
154
+ stats
155
+ }) {
156
+ const hookRunners = api.useHookRunners();
157
+ await hookRunners.afterBuild({
158
+ stats
159
+ });
160
+ await (0, _config.emitResolvedConfig)(appContext.appDirectory, normalizedConfig);
161
+ },
162
+ async onDevCompileDone({
163
+ isFirstCompile
164
+ }) {
165
+ const hookRunners = api.useHookRunners();
166
+ if (process.stdout.isTTY || isFirstCompile) {
167
+ hookRunners.afterDev();
168
+ if (isFirstCompile) {
169
+ (0, _printInstructions.printInstructions)(hookRunners, appContext, normalizedConfig);
170
+ }
171
+ }
172
+ },
173
+ async onBeforeCreateCompiler({
174
+ bundlerConfigs
175
+ }) {
176
+ const hookRunners = api.useHookRunners();
177
+ // run modernjs framework `beforeCreateCompiler` hook
178
+ await hookRunners.beforeCreateCompiler({
179
+ bundlerConfigs
180
+ });
181
+ },
182
+ async onAfterCreateCompiler({
183
+ compiler
184
+ }) {
185
+ const hookRunners = api.useHookRunners();
186
+ // run modernjs framework afterCreateCompiler hooks
187
+ await hookRunners.afterCreateCompiler({
188
+ compiler
189
+ });
190
+ }
191
+ }
192
+ });
193
+ appContext = _objectSpread(_objectSpread({}, appContext), {}, {
194
+ builder
195
+ });
196
+ api.setAppContext(appContext);
197
+ }
143
198
  },
144
-
145
199
  watchFiles() {
146
200
  return pagesDir;
147
201
  },
148
-
149
202
  async fileChange(e) {
150
203
  const appContext = api.useAppContext();
151
204
  const {
@@ -155,12 +208,9 @@ var _default = () => ({
155
208
  filename,
156
209
  eventType
157
210
  } = e;
158
-
159
211
  const isPageFile = name => pagesDir.some(pageDir => name.includes(pageDir));
160
-
161
212
  const absoluteFilePath = path.resolve(appDirectory, filename);
162
213
  const isRouteComponent = isPageFile(absoluteFilePath) && (0, _utils2.isRouteComponentFile)(absoluteFilePath);
163
-
164
214
  if (isRouteComponent && (eventType === 'add' || eventType === 'unlink')) {
165
215
  const resolvedConfig = api.useResolvedConfigContext();
166
216
  const {
@@ -170,9 +220,7 @@ var _default = () => ({
170
220
  generateCode(appContext, resolvedConfig, entrypoints, api);
171
221
  }
172
222
  }
173
-
174
223
  };
175
224
  }
176
225
  });
177
-
178
226
  exports.default = _default;
@@ -4,28 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.isDefaultExportFunction = void 0;
7
-
8
7
  var _fs = _interopRequireDefault(require("fs"));
9
-
10
8
  var _parser = require("@babel/parser");
11
-
12
9
  var _traverse = _interopRequireDefault(require("@babel/traverse"));
13
-
14
10
  var t = _interopRequireWildcard(require("@babel/types"));
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
  const isFunction = node => t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
23
-
24
15
  const isDefaultExportFunction = file => {
25
16
  if (!file || !_fs.default.existsSync(file)) {
26
17
  return false;
27
18
  }
28
-
29
19
  const ast = (0, _parser.parse)(_fs.default.readFileSync(file, 'utf8'), {
30
20
  sourceType: 'unambiguous',
31
21
  plugins: ['jsx', 'typescript', 'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'decorators-legacy', 'functionBind', 'classPrivateMethods', ['pipelineOperator', {
@@ -38,7 +28,6 @@ const isDefaultExportFunction = file => {
38
28
  const {
39
29
  declaration
40
30
  } = path.node;
41
-
42
31
  if (isFunction(declaration)) {
43
32
  isExportFunction = true;
44
33
  }
@@ -46,5 +35,4 @@ const isDefaultExportFunction = file => {
46
35
  });
47
36
  return isExportFunction;
48
37
  };
49
-
50
38
  exports.isDefaultExportFunction = isDefaultExportFunction;
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.makeLegalIdentifier = makeLegalIdentifier;
7
-
8
7
  /**
9
8
  * modified from https://github.com/rollup/plugins/blob/master/packages/pluginutils
10
9
  * license at https://github.com/rollup/plugins/blob/master/LICENSE
@@ -12,13 +11,10 @@ exports.makeLegalIdentifier = makeLegalIdentifier;
12
11
  const 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';
13
12
  const 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';
14
13
  const forbidList = new Set(`${reservedWords} ${builtins}`.split(' '));
15
-
16
14
  function makeLegalIdentifier(str) {
17
15
  const identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');
18
-
19
16
  if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
20
17
  return `_${identifier}`;
21
18
  }
22
-
23
19
  return identifier || '_';
24
20
  }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.walk = void 0;
7
+ var path = _interopRequireWildcard(require("path"));
8
+ var _utils = require("@modern-js/utils");
9
+ var _constants = require("./constants");
10
+ var _utils2 = require("./utils");
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); }
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; }
13
+ 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; }
14
+ 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; }
15
+ 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; }
16
+ const LAYOUT_FILE = 'layout';
17
+ const PAGE_FILE = 'page';
18
+ const LOADING_FILE = 'loading';
19
+ const ERROR_FILE = 'error';
20
+ const LOADER_FILE = 'loader';
21
+ const conventionNames = [LAYOUT_FILE, PAGE_FILE, LOADING_FILE, ERROR_FILE, LOADER_FILE];
22
+ const replaceDynamicPath = routePath => {
23
+ return routePath.replace(/\[(.*?)\]/g, ':$1');
24
+ };
25
+ const createIndexRoute = (routeInfo, rootDir, filename, entryName) => {
26
+ return createRoute(_objectSpread(_objectSpread({}, routeInfo), {}, {
27
+ index: true,
28
+ children: undefined
29
+ }), rootDir, filename, entryName);
30
+ };
31
+ const createRoute = (routeInfo, rootDir, filename, entryName) => {
32
+ const id = (0, _utils.getRouteId)(filename, rootDir, entryName);
33
+ return _objectSpread(_objectSpread({}, routeInfo), {}, {
34
+ id,
35
+ type: 'nested'
36
+ });
37
+ };
38
+ const walk = async (dirname, rootDir, alias, entryName) => {
39
+ if (!(await _utils.fs.pathExists(dirname))) {
40
+ return null;
41
+ }
42
+ const isDirectory = (await _utils.fs.stat(dirname)).isDirectory();
43
+ if (!isDirectory) {
44
+ return null;
45
+ }
46
+ const relativeDir = path.relative(rootDir, dirname);
47
+ const pathSegments = relativeDir.split(path.sep);
48
+ const lastSegment = pathSegments[pathSegments.length - 1];
49
+ const isRoot = lastSegment === '';
50
+ const isPathlessLayout = lastSegment.startsWith('__');
51
+ const isWithoutLayoutPath = lastSegment.includes('.');
52
+ let routePath = isRoot || isPathlessLayout ? '/' : `${lastSegment}`;
53
+ if (isWithoutLayoutPath) {
54
+ routePath = lastSegment.split('.').join('/');
55
+ }
56
+ routePath = replaceDynamicPath(routePath);
57
+ const route = {
58
+ path: routePath,
59
+ children: []
60
+ };
61
+ const items = await _utils.fs.readdir(dirname);
62
+ for (const item of items) {
63
+ const itemPath = path.join(dirname, item);
64
+ const extname = path.extname(item);
65
+ const itemWithoutExt = item.slice(0, -extname.length);
66
+ const isDirectory = (await _utils.fs.stat(itemPath)).isDirectory();
67
+ if (isDirectory) {
68
+ const childRoute = await walk(itemPath, rootDir, alias, entryName);
69
+ if (childRoute) {
70
+ var _route$children;
71
+ (_route$children = route.children) === null || _route$children === void 0 ? void 0 : _route$children.push(childRoute);
72
+ }
73
+ }
74
+ if (extname && (!_constants.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
75
+ continue;
76
+ }
77
+ if (itemWithoutExt === LAYOUT_FILE) {
78
+ route._component = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
79
+ }
80
+ if (itemWithoutExt === PAGE_FILE) {
81
+ var _route$children2;
82
+ const childRoute = createIndexRoute({
83
+ _component: (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name)
84
+ }, rootDir, itemPath, entryName);
85
+ (_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.push(childRoute);
86
+ }
87
+ if (itemWithoutExt === LOADER_FILE) {
88
+ route.loader = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
89
+ }
90
+ if (itemWithoutExt === LOADING_FILE) {
91
+ route.loading = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
92
+ }
93
+ if (itemWithoutExt === ERROR_FILE) {
94
+ route.error = (0, _utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
95
+ }
96
+ }
97
+ const finalRoute = createRoute(route, rootDir, path.join(dirname, `${LAYOUT_FILE}.ts`), entryName);
98
+
99
+ /**
100
+ * when the url is /, the __auth/layout.tsx component should not be rendered
101
+ * - routes
102
+ * - __auth
103
+ * - layout.tsx
104
+ * - layout.tsx
105
+ */
106
+ if (isPathlessLayout) {
107
+ delete finalRoute.path;
108
+ }
109
+ return finalRoute;
110
+ };
111
+ exports.walk = walk;