vxrn 0.1.16 → 0.1.18

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 (139) hide show
  1. package/dist/cjs/VXRNConfigFilled.js +53 -0
  2. package/dist/cjs/VXRNConfigFilled.js.map +6 -0
  3. package/dist/cjs/VXRNConfigFilled.native.js +307 -0
  4. package/dist/cjs/VXRNConfigFilled.native.js.map +6 -0
  5. package/dist/cjs/build.js +96 -0
  6. package/dist/cjs/build.js.map +6 -0
  7. package/dist/cjs/build.native.js +237 -0
  8. package/dist/cjs/build.native.js.map +6 -0
  9. package/dist/cjs/commands/cli-build.js +36 -0
  10. package/dist/cjs/commands/cli-build.js.map +6 -0
  11. package/dist/cjs/commands/cli-build.native.js +171 -0
  12. package/dist/cjs/commands/cli-build.native.js.map +6 -0
  13. package/dist/cjs/commands/cli-dev.js +2 -2
  14. package/dist/cjs/commands/cli-dev.js.map +1 -1
  15. package/dist/cjs/commands/cli-dev.native.js +2 -2
  16. package/dist/cjs/commands/cli-dev.native.js.map +2 -2
  17. package/dist/cjs/commands/index.js +3 -1
  18. package/dist/cjs/commands/index.js.map +1 -1
  19. package/dist/cjs/commands/index.native.js +4 -0
  20. package/dist/cjs/commands/index.native.js.map +2 -2
  21. package/dist/cjs/createDevServer.js +602 -0
  22. package/dist/cjs/createDevServer.js.map +6 -0
  23. package/dist/cjs/createDevServer.native.js +1222 -0
  24. package/dist/cjs/createDevServer.native.js.map +6 -0
  25. package/dist/cjs/getBaseViteConfig.js +40 -0
  26. package/dist/cjs/getBaseViteConfig.js.map +6 -0
  27. package/dist/cjs/getBaseViteConfig.native.js +46 -0
  28. package/dist/cjs/getBaseViteConfig.native.js.map +6 -0
  29. package/dist/cjs/getOptionsFIlled.js +53 -0
  30. package/dist/cjs/getOptionsFIlled.js.map +6 -0
  31. package/dist/cjs/getOptionsFIlled.native.js +307 -0
  32. package/dist/cjs/getOptionsFIlled.native.js.map +6 -0
  33. package/dist/cjs/index.js +3 -2
  34. package/dist/cjs/index.js.map +1 -1
  35. package/dist/cjs/index.native.js +4 -2
  36. package/dist/cjs/index.native.js.map +1 -1
  37. package/dist/cjs/nativePlugin.js +1 -1
  38. package/dist/cjs/nativePlugin.native.js +1 -1
  39. package/dist/cjs/types.native.js.map +1 -1
  40. package/dist/cjs/utils/getBaseViteConfig.js +40 -0
  41. package/dist/cjs/utils/getBaseViteConfig.js.map +6 -0
  42. package/dist/cjs/utils/getBaseViteConfig.native.js +46 -0
  43. package/dist/cjs/utils/getBaseViteConfig.native.js.map +6 -0
  44. package/dist/cjs/utils/getOptionsFilled.js +53 -0
  45. package/dist/cjs/utils/getOptionsFilled.js.map +6 -0
  46. package/dist/cjs/utils/getOptionsFilled.native.js +307 -0
  47. package/dist/cjs/utils/getOptionsFilled.native.js.map +6 -0
  48. package/dist/cjs/utils/getVitePath.js +49 -0
  49. package/dist/cjs/utils/getVitePath.js.map +6 -0
  50. package/dist/cjs/utils/getVitePath.native.js +198 -0
  51. package/dist/cjs/utils/getVitePath.native.js.map +6 -0
  52. package/dist/esm/VXRNConfigFilled.js +30 -0
  53. package/dist/esm/VXRNConfigFilled.js.map +6 -0
  54. package/dist/esm/VXRNConfigFilled.mjs +34 -0
  55. package/dist/esm/VXRNConfigFilled.native.js +279 -0
  56. package/dist/esm/VXRNConfigFilled.native.js.map +6 -0
  57. package/dist/esm/build.js +83 -0
  58. package/dist/esm/build.js.map +6 -0
  59. package/dist/esm/build.mjs +56 -0
  60. package/dist/esm/build.native.js +217 -0
  61. package/dist/esm/build.native.js.map +6 -0
  62. package/dist/esm/commands/cli-build.js +23 -0
  63. package/dist/esm/commands/cli-build.js.map +6 -0
  64. package/dist/esm/commands/cli-build.mjs +22 -0
  65. package/dist/esm/commands/cli-build.native.js +156 -0
  66. package/dist/esm/commands/cli-build.native.js.map +6 -0
  67. package/dist/esm/commands/cli-dev.js +2 -2
  68. package/dist/esm/commands/cli-dev.js.map +1 -1
  69. package/dist/esm/commands/cli-dev.mjs +2 -2
  70. package/dist/esm/commands/cli-dev.native.js +2 -2
  71. package/dist/esm/commands/cli-dev.native.js.map +2 -2
  72. package/dist/esm/commands/index.js +3 -1
  73. package/dist/esm/commands/index.js.map +1 -1
  74. package/dist/esm/commands/index.mjs +3 -1
  75. package/dist/esm/commands/index.native.js +4 -0
  76. package/dist/esm/commands/index.native.js.map +2 -2
  77. package/dist/esm/createDevServer.js +613 -0
  78. package/dist/esm/createDevServer.js.map +6 -0
  79. package/dist/esm/createDevServer.mjs +574 -0
  80. package/dist/esm/createDevServer.native.js +1211 -0
  81. package/dist/esm/createDevServer.native.js.map +6 -0
  82. package/dist/esm/getBaseViteConfig.js +24 -0
  83. package/dist/esm/getBaseViteConfig.js.map +6 -0
  84. package/dist/esm/getBaseViteConfig.mjs +23 -0
  85. package/dist/esm/getBaseViteConfig.native.js +25 -0
  86. package/dist/esm/getBaseViteConfig.native.js.map +6 -0
  87. package/dist/esm/getOptionsFIlled.js +30 -0
  88. package/dist/esm/getOptionsFIlled.js.map +6 -0
  89. package/dist/esm/getOptionsFIlled.mjs +34 -0
  90. package/dist/esm/getOptionsFIlled.native.js +279 -0
  91. package/dist/esm/getOptionsFIlled.native.js.map +6 -0
  92. package/dist/esm/index.js +4 -2
  93. package/dist/esm/index.js.map +1 -1
  94. package/dist/esm/index.mjs +3 -2
  95. package/dist/esm/index.native.js +4 -2
  96. package/dist/esm/index.native.js.map +2 -2
  97. package/dist/esm/nativePlugin.js +1 -1
  98. package/dist/esm/nativePlugin.mjs +1 -1
  99. package/dist/esm/nativePlugin.native.js +1 -1
  100. package/dist/esm/utils/getBaseViteConfig.js +24 -0
  101. package/dist/esm/utils/getBaseViteConfig.js.map +6 -0
  102. package/dist/esm/utils/getBaseViteConfig.mjs +23 -0
  103. package/dist/esm/utils/getBaseViteConfig.native.js +25 -0
  104. package/dist/esm/utils/getBaseViteConfig.native.js.map +6 -0
  105. package/dist/esm/utils/getOptionsFilled.js +30 -0
  106. package/dist/esm/utils/getOptionsFilled.js.map +6 -0
  107. package/dist/esm/utils/getOptionsFilled.mjs +34 -0
  108. package/dist/esm/utils/getOptionsFilled.native.js +279 -0
  109. package/dist/esm/utils/getOptionsFilled.native.js.map +6 -0
  110. package/dist/esm/utils/getVitePath.js +27 -0
  111. package/dist/esm/utils/getVitePath.js.map +6 -0
  112. package/dist/esm/utils/getVitePath.mjs +20 -0
  113. package/dist/esm/utils/getVitePath.native.js +171 -0
  114. package/dist/esm/utils/getVitePath.native.js.map +6 -0
  115. package/package.json +6 -6
  116. package/src/build.ts +96 -0
  117. package/src/commands/cli-build.ts +25 -0
  118. package/src/commands/cli-dev.ts +2 -2
  119. package/src/commands/index.ts +2 -1
  120. package/src/{create.ts → createDevServer.ts} +122 -180
  121. package/src/index.ts +2 -1
  122. package/src/nativePlugin.ts +1 -1
  123. package/src/types.ts +2 -1
  124. package/src/utils/getBaseViteConfig.ts +22 -0
  125. package/src/utils/getOptionsFilled.ts +43 -0
  126. package/src/{getVitePath.ts → utils/getVitePath.ts} +17 -17
  127. package/types/VXRNConfigFilled.d.ts +21 -0
  128. package/types/build.d.ts +4 -0
  129. package/types/commands/cli-build.d.ts +3 -0
  130. package/types/commands/index.d.ts +1 -0
  131. package/types/create.d.ts +2 -1
  132. package/types/createDevServer.d.ts +13 -0
  133. package/types/getBaseViteConfig.d.ts +5 -0
  134. package/types/getOptionsFIlled.d.ts +21 -0
  135. package/types/index.d.ts +2 -1
  136. package/types/types.d.ts +2 -1
  137. package/types/utils/getBaseViteConfig.d.ts +5 -0
  138. package/types/utils/getOptionsFilled.d.ts +22 -0
  139. package/types/utils/getVitePath.d.ts +2 -0
