vxrn 0.1.96 → 0.1.98

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 (243) hide show
  1. package/dist/cjs/cli.js +9 -11
  2. package/dist/cjs/cli.js.map +1 -1
  3. package/dist/cjs/cli.native.js +22 -72
  4. package/dist/cjs/cli.native.js.map +2 -2
  5. package/dist/cjs/exports/build.js +19 -17
  6. package/dist/cjs/exports/build.js.map +1 -1
  7. package/dist/cjs/exports/build.native.js +64 -21
  8. package/dist/cjs/exports/build.native.js.map +2 -2
  9. package/dist/cjs/exports/createServer.js +1 -1
  10. package/dist/cjs/exports/createServer.js.map +1 -1
  11. package/dist/cjs/exports/createServer.native.js +1 -1
  12. package/dist/cjs/exports/createServer.native.js.map +2 -2
  13. package/dist/cjs/exports/dev.js +2 -2
  14. package/dist/cjs/exports/dev.js.map +1 -1
  15. package/dist/cjs/exports/dev.native.js +6 -6
  16. package/dist/cjs/exports/dev.native.js.map +2 -2
  17. package/dist/cjs/exports/serve.js +1 -1
  18. package/dist/cjs/exports/serve.js.map +1 -1
  19. package/dist/cjs/exports/serve.native.js +1 -1
  20. package/dist/cjs/exports/serve.native.js.map +2 -2
  21. package/dist/cjs/index.js +1 -0
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/index.native.js +2 -0
  24. package/dist/cjs/index.native.js.map +1 -1
  25. package/dist/cjs/plugins/clientInjectPlugin.js +3 -3
  26. package/dist/cjs/plugins/clientInjectPlugin.js.map +1 -1
  27. package/dist/cjs/plugins/clientInjectPlugin.native.js +3 -3
  28. package/dist/cjs/plugins/clientInjectPlugin.native.js.map +2 -2
  29. package/dist/cjs/plugins/reactNativeCommonJsPlugin.js +116 -48
  30. package/dist/cjs/plugins/reactNativeCommonJsPlugin.js.map +1 -1
  31. package/dist/cjs/plugins/reactNativeCommonJsPlugin.native.js +208 -147
  32. package/dist/cjs/plugins/reactNativeCommonJsPlugin.native.js.map +2 -2
  33. package/dist/cjs/plugins/reactNativeHMRPlugin.js +2 -0
  34. package/dist/cjs/plugins/reactNativeHMRPlugin.js.map +1 -1
  35. package/dist/cjs/plugins/reactNativeHMRPlugin.native.js +111 -107
  36. package/dist/cjs/plugins/reactNativeHMRPlugin.native.js.map +1 -1
  37. package/dist/cjs/{exports/isWithin.js → utils/environmentUtils.js} +19 -9
  38. package/dist/cjs/utils/environmentUtils.js.map +6 -0
  39. package/dist/cjs/utils/environmentUtils.native.js +43 -0
  40. package/dist/cjs/utils/environmentUtils.native.js.map +6 -0
  41. package/dist/cjs/utils/getBaseViteConfig.js +17 -2
  42. package/dist/cjs/utils/getBaseViteConfig.js.map +1 -1
  43. package/dist/cjs/utils/getBaseViteConfig.native.js +32 -3
  44. package/dist/cjs/utils/getBaseViteConfig.native.js.map +2 -2
  45. package/dist/cjs/utils/getOptimizeDeps.js +3 -2
  46. package/dist/cjs/utils/getOptimizeDeps.js.map +1 -1
  47. package/dist/cjs/utils/getOptimizeDeps.native.js +3 -2
  48. package/dist/cjs/utils/getOptimizeDeps.native.js.map +1 -1
  49. package/dist/cjs/utils/getReactNativeBundle.js +4 -3
  50. package/dist/cjs/utils/getReactNativeBundle.js.map +1 -1
  51. package/dist/cjs/utils/getReactNativeBundle.native.js +11 -5
  52. package/dist/cjs/utils/getReactNativeBundle.native.js.map +2 -2
  53. package/dist/cjs/utils/getViteServerConfig.js +14 -11
  54. package/dist/cjs/utils/getViteServerConfig.js.map +1 -1
  55. package/dist/cjs/utils/getViteServerConfig.native.js +56 -45
  56. package/dist/cjs/utils/getViteServerConfig.native.js.map +2 -2
  57. package/dist/esm/cli.js +8 -11
  58. package/dist/esm/cli.js.map +1 -1
  59. package/dist/esm/cli.mjs +16 -22
  60. package/dist/esm/cli.native.js +21 -72
  61. package/dist/esm/cli.native.js.map +2 -2
  62. package/dist/esm/exports/build.js +24 -18
  63. package/dist/esm/exports/build.js.map +1 -1
  64. package/dist/esm/exports/build.mjs +19 -19
  65. package/dist/esm/exports/build.native.js +65 -22
  66. package/dist/esm/exports/build.native.js.map +2 -2
  67. package/dist/esm/exports/createServer.js +1 -1
  68. package/dist/esm/exports/createServer.js.map +1 -1
  69. package/dist/esm/exports/createServer.mjs +1 -1
  70. package/dist/esm/exports/createServer.native.js +1 -1
  71. package/dist/esm/exports/createServer.native.js.map +2 -2
  72. package/dist/esm/exports/dev.js +2 -2
  73. package/dist/esm/exports/dev.js.map +1 -1
  74. package/dist/esm/exports/dev.mjs +1 -2
  75. package/dist/esm/exports/dev.native.js +6 -6
  76. package/dist/esm/exports/dev.native.js.map +2 -2
  77. package/dist/esm/exports/serve.js +1 -1
  78. package/dist/esm/exports/serve.js.map +1 -1
  79. package/dist/esm/exports/serve.mjs +1 -1
  80. package/dist/esm/exports/serve.native.js +1 -1
  81. package/dist/esm/exports/serve.native.js.map +2 -2
  82. package/dist/esm/index.js +1 -0
  83. package/dist/esm/index.js.map +1 -1
  84. package/dist/esm/index.mjs +1 -0
  85. package/dist/esm/index.native.js +1 -0
  86. package/dist/esm/index.native.js.map +1 -1
  87. package/dist/esm/plugins/clientInjectPlugin.js +3 -2
  88. package/dist/esm/plugins/clientInjectPlugin.js.map +1 -1
  89. package/dist/esm/plugins/clientInjectPlugin.mjs +3 -2
  90. package/dist/esm/plugins/clientInjectPlugin.native.js +3 -2
  91. package/dist/esm/plugins/clientInjectPlugin.native.js.map +2 -2
  92. package/dist/esm/plugins/reactNativeCommonJsPlugin.js +117 -48
  93. package/dist/esm/plugins/reactNativeCommonJsPlugin.js.map +1 -1
  94. package/dist/esm/plugins/reactNativeCommonJsPlugin.mjs +107 -41
  95. package/dist/esm/plugins/reactNativeCommonJsPlugin.native.js +209 -147
  96. package/dist/esm/plugins/reactNativeCommonJsPlugin.native.js.map +2 -2
  97. package/dist/esm/plugins/reactNativeHMRPlugin.js +2 -0
  98. package/dist/esm/plugins/reactNativeHMRPlugin.js.map +1 -1
  99. package/dist/esm/plugins/reactNativeHMRPlugin.mjs +2 -0
  100. package/dist/esm/plugins/reactNativeHMRPlugin.native.js +111 -107
  101. package/dist/esm/plugins/reactNativeHMRPlugin.native.js.map +1 -1
  102. package/dist/esm/utils/environmentUtils.js +19 -0
  103. package/dist/esm/utils/environmentUtils.js.map +6 -0
  104. package/dist/esm/utils/environmentUtils.mjs +13 -0
  105. package/dist/esm/utils/environmentUtils.native.js +19 -0
  106. package/dist/esm/utils/environmentUtils.native.js.map +6 -0
  107. package/dist/esm/utils/getBaseViteConfig.js +17 -2
  108. package/dist/esm/utils/getBaseViteConfig.js.map +1 -1
  109. package/dist/esm/utils/getBaseViteConfig.mjs +17 -2
  110. package/dist/esm/utils/getBaseViteConfig.native.js +32 -3
  111. package/dist/esm/utils/getBaseViteConfig.native.js.map +2 -2
  112. package/dist/esm/utils/getOptimizeDeps.js +3 -2
  113. package/dist/esm/utils/getOptimizeDeps.js.map +1 -1
  114. package/dist/esm/utils/getOptimizeDeps.mjs +1 -1
  115. package/dist/esm/utils/getOptimizeDeps.native.js +3 -2
  116. package/dist/esm/utils/getOptimizeDeps.native.js.map +1 -1
  117. package/dist/esm/utils/getReactNativeBundle.js +9 -4
  118. package/dist/esm/utils/getReactNativeBundle.js.map +1 -1
  119. package/dist/esm/utils/getReactNativeBundle.mjs +7 -4
  120. package/dist/esm/utils/getReactNativeBundle.native.js +12 -6
  121. package/dist/esm/utils/getReactNativeBundle.native.js.map +2 -2
  122. package/dist/esm/utils/getViteServerConfig.js +15 -12
  123. package/dist/esm/utils/getViteServerConfig.js.map +1 -1
  124. package/dist/esm/utils/getViteServerConfig.mjs +37 -32
  125. package/dist/esm/utils/getViteServerConfig.native.js +57 -46
  126. package/dist/esm/utils/getViteServerConfig.native.js.map +2 -2
  127. package/package.json +5 -5
  128. package/src/cli.ts +5 -11
  129. package/src/exports/build.ts +26 -17
  130. package/src/exports/createServer.ts +0 -4
  131. package/src/exports/dev.ts +5 -3
  132. package/src/exports/serve.ts +3 -0
  133. package/src/index.ts +2 -0
  134. package/src/plugins/clientInjectPlugin.ts +6 -3
  135. package/src/plugins/reactNativeCommonJsPlugin.ts +159 -146
  136. package/src/plugins/reactNativeHMRPlugin.ts +4 -1
  137. package/src/types.ts +2 -4
  138. package/src/utils/environmentUtils.ts +17 -0
  139. package/src/utils/getBaseViteConfig.ts +19 -1
  140. package/src/utils/getOptimizeDeps.ts +3 -2
  141. package/src/utils/getReactNativeBundle.ts +17 -6
  142. package/src/utils/getViteServerConfig.ts +50 -45
  143. package/types/exports/build.d.ts +28 -1
  144. package/types/index.d.ts +1 -0
  145. package/types/plugins/reactNativeCommonJsPlugin.d.ts +1 -1
  146. package/types/plugins/reactNativeHMRPlugin.d.ts +1 -5
  147. package/types/types.d.ts +2 -4
  148. package/types/utils/environmentUtils.d.ts +6 -0
  149. package/types/utils/getOptionsFilled.d.ts +1 -3
  150. package/dist/cjs/exports/bindKeypressInput.js +0 -58
  151. package/dist/cjs/exports/bindKeypressInput.js.map +0 -6
  152. package/dist/cjs/exports/bindKeypressInput.native.js +0 -63
  153. package/dist/cjs/exports/bindKeypressInput.native.js.map +0 -6
  154. package/dist/cjs/exports/coerceToArray.js +0 -23
  155. package/dist/cjs/exports/coerceToArray.js.map +0 -6
  156. package/dist/cjs/exports/connectedNativeClients.js +0 -21
  157. package/dist/cjs/exports/connectedNativeClients.js.map +0 -6
  158. package/dist/cjs/exports/empty.js +0 -1
  159. package/dist/cjs/exports/empty.js.map +0 -6
  160. package/dist/cjs/exports/empty.native.js +0 -2
  161. package/dist/cjs/exports/empty.native.js.map +0 -6
  162. package/dist/cjs/exports/entryRoot.js +0 -69
  163. package/dist/cjs/exports/entryRoot.js.map +0 -6
  164. package/dist/cjs/exports/getIndexJsonResponse.js +0 -71
  165. package/dist/cjs/exports/getIndexJsonResponse.js.map +0 -6
  166. package/dist/cjs/exports/getIndexJsonResponse.native.js +0 -99
  167. package/dist/cjs/exports/getIndexJsonResponse.native.js.map +0 -6
  168. package/dist/cjs/exports/getReactNativeBundle.js +0 -160
  169. package/dist/cjs/exports/getReactNativeBundle.js.map +0 -6
  170. package/dist/cjs/exports/getViteServerConfig.js +0 -77
  171. package/dist/cjs/exports/getViteServerConfig.js.map +0 -6
  172. package/dist/cjs/exports/getViteServerConfig.native.js +0 -253
  173. package/dist/cjs/exports/getViteServerConfig.native.js.map +0 -6
  174. package/dist/cjs/exports/hotUpdateCache.js +0 -21
  175. package/dist/cjs/exports/hotUpdateCache.js.map +0 -6
  176. package/dist/cjs/exports/isBuildingNativeBundle.js +0 -21
  177. package/dist/cjs/exports/isBuildingNativeBundle.js.map +0 -6
  178. package/dist/cjs/exports/isWithin.js.map +0 -6
  179. package/dist/cjs/exports/reactNativeHMRPlugin.js +0 -69
  180. package/dist/cjs/exports/reactNativeHMRPlugin.js.map +0 -6
  181. package/dist/cjs/exports/swapPrebuiltReactModules.js +0 -100
  182. package/dist/cjs/exports/swapPrebuiltReactModules.js.map +0 -6
  183. package/dist/cjs/exports/uniq.js +0 -23
  184. package/dist/cjs/exports/uniq.js.map +0 -6
  185. package/dist/cjs/utils/getHtml.js +0 -49
  186. package/dist/cjs/utils/getHtml.js.map +0 -6
  187. package/dist/cjs/utils/getHtml.native.js +0 -76
  188. package/dist/cjs/utils/getHtml.native.js.map +0 -6
  189. package/dist/esm/exports/bindKeypressInput.js +0 -34
  190. package/dist/esm/exports/bindKeypressInput.js.map +0 -6
  191. package/dist/esm/exports/bindKeypressInput.mjs +0 -31
  192. package/dist/esm/exports/bindKeypressInput.native.js +0 -34
  193. package/dist/esm/exports/bindKeypressInput.native.js.map +0 -6
  194. package/dist/esm/exports/coerceToArray.js +0 -7
  195. package/dist/esm/exports/coerceToArray.js.map +0 -6
  196. package/dist/esm/exports/coerceToArray.mjs +0 -4
  197. package/dist/esm/exports/connectedNativeClients.js +0 -5
  198. package/dist/esm/exports/connectedNativeClients.js.map +0 -6
  199. package/dist/esm/exports/connectedNativeClients.mjs +0 -2
  200. package/dist/esm/exports/empty.js +0 -1
  201. package/dist/esm/exports/empty.js.map +0 -6
  202. package/dist/esm/exports/empty.mjs +0 -0
  203. package/dist/esm/exports/empty.native.js +0 -1
  204. package/dist/esm/exports/empty.native.js.map +0 -6
  205. package/dist/esm/exports/entryRoot.js +0 -58
  206. package/dist/esm/exports/entryRoot.js.map +0 -6
  207. package/dist/esm/exports/entryRoot.mjs +0 -62
  208. package/dist/esm/exports/getIndexJsonResponse.js +0 -55
  209. package/dist/esm/exports/getIndexJsonResponse.js.map +0 -6
  210. package/dist/esm/exports/getIndexJsonResponse.mjs +0 -71
  211. package/dist/esm/exports/getIndexJsonResponse.native.js +0 -78
  212. package/dist/esm/exports/getIndexJsonResponse.native.js.map +0 -6
  213. package/dist/esm/exports/getReactNativeBundle.js +0 -146
  214. package/dist/esm/exports/getReactNativeBundle.js.map +0 -6
  215. package/dist/esm/exports/getReactNativeBundle.mjs +0 -136
  216. package/dist/esm/exports/getViteServerConfig.js +0 -64
  217. package/dist/esm/exports/getViteServerConfig.js.map +0 -6
  218. package/dist/esm/exports/getViteServerConfig.mjs +0 -44
  219. package/dist/esm/exports/getViteServerConfig.native.js +0 -235
  220. package/dist/esm/exports/getViteServerConfig.native.js.map +0 -6
  221. package/dist/esm/exports/hotUpdateCache.js +0 -5
  222. package/dist/esm/exports/hotUpdateCache.js.map +0 -6
  223. package/dist/esm/exports/hotUpdateCache.mjs +0 -2
  224. package/dist/esm/exports/isBuildingNativeBundle.js +0 -5
  225. package/dist/esm/exports/isBuildingNativeBundle.js.map +0 -6
  226. package/dist/esm/exports/isBuildingNativeBundle.mjs +0 -2
  227. package/dist/esm/exports/isWithin.js +0 -9
  228. package/dist/esm/exports/isWithin.js.map +0 -6
  229. package/dist/esm/exports/isWithin.mjs +0 -6
  230. package/dist/esm/exports/reactNativeHMRPlugin.js +0 -58
  231. package/dist/esm/exports/reactNativeHMRPlugin.js.map +0 -6
  232. package/dist/esm/exports/reactNativeHMRPlugin.mjs +0 -62
  233. package/dist/esm/exports/swapPrebuiltReactModules.js +0 -87
  234. package/dist/esm/exports/swapPrebuiltReactModules.js.map +0 -6
  235. package/dist/esm/exports/swapPrebuiltReactModules.mjs +0 -73
  236. package/dist/esm/exports/uniq.js +0 -7
  237. package/dist/esm/exports/uniq.js.map +0 -6
  238. package/dist/esm/exports/uniq.mjs +0 -4
  239. package/dist/esm/utils/getHtml.js +0 -33
  240. package/dist/esm/utils/getHtml.js.map +0 -6
  241. package/dist/esm/utils/getHtml.mjs +0 -23
  242. package/dist/esm/utils/getHtml.native.js +0 -55
  243. package/dist/esm/utils/getHtml.native.js.map +0 -6
