@modern-js/utils 2.12.0 → 2.12.1-alpha.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 (228) hide show
  1. package/dist/{alias.js → cjs/alias.js} +4 -15
  2. package/dist/{analyzeProject.js → cjs/analyzeProject.js} +5 -25
  3. package/dist/{babel.js → cjs/babel.js} +9 -16
  4. package/dist/{compatRequire.js → cjs/compatRequire.js} +4 -17
  5. package/dist/cjs/emptyDir.js +32 -0
  6. package/dist/{getEntryOptions.js → cjs/getEntryOptions.js} +1 -15
  7. package/dist/{getPackageManager.js → cjs/getPackageManager.js} +19 -41
  8. package/dist/{getPort.js → cjs/getPort.js} +4 -24
  9. package/dist/{getServerConfig.js → cjs/getServerConfig.js} +2 -22
  10. package/dist/{logger.js → cjs/logger.js} +5 -16
  11. package/dist/{monorepo.js → cjs/monorepo.js} +4 -15
  12. package/dist/cjs/nodeEnv.js +55 -0
  13. package/dist/{path.js → cjs/path.js} +1 -1
  14. package/dist/{pathSerializer.js → cjs/pathSerializer.js} +1 -1
  15. package/dist/{react.js → cjs/react.js} +4 -15
  16. package/dist/{runtimeExports.js → cjs/runtimeExports.js} +1 -1
  17. package/dist/{universal → cjs/universal}/nestedRoutes.js +2 -19
  18. package/dist/cjs/version.js +59 -0
  19. package/dist/{watch.js → cjs/watch.js} +9 -29
  20. package/dist/esm/FileSizeReporter.js +180 -0
  21. package/dist/esm/alias.js +83 -0
  22. package/dist/esm/analyzeProject.js +168 -0
  23. package/dist/esm/applyOptionsChain.js +26 -0
  24. package/dist/esm/babel.js +169 -0
  25. package/dist/esm/chainId.js +92 -0
  26. package/dist/esm/clearConsole.js +6 -0
  27. package/dist/esm/commands.js +17 -0
  28. package/dist/esm/compatRequire.js +68 -0
  29. package/dist/esm/compiled.js +34 -0
  30. package/dist/esm/constants.js +317 -0
  31. package/dist/esm/debug.js +5 -0
  32. package/dist/esm/emptyDir.js +158 -0
  33. package/dist/esm/ensureAbsolutePath.js +5 -0
  34. package/dist/esm/ensureArray.js +9 -0
  35. package/dist/esm/findExists.js +27 -0
  36. package/dist/esm/generateMetaTags.js +40 -0
  37. package/dist/esm/getBrowserslist.js +12 -0
  38. package/dist/esm/getCoreJsVersion.js +57 -0
  39. package/dist/esm/getEntryOptions.js +45 -0
  40. package/dist/esm/getPackageManager.js +187 -0
  41. package/dist/esm/getPort.js +215 -0
  42. package/dist/esm/getServerConfig.js +145 -0
  43. package/dist/esm/getTargetDir.js +6 -0
  44. package/dist/esm/import.js +8 -0
  45. package/dist/esm/index.js +44 -0
  46. package/dist/esm/is/index.js +85 -0
  47. package/dist/esm/is/nodeEnv.js +16 -0
  48. package/dist/esm/is/platform.js +7 -0
  49. package/dist/esm/is/type.js +25 -0
  50. package/dist/esm/logger.js +207 -0
  51. package/dist/esm/monorepo.js +109 -0
  52. package/dist/esm/nodeEnv.js +258 -0
  53. package/dist/esm/path.js +85 -0
  54. package/dist/esm/pathSerializer.js +85 -0
  55. package/dist/esm/plugin.js +45 -0
  56. package/dist/esm/prettyInstructions.js +96 -0
  57. package/dist/esm/printBuildError.js +24 -0
  58. package/dist/esm/react.js +43 -0
  59. package/dist/esm/readTsConfig.js +10 -0
  60. package/dist/esm/removeSlash.js +10 -0
  61. package/dist/esm/resolve.js +42 -0
  62. package/dist/esm/routes.js +19 -0
  63. package/dist/esm/runtimeExports.js +65 -0
  64. package/dist/esm/ssr.js +3 -0
  65. package/dist/esm/storage.js +36 -0
  66. package/dist/esm/testUtils.js +13 -0
  67. package/dist/esm/types.js +1 -0
  68. package/dist/esm/universal/constants.js +3 -0
  69. package/dist/esm/universal/formatWebpack.js +108 -0
  70. package/dist/esm/universal/nestedRoutes.js +174 -0
  71. package/dist/esm/universal/remixRouter.js +1 -0
  72. package/dist/esm/universal/serialize.js +7 -0
  73. package/dist/esm/version.js +193 -0
  74. package/dist/esm/wait.js +7 -0
  75. package/dist/esm/watch.js +230 -0
  76. package/dist/esm-node/FileSizeReporter.js +133 -0
  77. package/dist/esm-node/alias.js +61 -0
  78. package/dist/esm-node/analyzeProject.js +17 -0
  79. package/dist/esm-node/applyOptionsChain.js +35 -0
  80. package/dist/esm-node/babel.js +105 -0
  81. package/dist/esm-node/chainId.js +173 -0
  82. package/dist/esm-node/clearConsole.js +8 -0
  83. package/dist/esm-node/commands.js +22 -0
  84. package/dist/esm-node/compatRequire.js +42 -0
  85. package/dist/esm-node/compiled.js +77 -0
  86. package/dist/esm-node/constants.js +264 -0
  87. package/dist/esm-node/debug.js +5 -0
  88. package/dist/esm-node/emptyDir.js +9 -0
  89. package/dist/esm-node/ensureAbsolutePath.js +5 -0
  90. package/dist/esm-node/ensureArray.js +9 -0
  91. package/dist/esm-node/findExists.js +12 -0
  92. package/dist/esm-node/generateMetaTags.js +38 -0
  93. package/dist/esm-node/getBrowserslist.js +7 -0
  94. package/dist/esm-node/getCoreJsVersion.js +13 -0
  95. package/dist/esm-node/getEntryOptions.js +17 -0
  96. package/dist/esm-node/getPackageManager.js +32 -0
  97. package/dist/esm-node/getPort.js +59 -0
  98. package/dist/esm-node/getServerConfig.js +14 -0
  99. package/dist/esm-node/getTargetDir.js +8 -0
  100. package/dist/esm-node/import.js +9 -0
  101. package/dist/esm-node/index.js +44 -0
  102. package/dist/esm-node/is/index.js +71 -0
  103. package/dist/esm-node/is/nodeEnv.js +12 -0
  104. package/dist/esm-node/is/platform.js +6 -0
  105. package/dist/esm-node/is/type.js +34 -0
  106. package/dist/esm-node/logger.js +100 -0
  107. package/dist/esm-node/monorepo.js +87 -0
  108. package/dist/esm-node/nodeEnv.js +30 -0
  109. package/dist/esm-node/path.js +50 -0
  110. package/dist/esm-node/pathSerializer.js +46 -0
  111. package/dist/esm-node/plugin.js +23 -0
  112. package/dist/esm-node/prettyInstructions.js +77 -0
  113. package/dist/esm-node/printBuildError.js +34 -0
  114. package/dist/esm-node/react.js +20 -0
  115. package/dist/esm-node/readTsConfig.js +13 -0
  116. package/dist/esm-node/removeSlash.js +8 -0
  117. package/dist/esm-node/resolve.js +38 -0
  118. package/dist/esm-node/routes.js +24 -0
  119. package/dist/esm-node/runtimeExports.js +40 -0
  120. package/dist/esm-node/ssr.js +6 -0
  121. package/dist/esm-node/storage.js +42 -0
  122. package/dist/esm-node/testUtils.js +15 -0
  123. package/dist/esm-node/types.js +0 -0
  124. package/dist/esm-node/universal/constants.js +6 -0
  125. package/dist/esm-node/universal/formatWebpack.js +77 -0
  126. package/dist/esm-node/universal/nestedRoutes.js +93 -0
  127. package/dist/esm-node/universal/remixRouter.js +1 -0
  128. package/dist/esm-node/universal/serialize.js +7 -0
  129. package/dist/esm-node/version.js +25 -0
  130. package/dist/esm-node/wait.js +6 -0
  131. package/dist/esm-node/watch.js +36 -0
  132. package/package.json +2 -2
  133. package/dist/emptyDir.js +0 -52
  134. package/dist/nodeEnv.js +0 -81
  135. package/dist/version.js +0 -92
  136. /package/dist/{FileSizeReporter.js → cjs/FileSizeReporter.js} +0 -0
  137. /package/dist/{applyOptionsChain.js → cjs/applyOptionsChain.js} +0 -0
  138. /package/dist/{chainId.js → cjs/chainId.js} +0 -0
  139. /package/dist/{clearConsole.js → cjs/clearConsole.js} +0 -0
  140. /package/dist/{commands.js → cjs/commands.js} +0 -0
  141. /package/dist/{compiled.js → cjs/compiled.js} +0 -0
  142. /package/dist/{constants.js → cjs/constants.js} +0 -0
  143. /package/dist/{debug.js → cjs/debug.js} +0 -0
  144. /package/dist/{ensureAbsolutePath.js → cjs/ensureAbsolutePath.js} +0 -0
  145. /package/dist/{ensureArray.js → cjs/ensureArray.js} +0 -0
  146. /package/dist/{findExists.js → cjs/findExists.js} +0 -0
  147. /package/dist/{generateMetaTags.js → cjs/generateMetaTags.js} +0 -0
  148. /package/dist/{getBrowserslist.js → cjs/getBrowserslist.js} +0 -0
  149. /package/dist/{getCoreJsVersion.js → cjs/getCoreJsVersion.js} +0 -0
  150. /package/dist/{getTargetDir.js → cjs/getTargetDir.js} +0 -0
  151. /package/dist/{import.js → cjs/import.js} +0 -0
  152. /package/dist/{index.js → cjs/index.js} +0 -0
  153. /package/dist/{is → cjs/is}/index.js +0 -0
  154. /package/dist/{is → cjs/is}/nodeEnv.js +0 -0
  155. /package/dist/{is → cjs/is}/platform.js +0 -0
  156. /package/dist/{is → cjs/is}/type.js +0 -0
  157. /package/dist/{plugin.js → cjs/plugin.js} +0 -0
  158. /package/dist/{prettyInstructions.js → cjs/prettyInstructions.js} +0 -0
  159. /package/dist/{printBuildError.js → cjs/printBuildError.js} +0 -0
  160. /package/dist/{readTsConfig.js → cjs/readTsConfig.js} +0 -0
  161. /package/dist/{removeSlash.js → cjs/removeSlash.js} +0 -0
  162. /package/dist/{resolve.js → cjs/resolve.js} +0 -0
  163. /package/dist/{routes.js → cjs/routes.js} +0 -0
  164. /package/dist/{ssr.js → cjs/ssr.js} +0 -0
  165. /package/dist/{storage.js → cjs/storage.js} +0 -0
  166. /package/dist/{testUtils.js → cjs/testUtils.js} +0 -0
  167. /package/dist/{types.js → cjs/types.js} +0 -0
  168. /package/dist/{universal → cjs/universal}/constants.js +0 -0
  169. /package/dist/{universal → cjs/universal}/formatWebpack.js +0 -0
  170. /package/dist/{universal → cjs/universal}/remixRouter.js +0 -0
  171. /package/dist/{universal → cjs/universal}/serialize.js +0 -0
  172. /package/dist/{wait.js → cjs/wait.js} +0 -0
  173. /package/dist/{FileSizeReporter.d.ts → types/FileSizeReporter.d.ts} +0 -0
  174. /package/dist/{alias.d.ts → types/alias.d.ts} +0 -0
  175. /package/dist/{analyzeProject.d.ts → types/analyzeProject.d.ts} +0 -0
  176. /package/dist/{applyOptionsChain.d.ts → types/applyOptionsChain.d.ts} +0 -0
  177. /package/dist/{babel.d.ts → types/babel.d.ts} +0 -0
  178. /package/dist/{chainId.d.ts → types/chainId.d.ts} +0 -0
  179. /package/dist/{clearConsole.d.ts → types/clearConsole.d.ts} +0 -0
  180. /package/dist/{commands.d.ts → types/commands.d.ts} +0 -0
  181. /package/dist/{compatRequire.d.ts → types/compatRequire.d.ts} +0 -0
  182. /package/dist/{compiled.d.ts → types/compiled.d.ts} +0 -0
  183. /package/dist/{constants.d.ts → types/constants.d.ts} +0 -0
  184. /package/dist/{debug.d.ts → types/debug.d.ts} +0 -0
  185. /package/dist/{emptyDir.d.ts → types/emptyDir.d.ts} +0 -0
  186. /package/dist/{ensureAbsolutePath.d.ts → types/ensureAbsolutePath.d.ts} +0 -0
  187. /package/dist/{ensureArray.d.ts → types/ensureArray.d.ts} +0 -0
  188. /package/dist/{findExists.d.ts → types/findExists.d.ts} +0 -0
  189. /package/dist/{generateMetaTags.d.ts → types/generateMetaTags.d.ts} +0 -0
  190. /package/dist/{getBrowserslist.d.ts → types/getBrowserslist.d.ts} +0 -0
  191. /package/dist/{getCoreJsVersion.d.ts → types/getCoreJsVersion.d.ts} +0 -0
  192. /package/dist/{getEntryOptions.d.ts → types/getEntryOptions.d.ts} +0 -0
  193. /package/dist/{getPackageManager.d.ts → types/getPackageManager.d.ts} +0 -0
  194. /package/dist/{getPort.d.ts → types/getPort.d.ts} +0 -0
  195. /package/dist/{getServerConfig.d.ts → types/getServerConfig.d.ts} +0 -0
  196. /package/dist/{getTargetDir.d.ts → types/getTargetDir.d.ts} +0 -0
  197. /package/dist/{import.d.ts → types/import.d.ts} +0 -0
  198. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  199. /package/dist/{is → types/is}/index.d.ts +0 -0
  200. /package/dist/{is → types/is}/nodeEnv.d.ts +0 -0
  201. /package/dist/{is → types/is}/platform.d.ts +0 -0
  202. /package/dist/{is → types/is}/type.d.ts +0 -0
  203. /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
  204. /package/dist/{monorepo.d.ts → types/monorepo.d.ts} +0 -0
  205. /package/dist/{nodeEnv.d.ts → types/nodeEnv.d.ts} +0 -0
  206. /package/dist/{path.d.ts → types/path.d.ts} +0 -0
  207. /package/dist/{pathSerializer.d.ts → types/pathSerializer.d.ts} +0 -0
  208. /package/dist/{plugin.d.ts → types/plugin.d.ts} +0 -0
  209. /package/dist/{prettyInstructions.d.ts → types/prettyInstructions.d.ts} +0 -0
  210. /package/dist/{printBuildError.d.ts → types/printBuildError.d.ts} +0 -0
  211. /package/dist/{react.d.ts → types/react.d.ts} +0 -0
  212. /package/dist/{readTsConfig.d.ts → types/readTsConfig.d.ts} +0 -0
  213. /package/dist/{removeSlash.d.ts → types/removeSlash.d.ts} +0 -0
  214. /package/dist/{resolve.d.ts → types/resolve.d.ts} +0 -0
  215. /package/dist/{routes.d.ts → types/routes.d.ts} +0 -0
  216. /package/dist/{runtimeExports.d.ts → types/runtimeExports.d.ts} +0 -0
  217. /package/dist/{ssr.d.ts → types/ssr.d.ts} +0 -0
  218. /package/dist/{storage.d.ts → types/storage.d.ts} +0 -0
  219. /package/dist/{testUtils.d.ts → types/testUtils.d.ts} +0 -0
  220. /package/dist/{types.d.ts → types/types.d.ts} +0 -0
  221. /package/dist/{universal → types/universal}/constants.d.ts +0 -0
  222. /package/dist/{universal → types/universal}/formatWebpack.d.ts +0 -0
  223. /package/dist/{universal → types/universal}/nestedRoutes.d.ts +0 -0
  224. /package/dist/{universal → types/universal}/remixRouter.d.ts +0 -0
  225. /package/dist/{universal → types/universal}/serialize.d.ts +0 -0
  226. /package/dist/{version.d.ts → types/version.d.ts} +0 -0
  227. /package/dist/{wait.d.ts → types/wait.d.ts} +0 -0
  228. /package/dist/{watch.d.ts → types/watch.d.ts} +0 -0
