@vxrn/compiler 1.1.397 → 1.1.398

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 (76) hide show
  1. package/dist/cjs/configure.cjs +1 -0
  2. package/dist/cjs/configure.js +1 -0
  3. package/dist/cjs/configure.js.map +1 -1
  4. package/dist/cjs/index.cjs +57 -7
  5. package/dist/cjs/index.js +51 -6
  6. package/dist/cjs/index.js.map +1 -1
  7. package/dist/cjs/refresh-runtime.cjs +1 -3
  8. package/dist/cjs/refresh-runtime.js +1 -1
  9. package/dist/cjs/refresh-runtime.js.map +1 -1
  10. package/dist/cjs/transformBabel.cjs +22 -18
  11. package/dist/cjs/transformBabel.js +19 -22
  12. package/dist/cjs/transformBabel.js.map +1 -1
  13. package/dist/cjs/transformSWC.cjs +77 -76
  14. package/dist/cjs/transformSWC.js +76 -76
  15. package/dist/cjs/transformSWC.js.map +1 -1
  16. package/dist/esm/configure.js +1 -0
  17. package/dist/esm/configure.js.map +1 -1
  18. package/dist/esm/configure.mjs +1 -0
  19. package/dist/esm/configure.mjs.map +1 -1
  20. package/dist/esm/index.js +53 -6
  21. package/dist/esm/index.js.map +1 -1
  22. package/dist/esm/index.mjs +58 -8
  23. package/dist/esm/index.mjs.map +1 -1
  24. package/dist/esm/refresh-runtime.js +1 -1
  25. package/dist/esm/refresh-runtime.js.map +1 -1
  26. package/dist/esm/refresh-runtime.mjs +1 -3
  27. package/dist/esm/refresh-runtime.mjs.map +1 -1
  28. package/dist/esm/transformBabel.js +19 -21
  29. package/dist/esm/transformBabel.js.map +1 -1
  30. package/dist/esm/transformBabel.mjs +22 -18
  31. package/dist/esm/transformBabel.mjs.map +1 -1
  32. package/dist/esm/transformSWC.js +77 -77
  33. package/dist/esm/transformSWC.js.map +1 -1
  34. package/dist/esm/transformSWC.mjs +78 -77
  35. package/dist/esm/transformSWC.mjs.map +1 -1
  36. package/package.json +10 -8
  37. package/src/configure.ts +2 -0
  38. package/src/index.ts +108 -11
  39. package/src/refresh-runtime.js +1 -1
  40. package/src/transformBabel.ts +36 -17
  41. package/src/transformSWC.ts +102 -102
  42. package/types/configure.d.ts +1 -0
  43. package/types/configure.d.ts.map +1 -1
  44. package/types/index.d.ts.map +1 -1
  45. package/types/transformBabel.d.ts +1 -1
  46. package/types/transformBabel.d.ts.map +1 -1
  47. package/types/transformSWC.d.ts +4 -1
  48. package/types/transformSWC.d.ts.map +1 -1
  49. package/dist/cjs/configure.native.js +0 -35
  50. package/dist/cjs/configure.native.js.map +0 -6
  51. package/dist/cjs/constants.native.js +0 -65
  52. package/dist/cjs/constants.native.js.map +0 -6
  53. package/dist/cjs/index.native.js +0 -123
  54. package/dist/cjs/index.native.js.map +0 -6
  55. package/dist/cjs/refresh-runtime.native.js +0 -316
  56. package/dist/cjs/refresh-runtime.native.js.map +0 -6
  57. package/dist/cjs/transformBabel.native.js +0 -152
  58. package/dist/cjs/transformBabel.native.js.map +0 -6
  59. package/dist/cjs/transformSWC.native.js +0 -275
  60. package/dist/cjs/transformSWC.native.js.map +0 -6
  61. package/dist/cjs/types.native.js +0 -15
  62. package/dist/cjs/types.native.js.map +0 -6
  63. package/dist/esm/configure.native.js +0 -13
  64. package/dist/esm/configure.native.js.map +0 -6
  65. package/dist/esm/constants.native.js +0 -41
  66. package/dist/esm/constants.native.js.map +0 -6
  67. package/dist/esm/index.native.js +0 -104
  68. package/dist/esm/index.native.js.map +0 -6
  69. package/dist/esm/refresh-runtime.native.js +0 -290
  70. package/dist/esm/refresh-runtime.native.js.map +0 -6
  71. package/dist/esm/transformBabel.native.js +0 -125
  72. package/dist/esm/transformBabel.native.js.map +0 -6
  73. package/dist/esm/transformSWC.native.js +0 -257
  74. package/dist/esm/transformSWC.native.js.map +0 -6
  75. package/dist/esm/types.native.js +0 -1
  76. package/dist/esm/types.native.js.map +0 -6
