vxrn 0.1.19 → 0.1.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 (270) hide show
  1. package/dist/cjs/build.js +57 -8
  2. package/dist/cjs/build.js.map +2 -2
  3. package/dist/cjs/build.native.js +180 -8
  4. package/dist/cjs/build.native.js.map +2 -2
  5. package/dist/cjs/cli.js +77 -3
  6. package/dist/cjs/cli.js.map +2 -2
  7. package/dist/cjs/cli.native.js +268 -2
  8. package/dist/cjs/cli.native.js.map +2 -2
  9. package/dist/cjs/constants.js +21 -0
  10. package/dist/cjs/constants.js.map +6 -0
  11. package/dist/cjs/constants.native.js +26 -0
  12. package/dist/cjs/constants.native.js.map +6 -0
  13. package/dist/cjs/createDevServer.js +9 -10
  14. package/dist/cjs/createDevServer.js.map +1 -1
  15. package/dist/cjs/createDevServer.native.js +9 -11
  16. package/dist/cjs/createDevServer.native.js.map +2 -2
  17. package/dist/cjs/createExpoServer.js +34 -0
  18. package/dist/cjs/createExpoServer.js.map +6 -0
  19. package/dist/cjs/createExpoServer.native.js +42 -0
  20. package/dist/cjs/createExpoServer.native.js.map +6 -0
  21. package/dist/cjs/{getBaseViteConfig.js → dispose.js} +12 -24
  22. package/dist/cjs/dispose.js.map +6 -0
  23. package/dist/cjs/{getBaseViteConfig.native.js → dispose.native.js} +16 -26
  24. package/dist/cjs/dispose.native.js.map +6 -0
  25. package/dist/cjs/exports/build.js +145 -0
  26. package/dist/cjs/exports/build.js.map +6 -0
  27. package/dist/cjs/exports/build.native.js +409 -0
  28. package/dist/cjs/exports/build.native.js.map +6 -0
  29. package/dist/cjs/exports/checkPatches.js +62 -0
  30. package/dist/cjs/exports/checkPatches.js.map +6 -0
  31. package/dist/cjs/{getOptionsFIlled.native.js → exports/checkPatches.native.js} +106 -124
  32. package/dist/cjs/exports/checkPatches.native.js.map +6 -0
  33. package/dist/cjs/{create.js → exports/dev.js} +90 -168
  34. package/dist/cjs/exports/dev.js.map +6 -0
  35. package/dist/cjs/{create.native.js → exports/dev.native.js} +148 -357
  36. package/dist/cjs/exports/dev.native.js.map +6 -0
  37. package/dist/cjs/{dev/bindKeypressInput.js → exports/serve.js} +31 -40
  38. package/dist/cjs/exports/serve.js.map +6 -0
  39. package/dist/cjs/{getVitePath.native.js → exports/serve.native.js} +99 -60
  40. package/dist/cjs/exports/serve.native.js.map +6 -0
  41. package/dist/cjs/index.js +2 -3
  42. package/dist/cjs/index.js.map +1 -1
  43. package/dist/cjs/index.native.js +3 -5
  44. package/dist/cjs/index.native.js.map +1 -1
  45. package/dist/cjs/plugins/clientBundleTreeShakePlugin.js +69 -0
  46. package/dist/cjs/plugins/clientBundleTreeShakePlugin.js.map +6 -0
  47. package/dist/cjs/plugins/clientBundleTreeShakePlugin.native.js +97 -0
  48. package/dist/cjs/plugins/clientBundleTreeShakePlugin.native.js.map +6 -0
  49. package/dist/cjs/plugins/clientInjectPlugin.js +66 -0
  50. package/dist/cjs/plugins/clientInjectPlugin.js.map +6 -0
  51. package/dist/cjs/plugins/clientInjectPlugin.native.js +190 -0
  52. package/dist/cjs/plugins/clientInjectPlugin.native.js.map +6 -0
  53. package/dist/cjs/plugins/reactNativeCommonJsPlugin.js +89 -0
  54. package/dist/cjs/plugins/reactNativeCommonJsPlugin.js.map +6 -0
  55. package/dist/cjs/plugins/reactNativeCommonJsPlugin.native.js +346 -0
  56. package/dist/cjs/plugins/reactNativeCommonJsPlugin.native.js.map +6 -0
  57. package/dist/cjs/utils/checkPatches.js +62 -0
  58. package/dist/cjs/utils/checkPatches.js.map +6 -0
  59. package/dist/cjs/{VXRNConfigFilled.native.js → utils/checkPatches.native.js} +106 -124
  60. package/dist/cjs/utils/checkPatches.native.js.map +6 -0
  61. package/dist/cjs/utils/dispose.js +28 -0
  62. package/dist/cjs/utils/dispose.js.map +6 -0
  63. package/dist/cjs/utils/dispose.native.js +36 -0
  64. package/dist/cjs/utils/dispose.native.js.map +6 -0
  65. package/dist/cjs/utils/getBaseViteConfig.js +2 -2
  66. package/dist/cjs/utils/getBaseViteConfig.js.map +1 -1
  67. package/dist/cjs/utils/getBaseViteConfig.native.js +2 -2
  68. package/dist/cjs/utils/getBaseViteConfig.native.js.map +1 -1
  69. package/dist/cjs/utils/getOptionsFilled.js +3 -3
  70. package/dist/cjs/utils/getOptionsFilled.js.map +2 -2
  71. package/dist/cjs/utils/getOptionsFilled.native.js +4 -3
  72. package/dist/cjs/utils/getOptionsFilled.native.js.map +2 -2
  73. package/dist/cjs/utils/patches.js +62 -0
  74. package/dist/cjs/utils/patches.js.map +6 -0
  75. package/dist/cjs/utils/patches.native.js +289 -0
  76. package/dist/cjs/utils/patches.native.js.map +6 -0
  77. package/dist/cjs/utils/prerender.js +1 -0
  78. package/dist/cjs/utils/prerender.js.map +6 -0
  79. package/dist/cjs/utils/prerender.native.js +2 -0
  80. package/dist/cjs/utils/prerender.native.js.map +6 -0
  81. package/dist/cjs/vendor/createExpoServer.js +34 -0
  82. package/dist/cjs/vendor/createExpoServer.js.map +6 -0
  83. package/dist/cjs/vendor/createExpoServer.native.js +42 -0
  84. package/dist/cjs/vendor/createExpoServer.native.js.map +6 -0
  85. package/dist/esm/build.js +51 -6
  86. package/dist/esm/build.js.map +2 -2
  87. package/dist/esm/build.mjs +68 -5
  88. package/dist/esm/build.native.js +174 -6
  89. package/dist/esm/build.native.js.map +2 -2
  90. package/dist/esm/cli.js +58 -3
  91. package/dist/esm/cli.js.map +2 -2
  92. package/dist/esm/cli.mjs +81 -9
  93. package/dist/esm/cli.native.js +249 -3
  94. package/dist/esm/cli.native.js.map +2 -2
  95. package/dist/esm/constants.js +5 -0
  96. package/dist/esm/constants.js.map +6 -0
  97. package/dist/esm/constants.mjs +2 -0
  98. package/dist/esm/constants.native.js +5 -0
  99. package/dist/esm/constants.native.js.map +6 -0
  100. package/dist/esm/createDevServer.js +10 -9
  101. package/dist/esm/createDevServer.js.map +1 -1
  102. package/dist/esm/createDevServer.mjs +8 -12
  103. package/dist/esm/createDevServer.native.js +10 -10
  104. package/dist/esm/createDevServer.native.js.map +2 -2
  105. package/dist/esm/createExpoServer.js +20 -0
  106. package/dist/esm/createExpoServer.js.map +6 -0
  107. package/dist/esm/createExpoServer.mjs +17 -0
  108. package/dist/esm/createExpoServer.native.js +22 -0
  109. package/dist/esm/createExpoServer.native.js.map +6 -0
  110. package/dist/esm/dispose.js +12 -0
  111. package/dist/esm/dispose.js.map +6 -0
  112. package/dist/esm/dispose.mjs +8 -0
  113. package/dist/esm/dispose.native.js +14 -0
  114. package/dist/esm/dispose.native.js.map +6 -0
  115. package/dist/esm/exports/build.js +128 -0
  116. package/dist/esm/exports/build.js.map +6 -0
  117. package/dist/esm/exports/build.mjs +119 -0
  118. package/dist/esm/exports/build.native.js +385 -0
  119. package/dist/esm/exports/build.native.js.map +6 -0
  120. package/dist/esm/exports/checkPatches.js +40 -0
  121. package/dist/esm/exports/checkPatches.js.map +6 -0
  122. package/dist/esm/exports/checkPatches.mjs +31 -0
  123. package/dist/esm/exports/checkPatches.native.js +262 -0
  124. package/dist/esm/exports/checkPatches.native.js.map +6 -0
  125. package/dist/esm/{create.js → exports/dev.js} +94 -171
  126. package/dist/esm/exports/dev.js.map +6 -0
  127. package/dist/esm/exports/dev.mjs +543 -0
  128. package/dist/esm/{create.native.js → exports/dev.native.js} +150 -359
  129. package/dist/esm/exports/dev.native.js.map +6 -0
  130. package/dist/esm/exports/serve.js +33 -0
  131. package/dist/esm/exports/serve.js.map +6 -0
  132. package/dist/esm/exports/serve.mjs +38 -0
  133. package/dist/esm/exports/serve.native.js +209 -0
  134. package/dist/esm/exports/serve.native.js.map +6 -0
  135. package/dist/esm/index.js +3 -5
  136. package/dist/esm/index.js.map +1 -1
  137. package/dist/esm/index.mjs +3 -4
  138. package/dist/esm/index.native.js +3 -5
  139. package/dist/esm/index.native.js.map +2 -2
  140. package/dist/esm/plugins/clientBundleTreeShakePlugin.js +46 -0
  141. package/dist/esm/plugins/clientBundleTreeShakePlugin.js.map +6 -0
  142. package/dist/esm/plugins/clientBundleTreeShakePlugin.mjs +41 -0
  143. package/dist/esm/plugins/clientBundleTreeShakePlugin.native.js +70 -0
  144. package/dist/esm/plugins/clientBundleTreeShakePlugin.native.js.map +6 -0
  145. package/dist/esm/plugins/clientInjectPlugin.js +42 -0
  146. package/dist/esm/plugins/clientInjectPlugin.js.map +6 -0
  147. package/dist/esm/plugins/clientInjectPlugin.mjs +56 -0
  148. package/dist/esm/plugins/clientInjectPlugin.native.js +161 -0
  149. package/dist/esm/plugins/clientInjectPlugin.native.js.map +6 -0
  150. package/dist/esm/plugins/reactNativeCommonJsPlugin.js +75 -0
  151. package/dist/esm/plugins/reactNativeCommonJsPlugin.js.map +6 -0
  152. package/dist/esm/plugins/reactNativeCommonJsPlugin.mjs +55 -0
  153. package/dist/esm/plugins/reactNativeCommonJsPlugin.native.js +327 -0
  154. package/dist/esm/plugins/reactNativeCommonJsPlugin.native.js.map +6 -0
  155. package/dist/esm/utils/checkPatches.js +40 -0
  156. package/dist/esm/utils/checkPatches.js.map +6 -0
  157. package/dist/esm/utils/checkPatches.mjs +31 -0
  158. package/dist/esm/utils/checkPatches.native.js +262 -0
  159. package/dist/esm/utils/checkPatches.native.js.map +6 -0
  160. package/dist/esm/utils/dispose.js +12 -0
  161. package/dist/esm/utils/dispose.js.map +6 -0
  162. package/dist/esm/utils/dispose.mjs +8 -0
  163. package/dist/esm/utils/dispose.native.js +14 -0
  164. package/dist/esm/utils/dispose.native.js.map +6 -0
  165. package/dist/esm/utils/getBaseViteConfig.js +1 -1
  166. package/dist/esm/utils/getBaseViteConfig.mjs +1 -1
  167. package/dist/esm/utils/getBaseViteConfig.native.js +1 -1
  168. package/dist/esm/utils/getOptionsFilled.js +3 -1
  169. package/dist/esm/utils/getOptionsFilled.js.map +2 -2
  170. package/dist/esm/utils/getOptionsFilled.mjs +3 -1
  171. package/dist/esm/utils/getOptionsFilled.native.js +4 -2
  172. package/dist/esm/utils/getOptionsFilled.native.js.map +2 -2
  173. package/dist/esm/utils/patches.js +40 -0
  174. package/dist/esm/utils/patches.js.map +6 -0
  175. package/dist/esm/utils/patches.mjs +31 -0
  176. package/dist/esm/utils/patches.native.js +262 -0
  177. package/dist/esm/utils/patches.native.js.map +6 -0
  178. package/dist/esm/utils/prerender.js +1 -0
  179. package/dist/esm/utils/prerender.js.map +6 -0
  180. package/dist/esm/utils/prerender.mjs +0 -0
  181. package/dist/esm/utils/prerender.native.js +1 -0
  182. package/dist/esm/utils/prerender.native.js.map +6 -0
  183. package/dist/esm/vendor/createExpoServer.js +20 -0
  184. package/dist/esm/vendor/createExpoServer.js.map +6 -0
  185. package/dist/esm/vendor/createExpoServer.mjs +17 -0
  186. package/dist/esm/vendor/createExpoServer.native.js +22 -0
  187. package/dist/esm/vendor/createExpoServer.native.js.map +6 -0
  188. package/package.json +9 -6
  189. package/react-native-template.js +11 -0
  190. package/src/cli.ts +78 -2
  191. package/src/exports/build.ts +197 -0
  192. package/src/{createDevServer.ts → exports/dev.ts} +25 -72
  193. package/src/exports/serve.ts +51 -0
  194. package/src/index.ts +3 -3
  195. package/src/plugins/clientBundleTreeShakePlugin.tsx +105 -0
  196. package/src/{dev → plugins}/clientInjectPlugin.ts +1 -12
  197. package/src/{nativePlugin.ts → plugins/reactNativeCommonJsPlugin.ts} +3 -26
  198. package/src/utils/getBaseViteConfig.ts +1 -1
  199. package/src/utils/getOptionsFilled.ts +6 -1
  200. package/src/utils/patches.ts +54 -0
  201. package/src/vendor/createExpoServer.ts +27 -0
  202. package/types/build.d.ts +4 -4
  203. package/types/constants.d.ts +2 -0
  204. package/types/createExpoServer.d.ts +4 -0
  205. package/types/dispose.d.ts +3 -0
  206. package/types/exports/build.d.ts +4 -0
  207. package/types/exports/dev.d.ts +13 -0
  208. package/types/exports/serve.d.ts +3 -0
  209. package/types/index.d.ts +2 -3
  210. package/types/plugins/clientBundleTreeShakePlugin.d.ts +13 -0
  211. package/types/plugins/clientBundleTreeShakePlugni.d.ts +13 -0
  212. package/types/plugins/clientInjectPlugin.d.ts +7 -0
  213. package/types/plugins/reactNativeCommonJsPlugin.d.ts +7 -0
  214. package/types/plugins/server-tree-shake.d.ts +13 -0
  215. package/types/utils/checkPatches.d.ts +3 -0
  216. package/types/utils/dispose.d.ts +3 -0
  217. package/types/utils/patches.d.ts +3 -0
  218. package/types/utils/prerender.d.ts +1 -0
  219. package/types/vendor/createExpoServer.d.ts +4 -0
  220. package/dist/cjs/VXRNConfigFilled.js +0 -53
  221. package/dist/cjs/VXRNConfigFilled.js.map +0 -6
  222. package/dist/cjs/VXRNConfigFilled.native.js.map +0 -6
  223. package/dist/cjs/create.js.map +0 -6
  224. package/dist/cjs/create.native.js.map +0 -6
  225. package/dist/cjs/dev/bindKeypressInput.js.map +0 -6
  226. package/dist/cjs/dev/bindKeypressInput.native.js +0 -73
  227. package/dist/cjs/dev/bindKeypressInput.native.js.map +0 -6
  228. package/dist/cjs/getBaseViteConfig.js.map +0 -6
  229. package/dist/cjs/getBaseViteConfig.native.js.map +0 -6
  230. package/dist/cjs/getOptionsFIlled.js +0 -53
  231. package/dist/cjs/getOptionsFIlled.js.map +0 -6
  232. package/dist/cjs/getOptionsFIlled.native.js.map +0 -6
  233. package/dist/cjs/getVitePath.js +0 -50
  234. package/dist/cjs/getVitePath.js.map +0 -6
  235. package/dist/cjs/getVitePath.native.js.map +0 -6
  236. package/dist/esm/VXRNConfigFilled.js +0 -30
  237. package/dist/esm/VXRNConfigFilled.js.map +0 -6
  238. package/dist/esm/VXRNConfigFilled.mjs +0 -34
  239. package/dist/esm/VXRNConfigFilled.native.js +0 -279
  240. package/dist/esm/VXRNConfigFilled.native.js.map +0 -6
  241. package/dist/esm/create.js.map +0 -6
  242. package/dist/esm/create.mjs +0 -624
  243. package/dist/esm/create.native.js.map +0 -6
  244. package/dist/esm/dev/bindKeypressInput.js +0 -40
  245. package/dist/esm/dev/bindKeypressInput.js.map +0 -6
  246. package/dist/esm/dev/bindKeypressInput.mjs +0 -41
  247. package/dist/esm/dev/bindKeypressInput.native.js +0 -44
  248. package/dist/esm/dev/bindKeypressInput.native.js.map +0 -6
  249. package/dist/esm/getBaseViteConfig.js +0 -24
  250. package/dist/esm/getBaseViteConfig.js.map +0 -6
  251. package/dist/esm/getBaseViteConfig.mjs +0 -23
  252. package/dist/esm/getBaseViteConfig.native.js +0 -25
  253. package/dist/esm/getBaseViteConfig.native.js.map +0 -6
  254. package/dist/esm/getOptionsFIlled.js +0 -30
  255. package/dist/esm/getOptionsFIlled.js.map +0 -6
  256. package/dist/esm/getOptionsFIlled.mjs +0 -34
  257. package/dist/esm/getOptionsFIlled.native.js +0 -279
  258. package/dist/esm/getOptionsFIlled.native.js.map +0 -6
  259. package/dist/esm/getVitePath.js +0 -28
  260. package/dist/esm/getVitePath.js.map +0 -6
  261. package/dist/esm/getVitePath.mjs +0 -21
  262. package/dist/esm/getVitePath.native.js +0 -170
  263. package/dist/esm/getVitePath.native.js.map +0 -6
  264. package/src/build.ts +0 -96
  265. package/src/commands/cli-build.ts +0 -25
  266. package/src/commands/cli-dev.ts +0 -47
  267. package/src/commands/index.ts +0 -10
  268. package/src/dev/createDevServer.ts +0 -234
  269. /package/src/{utils/constants.ts → constants.ts} +0 -0
  270. /package/src/{utils.ts → utils/dispose.ts} +0 -0