package/src/build.ts ADDED
@@ -0,0 +1,96 @@
1
+ import { InlineConfig, mergeConfig, build as viteBuild, type UserConfig } from 'vite'
2
+
3
+ import { resolve as importMetaResolve } from 'import-meta-resolve'
4
+ import type { VXRNConfig } from './types'
5
+ import { getBaseViteConfig } from './utils/getBaseViteConfig'
6
+ import { getOptionsFilled } from './utils/getOptionsFilled'
7
+
8
+ export const resolveFile = (path: string) => {
9
+ try {
10
+ return importMetaResolve(path, import.meta.url).replace('file://', '')
11
+ } catch {
12
+ return require.resolve(path)
13
+ }
14
+ }
15
+
16
+ const extensions = [
17
+ '.web.tsx',
18
+ '.tsx',
19
+ '.web.ts',
20
+ '.ts',
21
+ '.web.jsx',
22
+ '.jsx',
23
+ '.web.js',
24
+ '.js',
25
+ '.css',
26
+ '.json',
27
+ ]
28
+
29
+ export const build = async (optionsIn: VXRNConfig) => {
30
+ const options = await getOptionsFilled(optionsIn)
31
+ const depsToOptimize = [
32
+ 'react',
33
+ 'react-dom',
34
+ '@react-native/normalize-color',
35
+ '@react-navigation/native',
36
+ 'expo-constants',
37
+ 'expo-modules-core',
38
+ 'expo-status-bar',
39
+ ]
40
+
41
+ let buildConfig = mergeConfig(
42
+ getBaseViteConfig({
43
+ mode: 'production',
44
+ }),
45
+ {
46
+ root: options.root,
47
+ clearScreen: false,
48
+ optimizeDeps: {
49
+ include: depsToOptimize,
50
+ esbuildOptions: {
51
+ resolveExtensions: extensions,
52
+ },
53
+ },
54
+ }
55
+ ) satisfies UserConfig
56
+
57
+ if (options.webConfig) {
58
+ buildConfig = mergeConfig(buildConfig, options.webConfig) as any
59
+ }
60
+
61
+ console.info(`build client`)
62
+ await viteBuild(
63
+ mergeConfig(buildConfig, {
64
+ build: {
65
+ ssrManifest: true,
66
+ outDir: 'dist/client',
67
+ },
68
+ } satisfies UserConfig)
69
+ )
70
+
71
+ console.info(`build server`)
72
+ await viteBuild(
73
+ mergeConfig(buildConfig, {
74
+ resolve: {
75
+ alias: {
76
+ 'react-native': 'react-native-web-lite',
77
+ },
78
+ },
79
+ optimizeDeps: {
80
+ esbuildOptions: {
81
+ format: 'cjs',
82
+ },
83
+ },
84
+ ssr: {
85
+ noExternal: true,
86
+ },
87
+ build: {
88
+ ssr: 'src/entry-server.tsx',
89
+ outDir: 'dist/server',
90
+ rollupOptions: {
91
+ external: [],
92
+ },
93
+ },
94
+ } satisfies UserConfig)
95
+ )
96
+ }
@@ -0,0 +1,25 @@
1
+ import { defineCommand } from 'citty'
2
+ import { build } from '../build'
3
+ import { readVXRNConfig } from '../utils/readVXRNConfig'
4
+
5
+ export default defineCommand({
6
+ meta: {
7
+ name: 'build',
8
+ version: '0.0.0',
9
+ description: 'Build your app',
10
+ },
11
+ args: {},
12
+ async run({ args }) {
13
+ const userConfig = await readVXRNConfig()
14
+
15
+ process.on('uncaughtException', (err) => {
16
+ console.error(err?.message || err)
17
+ })
18
+
19
+ const results = await build(userConfig)
20
+
21
+ if (process.env.DEBUG) {
22
+ console.info('results', results)
23
+ }
24
+ },
25
+ })
@@ -1,6 +1,6 @@
1
1
  import { defineCommand } from 'citty'