package/src/index.ts CHANGED
@@ -6,8 +6,11 @@
6
6
  import { resolvePath } from '@vxrn/utils'
7
7
  import { readFileSync } from 'node:fs'
8
8
  import { readFile } from 'node:fs/promises'
9
- import { extname, join } from 'node:path'
9
+ import { extname, join, sep } from 'node:path'
10
+ import { cssToReactNativeRuntime } from 'react-native-css-interop/css-to-rn/index.js'
11
+ import type { OutputChunk } from 'rollup'
10
12
  import type { PluginOption, UserConfig } from 'vite'
13
+ import { configuration } from './configure'
11
14
  import { debug, runtimePublicPath, validParsers } from './constants'
12
15
  import { getBabelOptions, transformBabel } from './transformBabel'
13
16
  import { transformSWC } from './transformSWC'
@@ -39,6 +42,14 @@ export async function createVXRNCompilerPlugin(
39
42
  return name as Environment
40
43
  }
41
44
 
45
+ const reactForRNVersion = reactVersion.split('.')[0] as '18' | '19'
46
+
47
+ const cssTransformCache = new Map<string, string>()
48
+
49
+ // fix so we can align the diff between vite and rollup id (rollup resolves from root monorepo)
50
+ const rollupPath = resolvePath('rollup')
51
+ const rollupNodeMods = rollupPath.slice(0, rollupPath.indexOf(sep + 'node_modules'))
52
+
42
53
  return [
43
54
  {
44
55
  name: 'one:compiler-resolve-refresh-runtime',
@@ -51,6 +62,63 @@ export async function createVXRNCompilerPlugin(
51
62
  : undefined,
52
63
  },
53
64
 
65
+ {
66
+ name: `one:compiler-css-to-js`,
67
+
68
+ transform(codeIn, id) {
69
+ const environment = getEnvName(this.environment.name)
70
+ if (configuration.enableNativeCSS && (environment === 'ios' || environment === 'android')) {
71
+ if (extname(id) === '.css') {
72
+ const data = JSON.stringify(cssToReactNativeRuntime(codeIn, { inlineRem: 16 }))
73
+ // TODO were hardcoding the require id we bundle as: nativewind/dist/index.js
74
+ // could at least resolve this using resolvePath
75
+ const code = `require("nativewind/dist/index.js").StyleSheet.registerCompiled(${data})`
76
+ const newId = `${id}.js`
77
+
78
+ // rollup uses relative to its node_modules parent dir, vite here uses absolute
79
+ const cssId = newId.replace(rollupNodeMods + sep, '')
80
+ cssTransformCache.set(cssId, code)
81
+
82
+ return {
83
+ code,
84
+ id: newId,
85
+ map: null,
86
+ }
87
+ }
88
+ }
89
+ },
90
+
91
+ generateBundle(_, bundle) {
92
+ const environment = getEnvName(this.environment.name)
93
+
94
+ if (configuration.enableNativeCSS && (environment === 'ios' || environment === 'android')) {
95
+ const rootJSName = Object.keys(bundle).find((i) => {
96
+ const chunk = bundle[i]
97
+ return chunk.type == 'chunk' && chunk.fileName.match(/.[cm]?js(?:\?.+)?$/) != null
98
+ })
99
+ if (!rootJSName) {
100
+ throw new Error(`Can't find root js, internal one error`)
101
+ }
102
+
103
+ const rootJS = bundle[rootJSName] as OutputChunk
104
+
105
+ const cssAssets = Object.keys(bundle).filter((i) =>
106
+ bundle[i].fileName.endsWith('.css.js')
107
+ )
108
+
109
+ for (const name of cssAssets) {
110
+ delete bundle[name]
111
+
112
+ const jsCSS = cssTransformCache.get(name)
113
+ rootJS.code = `
114
+ ${jsCSS}
115
+ ${rootJS.code}
116
+ `
117
+ }
118
+ }
119
+ },
120
+ },
121
+
54
122
  {
55
123
  name: 'one:compiler',
56
124
  enforce: 'pre',
@@ -61,6 +129,10 @@ export async function createVXRNCompilerPlugin(
61
129
  optimizeDeps: {
62
130
  noDiscovery: true,
63
131
  },
132
+
133
+ define: {
134
+ 'process.env.NATIVEWIND_OS': 'native',
135
+ },
64
136
  } satisfies UserConfig
65
137
 
66
138
  return {
@@ -75,6 +147,29 @@ export async function createVXRNCompilerPlugin(
75
147
  order: 'pre',
76
148
  async handler(codeIn, _id) {
77
149
  let code = codeIn
150
+ const environment = getEnvName(this.environment.name)
151
+ const isNative = environment === 'ios' || environment === 'android'
152
+ const production =
153
+ process.env.NODE_ENV === 'production' ||
154
+ JSON.parse(this.environment.config?.define?.['process.env.NODE_ENV'] || '""') ===
155
+ 'production'
156
+
157
+ // it has a hidden special character
158
+ // TODO: use === special char this is in sensitive perf path
159
+ const isEntry = _id.includes('one-entry-native')
160
+
161
+ if (isEntry) {
162
+ if (isNative && !production) {
163
+ code = `import '@vxrn/vite-native-client'\n${code}`
164
+ }
165
+ if (isNative && configuration.enableNativewind) {
166
+ // ensure we have nativewind import in bundle root
167
+ code = `import * as x from 'nativewind'\n${code}`
168
+ }
169
+
170
+ // TODO sourcemap add two ';;'?
171
+ return code
172
+ }
78
173
 
79
174
  const shouldDebug =
80
175
  process.env.NODE_ENV === 'development' && codeIn.startsWith('// debug')
@@ -85,12 +180,14 @@ export async function createVXRNCompilerPlugin(
85
180
  }
86
181
 
87
182
  const extension = extname(_id)
88
- if (!validParsers.has(extension)) {
183
+
184
+ if (extension === '.css') {
185
+ // handled in one:compiler-css-to-js
89
186
  return
90
187
  }
91
188
 
92
- if (extension === '.css') {
93
- //
189
+ if (!validParsers.has(extension)) {
190
+ return
94
191
  }
95
192
 
96
193
  let id = _id.split('?')[0]
@@ -105,15 +202,12 @@ export async function createVXRNCompilerPlugin(
105
202
  return
106
203
  }
107
204
 
108
- const environment = getEnvName(this.environment.name)
109
- const production = process.env.NODE_ENV === 'production'
110
-
111
205
  const transformProps: GetTransformProps = {
112
206
  id,
113
207
  code,
114
208
  development: !production,
115
209
  environment,
116
- reactForRNVersion: reactVersion.split('.')[0] as '18' | '19',
210
+ reactForRNVersion,
117
211
  }
118
212
 
119
213
  const userTransform = optionsIn?.transform?.(transformProps)
@@ -129,10 +223,13 @@ export async function createVXRNCompilerPlugin(
129
223
  })
130
224
 
131
225
  if (babelOptions) {
226
+ // TODO we probably need to forward sourceMap here?
132
227
  const babelOut = await transformBabel(id, code, babelOptions)
133
- if (babelOut) {
228
+ if (babelOut?.code) {
134
229
  debug?.(`[${id}] transformed with babel options: ${JSON.stringify(babelOptions)}`)
135
- code = babelOut
230
+ // TODO we may want to just avoid SWC after babel it likely is faster
231
+ // we'd need to have metro or metro-like preset
232
+ code = babelOut.code
136
233
  }
137
234
  }
138
235
 
@@ -142,7 +239,7 @@ export async function createVXRNCompilerPlugin(
142
239
 
143
240
  const swcOptions = {
144
241
  environment: environment,
145
- mode: 'serve',
242
+ mode: optionsIn?.mode || 'serve',
146
243
  production,
147
244
  ...optionsIn,
148
245
  } satisfies Options
@@ -575,7 +575,7 @@ const enqueueUpdate = debounce(async () => {
575
575
  }, 16)
576
576
 
577
577
  export function validateRefreshBoundaryAndEnqueueUpdate(id, prevExports, nextExports) {
578
- const ignoredExports = window.__getReactRefreshIgnoredExports?.({ id }) ?? []
578
+ const ignoredExports = []
579
579
  if (predicateOnExport(ignoredExports, prevExports, (key) => key in nextExports) !== true) {
580
580
  return 'Could not Fast Refresh (export removed)'
581
581
  }
@@ -1,4 +1,5 @@
1
1
  import babel from '@babel/core'
2
+ import { resolvePath } from '@vxrn/utils'
2
3
  import { relative } from 'node:path'
3
4
  import { configuration } from './configure'
4
5
  import { asyncGeneratorRegex, debug } from './constants'
@@ -10,9 +11,7 @@ type Props = GetTransformProps & {
10
11
 
11
12
  export function getBabelOptions(props: Props): babel.TransformOptions | null {
12
13
  if (props.userSetting === 'babel') {
13
- return {
14
- plugins: getPlugins(props, true),
15
- }
14
+ return getOptions(props, true)
16
15
  }
17
16
  if (
18
17
  typeof props.userSetting === 'undefined' ||
@@ -21,24 +20,32 @@ export function getBabelOptions(props: Props): babel.TransformOptions | null {
21
20
  if (props.userSetting?.excludeDefaultPlugins) {
22
21
  return props.userSetting
23
22
  }
24
- const plugins = getPlugins(props)
25
- if (plugins.length) {
26
- return {
27
- plugins,
28
- }
29
- }
23
+ return getOptions(props)
30
24
  }
31
25
  return null
32
26
  }
33
27
 
34
- const getPlugins = (props: Props, force = false) => {
28
+ const getOptions = (props: Props, force = false): babel.TransformOptions | null => {
29
+ const presets: string[] = []
35
30
  let plugins: babel.PluginItem[] = []
36
31
 
37
32
  if (force || shouldBabelGenerators(props)) {
38
33
  plugins = getBasePlugins(props)
39
34
  }
40
35
 
41
- if (shouldBabelReanimated(props)) {
36
+ const enableNativewind =
37
+ configuration.enableNativewind &&
38
+ (props.environment === 'ios' || props.environment === 'android') &&
39
+ // only needed for createElement calls, so be a bit conservative
40
+ props.code.includes('createElement')
41
+
42
+ if (enableNativewind) {
43
+ if (!props.id.includes('node_modules')) {
44
+ plugins.push(resolvePath('react-native-css-interop/dist/babel-plugin.js'))
45
+ }
46
+ }
47
+
48
+ if (enableNativewind || shouldBabelReanimated(props)) {
42
49
  debug?.(`Using babel reanimated on file`)
43
50
  plugins.push('react-native-reanimated/plugin')
44
51
  }
@@ -53,36 +60,47 @@ const getPlugins = (props: Props, force = false) => {
53
60
  plugins.push('@react-native/babel-plugin-codegen')
54
61
  }
55
62
 
56
- return plugins
63
+ if (plugins.length || presets.length) {
64
+ return { plugins, presets }
65
+ }
66
+
67
+ return null
57
68
  }
69
+
58
70
  /**
59
71
  * Transform input to mostly ES5 compatible code, keep ESM syntax, and transform generators.
60
72
  */
61
73
  export async function transformBabel(id: string, code: string, options: babel.TransformOptions) {
62
74
  const compilerPlugin = options.plugins?.find((x) => x && x[0] === 'babel-plugin-react-compiler')
63
75
 
64
- const out = await new Promise<string>((res, rej) => {
76
+ const out = await new Promise<babel.BabelFileResult>((res, rej) => {
65
77
  babel.transform(
66
78
  code,
67
79
  {
68
80
  filename: id,
69
81
  compact: false,
82
+ babelrc: false,
83
+ configFile: false,
84
+ sourceMaps: true,
70
85
  minified: false,
71
- presets: ['@babel/preset-typescript'],
72
86
  ...options,
87
+ presets: ['@babel/preset-typescript', ...(options.presets || [])],
73
88
  },
74
89
  (err: any, result) => {
75
90
  if (!result || err) {
76
91
  return rej(err || 'no res')
77
92
  }
78
- res(result!.code!)
93
+ res(result!)
79
94
  }
80
95
  )
81
96
  })
82
97
 
83
98
  if (
84
99
  compilerPlugin &&
85
- out.includes(compilerPlugin[1] === '18' ? `react-compiler-runtime` : `react/compiler-runtime`)
100
+ // TODO this detection could be a lot faster
101
+ out.code?.includes(
102
+ compilerPlugin[1] === '18' ? `react-compiler-runtime` : `react/compiler-runtime`
103
+ )
86
104
  ) {
87
105
  console.info(` 🪄 [compiler] ${relative(process.cwd(), id)}`)
88
106
  }
@@ -139,8 +157,9 @@ const shouldBabelReactCompiler = (props: Props) => {
139
157
  }
140
158
  }
141
159
  if (!/.*(.tsx?)$/.test(props.id)) return false
160
+ // disable node modules for now...
161
+ if (props.id.includes('node_modules')) return false
142
162
  if (props.code.startsWith('// disable-compiler')) return false
143
- // may want to disable in node modules? but rare to have tsx in node mods
144
163
  return true
145
164
  }
146
165
 
@@ -6,19 +6,21 @@ import {
6
6
  type TransformConfig,
7
7
  } from '@swc/core'
8
8
  import type { SourceMapPayload } from 'node:module'
9
- import { extname } from 'node:path'
9
+ import { extname, sep } from 'node:path'
10
10
  import { merge } from 'ts-deepmerge'
11
11
  import { configuration } from './configure'
12
12
  import { asyncGeneratorRegex, debug, parsers, runtimePublicPath } from './constants'
13
13
  import type { Options } from './types'
14
14
 
15
+ const ignoreId = new RegExp(`node_modules\\${sep}(\\.vite|vite)\\${sep}`)
16
+
15
17
  export async function transformSWC(
16
18
  id: string,
17
19
  code: string,
18
20
  options: Options & { es5?: boolean },
19
21
  swcOptions?: SWCOptions
20
22
  ) {
21
- if (id.includes('.vite')) {
23
+ if (ignoreId.test(id)) {
22
24
  return
23
25
  }
24
26
 
@@ -37,11 +39,11 @@ export async function transformSWC(
37
39
  return
38
40
  }
39
41
 
40
- const enableNativeCSS =
41
- configuration.enableNativeCSS &&
42
- // temp fix idk why this error:
43
- // node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx (19:9): "createInteropElement" is not exported by "../../node_modules/react-native-css-interop/dist/runtime/jsx-dev-runtime.js", imported by "node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx
44
- !id.includes('node_modules')
42
+ const enableNativeCSS = configuration.enableNativeCSS
43
+
44
+ // temp fix idk why this error:
45
+ // node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx (19:9): "createInteropElement" is not exported by "../../node_modules/react-native-css-interop/dist/runtime/jsx-dev-runtime.js", imported by "node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx
46
+ !id.includes('node_modules')
45
47
 
46
48
  const refresh =
47
49
  options.environment !== 'ssr' && !options.production && !options.noHMR && !options.forceJSX
@@ -53,10 +55,11 @@ export async function transformSWC(
53
55
  importSource: 'react',
54
56
  ...(enableNativeCSS
55
57
  ? {
56
- importSource: 'react-native-css-interop',
57
- pragma: 'createInteropElement',
58
+ importSource: 'nativewind',
59
+ // pragma: 'createInteropElement',
60
+ // pragmaFrag: '_InteropFragment',
58
61
  // swc doesnt actually change the import right
59
- runtime: 'classic',
62
+ // runtime: 'classic',
60
63
  }
61
64
  : {}),
62
65
  } satisfies TransformConfig['react']
@@ -149,12 +152,13 @@ export async function transformSWC(
149
152
  })()
150
153
 
151
154
  if (enableNativeCSS) {
152
- if (result.code.includes(`createInteropElement`)) {
153
- result.code = `import { createInteropElement } from 'react-native-css-interop/jsx-dev-runtime'\n${result.code}`
155
+ if (result.code.includes(`createInteropElement(`)) {
156
+ // TODO need to fix sourceMap adding a ';'
157
+ result.code = `import { createInteropElement, Fragment as _InteropFragment } from 'react-native-css-interop/jsx-dev-runtime'\n${result.code}`
154
158
  }
155
159
  }
156
160
 
157
- const shouldHMR = refresh && refreshContentRE.test(result.code)
161
+ const hasRefreshRuntime = refresh && refreshContentRE.test(result.code)
158
162
 
159
163
  // fix for node_modules that ship tsx but don't use type-specific imports
160
164
  if (
@@ -188,70 +192,42 @@ export async function transformSWC(
188
192
  }
189
193
  }
190
194
 
191
- if (!result || options.noHMR || !shouldHMR) {
192
- return result
195
+ if (result && !options.production && !options.noHMR) {
196
+ return wrapSourceInRefreshRuntime(id, result, options, hasRefreshRuntime)
193
197
  }
194
198
 
195
- wrapSourceInRefreshRuntime(id, result, options, shouldHMR)
196
-
197
- // TODO bring back?
198
- // if (result.map) {
199
- // const sourceMap: SourceMapPayload = JSON.parse(result.map)
200
- // sourceMap.mappings = ';;;;;;;;' + sourceMap.mappings
201
- // return { code: result.code, map: sourceMap }
202
- // }
203
-
204
- return { code: result.code }
205
- }
206
-
207
- const SWC_ENV = {
208
- targets: {
209
- node: '4',
210
- },
211
- // debug: true,
212
- include: [],
213
- // this breaks the uniswap app for any file with a ...spread
214
- exclude: [
215
- 'transform-spread',
216
- 'transform-destructuring',
217
- 'transform-object-rest-spread',
218
- // `transform-async-to-generator` is relying on `transform-destructuring`.
219
- // If we exclude `transform-destructuring` but not `transform-async-to-generator`, the SWC binary will panic
220
- // with error: `called `Option::unwrap()` on a `None` value`.
221
- // See: https://github.com/swc-project/swc/blob/v1.7.14/crates/swc_ecma_compat_es2015/src/generator.rs#L703-L705
222
- 'transform-async-to-generator',
223
- 'transform-regenerator', // Similar to above
224
- ],
225
- } satisfies SWCOptions['env']
226
-
227
- const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/
228
-
229
- function shouldSourceMap() {
230
- return process.env.VXRN_ENABLE_SOURCE_MAP === '1'
199
+ return result
231
200
  }
232
201
 
233
202
  function wrapSourceInRefreshRuntime(
234
203
  id: string,
235
204
  result: Output,
236
205
  options: Options,
237
- shouldHMR: boolean
206
+ hasRefreshRuntime: boolean
238
207
  ) {
239
- if (options.environment === 'client' || options.environment === 'ssr') {
240
- return wrapSourceInRefreshRuntimeWeb(id, result, shouldHMR)
208
+ if (options.environment === 'ssr') {
209
+ return result
210
+ }
211
+ if (options.environment === 'client') {
212
+ return wrapSourceInRefreshRuntimeWeb(id, result, hasRefreshRuntime)
241
213
  }
242
- return wrapSourceInRefreshRuntimeNative(id, result, options, shouldHMR)
214
+ return wrapSourceInRefreshRuntimeNative(id, result, options, hasRefreshRuntime)
243
215
  }
244
216
 
245
- function wrapSourceInRefreshRuntimeWeb(id: string, result: Output, shouldHMR: boolean) {
246
- const sourceMap: SourceMapPayload = JSON.parse(result.map!)
247
- sourceMap.mappings = ';;' + sourceMap.mappings
217
+ function wrapSourceInRefreshRuntimeWeb(id: string, result: Output, hasRefreshRuntime: boolean) {
218
+ const sourceMap = result.map ? JSON.parse(result.map) : undefined
219
+ if (sourceMap) {
220
+ sourceMap.mappings = ';;' + sourceMap.mappings
221
+ }
248
222
 
249
223
  result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
250
224
 
251
225
  ${result.code}`
252
226
 
253
- if (shouldHMR) {
254
- sourceMap.mappings = ';;;;;;' + sourceMap.mappings
227
+ if (hasRefreshRuntime) {
228
+ if (sourceMap) {
229
+ sourceMap.mappings = ';;;;;;' + sourceMap.mappings
230
+ }
255
231
  result.code = `if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
256
232
  const prevRefreshReg = window.$RefreshReg$;
257
233
  const prevRefreshSig = window.$RefreshSig$;
@@ -275,65 +251,89 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
275
251
  });
276
252
  });
277
253
  `
254
+
255
+ return { code: result.code, map: sourceMap }
278
256
  }
279
257
 
280
258
  function wrapSourceInRefreshRuntimeNative(
281
259
  id: string,
282
260
  result: Output,
283
261
  options: Options,
284
- shouldHMR: boolean
262
+ hasRefreshRuntime: boolean
285
263
  ) {
286
- const prefixCode =
287
- options.mode === 'build'
288
- ? `
289
- import 'react-native'
290
- import 'react'
291
- import '@vxrn/vite-native-client'
292
- `
293
- : ``
294
-
295
- if (options.production) {
296
- return `
297
- ${prefixCode}
298
- module.url = '${id}'
299
- ${result.code}
300
- `
264
+ const postfixCode = `if (module.hot) {
265
+ if (module.hot.accept) {
266
+ module.hot.accept((nextExports) => {
267
+ RefreshRuntime.performReactRefresh()
268
+ });
301
269
  }
270
+ }`
302
271
 
303
- if (shouldHMR) {
304
- result.code = `const RefreshRuntime = __cachedModules["react-refresh/cjs/react-refresh-runtime.development"];
305
- const prevRefreshReg = globalThis.$RefreshReg$;
306
- const prevRefreshSig = globalThis.$RefreshSig$ || (() => {
307
- console.info("no react refresh setup!")
308
- return (x) => x
309
- });
310
- globalThis.$RefreshReg$ = (type, id) => RefreshRuntime.register(type, "${id}" + " " + id);
311
- globalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
272
+ if (hasRefreshRuntime) {
273
+ // do we need this vite-native-client here? cant we do this on its own?
274
+ const prefixCode = `const RefreshRuntime = __cachedModules["react-refresh/cjs/react-refresh-runtime.development"];
275
+ const prevRefreshReg = globalThis.$RefreshReg$;
276
+ const prevRefreshSig = globalThis.$RefreshSig$ || (() => {
277
+ console.info("no react refresh setup!")
278
+ return (x) => x
279
+ });
280
+ globalThis.$RefreshReg$ = (type, id) => RefreshRuntime.register(type, "${id}" + " " + id);
281
+ globalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
282
+ module.url = '${id}'
283
+ module.hot = createHotContext(module.url)`
312
284
 
313
- ${prefixCode}
285
+ const sourceMap = result.map ? JSON.parse(result.map) : undefined
314
286
 
315
- module.url = '${id}'
316
- module.hot = createHotContext(module.url)
287
+ if (sourceMap) {
288
+ // we need ";" equal to number of lines added to the top
289
+ const prefixLen = prefixCode.split('\n').length + 1
290
+ sourceMap.mappings = new Array(prefixLen).fill(';').join('') + sourceMap.mappings
291
+ }
317
292
 
293
+ return {
294
+ code: `${prefixCode}
318
295
  ${result.code}
319
296
 
320
- if (module.hot) {
321
- globalThis.$RefreshReg$ = prevRefreshReg;
322
- globalThis.$RefreshSig$ = prevRefreshSig;
323
- globalThis['lastHmrExports'] = JSON.stringify(Object.keys(exports))
324
- }
325
- `
297
+ if (module.hot) {
298
+ globalThis.$RefreshReg$ = prevRefreshReg;
299
+ globalThis.$RefreshSig$ = prevRefreshSig;
300
+ globalThis['lastHmrExports'] = JSON.stringify(Object.keys(exports))
301
+ }
302
+
303
+ ${postfixCode}
304
+ `,
305
+ map: sourceMap,
306
+ }
326
307
  }
327
308
 
328
- result.code = `${result.code}
309
+ result.code += postfixCode
310
+ return result
311
+ }
329
312
 
330
- if (module.hot) {
331
- if (module.hot.accept) {
332
- module.hot.accept((nextExports) => {
333
- RefreshRuntime.performReactRefresh()
334
- });
335
- }
336
- }`
313
+ const SWC_ENV = {
314
+ targets: {
315
+ node: '4',
316
+ },
317
+ // debug: true,
318
+ include: [],
319
+ // this breaks the uniswap app for any file with a ...spread
320
+ exclude: [
321
+ 'transform-spread',
322
+ 'transform-destructuring',
323
+ 'transform-object-rest-spread',
324
+ // `transform-async-to-generator` is relying on `transform-destructuring`.
325
+ // If we exclude `transform-destructuring` but not `transform-async-to-generator`, the SWC binary will panic
326
+ // with error: `called `Option::unwrap()` on a `None` value`.
327
+ // See: https://github.com/swc-project/swc/blob/v1.7.14/crates/swc_ecma_compat_es2015/src/generator.rs#L703-L705
328
+ 'transform-async-to-generator',
329
+ 'transform-regenerator', // Similar to above
330
+ ],
331
+ } satisfies SWCOptions['env']
332
+
333
+ const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/
334
+
335
+ function shouldSourceMap() {
336
+ return process.env.VXRN_ENABLE_SOURCE_MAP === '1'
337
337
  }
338
338
 
339
339
  function getParser(id: string, forceJSX = false) {
@@ -1,5 +1,6 @@
1
1
  import type { Environment } from './types';
2
2
  type Conf = {
3
+ enableNativewind?: boolean;
3
4
  enableReanimated?: boolean;
4
5
  enableCompiler?: boolean | Environment[];
5
6
  enableNativeCSS?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../src/configure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,KAAK,IAAI,GAAG;IACV,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,cAAc,CAAC,EAAE,OAAO,GAAG,WAAW,EAAE,CAAA;IACxC,eAAe,CAAC,EAAE,OAAO,CAAA;CAC1B,CAAA;AAED,eAAO,MAAM,aAAa,EAAE,IAI3B,CAAA;AAED,wBAAgB,2BAA2B,CAAC,CAAC,EAAE,IAAI,QAElD"}
1
+ {"version":3,"file":"configure.d.ts","sourceRoot":"","sources":["../src/configure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,KAAK,IAAI,GAAG;IACV,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,cAAc,CAAC,EAAE,OAAO,GAAG,WAAW,EAAE,CAAA;IACxC,eAAe,CAAC,EAAE,OAAO,CAAA;CAC1B,CAAA;AAED,eAAO,MAAM,aAAa,EAAE,IAK3B,CAAA;AAED,wBAAgB,2BAA2B,CAAC,CAAC,EAAE,IAAI,QAElD"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,KAAK,EAAE,YAAY,EAAc,MAAM,MAAM,CAAA;AAIpD,OAAO,KAAK,EAAkC,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtE,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,wBAAsB,wBAAwB,CAC5C,SAAS,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAC3B,OAAO,CAAC,YAAY,EAAE,CAAC,CA+IzB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,KAAK,EAAE,YAAY,EAAc,MAAM,MAAM,CAAA;AAKpD,OAAO,KAAK,EAAkC,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtE,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,wBAAsB,wBAAwB,CAC5C,SAAS,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAC3B,OAAO,CAAC,YAAY,EAAE,CAAC,CA6OzB"}
@@ -7,6 +7,6 @@ export declare function getBabelOptions(props: Props): babel.TransformOptions |
7
7
  /**
8
8
  * Transform input to mostly ES5 compatible code, keep ESM syntax, and transform generators.
9
9
  */
10
- export declare function transformBabel(id: string, code: string, options: babel.TransformOptions): Promise<string>;
10
+ export declare function transformBabel(id: string, code: string, options: babel.TransformOptions): Promise<babel.BabelFileResult>;
11
11
  export {};
12
12
  //# sourceMappingURL=transformBabel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformBabel.d.ts","sourceRoot":"","sources":["../src/transformBabel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,aAAa,CAAA;AAI/B,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEtE,KAAK,KAAK,GAAG,iBAAiB,GAAG;IAC/B,WAAW,CAAC,EAAE,oBAAoB,CAAA;CACnC,CAAA;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,gBAAgB,GAAG,IAAI,CAqB3E;AA0BD;;GAEG;AACH,wBAAsB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,gBAAgB,mBA8B7F"}
1
+ {"version":3,"file":"transformBabel.d.ts","sourceRoot":"","sources":["../src/transformBabel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,aAAa,CAAA;AAK/B,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEtE,KAAK,KAAK,GAAG,iBAAiB,GAAG;IAC/B,WAAW,CAAC,EAAE,oBAAoB,CAAA;CACnC,CAAA;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,gBAAgB,GAAG,IAAI,CAc3E;AA4CD;;GAEG;AACH,wBAAsB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,gBAAgB,kCAoC7F"}
@@ -2,6 +2,9 @@ import { type Output, type Options as SWCOptions } from '@swc/core';
2
2
  import type { Options } from './types';
3
3
  export declare function transformSWC(id: string, code: string, options: Options & {
4
4
  es5?: boolean;
5
- }, swcOptions?: SWCOptions): Promise<Output | undefined>;
5
+ }, swcOptions?: SWCOptions): Promise<Output | {
6
+ code: string;
7
+ map: any;
8
+ } | undefined>;
6
9
  export declare const transformSWCStripJSX: (id: string, code: string) => Promise<Output | undefined>;
7
10
  //# sourceMappingURL=transformSWC.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformSWC.d.ts","sourceRoot":"","sources":["../src/transformSWC.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,MAAM,EAEX,KAAK,OAAO,IAAI,UAAU,EAE3B,MAAM,WAAW,CAAA;AAMlB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtC,wBAAsB,YAAY,CAChC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GAAG;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,EACpC,UAAU,CAAC,EAAE,UAAU,+BA0LxB;AA2JD,eAAO,MAAM,oBAAoB,OAAc,MAAM,QAAQ,MAAM,gCAqBlE,CAAA"}
1
+ {"version":3,"file":"transformSWC.d.ts","sourceRoot":"","sources":["../src/transformSWC.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,MAAM,EAEX,KAAK,OAAO,IAAI,UAAU,EAE3B,MAAM,WAAW,CAAA;AAMlB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAItC,wBAAsB,YAAY,CAChC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GAAG;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,EACpC,UAAU,CAAC,EAAE,UAAU;;;eAmLxB;AAgKD,eAAO,MAAM,oBAAoB,OAAc,MAAM,QAAQ,MAAM,gCAqBlE,CAAA"}