@@ -0,0 +1,77 @@
1
+ import os from "os";
2
+ import { chalk } from "./compiled";
3
+ import { isDev, isSingleEntry } from "./is";
4
+ const normalizeUrl = (url) => url.replace(/([^:]\/)\/+/g, "$1");
5
+ const getIpv4Interfaces = () => {
6
+ const interfaces = os.networkInterfaces();
7
+ const ipv4Interfaces = [];
8
+ Object.keys(interfaces).forEach((key) => {
9
+ interfaces[key].forEach((detail) => {
10
+ const familyV4Value = typeof detail.family === "string" ? "IPv4" : 4;
11
+ if (detail.family === familyV4Value) {
12
+ ipv4Interfaces.push(detail);
13
+ }
14
+ });
15
+ });
16
+ return ipv4Interfaces;
17
+ };
18
+ const getAddressUrls = (protocol = "http", port) => {
19
+ const ipv4Interfaces = getIpv4Interfaces();
20
+ return ipv4Interfaces.reduce((memo, detail) => {
21
+ let label = "Network: ";
22
+ let url = `${protocol}://${detail.address}:${port}`;
23
+ if (detail.address.includes(`localhost`) || detail.internal) {
24
+ label = "Local: ";
25
+ url = `${protocol}://localhost:${port}`;
26
+ }
27
+ memo.push({ label, url });
28
+ return memo;
29
+ }, []);
30
+ };
31
+ const prettyInstructions = (appContext, config) => {
32
+ const { entrypoints, serverRoutes, port, apiOnly, checkedEntries } = appContext;
33
+ const urls = getAddressUrls(
34
+ config.dev.https && isDev() ? "https" : "http",
35
+ port
36
+ );
37
+ const routes = !apiOnly ? serverRoutes.filter((route) => route.entryName) : serverRoutes;
38
+ let message = "App running at:\n\n";
39
+ if (isSingleEntry(entrypoints) || apiOnly) {
40
+ message += urls.map(
41
+ ({ label, url }) => ` ${chalk.bold(`> ${label.padEnd(10)}`)}${chalk.cyanBright(
42
+ normalizeUrl(`${url}/${routes[0].urlPath}`)
43
+ )}
44
+ `
45
+ ).join("");
46
+ } else {
47
+ const maxNameLength = Math.max(...routes.map((r) => r.entryName.length));
48
+ urls.forEach(({ label, url }) => {
49
+ message += ` ${chalk.bold(`> ${label}`)}
50
+ `;
51
+ routes.forEach(({ entryName, urlPath, isSSR }) => {
52
+ if (!checkedEntries.includes(entryName)) {
53
+ return;
54
+ }
55
+ message += ` ${chalk.yellowBright(
56
+ isSSR ? "λ" : "○"
57
+ )} ${chalk.yellowBright(
58
+ entryName.padEnd(maxNameLength + 8)
59
+ )}${chalk.cyanBright(normalizeUrl(`${url}/${urlPath}`))}
60
+ `;
61
+ });
62
+ });
63
+ message += "\n";
64
+ message += chalk.cyanBright(
65
+ " λ (Server) server-side renders at runtime\n"
66
+ );
67
+ message += chalk.cyanBright(
68
+ " ○ (Static) client-side renders as static HTML\n"
69
+ );
70
+ }
71
+ return message;
72
+ };
73
+ export {
74
+ getAddressUrls,
75
+ getIpv4Interfaces,
76
+ prettyInstructions
77
+ };
@@ -0,0 +1,34 @@
1
+ import { chalk } from "./compiled";
2
+ import { logger } from "./logger";
3
+ function printBuildError(err) {
4
+ const message = err != null && err.message;
5
+ const stack = err != null && err.stack;
6
+ if (stack && typeof message === "string" && message.indexOf("from Terser") !== -1) {
7
+ try {
8
+ const matched = /(.+)\[(.+):(.+),(.+)\]\[.+\]/.exec(stack);
9
+ if (!matched) {
10
+ throw new Error("Using errors for control flow is bad.");
11
+ }
12
+ const problemPath = matched[2];
13
+ const line = matched[3];
14
+ const column = matched[4];
15
+ logger.error(
16
+ `Failed to minify the code from this file:
17
+
18
+ ${chalk.yellow(
19
+ ` ${problemPath}:${line}${column !== "0" ? ":" + column : ""}`
20
+ )}
21
+ `
22
+ );
23
+ } catch (ignored) {
24
+ logger.error(`Failed to minify the bundle. ${err}
25
+ `);
26
+ }
27
+ } else {
28
+ logger.error((message || err) + "\n");
29
+ }
30
+ logger.log();
31
+ }
32
+ export {
33
+ printBuildError
34
+ };
@@ -0,0 +1,20 @@
1
+ import path from "path";
2
+ import { fs, semver } from ".";
3
+ const isBeyondReact17 = (cwd) => {
4
+ const pkgPath = path.join(cwd, "package.json");
5
+ if (!fs.existsSync(pkgPath)) {
6
+ return false;
7
+ }
8
+ const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
9
+ const deps = {
10
+ ...pkgInfo.devDependencies,
11
+ ...pkgInfo.dependencies
12
+ };
13
+ if (typeof deps.react !== "string") {
14
+ return false;
15
+ }
16
+ return semver.satisfies(semver.minVersion(deps.react), ">=17.0.0");
17
+ };
18
+ export {
19
+ isBeyondReact17
20
+ };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fs, json5 } from "./compiled";
3
+ const readTsConfig = (root) => {
4
+ return readTsConfigByFile(path.resolve(root, "./tsconfig.json"));
5
+ };
6
+ const readTsConfigByFile = (filename) => {
7
+ const content = fs.readFileSync(path.resolve(filename), "utf-8");
8
+ return json5.parse(content);
9
+ };
10
+ export {
11
+ readTsConfig,
12
+ readTsConfigByFile
13
+ };
@@ -0,0 +1,8 @@
1
+ const removeLeadingSlash = (s) => s.replace(/^\/+/, "");
2
+ const removeTailSlash = (s) => s.replace(/\/+$/, "");
3
+ const removeSlash = (s) => removeLeadingSlash(removeTailSlash(s));
4
+ export {
5
+ removeLeadingSlash,
6
+ removeSlash,
7
+ removeTailSlash
8
+ };
@@ -0,0 +1,38 @@
1
+ import { ensureArray } from "./ensureArray";
2
+ const tryResolve = (name, resolvePath) => {
3
+ let filePath = "";
4
+ try {
5
+ filePath = require.resolve(name, { paths: [resolvePath] });
6
+ delete require.cache[filePath];
7
+ } catch (err) {
8
+ if (err.code === "MODULE_NOT_FOUND") {
9
+ throw new Error(`Can not find module ${name}.`);
10
+ }
11
+ throw err;
12
+ }
13
+ return filePath;
14
+ };
15
+ const isPackageInstalled = (name, resolvePaths) => {
16
+ try {
17
+ require.resolve(name, { paths: ensureArray(resolvePaths) });
18
+ return true;
19
+ } catch (err) {
20
+ return false;
21
+ }
22
+ };
23
+ const getAntdMajorVersion = (appDirectory) => {
24
+ try {
25
+ const pkgJsonPath = require.resolve("antd/package.json", {
26
+ paths: [appDirectory]
27
+ });
28
+ const { version } = require(pkgJsonPath);
29
+ return Number(version.split(".")[0]);
30
+ } catch (err) {
31
+ return null;
32
+ }
33
+ };
34
+ export {
35
+ getAntdMajorVersion,
36
+ isPackageInstalled,
37
+ tryResolve
38
+ };
@@ -0,0 +1,24 @@
1
+ import path from "path";
2
+ import { normalizeToPosixPath } from "./path";
3
+ import { MAIN_ENTRY_NAME } from "./constants";
4
+ const getPathWithoutExt = (filename) => {
5
+ const extname = path.extname(filename);
6
+ return filename.slice(0, -extname.length);
7
+ };
8
+ const getRouteId = (componentPath, routesDir, entryName) => {
9
+ const relativePath = normalizeToPosixPath(
10
+ path.relative(routesDir, componentPath)
11
+ );
12
+ const pathWithoutExt = getPathWithoutExt(relativePath);
13
+ let id = ``;
14
+ if (entryName === MAIN_ENTRY_NAME) {
15
+ id = pathWithoutExt;
16
+ } else {
17
+ id = `${entryName}_${pathWithoutExt}`;
18
+ }
19
+ return id;
20
+ };
21
+ export {
22
+ getPathWithoutExt,
23
+ getRouteId
24
+ };
@@ -0,0 +1,40 @@
1
+ import path from "path";
2
+ import { fs } from "./compiled";
3
+ import { normalizeOutputPath } from "./path";
4
+ const memo = (fn) => {
5
+ const cache = /* @__PURE__ */ new Map();
6
+ return (...params) => {
7
+ const stringifiedParams = JSON.stringify(params);
8
+ const cachedResult = cache.get(stringifiedParams);
9
+ if (cachedResult) {
10
+ return cachedResult;
11
+ }
12
+ const res = fn(...params);
13
+ cache.set(stringifiedParams, res);
14
+ return res;
15
+ };
16
+ };
17
+ const createRuntimeExportsUtils = memo(
18
+ (pwd = "", namespace = "index") => {
19
+ const entryExportFile = path.join(pwd, `.runtime-exports/${namespace}.js`);
20
+ const addExport = (statement) => {
21
+ statement = normalizeOutputPath(statement);
22
+ try {
23
+ fs.ensureFileSync(entryExportFile);
24
+ if (!fs.readFileSync(entryExportFile, "utf8").includes(statement)) {
25
+ fs.appendFileSync(entryExportFile, `${statement}
26
+ `);
27
+ }
28
+ } catch {
29
+ }
30
+ };
31
+ const getPath = () => entryExportFile;
32
+ return {
33
+ addExport,
34
+ getPath
35
+ };
36
+ }
37
+ );
38
+ export {
39
+ createRuntimeExportsUtils
40
+ };
@@ -0,0 +1,6 @@
1
+ import { createStorage } from "./storage";
2
+ const { run, useContext: useHeaders } = createStorage();
3
+ export {
4
+ run,
5
+ useHeaders
6
+ };
@@ -0,0 +1,42 @@
1
+ import * as ah from "async_hooks";
2
+ const createStorage = () => {
3
+ let storage;
4
+ if (typeof ah.AsyncLocalStorage !== "undefined") {
5
+ storage = new ah.AsyncLocalStorage();
6
+ }
7
+ const run = (context, cb) => {
8
+ if (!storage) {
9
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
10
+ `);
11
+ }
12
+ return new Promise((resolve, reject) => {
13
+ storage.run(context, () => {
14
+ try {
15
+ return resolve(cb());
16
+ } catch (error) {
17
+ return reject(error);
18
+ }
19
+ });
20
+ });
21
+ };
22
+ const useContext = () => {
23
+ if (!storage) {
24
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
25
+ `);
26
+ }
27
+ const context = storage.getStore();
28
+ if (!context) {
29
+ throw new Error(
30
+ `Can't call useContext out of scope, make sure @modern-js/utils is a single version in node_modules`
31
+ );
32
+ }
33
+ return context;
34
+ };
35
+ return {
36
+ run,
37
+ useContext
38
+ };
39
+ };
40
+ export {
41
+ createStorage
42
+ };
@@ -0,0 +1,15 @@
1
+ const initSnapshotSerializer = (root) => {
2
+ expect.addSnapshotSerializer({
3
+ test: (val) => typeof val === "string" && (val.includes("modern.js") || val.includes("node_modules") || val.includes(root)),
4
+ print: (val) => (
5
+ // eslint-disable-next-line no-nested-ternary
6
+ typeof val === "string" ? (
7
+ // eslint-disable-next-line no-nested-ternary
8
+ val.includes("node_modules") ? `"${val.replace(/.+node_modules/, ``).replace(/\\/g, "/")}"` : val.includes("modern.js") ? `"${val.replace(/.+modern\.js/, ``).replace(/\\/g, "/")}"` : `"${val.replace(root, "").replace(/\\/g, "/")}"`
9
+ ) : val
10
+ )
11
+ });
12
+ };
13
+ export {
14
+ initSnapshotSerializer
15
+ };
File without changes
@@ -0,0 +1,6 @@
1
+ const ROUTE_MANIFEST = `_MODERNJS_ROUTE_MANIFEST`;
2
+ const HMR_SOCK_PATH = "/webpack-hmr";
3
+ export {
4
+ HMR_SOCK_PATH,
5
+ ROUTE_MANIFEST
6
+ };
@@ -0,0 +1,77 @@
1
+ const friendlySyntaxErrorLabel = "SyntaxError:";
2
+ function isLikelyASyntaxError(message) {
3
+ return message.includes(friendlySyntaxErrorLabel);
4
+ }
5
+ function formatMessage(stats) {
6
+ let lines = [];
7
+ let message;
8
+ if (typeof stats === "object") {
9
+ const fileName = stats.moduleName ? `File: ${stats.moduleName}
10
+ ` : "";
11
+ const mainMessage = typeof stats.formatted === "string" ? stats.formatted : stats.message;
12
+ const details = stats.details ? `
13
+ Details: ${stats.details}
14
+ ` : "";
15
+ const stack = stats.stack ? `
16
+ ${stats.stack}` : "";
17
+ message = `${fileName}${mainMessage}${details}${stack}`;
18
+ } else {
19
+ message = stats;
20
+ }
21
+ lines = message.split("\n");
22
+ lines = lines.map((line) => {
23
+ const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
24
+ line
25
+ );
26
+ if (!parsingError) {
27
+ return line;
28
+ }
29
+ const [, errorLine, errorColumn, errorMessage] = parsingError;
30
+ return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
31
+ });
32
+ message = lines.join("\n");
33
+ message = message.replace(
34
+ /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
35
+ `${friendlySyntaxErrorLabel} $3 ($1:$2)
36
+ `
37
+ );
38
+ lines = message.split("\n");
39
+ if (lines.length > 2 && lines[1].trim() === "") {
40
+ lines.splice(1, 1);
41
+ }
42
+ lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, "$1");
43
+ if (lines[1] && lines[1].indexOf("Module not found:") !== -1) {
44
+ lines[1] = lines[1].replace("Error: ", "");
45
+ }
46
+ message = lines.join("\n");
47
+ message = message.replace(
48
+ /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
49
+ ""
50
+ );
51
+ message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, "");
52
+ lines = message.split("\n");
53
+ lines = lines.filter(
54
+ (line, index, arr) => index === 0 || line.trim() !== "" || line.trim() !== arr[index - 1].trim()
55
+ );
56
+ message = lines.join("\n");
57
+ return message.trim();
58
+ }
59
+ function formatWebpackMessages(json) {
60
+ var _a, _b, _c;
61
+ const formattedErrors = (_a = json == null ? void 0 : json.errors) == null ? void 0 : _a.map(formatMessage);
62
+ const formattedWarnings = (_b = json == null ? void 0 : json.warnings) == null ? void 0 : _b.map(formatMessage);
63
+ const result = {
64
+ errors: formattedErrors || [],
65
+ warnings: formattedWarnings || []
66
+ };
67
+ if ((_c = result.errors) == null ? void 0 : _c.some(isLikelyASyntaxError)) {
68
+ result.errors = result.errors.filter(isLikelyASyntaxError);
69
+ }
70
+ if (result.errors.length > 1) {
71
+ result.errors.length = 1;
72
+ }
73
+ return result;
74
+ }
75
+ export {
76
+ formatWebpackMessages
77
+ };
@@ -0,0 +1,93 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { Suspense } from "react";
3
+ import {
4
+ createRoutesFromElements,
5
+ Route
6
+ } from "react-router-dom";
7
+ const transformNestedRoutes = (routes) => {
8
+ const routeElements = [];
9
+ for (const route of routes) {
10
+ const routeElement = renderNestedRoute(route);
11
+ routeElements.push(routeElement);
12
+ }
13
+ return createRoutesFromElements(routeElements);
14
+ };
15
+ const renderNestedRoute = (nestedRoute, options = {}) => {
16
+ const { children, index, id, component, isRoot, lazyImport } = nestedRoute;
17
+ const Component = component;
18
+ const { parent, DeferredDataComponent, props = {} } = options;
19
+ const routeProps = {
20
+ caseSensitive: nestedRoute.caseSensitive,
21
+ path: nestedRoute.path,
22
+ id: nestedRoute.id,
23
+ loader: createLoader(nestedRoute),
24
+ action: nestedRoute.action,
25
+ hasErrorBoundary: nestedRoute.hasErrorBoundary,
26
+ shouldRevalidate: nestedRoute.shouldRevalidate,
27
+ handle: nestedRoute.handle,
28
+ index: nestedRoute.index,
29
+ element: nestedRoute.element,
30
+ errorElement: nestedRoute.errorElement
31
+ };
32
+ if (nestedRoute.error) {
33
+ const errorElement = /* @__PURE__ */ jsx(nestedRoute.error, {});
34
+ routeProps.errorElement = errorElement;
35
+ }
36
+ let element;
37
+ if (Component) {
38
+ if ((parent == null ? void 0 : parent.loading) && lazyImport) {
39
+ const Loading = parent.loading;
40
+ if (isLoadableComponent(Component)) {
41
+ element = /* @__PURE__ */ jsx(Component, { fallback: /* @__PURE__ */ jsx(Loading, {}) });
42
+ } else {
43
+ element = /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(Loading, {}), children: /* @__PURE__ */ jsx(Component, {}) });
44
+ }
45
+ } else if (isLoadableComponent(Component) && lazyImport) {
46
+ element = /* @__PURE__ */ jsx(Component, {});
47
+ } else if (isRoot) {
48
+ element = /* @__PURE__ */ jsxs(Fragment, { children: [
49
+ /* @__PURE__ */ jsx(Component, { ...props }),
50
+ typeof document === "undefined" && DeferredDataComponent && /* @__PURE__ */ jsx(DeferredDataComponent, {})
51
+ ] });
52
+ } else if (lazyImport) {
53
+ element = /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(Component, {}) });
54
+ } else {
55
+ element = /* @__PURE__ */ jsx(Component, {});
56
+ }
57
+ } else {
58
+ nestedRoute.loading = parent == null ? void 0 : parent.loading;
59
+ }
60
+ if (element) {
61
+ routeProps.element = element;
62
+ }
63
+ const childElements = children == null ? void 0 : children.map((childRoute) => {
64
+ return renderNestedRoute(childRoute, { parent: nestedRoute });
65
+ });
66
+ const routeElement = index ? /* @__PURE__ */ jsx(Route, { ...routeProps, index: true }, id) : /* @__PURE__ */ jsx(Route, { ...routeProps, index: false, children: childElements }, id);
67
+ return routeElement;
68
+ };
69
+ function createLoader(route) {
70
+ const { loader } = route;
71
+ if (loader) {
72
+ return (args) => {
73
+ if (typeof route.lazyImport === "function") {
74
+ route.lazyImport();
75
+ }
76
+ return loader(args);
77
+ };
78
+ } else {
79
+ return () => {
80
+ if (typeof route.lazyImport === "function") {
81
+ route.lazyImport();
82
+ }
83
+ return null;
84
+ };
85
+ }
86
+ }
87
+ function isLoadableComponent(component) {
88
+ return component && component.displayName === "Loadable" && component.preload && typeof component.preload === "function";
89
+ }
90
+ export {
91
+ renderNestedRoute,
92
+ transformNestedRoutes
93
+ };
@@ -0,0 +1 @@
1
+ export * from "@remix-run/router";
@@ -0,0 +1,7 @@
1
+ import serialize from "serialize-javascript";
2
+ const serializeJson = (data) => {
3
+ return serialize(data, { isJSON: true });
4
+ };
5
+ export {
6
+ serializeJson
7
+ };
@@ -0,0 +1,25 @@
1
+ import path from "path";
2
+ import { fs, execa, semver } from "./compiled";
3
+ async function getPnpmVersion() {
4
+ const { stdout } = await execa("pnpm", ["--version"]);
5
+ return stdout;
6
+ }
7
+ const isReact18 = (cwd) => {
8
+ const pkgPath = path.join(cwd, "package.json");
9
+ if (!fs.existsSync(pkgPath)) {
10
+ return false;
11
+ }
12
+ const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
13
+ const deps = {
14
+ ...pkgInfo.devDependencies,
15
+ ...pkgInfo.dependencies
16
+ };
17
+ if (typeof deps.react !== "string") {
18
+ return false;
19
+ }
20
+ return semver.satisfies(semver.minVersion(deps.react), ">=18.0.0");
21
+ };
22
+ export {
23
+ getPnpmVersion,
24
+ isReact18
25
+ };
@@ -0,0 +1,6 @@
1
+ const wait = (time = 0) => new Promise((resolve) => {
2
+ setTimeout(resolve, time);
3
+ });
4
+ export {
5
+ wait
6
+ };
@@ -0,0 +1,36 @@
1
+ import * as path from "path";
2
+ import { chokidar } from "./compiled";
3
+ const WatchChangeType = {
4
+ ADD: "add",
5
+ UNLINK: "unlink",
6
+ CHANGE: "change"
7
+ };
8
+ const watch = (watchDir, runTask, ignored = []) => {
9
+ let ready = false;
10
+ const watcher = chokidar.watch(watchDir, {
11
+ ignored
12
+ });
13
+ watcher.on("ready", () => ready = true);
14
+ watcher.on("change", async (filePath) => {
15
+ const changedFilePath = path.resolve(filePath);
16
+ await runTask({ changedFilePath, changeType: WatchChangeType.CHANGE });
17
+ });
18
+ watcher.on("add", async (filePath) => {
19
+ const changedFilePath = path.resolve(filePath);
20
+ if (ready) {
21
+ await runTask({ changedFilePath, changeType: WatchChangeType.ADD });
22
+ }
23
+ });
24
+ watcher.on("unlink", async (filePath) => {
25
+ const changedFilePath = path.resolve(filePath);
26
+ await runTask({ changedFilePath, changeType: WatchChangeType.UNLINK });
27
+ });
28
+ watcher.on("error", (err) => {
29
+ throw err;
30
+ });
31
+ return watcher;
32
+ };
33
+ export {
34
+ WatchChangeType,
35
+ watch
36
+ };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.12.0",
14
+ "version": "2.12.1-alpha.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/index.d.ts",
17
17
  "main": "./dist/index.js",