@@ -1,7 +1,13 @@
1
1
  import FSExtra from 'fs-extra'
2
2
  import { rm } from 'node:fs/promises'
3
3
  import type { RollupOutput } from 'rollup'
4
- import { mergeConfig, build as viteBuild, type Plugin, type UserConfig } from 'vite'
4
+ import {
5
+ loadConfigFromFile,
6
+ mergeConfig,
7
+ build as viteBuild,
8
+ type Plugin,
9
+ type UserConfig,
10
+ } from 'vite'
5
11
  import { analyzer } from 'vite-bundle-analyzer'
6
12
  import type { BuildArgs, VXRNConfig } from '../types'
7
13
  import { getBaseViteConfig } from '../utils/getBaseViteConfig'
@@ -31,7 +37,13 @@ const disableOptimizationConfig = {
31
37
  } satisfies UserConfig
32
38
 
33
39
  export const build = async (optionsIn: VXRNConfig, buildArgs: BuildArgs = {}) => {
34
- const options = await getOptionsFilled(optionsIn)
40
+ const [options, viteConfig] = await Promise.all([
41
+ getOptionsFilled(optionsIn),
42
+ loadConfigFromFile({
43
+ command: 'build',
44
+ mode: 'prod',
45
+ }).then((_) => _?.config),
46
+ ])
35
47
 
36
48
  // clean
37
49
  await Promise.all([
@@ -74,8 +86,8 @@ export const build = async (optionsIn: VXRNConfig, buildArgs: BuildArgs = {}) =>
74
86
  },
75
87
  } satisfies Plugin
76
88
 
77
- if (options.webConfig) {
78
- webBuildConfig = mergeConfig(webBuildConfig, options.webConfig) as any
89
+ if (viteConfig) {
90
+ webBuildConfig = mergeConfig(webBuildConfig, viteConfig) as any
79
91
  }
80
92
 
81
93
  let clientOutput
@@ -145,19 +157,16 @@ export const build = async (optionsIn: VXRNConfig, buildArgs: BuildArgs = {}) =>
145
157
  // }
146
158
 
147
159
  const { output: serverOutput } = (await viteBuild(serverBuildConfig)) as RollupOutput
160
+ const clientManifest = await FSExtra.readJSON('dist/client/.vite/manifest.json')
148
161
 
149
- if (options.afterBuild) {
150
- const clientManifest = await FSExtra.readJSON('dist/client/.vite/manifest.json')
151
-
152
- await options.afterBuild({
153
- options,
154
- buildArgs,
155
- clientOutput,
156
- serverOutput,
157
- webBuildConfig,
158
- clientManifest,
159
- })
160
- }
162
+ console.info(`\n ✔️ vxrn build complete\n`)
161
163
 
162
- console.info(`\n ✔️ build complete\n`)
164
+ return {
165
+ options,
166
+ buildArgs,
167
+ clientOutput,
168
+ serverOutput,
169
+ webBuildConfig,
170
+ clientManifest,
171
+ }
163
172
  }
@@ -17,10 +17,6 @@ export const createProdServer = async (options: VXRNConfig) => {
17
17
  })