@@ -0,0 +1,197 @@
1
+ import { resolve as importMetaResolve } from 'import-meta-resolve'
2
+ import fs from 'node:fs'
3
+ import path from 'node:path'
4
+ import { mergeConfig, build as viteBuild, type UserConfig } from 'vite'
5
+
6
+ import FSExtra from 'fs-extra'
7
+ import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
8
+ import { clientBundleTreeShakePlugin } from '../plugins/clientBundleTreeShakePlugin'
9
+ import type { VXRNConfig } from '../types'
10
+ import { getBaseViteConfig } from '../utils/getBaseViteConfig'
11
+ import { getOptionsFilled, type VXRNConfigFilled } from '../utils/getOptionsFilled'
12
+
13
+ export const resolveFile = (path: string) => {
14
+ try {
15
+ return importMetaResolve(path, import.meta.url).replace('file://', '')
16
+ } catch {
17
+ return require.resolve(path)
18
+ }
19
+ }
20
+
21
+ const { ensureDir, existsSync, readFile } = FSExtra
22
+
23
+ const extensions = [
24
+ '.web.tsx',
25
+ '.tsx',
26
+ '.web.ts',
27
+ '.ts',
28
+ '.web.jsx',
29
+ '.jsx',
30
+ '.web.js',
31
+ '.js',
32
+ '.css',
33
+ '.json',
34
+ ]
35
+
36
+ export const build = async (optionsIn: VXRNConfig) => {
37
+ const options = await getOptionsFilled(optionsIn)
38
+ const depsToOptimize = [
39
+ 'react',
40
+ 'react-dom',
41
+ '@react-native/normalize-color',
42
+ '@react-navigation/native',
43
+ 'expo-constants',
44
+ 'expo-modules-core',
45
+ 'expo-status-bar',
46
+ ]
47
+
48
+ let buildConfig = mergeConfig(
49
+ getBaseViteConfig({
50
+ mode: 'production',
51
+ }),
52
+ {
53
+ root: options.root,
54
+ clearScreen: false,
55
+ optimizeDeps: {
56
+ include: depsToOptimize,
57
+ esbuildOptions: {
58
+ resolveExtensions: extensions,
59
+ },
60
+ },
61
+ }
62
+ ) satisfies UserConfig
63
+
64
+ if (options.webConfig) {
65
+ buildConfig = mergeConfig(buildConfig, options.webConfig) as any
66
+ }
67
+
68
+ console.info(`build client`)
69
+ await viteBuild(
70
+ mergeConfig(buildConfig, {
71
+ plugins: [clientBundleTreeShakePlugin({})],
72
+ build: {
73
+ ssrManifest: true,
74
+ outDir: 'dist/client',
75
+ },
76
+ } satisfies UserConfig)
77
+ )
78
+
79
+ console.info(`build server`)
80
+ const { output } = (await viteBuild(
81
+ mergeConfig(buildConfig, {
82
+ resolve: {
83
+ alias: {
84
+ 'react-native': 'react-native-web-lite',
85
+ },
86
+ },
87
+ optimizeDeps: {
88
+ esbuildOptions: {
89
+ format: 'cjs',
90
+ },
91
+ },
92
+ ssr: {
93
+ noExternal: true,
94
+ },
95
+ build: {
96
+ // we want one big file of css
97
+ cssCodeSplit: false,
98
+ ssr: 'src/entry-server.tsx',
99
+ outDir: 'dist/server',
100
+ rollupOptions: {
101
+ external: [],
102
+ },
103
+ },
104
+ } satisfies UserConfig)
105
+ )) as RollupOutput
106
+
107
+ console.info(`generating static pages`)
108
+ await generateStaticPages(options, output)
109
+ }
110
+
111
+ async function generateStaticPages(
112
+ options: VXRNConfigFilled,
113
+ serverOutput: (OutputChunk | OutputAsset)[]
114
+ ) {
115
+ const toAbsolute = (p) => path.resolve(options.root, p)
116
+
117
+ const staticDir = toAbsolute(`dist/static`)
118
+ await ensureDir(staticDir)
119
+ const template = fs.readFileSync(toAbsolute('index.html'), 'utf-8')
120
+
121
+ const render = (await import(`${options.root}/dist/server/entry-server.js`)).render
122
+
123
+ // load routes
124
+ const entry = serverOutput.find(
125
+ (x) => x.type === 'chunk' && x.facadeModuleId?.includes('entry-server')
126
+ )
127
+
128
+ const assets: OutputAsset[] = []
129
+
130
+ const allRoutes = (
131
+ await Promise.all(
132
+ serverOutput.flatMap(async (output) => {
133
+ if (output.type === 'asset') {
134
+ assets.push(output)
135
+ return []
136
+ }
137
+
138
+ const id = output.facadeModuleId || ''
139
+ const file = path.basename(id)
140
+ const name = file.replace(/\.[^/.]+$/, '')
141
+
142
+ if (!id || file[0] === '_' || file.includes('entry-server')) {
143
+ return []
144
+ }
145
+
146
+ const endpointPath = path.join(options.root, 'dist/server', output.fileName)
147
+ const exported = await import(endpointPath)
148
+ const staticParams = await exported.generateStaticParams?.()
149
+ const result = (staticParams || [{}]).map((params) => {
150
+ return getUrl(params)
151
+ })
152
+
153
+ function getUrl(params = {}) {
154
+ return name === 'index'
155
+ ? '/'
156
+ : `/${name
157
+ .split('/')
158
+ .map((part) => {
159
+ if (part[0] === '[') {
160
+ const found = params[part.slice(1, part.length - 1)]
161
+ if (!found) {
162
+ console.warn('not found', { params, part })
163
+ }
164
+ return found
165
+ }
166
+ return part
167
+ })
168
+ .join('/')}`
169
+ }
170
+
171
+ return result
172
+ })
173
+ )
174
+ ).flat()
175
+
176
+ // for now just inline
177
+ const cssString = assets
178
+ .filter((x) => x.name?.endsWith('.css'))
179
+ .map((x) => x.source)
180
+ .join('\n\n')
181
+
182
+ // pre-render each route...
183
+ for (const path of allRoutes) {
184
+ const { appHtml, headHtml } = await render({ path })
185
+ const slashFileName = `${path === '/' ? '/index' : path}.html`
186
+ const clientHtmlPath = toAbsolute(`dist/client${slashFileName}`)
187
+ const clientHtml = existsSync(clientHtmlPath) ? await readFile(clientHtmlPath, 'utf-8') : null
188
+ const html = (clientHtml || template)
189
+ .replace(`<!--ssr-outlet-->`, appHtml)
190
+ .replace(
191
+ `<!--head-outlet-->`,
192
+ `${headHtml}\n${cssString ? `<style>${cssString}</style>` : ``}`
193
+ )
194
+ const filePath = toAbsolute(`dist/static${slashFileName}`)
195
+ fs.writeFileSync(toAbsolute(filePath), html)
196
+ }
197
+ }
@@ -1,5 +1,4 @@
1
1
  import wsAdapter from 'crossws/adapters/node'