@@ -175,8 +175,8 @@
175
175
  "typescript": "^4",
176
176
  "webpack": "^5.76.2",
177
177
  "@types/serialize-javascript": "^5.0.1",
178
- "@modern-js/types": "2.12.0",
179
178
  "@scripts/build": "2.12.0",
179
+ "@modern-js/types": "2.12.0",
180
180
  "@scripts/jest-config": "2.12.0"
181
181
  },
182
182
  "sideEffects": false,
package/dist/emptyDir.js DELETED
@@ -1,52 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var __async = (__this, __arguments, generator) => {
19
- return new Promise((resolve, reject) => {
20
- var fulfilled = (value) => {
21
- try {
22
- step(generator.next(value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- };
27
- var rejected = (value) => {
28
- try {
29
- step(generator.throw(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- };
34
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
- step((generator = generator.apply(__this, __arguments)).next());
36
- });
37
- };
38
- var emptyDir_exports = {};
39
- __export(emptyDir_exports, {
40
- emptyDir: () => emptyDir
41
- });
42
- module.exports = __toCommonJS(emptyDir_exports);
43
- var import_compiled = require("./compiled");
44
- const emptyDir = (dir) => __async(void 0, null, function* () {
45
- if (yield import_compiled.fs.pathExists(dir)) {
46
- yield import_compiled.fs.emptyDir(dir);
47
- }
48
- });
49
- // Annotate the CommonJS export names for ESM import in node:
50
- 0 && (module.exports = {
51
- emptyDir
52
- });