2
2
  import { readVXRNConfig } from '../utils/readVXRNConfig'
3
- import { create } from '../create'
3
+ import { createDevServer } from '../createDevServer'
4
4
 
5
5
  export default defineCommand({
6
6
  meta: {
@@ -12,7 +12,7 @@ export default defineCommand({
12
12
  async run({ args }) {
13
13
  const userConfig = await readVXRNConfig()
14
14
 
15
- const { start, stop } = await create({
15
+ const { start, stop } = await createDevServer({
16
16
  root: process.cwd(),
17
17
  host: '127.0.0.1',
18
18
  webConfig: {
@@ -5,5 +5,6 @@ const runDefault = (r: any) => (r.default || r) as Promise<CommandDef>
5
5
  export const commands = {
6
6
  // @ts-ignore
7
7
  dev: () => import('./cli-dev.mjs').then(runDefault),
8
+ // @ts-ignore
9
+ build: () => import('./cli-build.mjs').then(runDefault),
8
10
  }
9
-
@@ -1,5 +1,5 @@
1
1
  import wsAdapter from 'crossws/adapters/node'
2
- import { readFile } from 'node:fs/promises'
2
+ import findNodeModules from 'find-node-modules'
3
3
  import {
4
4
  createApp,
5
5
  createRouter,
@@ -10,16 +10,14 @@ import {
10
10
  toNodeListener,
11
11
  } from 'h3'
12
12
  import { createProxyEventHandler } from 'h3-proxy'
13
+ import { readFile } from 'node:fs/promises'
13
14
  import { createServer as nodeCreateServer } from 'node:http'
14
15
  import { dirname, join, relative, resolve } from 'node:path'
15
16
  import readline from 'node:readline'
16
17
  import viteInspectPlugin from 'vite-plugin-inspect'
17
18
  import { WebSocket } from 'ws'
18
- import { readPackageJSON } from 'pkg-types'
19
- import findNodeModules from 'find-node-modules'
20
19
 
21
20
  import * as babel from '@babel/core'
22
- import react from '@vitejs/plugin-react-swc'
23
21
  import { buildReact, buildReactJSX, buildReactNative } from '@vxrn/react-native-prebuilt'
24
22
  import viteReactPlugin, { swcTransform, transformForBuild } from '@vxrn/vite-native-swc'
25
23
  import { parse } from 'es-module-lexer'
@@ -35,15 +33,17 @@ import {
35
33
  type UserConfig,
36
34
  } from 'vite'
37
35
 
36
+ import createViteFlow from '@vxrn/vite-flow'
38
37
  import type { Peer } from 'crossws'
39
38
  import { resolve as importMetaResolve } from 'import-meta-resolve'
40
39
  import { clientInjectionsPlugin } from './dev/clientInjectPlugin'
41
- import { getVitePath } from './getVitePath'
42
40
  import { nativePlugin } from './nativePlugin'
43
- import createViteFlow from '@vxrn/vite-flow'
44
- import type { HMRListener, VXRNConfig } from './types'
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'
45
45
 
46
- const resolveFile = (path: string) => {
46
+ export const resolveFile = (path: string) => {
47
47
  try {
48
48
  return importMetaResolve(path, import.meta.url).replace('file://', '')
49
49
  } catch {
@@ -75,45 +75,6 @@ const extensions = [
75
75
  '.json',
76
76
  ]
77
77
 
78
- type State = {
79
- applyPatches?: boolean
80
- }
81
-
82
- async function readState(cacheDir: string) {
83
- const statePath = join(cacheDir, 'state.json')
84
- const state: State = (await FSExtra.pathExists(statePath))
85
- ? await FSExtra.readJSON(statePath)
86
- : {}
87
- return state
88
- }
89
-
90
- async function getOptionsFilled(options: VXRNConfig) {
91
- const { host = '127.0.0.1', root = process.cwd(), port = 8081 } = options
92
- const packageRootDir = join(import.meta.url ?? __filename, '..', '..', '..').replace('file:', '')
93
- const cacheDir = join(root, 'node_modules', '.cache', 'vxrn')
94
- const internalPatchesDir = join(packageRootDir, 'patches')
95
- const userPatchesDir = join(root, 'patches')
96
- const [state, packageJSON] = await Promise.all([
97
- //
98
- readState(cacheDir),
99
- readPackageJSON(),
100
- ])
101
- return {
102
- ...options,
103
- packageJSON,
104
- state,
105
- packageRootDir,
106
- cacheDir,
107
- userPatchesDir,
108
- internalPatchesDir,
109
- host,
110
- root,
111
- port,
112
- }
113
- }
114
-
115
- type VXRNConfigFilled = Awaited<ReturnType<typeof getOptionsFilled>>
116
-
117
78
  const { ensureDir, pathExists, pathExistsSync } = FSExtra
118
79
 
119
80
  const patches = [
@@ -167,7 +128,7 @@ async function checkPatches(options: VXRNConfigFilled) {
167
128
  }
168
129
  }
169
130
 
170
- export const create = async (optionsIn: VXRNConfig) => {
131
+ export const createDevServer = async (optionsIn: VXRNConfig) => {
171
132
  const options = await getOptionsFilled(optionsIn)
172
133
  const { host, port, root, cacheDir } = options
173
134
 
@@ -303,147 +264,123 @@ export const create = async (optionsIn: VXRNConfig) => {
303
264
 
304
265
  const depsToOptimize = ['react', 'react-dom', '@react-native/normalize-color']
305
266
 
306
- let serverConfig: UserConfig = {
307
- root,
308
- mode: 'development',
309
- clearScreen: false,
310
- define: {
311
- __DEV__: 'true',
312
- 'process.env.NODE_ENV': `"development"`,
313
- },
267
+ const reactNativeHMRPlugin = {
268
+ name: 'client-transform',
314
269
 
315
- resolve: {
316
- // dedupe: ['react', 'react-dom'],
317
- alias: {
318
- // ...Object.fromEntries(Object.entries(virtualModules).map(([k, v]) => [k, v.alias])),
319
- 'react-native': resolveFile('react-native-web-lite'),
320
- },
321
- },
322
- optimizeDeps: {
323
- include: depsToOptimize,
324
- exclude: Object.values(virtualModules).map((v) => v.alias),
325
- force: true,
326
- esbuildOptions: {
327
- resolveExtensions: extensions,
328
- },
329
- },
330
- build: {
331
- commonjsOptions: {
332
- transformMixedEsModules: true,
333
- },
334
- },
335
- plugins: [
336
- // this breaks web, but for native maybe we need to manually run in the native handleHotUpdate below
337
- // swapRnPlugin,
338
-
339
- viteFlow,
340
- react(),
341
- // viteReactPlugin({
342
- // tsDecorators: true,
343
- // mode: 'serve',
344
- // }),
345
-
346
- {
347
- name: 'client-transform',
348
-
349
- async handleHotUpdate({ read, modules, file }) {
350
- try {
351
- if (!isWithin(root, file)) {
352
- return
353
- }
354
-
355
- const [module] = modules
356
- if (!module) return
357
-
358
- const id = module?.url || file.replace(root, '')
270
+ async handleHotUpdate({ read, modules, file }) {
271
+ try {
272
+ if (!isWithin(root, file)) {
273
+ return
274
+ }
359
275
 
360
- const code = await read()
276
+ const [module] = modules
277
+ if (!module) return
361
278
 
362
- // got a weird pre compiled file on startup
363
- if (code.startsWith(`'use strict';`)) return
279
+ const id = module?.url || file.replace(root, '')
364
280
 
365
- if (!code) {
366
- return
367
- }
281
+ const code = await read()
368
282
 
369
- let source = code
283
+ // got a weird pre compiled file on startup
284
+ if (code.startsWith(`'use strict';`)) return
370
285
 
371
- // we have to remove jsx before we can parse imports...
372
- source = (await transformForBuild(id, source))?.code || ''
286
+ if (!code) {
287
+ return
288
+ }
373
289
 
374
- const importsMap = {}
290
+ let source = code
375
291
 
376
- // parse imports of modules into ids:
377
- // eg `import x from '@tamagui/core'` => `import x from '/me/node_modules/@tamagui/core/index.js'`
378
- const [imports] = parse(source)
292
+ // we have to remove jsx before we can parse imports...
293
+ source = (await transformForBuild(id, source))?.code || ''
379
294
 
380
- let accumulatedSliceOffset = 0
295
+ const importsMap = {}
381
296
 
382
- for (const specifier of imports) {
383
- const { n: importName, s: start } = specifier
297
+ // parse imports of modules into ids:
298
+ // eg `import x from '@tamagui/core'` => `import x from '/me/node_modules/@tamagui/core/index.js'`
299
+ const [imports] = parse(source)
384
300
 
385
- if (importName) {
386
- const id = await getVitePath(entryRoot, file, importName)
387
- if (!id) {
388
- console.warn('???')
389
- continue
390
- }
301
+ let accumulatedSliceOffset = 0
391
302
 
392
- importsMap[id] = id.replace(/^(\.\.\/)+/, '')
303
+ for (const specifier of imports) {
304
+ const { n: importName, s: start } = specifier
393
305
 
394
- // replace module name with id for hmr
395
- const len = importName.length
396
- const extraLen = id.length - len
397
- source =
398
- source.slice(0, start + accumulatedSliceOffset) +
399
- id +
400
- source.slice(start + accumulatedSliceOffset + len)
401
- accumulatedSliceOffset += extraLen
402
- }
306
+ if (importName) {
307
+ const id = await getVitePath(entryRoot, file, importName)
308
+ if (!id) {
309
+ console.warn('???')
310
+ continue
403
311
  }
404
312
 
405
- // then we have to convert to commonjs..
313
+ importsMap[id] = id.replace(/^(\.\.\/)+/, '')
314
+
315
+ // replace module name with id for hmr
316
+ const len = importName.length
317
+ const extraLen = id.length - len
406
318
  source =
407
- (
408
- await swcTransform(id, source, {
409
- mode: 'serve-cjs',
410
- })
411
- )?.code || ''
412
-
413
- if (!source) {
414
- throw '❌ no source'
415
- }
319
+ source.slice(0, start + accumulatedSliceOffset) +
320
+ id +
321
+ source.slice(start + accumulatedSliceOffset + len)
322
+ accumulatedSliceOffset += extraLen
323
+ }
324
+ }
416
325
 
417
- importsMap['currentPath'] = id
326
+ // then we have to convert to commonjs..
327
+ source =
328
+ (
329
+ await swcTransform(id, source, {
330
+ mode: 'serve-cjs',
331
+ })
332
+ )?.code || ''
418
333
 
419
- const hotUpdateSource = `exports = ((exports) => {
420
- const require = createRequire(${JSON.stringify(importsMap, null, 2)})
421
- ${source
422
- .replace(`import.meta.hot.accept(() => {})`, ``)
423
- // replace import.meta.glob with empty array in hot reloads
424
- .replaceAll(/import.meta.glob\(.*\)/gi, `globalThis['__importMetaGlobbed'] || {}`)};
425
- return exports })({})`
334
+ if (!source) {
335
+ throw '❌ no source'
336
+ }
426
337
 
427
- if (process.env.DEBUG) {
428
- console.info(`Sending hot update`, hotUpdateSource)
429
- }
338
+ importsMap['currentPath'] = id
430
339
 
431
- hotUpdatedCJSFiles.set(id, hotUpdateSource)
432
- } catch (err) {
433
- console.error(`Error processing hmr update:`, err)
434
- }
340
+ const hotUpdateSource = `exports = ((exports) => {
341
+ const require = createRequire(${JSON.stringify(importsMap, null, 2)})
342
+ ${source
343
+ .replace(`import.meta.hot.accept(() => {})`, ``)
344
+ // replace import.meta.glob with empty array in hot reloads
345
+ .replaceAll(/import.meta.glob\(.*\)/gi, `globalThis['__importMetaGlobbed'] || {}`)};
346
+ return exports })({})`
347
+
348
+ if (process.env.DEBUG) {
349
+ console.info(`Sending hot update`, hotUpdateSource)
350
+ }
351
+
352
+ hotUpdatedCJSFiles.set(id, hotUpdateSource)
353
+ } catch (err) {
354
+ console.error(`Error processing hmr update:`, err)
355
+ }
356
+ },
357
+ }
358
+
359
+ let serverConfig: UserConfig = mergeConfig(
360
+ getBaseViteConfig({
361
+ mode: 'development',
362
+ }),
363
+ {
364
+ root,
365
+ clearScreen: false,
366
+ plugins: [reactNativeHMRPlugin],
367
+ optimizeDeps: {
368
+ include: depsToOptimize,
369
+ exclude: Object.values(virtualModules).map((v) => v.alias),
370
+ force: true,
371
+ esbuildOptions: {
372
+ resolveExtensions: extensions,
435
373
  },
436
374
  },
437
- ],
438
-
439
- server: {
440
- hmr: {
441
- path: '/__vxrnhmr',
375
+ server: {
376
+ hmr: {
377
+ path: '/__vxrnhmr',
378
+ },
379
+ cors: true,
380
+ host,
442
381
  },
443
- cors: true,
444
- host,
445
- },
446
- } satisfies InlineConfig
382
+ }
383
+ ) satisfies InlineConfig
447
384
 
448
385
  if (options.webConfig) {
449
386
  serverConfig = mergeConfig(serverConfig, options.webConfig) as any
@@ -718,6 +655,8 @@ export const create = async (optionsIn: VXRNConfig) => {
718
655
  })
719
656
  }
720
657
 
658
+ const buildInput = options.entryNative || 'index.jsx'
659
+
721
660
  // build app
722
661
  let buildConfig = {
723
662
  plugins: [
@@ -748,10 +687,10 @@ export const create = async (optionsIn: VXRNConfig) => {
748
687
  mode: 'build',
749
688
  }),
750
689
 
751
- viteInspectPlugin({
752
- build: true,
753
- outputDir: '.vite-inspect',
754
- }),
690
+ // viteInspectPlugin({
691
+ // build: true,
692
+ // outputDir: '.vite-inspect',
693
+ // }),
755
694
 
756
695
  {
757
696
  name: 'treat-js-files-as-jsx',
@@ -765,7 +704,7 @@ export const create = async (optionsIn: VXRNConfig) => {
765
704
  })
766
705
  },
767
706
  },
768
- ],
707
+ ].filter(Boolean),
769
708
  appType: 'custom',
770
709
  root,
771
710
  clearScreen: false,
@@ -777,6 +716,14 @@ export const create = async (optionsIn: VXRNConfig) => {
777
716
  },
778
717
  },
779
718
 
719
+ resolve: {
720
+ extensions: nativeExtensions,
721
+ },
722
+
723
+ mode: 'development',
724
+ define: {
725
+ 'process.env.NODE_ENV': `"development"`,
726
+ },
780
727
  build: {
781
728
  ssr: false,
782
729
  minify: false,
@@ -792,15 +739,6 @@ export const create = async (optionsIn: VXRNConfig) => {
792
739
  },
793
740
  },
794
741
  },
795
-
796
- resolve: {
797
- extensions: nativeExtensions,
798
- },
799
-
800
- mode: 'development',
801
- define: {
802
- 'process.env.NODE_ENV': `"development"`,
803
- },
804
742
  } satisfies InlineConfig
805
743
 
806
744
  if (options.buildConfig) {
@@ -810,6 +748,10 @@ export const create = async (optionsIn: VXRNConfig) => {
810
748
  // this fixes my swap-react-native plugin not being called pre 😳
811
749
  await resolveConfig(buildConfig, 'build')
812
750
 
751
+ // seems to be not working but needed to put it after the resolve or else it was cleared
752
+ // @ts-ignore
753
+ // buildConfig.build.rollupOptions.input = join(root, buildInput)
754
+
813
755
  const buildOutput = await build(buildConfig)
814
756
 
815
757
  if (!('output' in buildOutput)) {
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
- export { create } from './create'
1
+ export { createDevServer as create } from './createDevServer'
2
+ export { build } from './build'
2
3
  export { nativePlugin } from './nativePlugin'
3
4
  export * from './types'
@@ -4,7 +4,7 @@ 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 './getVitePath'
7
+ import { getVitePath } from './utils/getVitePath'
8
8
 
9
9
  const extensions = [
10
10
  '.ios.js',
package/src/types.ts CHANGED
@@ -1,7 +1,8 @@
1
- import type { FilterPattern, InlineConfig } from 'vite'
2
1
  import type { Options as FlowOptions } from '@vxrn/vite-flow'
2
+ import type { InlineConfig } from 'vite'
3
3
 
4
4
  export type VXRNConfig = {
5
+ entryNative?: string
5
6
  root?: string
6
7
  host?: string
7
8
  port?: number
@@ -0,0 +1,22 @@
1
+ import type { UserConfig } from 'vite'
2
+ import { resolveFile } from '../createDevServer'
3
+
4
+ export function getBaseViteConfig({ mode }: { mode: 'development' | 'production' }): UserConfig {
5
+ return {
6
+ mode,
7
+ define: {
8
+ __DEV__: `${mode === 'development'}`,
9
+ 'process.env.NODE_ENV': `"${mode}"`,
10
+ },
11
+ resolve: {
12
+ alias: {
13
+ 'react-native': resolveFile('react-native-web-lite'),
14
+ },
15
+ },
16
+ build: {
17
+ commonjsOptions: {
18
+ transformMixedEsModules: true,
19
+ },
20
+ },
21
+ }
22
+ }
@@ -0,0 +1,43 @@
1
+ import { join } from 'node:path'
2
+ import { readPackageJSON } from 'pkg-types'
3
+ import FSExtra from 'fs-extra'
4
+ import type { VXRNConfig } from '../types'
5
+
6
+ export type VXRNConfigFilled = Awaited<ReturnType<typeof getOptionsFilled>>
7
+
8
+ export async function getOptionsFilled(options: VXRNConfig) {
9
+ const { host = '127.0.0.1', root = process.cwd(), port = 8081 } = options
10
+ const packageRootDir = join(import.meta.url ?? __filename, '..', '..', '..').replace('file:', '')
11
+ const cacheDir = join(root, 'node_modules', '.cache', 'vxrn')
12
+ const internalPatchesDir = join(packageRootDir, 'patches')
13
+ const userPatchesDir = join(root, 'patches')
14
+ const [state, packageJSON] = await Promise.all([
15
+ //
16
+ readState(cacheDir),
17
+ readPackageJSON(),
18
+ ])
19
+ return {
20
+ ...options,
21
+ packageJSON,
22
+ state,
23
+ packageRootDir,
24
+ cacheDir,
25
+ userPatchesDir,
26
+ internalPatchesDir,
27
+ host,
28
+ root,
29
+ port,
30
+ }
31
+ }
32
+
33
+ type State = {
34
+ applyPatches?: boolean
35
+ }
36
+
37
+ async function readState(cacheDir: string) {
38
+ const statePath = join(cacheDir, 'state.json')
39
+ const state: State = (await FSExtra.pathExists(statePath))
40
+ ? await FSExtra.readJSON(statePath)
41
+ : {}
42
+ return state
43
+ }