18
18
  )
19
19
 
20
- if (options.serve) {
21
- options.serve(options, app)
22
- }
23
-
24
20
  // app.get(
25
21
  // '*',
26
22
  // cache({
@@ -33,7 +33,7 @@ const { ensureDir } = FSExtra
33
33
 
34
34
  export const dev = async ({ clean, ...rest }: VXRNConfig & { clean?: boolean }) => {
35
35
  const options = await getOptionsFilled(rest)
36
- const { host, port, root, cacheDir } = options
36
+ const { port, root, cacheDir } = options
37
37
 
38
38
  if (clean) {
39
39
  try {
@@ -134,8 +134,10 @@ export const dev = async ({ clean, ...rest }: VXRNConfig & { clean?: boolean })
134
134
  // TODO move these to router.get():
135
135
  app.use(
136
136
  defineEventHandler(async ({ node: { req } }) => {
137
- if (!req.headers['user-agent']?.match(/Expo|React/)) {
138
- return
137
+ if (!req.headers['expo-platform']) {
138
+ if (!req.headers['user-agent']?.match(/Expo|React/)) {
139
+ return
140
+ }
139
141
  }
140
142
 
141
143
  if (req.url === '/' || req.url?.startsWith('/?platform=')) {
@@ -7,6 +7,9 @@ import { getOptionsFilled } from '../utils/getOptionsFilled'
7
7
  export const serve = async (optionsIn: VXRNConfig) => {
8
8
  const options = await getOptionsFilled(optionsIn, { mode: 'prod' })
9
9
  const app = await createProdServer(options)
10
+ if (options.onServe) {
11
+ options.onServe(options, app)
12
+ }
10
13
  // strange prevents a cant listen on port issue
11
14
  await new Promise((res) => setTimeout(res, 1))
12
15
  const server = honoServe({
package/src/index.ts CHANGED
@@ -6,4 +6,6 @@ export * from './utils/getOptionsFilled'
6
6
  export * from './utils/getOptimizeDeps'
7
7
  export * from './utils/getBaseViteConfig'
8
8
 
9
+ export * from './utils/environmentUtils'
10
+
9
11
  export * from './types'
@@ -1,6 +1,7 @@
1
1
  import path from 'node:path'
2
2
 
3
3
  import type { HmrOptions, Plugin, ResolvedConfig, UserConfig } from 'vite'
4
+ import { isNativeEnvironment } from '../utils/environmentUtils'
4
5
 
5
6
  const process_env_NODE_ENV_RE = /(\bglobal(This)?\.)?\bprocess\.env\.NODE_ENV\b/g
6
7
 
@@ -78,9 +79,11 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
78
79
  }
79
80
  },
80
81
 
81
- transform(code, id, options) {
82
- if (id.includes('vite-native-client/dist/esm/client.')) {
83
- return injectConfigValues(code)
82
+ transform(code, id) {
83
+ if (isNativeEnvironment(this.environment!)) {
84
+ if (id.includes('vite-native-client/dist/esm/client.')) {
85
+ return injectConfigValues(code)
86
+ }
84
87
  }
85
88
  },
86
89
  }
@@ -1,21 +1,25 @@
1
- import { dirname } from 'node:path'
2
1
  import { parse } from 'es-module-lexer'
3
- import type { OutputOptions } from 'rollup'
4
- import type { Plugin } from 'vite'
2
+ import { dirname } from 'node:path'
3
+ import { mergeConfig, type Plugin, type UserConfig } from 'vite'
5
4
  import { getVitePath } from '../utils/getVitePath'
6
5
 
7
- const extensions = [
8
- '.ios.js',
9
- '.native.js',
10
- '.native.ts',
11
- '.native.tsx',
12
- '.js',
13
- '.jsx',
14
- '.json',
15
- '.ts',
16
- '.tsx',
17
- '.mjs',
18
- ]
6
+ const getNativeExtensions = (platform: 'ios' | 'android') => {
7
+ return [
8
+ `.${platform}.tsx`,
9
+ `.${platform}.ts`,
10
+ `.${platform}.jsx`,
11
+ `.${platform}.js`,
12
+ '.native.js',
13
+ '.native.ts',
14
+ '.native.tsx',
15
+ '.tsx',
16
+ '.ts',
17
+ '.js',
18
+ '.jsx',
19
+ '.json',
20
+ '.mjs',
21
+ ]
22
+ }
19
23
 
20
24
  export function reactNativeCommonJsPlugin(options: {
21
25
  root: string
@@ -26,151 +30,160 @@ export function reactNativeCommonJsPlugin(options: {
26
30
  name: 'native',
27
31
  enforce: 'pre',
28
32
 
29
- config: async (config) => {
30
- config.define ||= {}
31
- config.define['process.env.REACT_NATIVE_SERVER_PUBLIC_PORT'] = JSON.stringify(
32
- `${options.port}`
33
- )
34
- config.define['process.env.REACT_NATIVE_PLATFORM'] = JSON.stringify(`ios`)
35
-
36
- if (!config.build) config.build = {}
37
-
38
- config.build.modulePreload = { polyfill: false }
39
- // Ensures that even very large assets are inlined in your JavaScript.
40
- config.build.assetsInlineLimit = 100000000
41
- // Avoid warnings about large chunks.
42
- config.build.chunkSizeWarningLimit = 100000000
43
- // Emit all CSS as a single file, which `vite-plugin-singlefile` can then inline.
44
- config.build.cssCodeSplit = false
45
- // Avoids the extra step of testing Brotli compression, which isn't really pertinent to a file served locally.
46
- config.build.reportCompressedSize = false
47
- // Subfolder bases are not supported, and shouldn't be needed because we're embedding everything.
48
- config.base = undefined
49
-
50
- config.resolve ??= {}
33
+ config: async () => {
34
+ const sharedNativeConfig = {
35
+ // Subfolder bases are not supported, and shouldn't be needed because we're embedding everything
36
+ base: undefined,
51
37
 
52
- config.resolve.extensions = extensions
53
-
54
- config.resolve.conditions = ['react-native', 'require', 'default', 'import']
55
-
56
- config.optimizeDeps ??= {}
57
-
58
- config.optimizeDeps.noDiscovery = true
59
- config.optimizeDeps.include = undefined
38
+ define: {
39
+ 'process.env.REACT_NATIVE_SERVER_PUBLIC_PORT': JSON.stringify(`${options.port}`),
40
+ },
60
41
 
61
- config.optimizeDeps.esbuildOptions ??= {}
62
- config.optimizeDeps.esbuildOptions.resolveExtensions = extensions
42
+ build: {
43
+ modulePreload: {
44
+ polyfill: false,
45
+ },
46
+ // Ensures that even very large assets are inlined in your JavaScript.
47
+ assetsInlineLimit: 100000000,
48
+ // Avoid warnings about large chunks
49
+ chunkSizeWarningLimit: 100000000,
50
+ // Emit all CSS as a single file, which `vite-plugin-singlefile` can then inline
51
+ cssCodeSplit: false,
52
+ // Avoids the extra step of testing Brotli compression, which isn't really pertinent to a file served locally
53
+ reportCompressedSize: false,
54
+
55
+ rollupOptions: {
56
+ output: {
57
+ preserveModules: true,
58
+ manualChunks: undefined,
59
+ // Ensure that as many resources as possible are inlined.
60
+ // inlineDynamicImports: true,
61
+ // this fixes some warnings but breaks import { default as config }
62
+ exports: 'named',
63
+ // ensures we have clean names for our require paths
64
+ entryFileNames: () => `[name].js`,
65
+ },
63
66
 
64
- config.optimizeDeps.esbuildOptions.plugins ??= []
67
+ plugins: [
68
+ {
69
+ name: `force-export-all`,
70
+
71
+ async transform(code, id) {
72
+ // if (!id.includes('/node_modules/')) {
73
+ // return
74
+ // }
75
+
76
+ try {
77
+ const [imports, exports] = parse(code)
78
+
79
+ let forceExports = ''
80
+
81
+ // note that es-module-lexer parses export * from as an import (twice) for some reason
82
+ let counts = {}
83
+ for (const imp of imports) {
84
+ if (imp.n && imp.n[0] !== '.') {
85
+ counts[imp.n] ||= 0
86
+ counts[imp.n]++
87
+ if (counts[imp.n] == 2) {
88
+ // star export
89
+ const path = await getVitePath(options.root, dirname(id), imp.n)
90
+ forceExports += `Object.assign(exports, require("${path}"));`
91
+ }
92
+ }
93
+ }
94
+
95
+ forceExports += exports
96
+ .map((e) => {
97
+ if (e.n === 'default') {
98
+ return ''
99
+ }
100
+ let out = ''
101
+ if (e.ln !== e.n) {
102
+ // forces the "as x" to be referenced so it gets exported
103
+ out += `__ignore = typeof ${e.n} === 'undefined' ? 0 : 0;`
104
+ }
105
+ out += `globalThis.____forceExport = ${e.ln}`
106
+ return out
107
+ })
108
+ .join(';')
109
+
110
+ return code + '\n' + forceExports
111
+ } catch (err) {
112
+ console.warn(`Error forcing exports, probably ok`, id)
113
+ }
114
+ },
115
+ },
116
+ ],
117
+ },
118
+ },
65
119
 
66
- config.optimizeDeps.esbuildOptions.loader ??= {}
67
- config.optimizeDeps.esbuildOptions.loader['.js'] = 'jsx'
120
+ optimizeDeps: {
121
+ noDiscovery: true,
122
+ include: undefined,
68
123
 
69
- config.optimizeDeps.esbuildOptions.plugins.push({
70
- name: 'react-native-assets',
71
- setup(build) {
72
- build.onResolve(
73
- {
74
- filter: /\.(png|jpg|gif|webp)$/,
124
+ esbuildOptions: {
125
+ loader: {
126
+ '.js': 'jsx',
75
127
  },
76
- async ({ path, namespace }) => {
77
- return {
78
- path: '',
79
- external: true,
80
- }
81
- }
82
- )
128
+ },
83
129
  },
84
- })
85
-
86
- config.build.rollupOptions ??= {}
87
-
88
- config.build.rollupOptions.output ??= {}
89
-
90
- config.build.rollupOptions.plugins ??= []
91
-
92
- if (!Array.isArray(config.build.rollupOptions.plugins)) {
93
- throw `x`
94
- }
130
+ } satisfies UserConfig
95
131
 
96
- if (options.mode === 'build') {
97
- config.plugins ||= []
132
+ // per-enviroment config:
98
133
 
99
- config.build.rollupOptions.plugins.push({
100
- name: `force-export-all`,
101
-
102
- async transform(code, id) {
103
- // if (!id.includes('/node_modules/')) {
104
- // return
105
- // }
106
-
107
- try {
108
- const [imports, exports] = parse(code)
109
-
110
- let forceExports = ''
111
-
112
- // note that es-module-lexer parses export * from as an import (twice) for some reason
113
- let counts = {}
114
- for (const imp of imports) {
115
- if (imp.n && imp.n[0] !== '.') {
116
- counts[imp.n] ||= 0
117
- counts[imp.n]++
118
- if (counts[imp.n] == 2) {
119
- // star export
120
- const path = await getVitePath(options.root, dirname(id), imp.n)
121
- forceExports += `Object.assign(exports, require("${path}"));`
122
- }
123
- }
124
- }
125
-
126
- forceExports += exports
127
- .map((e) => {
128
- if (e.n === 'default') {
129
- return ''
130
- }
131
- let out = ''
132
- if (e.ln !== e.n) {
133
- // forces the "as x" to be referenced so it gets exported
134
- out += `__ignore = typeof ${e.n} === 'undefined' ? 0 : 0;`
135
- }
136
- out += `globalThis.____forceExport = ${e.ln}`
137
- return out
138
- })
139
- .join(';')
140
-
141
- return code + '\n' + forceExports
142
- } catch (err) {
143
- console.warn(`Error forcing exports, probably ok`, id)
144
- }
145
- },
146
- })
147
- }
148
-
149
- if (process.env.DEBUG) {
150
- console.info('config..', config)
151
- }
134
+ return {
135
+ environments: {
136
+ ios: mergeConfig(sharedNativeConfig, {
137
+ define: {
138
+ 'process.env.REACT_NATIVE_PLATFORM': JSON.stringify(`ios`),
139
+ },
152
140
 
153
- const updateOutputOptions = (out: OutputOptions) => {
154
- out.preserveModules = true
141
+ resolve: {
142
+ extensions: getNativeExtensions('ios'),
143
+ },
155
144
 
156
- // this fixes some warnings but breaks import { default as config }
157
- // out.exports = 'named'
145
+ optimizeDeps: {
146
+ esbuildOptions: {
147
+ resolveExtensions: getNativeExtensions('ios'),
148
+
149
+ plugins: [
150
+ {
151
+ name: 'react-native-assets',
152
+ setup(build) {
153
+ build.onResolve(
154
+ {
155
+ filter: /\.(png|jpg|gif|webp)$/,
156
+ },
157
+ async ({ path, namespace }) => {
158
+ return {
159
+ path: '',
160
+ external: true,
161
+ }
162
+ }
163
+ )
164
+ },
165
+ },
166
+ ],
167
+ },
168
+ },
169
+ } satisfies UserConfig),
158
170
 
159
- out.entryFileNames = (chunkInfo) => {
160
- // ensures we have clean names for our require paths
161
- return '[name].js'
162
- }
163
- // Ensure that as many resources as possible are inlined.
164
- // out.inlineDynamicImports = true
171
+ android: mergeConfig(sharedNativeConfig, {
172
+ define: {
173
+ 'process.env.REACT_NATIVE_PLATFORM': JSON.stringify(`android`),
174
+ },
165
175
 
166
- // added by me (nate):
167
- out.manualChunks = undefined
168
- }
176
+ resolve: {
177
+ extensions: getNativeExtensions('android'),
178
+ },
169
179
 
170
- if (Array.isArray(config.build.rollupOptions.output)) {
171
- for (const o in config.build.rollupOptions.output) updateOutputOptions(o as OutputOptions)
172
- } else {
173
- updateOutputOptions(config.build.rollupOptions.output as OutputOptions)
180
+ optimizeDeps: {
181
+ esbuildOptions: {
182
+ resolveExtensions: getNativeExtensions('android'),
183
+ },
184
+ },
185
+ } satisfies UserConfig),
186
+ },
174
187
  }
175
188
  },
176
189
  }
@@ -6,11 +6,14 @@ import { entryRoot } from '../utils/getReactNativeBundle'
6
6
  import { getVitePath } from '../utils/getVitePath'
7
7
  import { hotUpdateCache } from '../utils/hotUpdateCache'
8
8
  import { isWithin } from '../utils/isWithin'
9
+ import type { Plugin } from 'vite'
9
10
 
10
11
  export function reactNativeHMRPlugin({ root }: VXRNConfigFilled) {
11
12
  return {
12
13
  name: 'client-transform',
13
14
 
15
+ // TODO see about moving to hotUpdate
16
+ // https://deploy-preview-16089--vite-docs-main.netlify.app/guide/api-vite-environment.html#the-hotupdate-hook
14
17
  async handleHotUpdate({ read, modules, file }) {
15
18
  try {
16
19
  if (!isWithin(root, file)) {
@@ -101,5 +104,5 @@ export function reactNativeHMRPlugin({ root }: VXRNConfigFilled) {
101
104
  console.error(`Error processing hmr update:`, err)
102
105
  }
103
106
  },
104
- }
107
+ } satisfies Plugin
105
108
  }
package/src/types.ts CHANGED
@@ -43,13 +43,11 @@ export type VXRNConfig = {
43
43
  root?: string
44
44
  host?: string
45
45
  port?: number
46
- webConfig?: InlineConfig
47
- nativeConfig?: InlineConfig
48
46
  flow?: FlowOptions
49
47
 
48
+ // for hooking into things
50
49
  afterBuild?: (props: AfterBuildProps) => void | Promise<void>
51
-
52
- serve?: (options: VXRNConfig, app: Hono) => void
50
+ onServe?: (options: VXRNConfig, app: Hono) => void
53
51
  }
54
52
 
55
53
  export type HMRListener = (update: { file: string; contents: string }) => void
@@ -0,0 +1,17 @@
1
+ import type { Environment } from 'vite'
2
+
3
+ export function isWebEnvironment(environment: Environment) {
4
+ return environment.name === 'client' || environment.name === 'ssr'
5
+ }
6
+
7
+ export function isNativeEnvironment(environment: Environment) {
8
+ return environment.name === 'ios' || environment.name === 'android'
9
+ }
10
+
11
+ export function isIOSEnvironment(environment: Environment) {
12
+ return environment.name === 'ios'
13
+ }
14
+
15
+ export function isAndroidEnvironment(environment: Environment) {
16
+ return environment.name === 'android'
17
+ }
@@ -4,6 +4,15 @@ import type { UserConfig } from 'vite'
4
4
  // essentially base web config not base everything
5
5
 
6
6
  export function getBaseViteConfig({ mode }: { mode: 'development' | 'production' }): UserConfig {
7
+ const sharedWebConfig = {
8
+ resolve: {
9
+ alias: {
10
+ 'react-native': 'react-native-web',
11
+ 'react-native-safe-area-context': '@vxrn/safe-area',
12
+ },
13
+ },
14
+ } satisfies UserConfig
15
+
7
16
  return {
8
17
  mode,
9
18
 
@@ -17,7 +26,6 @@ export function getBaseViteConfig({ mode }: { mode: 'development' | 'production'
17
26
  resolve: {
18
27
  alias: {
19
28
  'react-native': 'react-native-web',
20
- 'react-native-safe-area-context': '@vxrn/safe-area',
21
29
  },
22
30
 
23
31
  // TODO auto dedupe all include optimize deps?
@@ -38,5 +46,15 @@ export function getBaseViteConfig({ mode }: { mode: 'development' | 'production'
38
46
  transformMixedEsModules: true,
39
47
  },
40
48
  },
49
+
50
+ environments: {
51
+ web: {
52
+ ...sharedWebConfig,
53
+ },
54
+
55
+ ssr: {
56
+ ...sharedWebConfig,
57
+ },
58
+ },
41
59
  }
42
60
  }
@@ -42,6 +42,9 @@ export function getOptimizeDeps(mode: 'build' | 'serve') {
42
42
  '@react-navigation/elements',
43
43
  '@react-navigation/bottom-tabs',
44
44
  '@react-navigation/native-stack',
45
+ 'vxs',
46
+ 'vxs/server-render',
47
+ 'vxs/headers',
45
48
  'requires-port',
46
49
  'querystringify',
47
50
  'compare-versions',
@@ -61,8 +64,6 @@ export function getOptimizeDeps(mode: 'build' | 'serve') {
61
64
  'tamagui/linear-gradient',
62
65
  '@tamagui/linear-gradient',
63
66
  '@react-native/normalize-color',
64
- 'vxs',
65
- 'vxs/server-render',
66
67
  'expo-modules-core',
67
68
  'expo-status-bar',
68
69
  'react-native-web',
@@ -3,7 +3,14 @@ import createViteFlow from '@vxrn/vite-flow'
3
3
  import viteReactPlugin from '@vxrn/vite-native-swc'
4
4
  import { readFile } from 'node:fs/promises'
5
5
  import { dirname, join, relative } from 'node:path'
6
- import { build, mergeConfig, resolveConfig, transformWithEsbuild, type InlineConfig } from 'vite'
6
+ import {
7
+ build,
8
+ createBuilder,
9
+ mergeConfig,
10
+ resolveConfig,
11
+ transformWithEsbuild,
12
+ type InlineConfig,
13
+ } from 'vite'
7
14
  import { nativeExtensions } from '../constants'
8
15
  import { resolveFile } from './resolveFile'
9
16
  import { isBuildingNativeBundle, setIsBuildingNativeBundle } from './isBuildingNativeBundle'
@@ -20,7 +27,7 @@ export let entryRoot = ''
20
27
 
21
28
  export async function getReactNativeBundle(options: VXRNConfigFilled, viteRNClientPlugin: any) {
22
29
  const { root, port, cacheDir } = options
23
- const { depsToOptimize } = getOptimizeDeps('build')
30
+ const { depsToOptimize, needsInterop } = getOptimizeDeps('build')
24
31
 
25
32
  if (process.env.LOAD_TMP_BUNDLE) {
26
33
  // for easier quick testing things:
@@ -111,6 +118,7 @@ export async function getReactNativeBundle(options: VXRNConfigFilled, viteRNClie
111
118
 
112
119
  optimizeDeps: {
113
120
  include: depsToOptimize,
121
+ needsInterop,
114
122
  esbuildOptions: {
115
123
  jsx: 'automatic',
116
124
  },
@@ -144,14 +152,17 @@ export async function getReactNativeBundle(options: VXRNConfigFilled, viteRNClie
144
152
  },
145
153
  } satisfies InlineConfig
146
154
 
147
- if (options.nativeConfig) {
148
- nativeBuildConfig = mergeConfig(nativeBuildConfig, options.nativeConfig) as any
149
- }
155
+ // TODO
156
+ // if (options.nativeConfig) {
157
+ // nativeBuildConfig = mergeConfig(nativeBuildConfig, options.nativeConfig) as any
158
+ // }
150
159
 
151
160
  // // this fixes my swap-react-native plugin not being called pre 😳
152
161
  await resolveConfig(nativeBuildConfig, 'build')
153
162
 
154
- const buildOutput = await build(nativeBuildConfig)
163
+ const builder = await createBuilder(nativeBuildConfig)
164
+
165
+ const buildOutput = await builder.build(builder.environments.ios)
155
166
 
156
167
  if (!('output' in buildOutput)) {
157
168
  throw `❌`