xanix 1.0.20 → 1.0.21

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 (275) hide show
  1. package/dist/bin/bundler/cache/buildCache.js +9 -11
  2. package/dist/bin/bundler/cache/index.js +70 -116
  3. package/dist/bin/bundler/cache/utils.d.ts +3 -2
  4. package/dist/bin/bundler/cache/utils.js +27 -24
  5. package/dist/bin/bundler/client/build.js +1 -1
  6. package/dist/bin/bundler/client/plugins/Transformer/TransformCacheFunction copy.d.ts +3 -0
  7. package/dist/bin/bundler/client/plugins/Transformer/TransformCacheFunction copy.js +32 -0
  8. package/dist/bin/bundler/client/plugins/Transformer/TransformCacheFunction.d.ts +1 -1
  9. package/dist/bin/bundler/client/plugins/Transformer/TransformCacheFunction.js +1 -1
  10. package/dist/bin/bundler/client/plugins/Transformer/TransformCreateSession.d.ts +1 -1
  11. package/dist/bin/bundler/client/plugins/Transformer/TransformCreateSession.js +1 -1
  12. package/dist/bin/bundler/client/plugins/Transformer/TransformDevModule.d.ts +13 -0
  13. package/dist/bin/bundler/client/plugins/Transformer/{transformModuleCache.js → TransformDevModule.js} +6 -18
  14. package/dist/bin/bundler/client/plugins/Transformer/TransformServerFunction.d.ts +1 -1
  15. package/dist/bin/bundler/client/plugins/Transformer/TransformServerFunction.js +1 -1
  16. package/dist/bin/bundler/client/plugins/Transformer/cacheFunctionReplacer.d.ts +3 -0
  17. package/dist/bin/bundler/client/plugins/Transformer/cacheFunctionReplacer.js +32 -0
  18. package/dist/bin/bundler/client/plugins/Transformer/cacheFunctionTransformer.d.ts +3 -0
  19. package/dist/bin/bundler/client/plugins/Transformer/cacheFunctionTransformer.js +32 -0
  20. package/dist/bin/bundler/client/plugins/Transformer/serverFunctionReplacer.d.ts +3 -0
  21. package/dist/bin/bundler/client/plugins/Transformer/serverFunctionReplacer.js +32 -0
  22. package/dist/bin/bundler/client/plugins/Transformer/useServerReplacer.d.ts +3 -0
  23. package/dist/bin/bundler/client/plugins/Transformer/useServerReplacer.js +42 -0
  24. package/dist/bin/bundler/client/plugins/Transformer.js +4 -2
  25. package/dist/bin/bundler/client/plugins/XanixAppRoot.d.ts +2 -0
  26. package/dist/bin/bundler/client/plugins/XanixAppRoot.js +46 -0
  27. package/dist/bin/bundler/client/plugins/XanixPage.js +30 -0
  28. package/dist/bin/bundler/client/plugins/XanixPages.js +8 -8
  29. package/dist/bin/bundler/client/plugins/replacer/cacheFunctionReplacer.d.ts +1 -1
  30. package/dist/bin/bundler/client/plugins/replacer/cacheFunctionReplacer.js +5 -1
  31. package/dist/bin/bundler/client/plugins/replacer/serverFunctionReplacer.d.ts +1 -1
  32. package/dist/bin/bundler/client/plugins/replacer/serverFunctionReplacer.js +5 -1
  33. package/dist/bin/bundler/client/plugins/replacer/useServerReplacer.d.ts +1 -1
  34. package/dist/bin/bundler/client/plugins/replacer/useServerReplacer.js +7 -3
  35. package/dist/bin/bundler/server/build.js +1 -1
  36. package/dist/bin/bundler/server/plugins/Transformer/TransformCacheFunction copy.d.ts +3 -0
  37. package/dist/bin/bundler/server/plugins/Transformer/TransformCacheFunction copy.js +32 -0
  38. package/dist/bin/bundler/server/plugins/Transformer/TransformCacheFunction.d.ts +1 -1
  39. package/dist/bin/bundler/server/plugins/Transformer/TransformCacheFunction.js +1 -1
  40. package/dist/bin/bundler/server/plugins/Transformer/TransformCreateSession.d.ts +1 -1
  41. package/dist/bin/bundler/server/plugins/Transformer/TransformCreateSession.js +1 -1
  42. package/dist/bin/bundler/server/plugins/Transformer/TransformExpressFunction.d.ts +1 -1
  43. package/dist/bin/bundler/server/plugins/Transformer/TransformExpressFunction.js +1 -1
  44. package/dist/bin/bundler/server/plugins/Transformer/TransformPage.js +1 -1
  45. package/dist/bin/bundler/server/plugins/Transformer/TransformServerFunction.d.ts +1 -1
  46. package/dist/bin/bundler/server/plugins/Transformer/TransformServerFunction.js +1 -1
  47. package/dist/bin/bundler/server/plugins/Transformer/cacheFunctionReplacer.d.ts +3 -0
  48. package/dist/bin/bundler/server/plugins/Transformer/cacheFunctionReplacer.js +32 -0
  49. package/dist/bin/bundler/server/plugins/Transformer/expressFunctionReplace.d.ts +3 -0
  50. package/dist/bin/bundler/server/plugins/Transformer/expressFunctionReplace.js +24 -0
  51. package/dist/bin/bundler/server/plugins/Transformer/pageReplacer.d.ts +4 -0
  52. package/dist/bin/bundler/server/plugins/Transformer/pageReplacer.js +31 -0
  53. package/dist/bin/bundler/server/plugins/Transformer/serverFunctionReplacer.d.ts +3 -0
  54. package/dist/bin/bundler/server/plugins/Transformer/serverFunctionReplacer.js +32 -0
  55. package/dist/bin/bundler/server/plugins/Transformer/useServerReplacer.d.ts +3 -0
  56. package/dist/bin/bundler/server/plugins/Transformer/useServerReplacer.js +42 -0
  57. package/dist/bin/bundler/server/plugins/Transformer copy.js +3 -3
  58. package/dist/bin/bundler/server/plugins/Transformer.js +2 -2
  59. package/dist/bin/bundler/server/plugins/replacer/cacheFunctionReplacer.d.ts +1 -1
  60. package/dist/bin/bundler/server/plugins/replacer/cacheFunctionReplacer.js +5 -1
  61. package/dist/bin/bundler/server/plugins/replacer/serverFunctionReplacer copy.d.ts +3 -0
  62. package/dist/bin/bundler/server/plugins/replacer/serverFunctionReplacer copy.js +26 -0
  63. package/dist/bin/bundler/server/plugins/replacer/serverFunctionReplacer.d.ts +1 -1
  64. package/dist/bin/bundler/server/plugins/replacer/serverFunctionReplacer.js +5 -1
  65. package/dist/bin/bundler/server/plugins/replacer/serverReplacer.d.ts +3 -0
  66. package/dist/bin/bundler/server/plugins/replacer/serverReplacer.js +26 -0
  67. package/dist/bin/bundler/server/plugins/replacer/useServerReplacer.d.ts +2 -2
  68. package/dist/bin/bundler/server/plugins/replacer/useServerReplacer.js +9 -5
  69. package/dist/bin/cli/dev.js +85 -71
  70. package/dist/bin/cli/start/index.d.ts +4 -1
  71. package/dist/bin/cli/start/index.js +60 -9
  72. package/dist/bin/finder/callbackReplacer.d.ts +2 -0
  73. package/dist/bin/finder/callbackReplacer.js +52 -0
  74. package/dist/bin/finder/findImport.d.ts +9 -0
  75. package/dist/bin/finder/findImport.js +50 -0
  76. package/dist/bin/finder/findImports.d.ts +16 -0
  77. package/dist/bin/finder/findImports.js +45 -0
  78. package/dist/bin/finder/findPage.d.ts +9 -0
  79. package/dist/bin/finder/findPage.js +30 -0
  80. package/dist/bin/finder/importFinder.d.ts +9 -0
  81. package/dist/bin/finder/importFinder.js +50 -0
  82. package/dist/bin/finder/pageFinder.d.ts +9 -0
  83. package/dist/bin/finder/pageFinder.js +30 -0
  84. package/dist/bin/finder/stripComments.d.ts +3 -0
  85. package/dist/bin/finder/stripComments.js +15 -0
  86. package/dist/bin/include/path.d.ts +5 -1
  87. package/dist/bin/include/path.js +15 -1
  88. package/dist/bin/include/sendFinder.d.ts +4 -1
  89. package/dist/bin/include/sendFinder.js +88 -53
  90. package/dist/bin/index.js +0 -0
  91. package/dist/bin/modifier/callbackReplacer.js +35 -80
  92. package/dist/bin/modifier/findImports.d.ts +16 -0
  93. package/dist/bin/modifier/findImports.js +45 -0
  94. package/dist/bin/modifier/importFinder.js +44 -63
  95. package/dist/bin/modifier/pageFinder.d.ts +1 -1
  96. package/dist/bin/modifier/pageFinder.js +19 -124
  97. package/dist/bin/modifier/sendFinder.d.ts +9 -0
  98. package/dist/bin/modifier/sendFinder.js +135 -0
  99. package/dist/bin/modifier/sriptCommnet.d.ts +1 -0
  100. package/dist/bin/modifier/sriptCommnet.js +12 -5
  101. package/dist/bin/modifier/stripComments.d.ts +3 -0
  102. package/dist/bin/modifier/stripComments.js +15 -0
  103. package/dist/bin/plugins/XanixAssets.js +2 -2
  104. package/dist/bin/plugins/XanixCss.js +13 -13
  105. package/dist/bin/plugins/XanixDocument.js +4 -4
  106. package/dist/components/Head.js +1 -1
  107. package/dist/hooks/encript.d.ts +2 -0
  108. package/dist/hooks/encript.js +13 -0
  109. package/dist/hooks/server.js +2 -2
  110. package/dist/hooks/session.js +2 -2
  111. package/dist/index.js +0 -0
  112. package/dist/libs/encoder.d.ts +2 -0
  113. package/dist/libs/encoder.js +13 -0
  114. package/dist/libs/encript.d.ts +2 -0
  115. package/dist/libs/encript.js +30 -0
  116. package/dist/navigate.js +1 -1
  117. package/dist/server/express/Router.js +4 -4
  118. package/dist/server/express/page.d.ts +1 -1
  119. package/dist/server/express/page.js +1 -1
  120. package/dist/types/assets.d.ts +72 -72
  121. package/dist/types/express.d.ts +11 -11
  122. package/dist/types/globals.d.ts +18 -18
  123. package/dist/types/xanix-virtual.d.ts +35 -35
  124. package/package.json +57 -60
  125. package/dist/bin/bundler/Cache.d.ts +0 -1
  126. package/dist/bin/bundler/Cache.js +0 -210
  127. package/dist/bin/bundler/External.d.ts +0 -3
  128. package/dist/bin/bundler/External.js +0 -6
  129. package/dist/bin/bundler/buildClient.d.ts +0 -3
  130. package/dist/bin/bundler/buildClient.js +0 -42
  131. package/dist/bin/bundler/buildServer.d.ts +0 -7
  132. package/dist/bin/bundler/buildServer.js +0 -50
  133. package/dist/bin/bundler/cache/plugin/Transformer.d.ts +0 -3
  134. package/dist/bin/bundler/cache/plugin/Transformer.js +0 -42
  135. package/dist/bin/bundler/client/plugins/Transformer/TransformExpress.d.ts +0 -9
  136. package/dist/bin/bundler/client/plugins/Transformer/TransformExpress.js +0 -26
  137. package/dist/bin/bundler/client/plugins/Transformer/TransformPage.d.ts +0 -29
  138. package/dist/bin/bundler/client/plugins/Transformer/TransformPage.js +0 -77
  139. package/dist/bin/bundler/client/plugins/Transformer/TransformReactContext.d.ts +0 -17
  140. package/dist/bin/bundler/client/plugins/Transformer/TransformReactContext.js +0 -101
  141. package/dist/bin/bundler/client/plugins/Transformer/transformModuleCache.d.ts +0 -15
  142. package/dist/bin/bundler/client/plugins/Transformer copy.d.ts +0 -3
  143. package/dist/bin/bundler/client/plugins/Transformer copy.js +0 -70
  144. package/dist/bin/bundler/client/plugins/VirtualDev.d.ts +0 -2
  145. package/dist/bin/bundler/client/plugins/VirtualDev.js +0 -34
  146. package/dist/bin/bundler/client/plugins/XanixReactRefresh.d.ts +0 -2
  147. package/dist/bin/bundler/client/plugins/XanixReactRefresh.js +0 -22
  148. package/dist/bin/bundler/config/defines.d.ts +0 -20
  149. package/dist/bin/bundler/config/defines.js +0 -21
  150. package/dist/bin/bundler/config/external.d.ts +0 -1
  151. package/dist/bin/bundler/config/external.js +0 -12
  152. package/dist/bin/bundler/config/loadEnv.d.ts +0 -20
  153. package/dist/bin/bundler/config/loadEnv.js +0 -48
  154. package/dist/bin/bundler/config/output.d.ts +0 -10
  155. package/dist/bin/bundler/config/output.js +0 -52
  156. package/dist/bin/bundler/externalResolver-dep.d.ts +0 -3
  157. package/dist/bin/bundler/externalResolver-dep.js +0 -203
  158. package/dist/bin/bundler/externalResolver.d.ts +0 -3
  159. package/dist/bin/bundler/externalResolver.js +0 -203
  160. package/dist/bin/bundler/plugins/TransformServer/TransformExpress.d.ts +0 -9
  161. package/dist/bin/bundler/plugins/TransformServer/TransformExpress.js +0 -26
  162. package/dist/bin/bundler/plugins/TransformServer/TransformPage.d.ts +0 -25
  163. package/dist/bin/bundler/plugins/TransformServer/TransformPage.js +0 -79
  164. package/dist/bin/bundler/plugins/TransformServer/index.d.ts +0 -3
  165. package/dist/bin/bundler/plugins/TransformServer/index.js +0 -103
  166. package/dist/bin/bundler/plugins/XanixAssets.d.ts +0 -5
  167. package/dist/bin/bundler/plugins/XanixAssets.js +0 -51
  168. package/dist/bin/bundler/plugins/XanixCacheDeps-dep.d.ts +0 -2
  169. package/dist/bin/bundler/plugins/XanixCacheDeps-dep.js +0 -150
  170. package/dist/bin/bundler/plugins/XanixCacheDeps.d.ts +0 -2
  171. package/dist/bin/bundler/plugins/XanixCacheDeps.js +0 -150
  172. package/dist/bin/bundler/plugins/XanixDocument/index.js +0 -53
  173. package/dist/bin/bundler/plugins/XanixEntryFinder/finder.d.ts +0 -11
  174. package/dist/bin/bundler/plugins/XanixEntryFinder/finder.js +0 -149
  175. package/dist/bin/bundler/plugins/XanixEntryFinder/index.d.ts +0 -6
  176. package/dist/bin/bundler/plugins/XanixEntryFinder/index.js +0 -37
  177. package/dist/bin/bundler/plugins/XanixEntryFinder-dep/finder.d.ts +0 -11
  178. package/dist/bin/bundler/plugins/XanixEntryFinder-dep/finder.js +0 -149
  179. package/dist/bin/bundler/plugins/XanixEntryFinder-dep/index.d.ts +0 -6
  180. package/dist/bin/bundler/plugins/XanixEntryFinder-dep/index.js +0 -37
  181. package/dist/bin/bundler/plugins/XanixEnv.d.ts +0 -15
  182. package/dist/bin/bundler/plugins/XanixEnv.js +0 -44
  183. package/dist/bin/bundler/plugins/XanixPageTransform/index.d.ts +0 -2
  184. package/dist/bin/bundler/plugins/XanixPageTransform/index.js +0 -62
  185. package/dist/bin/bundler/plugins/XanixPageTransform/transformer copy.d.ts +0 -18
  186. package/dist/bin/bundler/plugins/XanixPageTransform/transformer copy.js +0 -414
  187. package/dist/bin/bundler/plugins/XanixPageTransform/transformer.d.ts +0 -23
  188. package/dist/bin/bundler/plugins/XanixPageTransform/transformer.js +0 -589
  189. package/dist/bin/bundler/plugins/XanixReactRefresh copy.d.ts +0 -2
  190. package/dist/bin/bundler/plugins/XanixReactRefresh copy.js +0 -121
  191. package/dist/bin/bundler/plugins/XanixReactRefresh.d.ts +0 -2
  192. package/dist/bin/bundler/plugins/XanixReactRefresh.js +0 -170
  193. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/BuildClientCache.d.ts +0 -11
  194. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/BuildClientCache.js +0 -129
  195. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/index copy.d.ts +0 -15
  196. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/index copy.js +0 -228
  197. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/index.d.ts +0 -7
  198. package/dist/bin/bundler/plugins/XanixResolveCacheDeps/index.js +0 -166
  199. package/dist/bin/bundler/plugins/XanixResolveCacheDeps-dep/BuildClientCache.d.ts +0 -11
  200. package/dist/bin/bundler/plugins/XanixResolveCacheDeps-dep/BuildClientCache.js +0 -129
  201. package/dist/bin/bundler/plugins/XanixResolveCacheDeps-dep/index.d.ts +0 -5
  202. package/dist/bin/bundler/plugins/XanixResolveCacheDeps-dep/index.js +0 -26
  203. package/dist/bin/bundler/plugins/XanixResolver.d.ts +0 -11
  204. package/dist/bin/bundler/plugins/XanixResolver.js +0 -55
  205. package/dist/bin/bundler/plugins/XanixServerTransform.d.ts +0 -5
  206. package/dist/bin/bundler/plugins/XanixServerTransform.js +0 -207
  207. package/dist/bin/bundler/plugins/XanixTransform.d.ts +0 -3
  208. package/dist/bin/bundler/plugins/XanixTransform.js +0 -23
  209. package/dist/bin/bundler/plugins/XanixTsconfigAlias.d.ts +0 -19
  210. package/dist/bin/bundler/plugins/XanixTsconfigAlias.js +0 -197
  211. package/dist/bin/bundler/plugins/XanixUseServer.d.ts +0 -7
  212. package/dist/bin/bundler/plugins/XanixUseServer.js +0 -445
  213. package/dist/bin/bundler/plugins/plugins.d.ts +0 -9
  214. package/dist/bin/bundler/plugins/plugins.js +0 -37
  215. package/dist/bin/bundler/requireCache.d.ts +0 -3
  216. package/dist/bin/bundler/requireCache.js +0 -85
  217. package/dist/bin/bundler/server/plugins/ServerAction.d.ts +0 -3
  218. package/dist/bin/bundler/server/plugins/ServerAction.js +0 -9
  219. package/dist/bin/bundler/server/plugins/Transformer/ServerAction.d.ts +0 -11
  220. package/dist/bin/bundler/server/plugins/Transformer/ServerAction.js +0 -28
  221. package/dist/bin/bundler/server/plugins/Transformer/TransformExpress copy.d.ts +0 -11
  222. package/dist/bin/bundler/server/plugins/Transformer/TransformExpress copy.js +0 -22
  223. package/dist/bin/bundler/server/plugins/Transformer/TransformExpress.d.ts +0 -10
  224. package/dist/bin/bundler/server/plugins/Transformer/TransformExpress.js +0 -19
  225. package/dist/bin/bundler/server/plugins/Transformer/TransformReactContext.d.ts +0 -17
  226. package/dist/bin/bundler/server/plugins/Transformer/TransformReactContext.js +0 -101
  227. package/dist/bin/bundler/server/plugins/Transformer/TransformServerAction copy.d.ts +0 -14
  228. package/dist/bin/bundler/server/plugins/Transformer/TransformServerAction copy.js +0 -38
  229. package/dist/bin/bundler/server/types.d.ts +0 -5
  230. package/dist/bin/bundler/server/types.js +0 -1
  231. package/dist/bin/bundler/watchClient.d.ts +0 -12
  232. package/dist/bin/bundler/watchClient.js +0 -104
  233. package/dist/bin/bundler/watchServer.d.ts +0 -10
  234. package/dist/bin/bundler/watchServer.js +0 -139
  235. package/dist/bin/cli/dev/dev.d.ts +0 -2
  236. package/dist/bin/cli/dev/dev.js +0 -178
  237. package/dist/bin/cli/start/start.d.ts +0 -2
  238. package/dist/bin/cli/start/start.js +0 -27
  239. package/dist/bin/include/entry.d.ts +0 -2
  240. package/dist/bin/include/entry.js +0 -21
  241. package/dist/bin/plugins/XanixDocument/XanixDocument.d.ts +0 -2
  242. package/dist/bin/plugins/XanixDocument/XanixDocument.js +0 -50
  243. package/dist/bin/plugins/XanixDocument/index.d.ts +0 -2
  244. package/dist/bin/plugins/XanixDocument/index.js +0 -50
  245. package/dist/bin/plugins/XanixPageTransform-dep/index.d.ts +0 -2
  246. package/dist/bin/plugins/XanixPageTransform-dep/index.js +0 -62
  247. package/dist/bin/plugins/XanixPageTransform-dep/transformer.d.ts +0 -23
  248. package/dist/bin/plugins/XanixPageTransform-dep/transformer.js +0 -589
  249. package/dist/bin/plugins/XanixServerTransform.d.ts +0 -5
  250. package/dist/bin/plugins/XanixServerTransform.js +0 -207
  251. package/dist/bin/plugins/XanixUseServer.d.ts +0 -7
  252. package/dist/bin/plugins/XanixUseServer.js +0 -445
  253. package/dist/bin/plugins/plugins.d.ts +0 -8
  254. package/dist/bin/plugins/plugins.js +0 -31
  255. package/dist/components/Script.d.ts +0 -3
  256. package/dist/components/Script.js +0 -5
  257. package/dist/dev.d.ts +0 -1
  258. package/dist/dev.js +0 -25
  259. package/dist/react/createContext.d.ts +0 -3
  260. package/dist/react/createContext.js +0 -11
  261. package/dist/react/index.d.ts +0 -12
  262. package/dist/react/index.js +0 -3
  263. package/dist/server/express/Ruter.d.ts +0 -2
  264. package/dist/server/express/Ruter.js +0 -15
  265. package/dist/server/runtime/createServer.d.ts +0 -12
  266. package/dist/server/runtime/createServer.js +0 -62
  267. package/dist/server/runtime/index.d.ts +0 -3
  268. package/dist/server/runtime/index.js +0 -3
  269. package/dist/server/runtime/readManifest.d.ts +0 -14
  270. package/dist/server/runtime/readManifest.js +0 -9
  271. package/dist/server/runtime/xanixPage.d.ts +0 -11
  272. package/dist/server/runtime/xanixPage.js +0 -92
  273. /package/dist/bin/bundler/{plugins/XanixDocument/index.d.ts → client/plugins/XanixPage.d.ts} +0 -0
  274. /package/dist/bin/{cli/start/utils.d.ts → bundler/server/plugins/replacer/useServer.d.ts} +0 -0
  275. /package/dist/bin/{cli/start/utils.js → bundler/server/plugins/replacer/useServer.js} +0 -0