2
- import findNodeModules from 'find-node-modules'
3
2
  import {
4
3
  createApp,
5
4
  createRouter,
@@ -14,7 +13,6 @@ import { readFile } from 'node:fs/promises'
14
13
  import { createServer as nodeCreateServer } from 'node:http'
15
14
  import { dirname, join, relative, resolve } from 'node:path'
16
15
  import readline from 'node:readline'
17
- import viteInspectPlugin from 'vite-plugin-inspect'
18
16
  import { WebSocket } from 'ws'
19
17
 
20
18
  import * as babel from '@babel/core'
@@ -36,12 +34,15 @@ import {
36
34
  import createViteFlow from '@vxrn/vite-flow'
37
35
  import type { Peer } from 'crossws'
38
36
  import { resolve as importMetaResolve } from 'import-meta-resolve'
39
- import { clientInjectionsPlugin } from './dev/clientInjectPlugin'
40
- import { nativePlugin } from './nativePlugin'
41
- import type { VXRNConfig } from './types'
42
- import { getBaseViteConfig } from './utils/getBaseViteConfig'
43
- import { getOptionsFilled, type VXRNConfigFilled } from './utils/getOptionsFilled'
44
- import { getVitePath } from './utils/getVitePath'
37
+ import { clientBundleTreeShakePlugin } from '../plugins/clientBundleTreeShakePlugin'
38
+ import { clientInjectionsPlugin } from '../plugins/clientInjectPlugin'
39
+ import { reactNativeCommonJsPlugin } from '../plugins/reactNativeCommonJsPlugin'
40
+ import type { VXRNConfig } from '../types'
41
+ import { getBaseViteConfig } from '../utils/getBaseViteConfig'
42
+ import { getOptionsFilled } from '../utils/getOptionsFilled'
43
+ import { getVitePath } from '../utils/getVitePath'
44
+ import { checkPatches } from '../utils/patches'
45
+ import { createExpoServer } from '../vendor/createExpoServer'
45
46
 
46
47
  export const resolveFile = (path: string) => {
47
48
  try {
@@ -77,58 +78,7 @@ const extensions = [
77
78
 
78
79
  const { ensureDir, pathExists, pathExistsSync } = FSExtra
79
80
 
80
- const patches = [
81
- {
82
- module: 'react-native-screens',
83
- patchFile: 'react-native-screens+3.22.1.patch',
84
- },
85
- ]
86
-
87
- type Patch = (typeof patches)[0]
88
-
89
- async function checkPatches(options: VXRNConfigFilled) {
90
- if (options.state.applyPatches === false) {
91
- return
92
- }
93
-
94
- const nodeModulesDirs = findNodeModules({
95
- cwd: options.root,
96
- }).map((relativePath) => join(options.root, relativePath))
97
-
98
- const patchesToCopy = new Set<Patch>()
99
-
100
- await Promise.all(
101
- patches.flatMap((patch) => {
102
- return nodeModulesDirs.flatMap(async (dir) => {
103
- if (await FSExtra.pathExists(join(dir, patch.module))) {
104
- patchesToCopy.add(patch)
105
- }
106
- })
107
- })
108
- )
109
-
110
- let didCopy = false
111
-
112
- for (const patch of [...patchesToCopy]) {
113
- const dest = join(options.userPatchesDir, patch.patchFile)
114
- if (!(await pathExists(dest))) {
115
- didCopy = true
116
- console.info(`Copying patch ${patch.module}`)
117
- const src = join(options.internalPatchesDir, patch.patchFile)
118
- await FSExtra.copy(src, dest)
119
- }
120
- }
121
-
122
- if (didCopy) {
123
- console.info(
124
- `\nPlease restart after applying the patch by running "npx patch-package".
125
- Ideally add it to your devDependencies and as a postinstall script.\n`
126
- )
127
- process.exit(0)
128
- }
129
- }
130
-
131
- export const createDevServer = async (optionsIn: VXRNConfig) => {
81
+ export const dev = async (optionsIn: VXRNConfig) => {
132
82
  const options = await getOptionsFilled(optionsIn)
133
83
  const { host, port, root, cacheDir } = options
134
84
 
@@ -363,7 +313,11 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
363
313
  {
364
314
  root,
365
315
  clearScreen: false,
366
- plugins: [reactNativeHMRPlugin],
316
+ plugins: [
317
+ reactNativeHMRPlugin,
318
+
319
+ clientBundleTreeShakePlugin({}),
320
+ ],
367
321
  optimizeDeps: {
368
322
  include: depsToOptimize,
369
323
  exclude: Object.values(virtualModules).map((v) => v.alias),
@@ -379,7 +333,7 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
379
333
  cors: true,
380
334
  host,
381
335
  },
382
- }
336
+ } satisfies UserConfig
383
337
  ) satisfies InlineConfig
384
338
 
385
339
  if (options.webConfig) {
@@ -430,6 +384,8 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
430
384
  },
431
385
  })
432
386
 
387
+ createExpoServer(root, app)
388
+
433
389
  router.get(
434
390
  '/file',
435
391
  defineEventHandler((e) => {
@@ -557,6 +513,7 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
557
513
  data: string[]
558
514
  }
559
515
 
516
+ // react native log bridge
560
517
  app.use(
561
518
  '/__client',
562
519
  defineWebSocketHandler({
@@ -601,7 +558,7 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
601
558
  server.on('upgrade', handleUpgrade)
602
559
 
603
560
  return {
604
- nativeServer: server,
561
+ server,
605
562
  viteServer,
606
563
 
607
564
  async start() {
@@ -655,8 +612,6 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
655
612
  })
656
613
  }
657
614
 
658
- const buildInput = options.entryNative || 'index.jsx'
659
-
660
615
  // build app
661
616
  let buildConfig = {
662
617
  plugins: [
@@ -674,9 +629,10 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
674
629
  },
675
630
  },
676
631
 
632
+ clientBundleTreeShakePlugin({}),
677
633
  viteRNClientPlugin,
678
634
 
679
- nativePlugin({
635
+ reactNativeCommonJsPlugin({
680
636
  root,
681
637
  port,
682
638
  mode: 'build',
@@ -687,11 +643,6 @@ export const createDevServer = async (optionsIn: VXRNConfig) => {
687
643
  mode: 'build',
688
644
  }),
689
645
 
690
- // viteInspectPlugin({
691
- // build: true,
692
- // outputDir: '.vite-inspect',
693
- // }),
694
-
695
646
  {
696
647
  name: 'treat-js-files-as-jsx',
697
648
  async transform(code, id) {
@@ -807,6 +758,8 @@ __require("${outputModule.fileName}")
807
758
  .replaceAll('undefined.accept(function() {});', '')
808
759
  .replaceAll('(void 0).accept(() => {})', '')
809
760
  .replaceAll('(void 0).accept(function() {});', '')
761
+ // TEMP FIX for expo-router tamagui thing since expo router 3 upgrade
762
+ .replaceAll('dist/esm/index.mjs"', 'dist/esm/index.js"')
810
763
 
811
764
  // TODO this is not stable based on cwd
812
765
  const appRootParent = join(root, '..', '..')
@@ -861,7 +814,7 @@ function getIndexJsonResponse({ port, root }: { port: number | string; root }) {
861
814
  staticConfigPath: join(root, 'app.json'),
862
815
  packageJsonPath: join(root, 'package.json'),
863
816
  },
864
- sdkVersion: '47.0.0',
817
+ sdkVersion: '50.0.0',
865
818
  platforms: ['ios', 'android', 'web'],
866
819
  iconUrl: `http://127.0.0.1:${port}/assets/./assets/icon.png`,
867
820
  debuggerHost: `127.0.0.1:${port}`,
@@ -0,0 +1,51 @@
1
+ import sirv from 'sirv'
2
+ import type { VXRNConfig } from '../types'
3
+ import { getOptionsFilled } from '../utils/getOptionsFilled'
4
+ import { createApp, defineEventHandler } from 'h3'
5
+
6
+ export const serve = async (optionsIn: VXRNConfig) => {
7
+ const options = await getOptionsFilled(optionsIn)
8
+
9
+ const app = createApp()
10
+
11
+ const sirvStaticMiddleware = sirv('dist/static', {
12
+ gzip: true,
13
+ })
14
+
15
+ app.use(
16
+ defineEventHandler(async ({ node: { req, res } }) => {
17
+ await new Promise<void>((response) => {
18
+ sirvStaticMiddleware(req, res, () => {
19
+ response()
20
+ })
21
+ })
22
+ })
23
+ )
24
+
25
+ const sirvMiddleware = sirv('dist/client', {
26
+ gzip: true,
27
+ })
28
+
29
+ app.use(
30
+ defineEventHandler(async ({ node: { req, res } }) => {
31
+ await new Promise<void>((response) => {
32
+ sirvMiddleware(req, res, () => {
33
+ response()
34
+ })
35
+ })
36
+ })
37
+ )
38
+ }
39
+
40
+ // app.use(
41
+ // defineEventHandler(async ({ node: { req, res } }) => {
42
+ // const url = req.originalUrl
43
+ // const template = fs.readFileSync(path.resolve('dist/client/index.html'), 'utf-8')
44
+ // // @ts-ignore
45
+ // const render = (await import('./dist/server/entry-server.js')).render
46
+ // const appHtml = await render({ path: url })
47
+ // const html = template.replace(`<!--ssr-outlet-->`, appHtml)
48
+ // res.setHeader('Content-Type', 'text/html')
49
+ // return html
50
+ // })
51
+ // )
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createDevServer as create } from './createDevServer'
2
- export { build } from './build'
3
- export { nativePlugin } from './nativePlugin'
1
+ export { build } from './exports/build'
2
+ export { dev } from './exports/dev'
3
+
4
4
  export * from './types'
@@ -0,0 +1,105 @@
1
+ import type { Node, Program } from 'estree'
2
+ import { walk } from 'estree-walker'
3
+ import MagicString from 'magic-string'
4
+ import type { Plugin } from 'vite'
5
+
6
+ interface TreeShakeTemplatePluginOptions {
7
+ sourcemap?: boolean
8
+ }
9
+
10
+ type AcornNode<N extends Node> = N & { start: number; end: number }
11
+
12
+ export const clientBundleTreeShakePlugin = (options: TreeShakeTemplatePluginOptions) => {
13
+ return {
14
+ name: 'vxrn:client-tree-shake',
15
+ enforce: 'post',
16
+ transform(code, id) {
17
+ if (id.includes('node_modules')) {
18
+ return
19
+ }
20
+ if (!code.includes(`generateStaticParams`)) {
21
+ return
22
+ }
23
+
24
+ const s = new MagicString(code)
25
+ const codeAst = this.parse(code) as AcornNode<Program>
26
+
27
+ walk(codeAst, {
28
+ enter: (node) => {
29
+ if (node.type === 'ExportNamedDeclaration' || node.type === 'VariableDeclaration') {
30
+ let declarators = (
31
+ 'declarations' in node
32
+ ? node.declarations
33
+ : 'declaration' in node
34
+ ? [node.declaration]
35
+ : []
36
+ ) as any[]
37
+
38
+ let shouldRemove = false
39
+
40
+ declarators.forEach((declarator) => {
41
+ if (
42
+ declarator.id.type === 'Identifier' &&
43
+ declarator.id.name === 'generateStaticParams'
44
+ ) {
45
+ shouldRemove = true
46
+ }
47
+ })
48
+
49
+ const replaceStr = `function generateStaticParams() {};`
50
+ const length = node['end'] - node['start']
51
+
52
+ if (shouldRemove) {
53
+ // @ts-ignore
54
+ // s.remove(node.start, node.end + 1)
55
+ s.update(node.start, node.end + 1, replaceStr.padEnd(length - replaceStr.length))
56
+ // make sure it doesnt error with forceExports
57
+ // s.append(`function generateStaticParams {}`)
58
+
59
+ if (node.type === 'ExportNamedDeclaration') {
60
+ // remove import declaration if it exists
61
+ // @ts-ignore
62
+ removeImportDeclaration(codeAst, node, s)
63
+ }
64
+ }
65
+ }
66
+ },
67
+ })
68
+
69
+ if (s.hasChanged()) {
70
+ return {
71
+ code: s.toString(),
72
+ map: options.sourcemap ? s.generateMap({ hires: true }) : undefined,
73
+ }
74
+ }
75
+ },
76
+ } satisfies Plugin
77
+ }
78
+
79
+ function removeImportDeclaration(
80
+ ast: Program,
81
+ importName: string,
82
+ magicString: MagicString
83
+ ): boolean {
84
+ for (const node of ast.body) {
85
+ if (node.type === 'ImportDeclaration') {
86
+ const specifier = node.specifiers.find((s) => s.local.name === importName)
87
+ if (specifier) {
88
+ if (node.specifiers.length > 1) {
89
+ const specifierIndex = node.specifiers.findIndex((s) => s.local.name === importName)
90
+ if (specifierIndex > -1) {
91
+ magicString.remove(
92
+ (node.specifiers[specifierIndex] as AcornNode<Node>).start,
93
+ (node.specifiers[specifierIndex] as AcornNode<Node>).end + 1
94
+ )
95
+ node.specifiers.splice(specifierIndex, 1)
96
+ }
97
+ } else {
98
+ magicString.remove((node as AcornNode<Node>).start, (node as AcornNode<Node>).end)
99
+ }
100
+ return true
101
+ }
102
+ }
103
+ }
104
+ return false
105
+ }
@@ -81,15 +81,6 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
81
81
  transform(code, id, options) {
82
82
  if (id.includes('vite-native-client/dist/esm/client.')) {
83
83
  return injectConfigValues(code)
84
- } else if (!options?.ssr && code.includes('process.env.NODE_ENV')) {
85
- // replace process.env.NODE_ENV instead of defining a global
86
- // for it to avoid shimming a `process` object during dev,
87
- // avoiding inconsistencies between dev and build
88
- // return code.replace(
89
- // process_env_NODE_ENV_RE,
90
- // config.define?.['process.env.NODE_ENV'] ||
91
- // JSON.stringify(process.env.NODE_ENV || config.mode)
92
- // )
93
84
  }
94
85
  },
95
86
  }
@@ -104,9 +95,7 @@ function serializeDefine(define: Record<string, any>): string {
104
95
  let res = `{`
105
96
  for (const key in define) {
106
97
  const val = define[key]
107
- res += `${JSON.stringify(key)}: ${
108
- typeof val === 'string' ? `(${val})` : JSON.stringify(val)
109
- }, `
98
+ res += `${JSON.stringify(key)}: ${typeof val === 'string' ? `(${val})` : JSON.stringify(val)}, `
110
99
  }
111
100
  return res + `}`
112
101
  }
@@ -1,10 +1,10 @@
1
- import { dirname } from 'path'
1
+ import { dirname } from 'node:path'
2
2
 
3
3
  import { parse } from 'es-module-lexer'
4
4
  import type { OutputOptions } from 'rollup'
5
5
  import type { Plugin } from 'vite'
6
6
 
7
- import { getVitePath } from './utils/getVitePath'
7
+ import { getVitePath } from '../utils/getVitePath'
8
8
 
9
9
  const extensions = [
10
10
  '.ios.js',
@@ -19,7 +19,7 @@ const extensions = [
19
19
  '.mjs',
20
20
  ]
21
21
 
22
- export function nativePlugin(options: {
22
+ export function reactNativeCommonJsPlugin(options: {
23
23
  root: string
24
24
  port: number
25
25
  mode: 'build' | 'serve'
@@ -93,13 +93,6 @@ export function nativePlugin(options: {
93
93
 
94
94
  config.build.rollupOptions.plugins ??= []
95
95
 
96
- // config.build.rollupOptions.external = [
97
- // 'react-native',
98
- // 'react',
99
- // 'react/jsx-runtime',
100
- // 'react/jsx-dev-runtime',
101
- // ]
102
-
103
96
  if (!Array.isArray(config.build.rollupOptions.plugins)) {
104
97
  throw `x`
105
98
  }
@@ -107,22 +100,6 @@ export function nativePlugin(options: {
107
100
  if (options.mode === 'build') {
108
101
  config.plugins ||= []
109
102
 
110
- // https://vitejs.dev/config/dep-optimization-options.html
111
- // config.build.commonjsOptions ||= {}
112
- // config.build.commonjsOptions.include = []
113
-
114
- // CANT DO THIS BECAUSE TAMAGUI PLUGIN DOES THIS! they clobber each other!
115
- // config.plugins.push(
116
- // viteCommonjs({
117
- // include: ['escape-string-regexp'],
118
- // })
119
- // )
120
-
121
- // config.resolve.alias = {
122
- // ...config.resolve.alias,
123
- // 'react-native': virtualModuleId,
124
- // }
125
-
126
103
  config.build.rollupOptions.plugins.push({
127
104
  name: `force-export-all`,
128
105
 
@@ -1,5 +1,5 @@
1
1
  import type { UserConfig } from 'vite'
2
- import { resolveFile } from '../createDevServer'
2
+ import { resolveFile } from '../exports/dev'
3
3
 
4
4
  export function getBaseViteConfig({ mode }: { mode: 'development' | 'production' }): UserConfig {
5
5
  return {
@@ -1,13 +1,18 @@
1
1
  import { join } from 'node:path'
2
2
  import { readPackageJSON } from 'pkg-types'
3
+ import { createRequire } from 'module'
3
4
  import FSExtra from 'fs-extra'
4
5
  import type { VXRNConfig } from '../types'
5
6
 
7
+ const require = createRequire(import.meta.url)
8
+
6
9
  export type VXRNConfigFilled = Awaited<ReturnType<typeof getOptionsFilled>>
7
10
 
8
11
  export async function getOptionsFilled(options: VXRNConfig) {
9
12
  const { host = '127.0.0.1', root = process.cwd(), port = 8081 } = options
10
- const packageRootDir = join(import.meta.url ?? __filename, '..', '..', '..').replace('file:', '')
13
+
14
+ const packageRootDir = join(require.resolve('vxrn'), '../../..')
15
+
11
16
  const cacheDir = join(root, 'node_modules', '.cache', 'vxrn')
12
17
  const internalPatchesDir = join(packageRootDir, 'patches')
13
18
  const userPatchesDir = join(root, 'patches')