@@ -1,414 +0,0 @@
1
- import path from "node:path";
2
- import fs from "node:fs";
3
- import crypto from "node:crypto";
4
- import { parse } from "@babel/parser";
5
- import traverse from "@babel/traverse";
6
- import generate from "@babel/generator";
7
- import * as t from "@babel/types";
8
- const RUNTIME_IMPORT = "xanix/runtime";
9
- const SOURCE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".cjs"];
10
- function normalizeFilePath(file) {
11
- return path.resolve(file).split(path.sep).join("/");
12
- }
13
- export function resolveFile(file) {
14
- const absolute = path.resolve(file);
15
- if (fs.existsSync(absolute)) {
16
- const stat = fs.statSync(absolute);
17
- if (stat.isFile()) {
18
- return absolute;
19
- }
20
- }
21
- for (const ext of SOURCE_EXTENSIONS) {
22
- const candidate = `${absolute}${ext}`;
23
- if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
24
- return path.resolve(candidate);
25
- }
26
- }
27
- if (fs.existsSync(absolute) && fs.statSync(absolute).isDirectory()) {
28
- for (const ext of SOURCE_EXTENSIONS) {
29
- const candidate = path.join(absolute, `index${ext}`);
30
- if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
31
- return path.resolve(candidate);
32
- }
33
- }
34
- }
35
- return absolute;
36
- }
37
- export function resolveComponentFile(importer, importPath) {
38
- if (!importPath.startsWith(".")) {
39
- return importPath;
40
- }
41
- const base = path.resolve(path.dirname(importer), importPath);
42
- return resolveFile(base);
43
- }
44
- export function createPageId(file) {
45
- return `${crypto
46
- .createHash("sha256")
47
- .update(path.normalize(file))
48
- .digest("hex")
49
- .slice(0, 12)}`;
50
- }
51
- export function getJSXComponentName(node) {
52
- const name = node.openingElement.name;
53
- if (t.isJSXIdentifier(name)) {
54
- return name.name;
55
- }
56
- if (t.isJSXMemberExpression(name)) {
57
- if (t.isJSXIdentifier(name.object) && t.isJSXIdentifier(name.property)) {
58
- return `${name.object.name}.${name.property.name}`;
59
- }
60
- }
61
- return null;
62
- }
63
- function findComponentImport(ast, componentName) {
64
- for (const node of ast.program.body) {
65
- if (!t.isImportDeclaration(node)) {
66
- continue;
67
- }
68
- for (const specifier of node.specifiers) {
69
- if (t.isImportDefaultSpecifier(specifier) &&
70
- specifier.local.name === componentName) {
71
- return {
72
- file: node.source.value,
73
- export: "default",
74
- declaration: node,
75
- specifier,
76
- };
77
- }
78
- if (t.isImportSpecifier(specifier) &&
79
- specifier.local.name === componentName) {
80
- const imported = specifier.imported;
81
- return {
82
- file: node.source.value,
83
- export: t.isIdentifier(imported) ? imported.name : imported.value,
84
- declaration: node,
85
- specifier,
86
- };
87
- }
88
- }
89
- }
90
- return null;
91
- }
92
- function removeComponentImport(ast, componentImport) {
93
- const { declaration, specifier } = componentImport;
94
- const index = declaration.specifiers.indexOf(specifier);
95
- if (index !== -1) {
96
- declaration.specifiers.splice(index, 1);
97
- }
98
- if (declaration.specifiers.length === 0) {
99
- const bodyIndex = ast.program.body.indexOf(declaration);
100
- if (bodyIndex !== -1) {
101
- ast.program.body.splice(bodyIndex, 1);
102
- }
103
- }
104
- }
105
- function jsxNameToExpression(name) {
106
- if (t.isJSXIdentifier(name)) {
107
- return t.identifier(name.name);
108
- }
109
- if (t.isJSXMemberExpression(name)) {
110
- const object = jsxNameToExpressionFromJSXName(name.object);
111
- const property = jsxNameToExpressionFromJSXName(name.property);
112
- if (!object || !property) {
113
- return null;
114
- }
115
- return t.memberExpression(object, property);
116
- }
117
- return null;
118
- }
119
- function jsxNameToExpressionFromJSXName(name) {
120
- if (t.isJSXIdentifier(name)) {
121
- return t.identifier(name.name);
122
- }
123
- if (t.isJSXMemberExpression(name)) {
124
- const object = jsxNameToExpressionFromJSXName(name.object);
125
- const property = jsxNameToExpressionFromJSXName(name.property);
126
- if (!object || !property) {
127
- return null;
128
- }
129
- return t.memberExpression(object, property);
130
- }
131
- return null;
132
- }
133
- function jsxAttributesToProps(jsx) {
134
- const properties = [];
135
- for (const attribute of jsx.openingElement.attributes) {
136
- if (t.isJSXSpreadAttribute(attribute)) {
137
- properties.push(t.spreadElement(attribute.argument));
138
- continue;
139
- }
140
- if (!t.isJSXAttribute(attribute)) {
141
- continue;
142
- }
143
- function jsxAttributeNameToExpression(name) {
144
- if (t.isJSXIdentifier(name)) {
145
- return t.identifier(name.name);
146
- }
147
- return t.stringLiteral(`${name.namespace.name}:${name.name.name}`);
148
- }
149
- const key = jsxAttributeNameToExpression(attribute.name);
150
- if (!attribute.value) {
151
- properties.push(t.objectProperty(key, t.booleanLiteral(true)));
152
- continue;
153
- }
154
- if (t.isStringLiteral(attribute.value)) {
155
- properties.push(t.objectProperty(key, t.stringLiteral(attribute.value.value)));
156
- continue;
157
- }
158
- if (t.isJSXExpressionContainer(attribute.value)) {
159
- const expression = attribute.value.expression;
160
- if (t.isJSXEmptyExpression(expression)) {
161
- continue;
162
- }
163
- properties.push(t.objectProperty(key, expression));
164
- continue;
165
- }
166
- if (t.isJSXElement(attribute.value)) {
167
- properties.push(t.objectProperty(key, attribute.value));
168
- }
169
- }
170
- return t.objectExpression(properties);
171
- }
172
- function jsxToComponentAndProps(jsx) {
173
- const component = jsxNameToExpression(jsx.openingElement.name);
174
- if (!component) {
175
- return null;
176
- }
177
- const props = jsxAttributesToProps(jsx);
178
- return {
179
- component,
180
- props,
181
- };
182
- }
183
- function createDynamicComponentProperty(importPath) {
184
- const importCall = t.callExpression(t.import(), [
185
- t.stringLiteral(importPath),
186
- ]);
187
- const awaitedImport = t.awaitExpression(importCall);
188
- const arrowFunction = t.arrowFunctionExpression([], awaitedImport);
189
- arrowFunction.async = true;
190
- return t.objectProperty(t.identifier("component"), arrowFunction);
191
- }
192
- function hasRuntimeImport(ast, name) {
193
- for (const statement of ast.program.body) {
194
- if (!t.isImportDeclaration(statement)) {
195
- continue;
196
- }
197
- if (statement.source.value !== RUNTIME_IMPORT) {
198
- continue;
199
- }
200
- for (const specifier of statement.specifiers) {
201
- if (t.isImportSpecifier(specifier) &&
202
- t.isIdentifier(specifier.imported) &&
203
- specifier.imported.name === name) {
204
- return true;
205
- }
206
- }
207
- }
208
- return false;
209
- }
210
- function injectRuntimeImport(ast, name) {
211
- if (hasRuntimeImport(ast, name)) {
212
- return;
213
- }
214
- for (const statement of ast.program.body) {
215
- if (!t.isImportDeclaration(statement)) {
216
- continue;
217
- }
218
- if (statement.source.value !== RUNTIME_IMPORT) {
219
- continue;
220
- }
221
- statement.specifiers.push(t.importSpecifier(t.identifier(name), t.identifier(name)));
222
- return;
223
- }
224
- ast.program.body.unshift(t.importDeclaration([t.importSpecifier(t.identifier(name), t.identifier(name))], t.stringLiteral(RUNTIME_IMPORT)));
225
- }
226
- export function transformer(code, id) {
227
- const ast = parse(code, {
228
- sourceType: "module",
229
- plugins: ["typescript", "jsx"],
230
- });
231
- let changed = false;
232
- const entries = [];
233
- traverse(ast, {
234
- CallExpression(callPath) {
235
- const call = callPath.node;
236
- /*
237
- * Find:
238
- *
239
- * res.send(...)
240
- */
241
- if (!t.isMemberExpression(call.callee)) {
242
- return;
243
- }
244
- if (!t.isIdentifier(call.callee.property, {
245
- name: "send",
246
- })) {
247
- return;
248
- }
249
- const argument = call.arguments[0];
250
- /*
251
- * Only transform:
252
- *
253
- * res.send(<HomePage />)
254
- */
255
- if (!argument || !t.isJSXElement(argument)) {
256
- return;
257
- }
258
- /*
259
- * Find:
260
- *
261
- * (req, res) => {}
262
- */
263
- const functionPath = callPath.getFunctionParent();
264
- if (!functionPath) {
265
- return;
266
- }
267
- const params = functionPath.node.params;
268
- if (params.length < 2) {
269
- return;
270
- }
271
- const requestParam = params[0];
272
- const responseParam = params[1];
273
- if (!t.isIdentifier(requestParam) || !t.isIdentifier(responseParam)) {
274
- return;
275
- }
276
- /*
277
- * Find component name:
278
- *
279
- * <HomePage />
280
- */
281
- const componentName = getJSXComponentName(argument);
282
- if (!componentName) {
283
- return;
284
- }
285
- /*
286
- * Find:
287
- *
288
- * import HomePage from "../pages/Home";
289
- */
290
- const componentImport = findComponentImport(ast, componentName);
291
- if (!componentImport) {
292
- return;
293
- }
294
- const componentImportPath = componentImport.file;
295
- /*
296
- * Resolve:
297
- *
298
- * ../pages/Home
299
- *
300
- * ->
301
- *
302
- * C:/xampp/htdocs/xanix/app/pages/Home/index.tsx
303
- */
304
- const componentFile = resolveComponentFile(id, componentImportPath);
305
- if (!path.isAbsolute(componentFile)) {
306
- return;
307
- }
308
- /*
309
- * Normalize absolute file path.
310
- */
311
- const normalizedFile = normalizeFilePath(componentFile);
312
- /*
313
- * Stable page ID.
314
- */
315
- const pageId = createPageId(normalizedFile);
316
- /*
317
- * JSX:
318
- *
319
- * <HomePage title="Hello" />
320
- *
321
- * ->
322
- *
323
- * {
324
- * title: "Hello"
325
- * }
326
- */
327
- const componentAndProps = jsxToComponentAndProps(argument);
328
- if (!componentAndProps) {
329
- return;
330
- }
331
- /*
332
- * Create manifest entry.
333
- */
334
- entries.push({
335
- id: pageId,
336
- name: componentName,
337
- file: normalizedFile,
338
- path: componentImportPath,
339
- export: componentImport.export,
340
- });
341
- /*
342
- * Remove static component import.
343
- */
344
- removeComponentImport(ast, componentImport);
345
- /*
346
- * Create:
347
- *
348
- * component: async () =>
349
- * await import("../pages/Home")
350
- */
351
- const componentProperty = createDynamicComponentProperty(componentImportPath);
352
- /*
353
- * Make route handler async.
354
- */
355
- if (t.isFunction(functionPath.node)) {
356
- functionPath.node.async = true;
357
- }
358
- /*
359
- * Create:
360
- *
361
- * await xanixPage({
362
- * component: async () =>
363
- * await import("../pages/Home"),
364
- * pageId: "...",
365
- * req,
366
- * res,
367
- * props: {}
368
- * })
369
- */
370
- const pageOptions = t.objectExpression([
371
- componentProperty,
372
- t.objectProperty(t.identifier("pageId"), t.stringLiteral(pageId)),
373
- t.objectProperty(t.identifier("req"), t.identifier(requestParam.name)),
374
- t.objectProperty(t.identifier("res"), t.identifier(responseParam.name)),
375
- t.objectProperty(t.identifier("props"), componentAndProps.props),
376
- ]);
377
- const pageCall = t.awaitExpression(t.callExpression(t.identifier("xanixPage"), [pageOptions]));
378
- /*
379
- * Replace:
380
- *
381
- * res.send(<HomePage />);
382
- *
383
- * with:
384
- *
385
- * res.send(
386
- * await xanixPage({...})
387
- * );
388
- */
389
- call.arguments[0] = pageCall;
390
- changed = true;
391
- },
392
- });
393
- if (!changed) {
394
- return null;
395
- }
396
- /*
397
- * Add:
398
- *
399
- * import { xanixPage } from "xanix/runtime";
400
- */
401
- injectRuntimeImport(ast, "xanixPage");
402
- /*
403
- * Generate final code + source map.
404
- */
405
- const output = generate(ast, {
406
- sourceMaps: true,
407
- sourceFileName: id,
408
- }, code);
409
- return {
410
- code: output.code,
411
- map: output.map,
412
- entries,
413
- };
414
- }
@@ -1,23 +0,0 @@
1
- export interface XanixPageEntry {
2
- id: string;
3
- name: string;
4
- file: string;
5
- path: string;
6
- export: string;
7
- }
8
- export interface XanixTransformResult {
9
- code: string;
10
- entries: XanixPageEntry[];
11
- }
12
- type AstNode = {
13
- type: string;
14
- start?: number;
15
- end?: number;
16
- [key: string]: any;
17
- };
18
- export declare function normalizeFilePath(file: string): string;
19
- export declare function resolveFile(file: string): string;
20
- export declare function resolveComponentFile(importer: string, importPath: string): string;
21
- export declare function createPageId(file: string): string;
22
- export declare function transformer(code: string, id: string, ast: AstNode): XanixTransformResult | null;
23
- export {};