{"version":3,"file":"transformSWC.js","names":[],"sources":["esm/transformSWC.js"],"sourcesContent":["import { extname } from \"node:path\";\nimport { parseSync } from \"oxc-parser\";\nimport { normalizePath } from \"vite\";\nimport { configuration } from \"./configure\";\nimport { asyncGeneratorRegex, debug, runtimePublicPath } from \"./constants\";\nconst ignoreId = /node_modules\\/(\\.vite|vite)\\//;\nfunction shouldStripFlow(id, code) {\n  const filename = id.split(\"?\")[0];\n  if (!/\\.jsx?$/.test(filename)) return false;\n  const header = code.match(/^(?:#![^\\r\\n]*(?:\\r?\\n|$))?(?:\\s|\\/\\*[\\s\\S]*?\\*\\/|\\/\\/[^\\r\\n]*)*/)?.[0] || \"\";\n  const candidate = /node_modules[\\\\/](?:react-native|@react-native)[\\\\/].*\\.js$/.test(filename) || /@flow\\b/.test(header) || /\\b(?:import|export)\\s+type\\b/.test(code) || // Some published Flow sources omit the pragma and use ordinary imports.\n  // Typed function returns, declarations, and class fields still need stripping.\n  /\\(\\s*(?:\\.\\.\\.)?[\\w$]+\\??\\s*:\\s*/.test(code) || /\\)\\s*:\\s*(?:[\\w$]+(?:<[^;{}]*>)?(?:\\[\\])?\\s*)(?:=>|\\{)/.test(code) || /^\\s*[\\w$]+\\??\\s*:\\s*(?:string|number|boolean|any|mixed|void|[A-Z][\\w$]*)(?:[^;\\r\\n=]*)(?:=[^;\\r\\n]*)?;/m.test(\n    code\n  ) || /\\b(?:opaque\\s+)?type\\s+[\\w$]+(?:\\s*<[^;{}]*>)?\\s*=/.test(code);\n  return candidate && parseSync(filename, code, { lang: \"jsx\" }).errors.length > 0;\n}\nasync function transformSWC(id, code, options, swcOptions) {\n  id = normalizePath(id.split(\"?\")[0]).replace(normalizePath(process.cwd()), \"\");\n  if (ignoreId.test(id)) {\n    return;\n  }\n  if (id === runtimePublicPath) {\n    return;\n  }\n  const lang = getLang(id, options.forceJSX);\n  if (!lang) {\n    return;\n  }\n  const refresh = options.environment !== \"ssr\" && !options.production && !options.noHMR && !options.forceJSX && !id.includes(\"node_modules\");\n  const importSource = configuration.enableNativewind && !id.includes(\"node_modules\") ? \"nativewind\" : \"react\";\n  const shouldEs5Transform = options.es5 || !process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code);\n  const target = shouldEs5Transform ? \"es2015\" : \"es2020\";\n  const sourceMaps = swcOptions?.sourceMaps !== void 0 ? Boolean(swcOptions.sourceMaps) : shouldSourceMap();\n  const oxcOptions = {\n    lang,\n    target,\n    assumptions: {\n      setPublicClassFields: true,\n      ...swcOptions?.assumptions || {}\n    },\n    sourcemap: sourceMaps,\n    jsx: {\n      runtime: \"automatic\",\n      development: !options.forceJSX && !options.production,\n      refresh: Boolean(refresh),\n      importSource\n    }\n  };\n  const { transformSync } = await import(\"oxc-transform\");\n  const result = (() => {\n    try {\n      debug?.(\n        `transformSWC (oxc) ${id} using options:\n${JSON.stringify(oxcOptions, null, 2)}`\n      );\n      const res = transformSync(id, code, oxcOptions);\n      if (res.errors?.length) {\n        const err = res.errors[0];\n        const error = new Error(\n          err.message + (err.codeframe ? `\n${err.codeframe}` : \"\")\n        );\n        if (err.labels?.[0]) {\n          error.start = err.labels[0].start;\n          error.end = err.labels[0].end;\n        }\n        throw error;\n      }\n      return {\n        code: res.code,\n        map: res.map\n      };\n    } catch (e) {\n      throw e;\n    }\n  })();\n  if (configuration.enableNativeCSS) {\n    if (result.code.includes(`createInteropElement(`)) {\n      result.code = `import { createInteropElement, Fragment as _InteropFragment } from 'react-native-css-interop/jsx-dev-runtime'\n${result.code}`;\n    }\n  }\n  const hasRefreshRuntime = refresh && refreshContentRE.test(result.code);\n  if (options.fixNonTypeSpecificImports || id.includes(\"node_modules\") && (lang === \"ts\" || lang === \"tsx\")) {\n    const typeExportsMatch = code.match(/^\\s*export\\s+type\\s+([^\\s]+)/gi);\n    if (typeExportsMatch) {\n      for (const typeExport of Array.from(typeExportsMatch)) {\n        const [_export, _type, name] = typeExport.split(/\\s+/);\n        if (name.startsWith(\"{\")) continue;\n        if (name.includes(\"<\")) continue;\n        const alreadyExported = new RegExp(\n          `export (const|let|class|function) ${name}\\\\s+`\n        ).test(result.code);\n        if (!alreadyExported) {\n          const fakeExport = `export let ${name} = {};`;\n          console.info(\n            ` \\u26A0\\uFE0F Fixing non-type-specific import in node_module, this should be fixed upstream: ${fakeExport} in ${id}`\n          );\n          result.code += `\n${fakeExport}\n`;\n        }\n      }\n    }\n  }\n  if (result && !options.production && !options.noHMR) {\n    return wrapSourceInRefreshRuntime(id, result, options, hasRefreshRuntime);\n  }\n  return result;\n}\nconst transformOxc = transformSWC;\nfunction wrapSourceInRefreshRuntime(id, result, options, hasRefreshRuntime) {\n  if (options.environment === \"ssr\") {\n    return result;\n  }\n  if (options.environment === \"client\") {\n    return wrapSourceInRefreshRuntimeWeb(id, result, hasRefreshRuntime);\n  }\n  return wrapSourceInRefreshRuntimeNative(id, result, options, hasRefreshRuntime);\n}\nfunction wrapSourceInRefreshRuntimeWeb(id, result, hasRefreshRuntime) {\n  const sourceMap = result.map ? typeof result.map === \"string\" ? JSON.parse(result.map) : { ...result.map } : void 0;\n  if (sourceMap) {\n    sourceMap.mappings = \";;\" + sourceMap.mappings;\n  }\n  result.code = `import * as RefreshRuntime from \"${runtimePublicPath}\";\n\n${result.code}`;\n  if (hasRefreshRuntime) {\n    if (sourceMap) {\n      sourceMap.mappings = \";;;;;;\" + sourceMap.mappings;\n    }\n    result.code = `if (!window.$RefreshReg$) throw new Error(\"React refresh preamble was not loaded. Something is wrong.\");\nconst prevRefreshReg = window.$RefreshReg$;\nconst prevRefreshSig = window.$RefreshSig$;\nwindow.$RefreshReg$ = RefreshRuntime.getRefreshReg(\"${id}\");\nwindow.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;\n\n${result.code}\n\nwindow.$RefreshReg$ = prevRefreshReg;\nwindow.$RefreshSig$ = prevRefreshSig;\n`;\n  }\n  result.code += `\nRefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {\n  RefreshRuntime.registerExportsForReactRefresh(\"${id}\", currentExports);\n  import.meta.hot.accept((nextExports) => {\n    if (!nextExports) return;\n    const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(\"${id}\", currentExports, nextExports);\n    if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);\n  });\n});\n`;\n  return { code: result.code, map: sourceMap };\n}\nfunction wrapSourceInRefreshRuntimeNative(id, result, options, hasRefreshRuntime) {\n  const postfixCode = `if (module.hot) {\n  if (module.hot.accept) {\n    module.hot.accept((nextExports) => {\n      RefreshRuntime.performReactRefresh()\n    });\n  }\n}`;\n  if (hasRefreshRuntime) {\n    const prefixCode = `const RefreshRuntime = __cachedModules[\"react-refresh/cjs/react-refresh-runtime.development\"];\nconst prevRefreshReg = globalThis.$RefreshReg$;\nconst prevRefreshSig = globalThis.$RefreshSig$ || (() => {\n  console.info(\"no react refresh setup!\")\n  return (x) => x\n});\nglobalThis.$RefreshReg$ = (type, id) => RefreshRuntime.register(type, \"${id}\" + \" \" + id);\nglobalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;\nmodule.url = '${id}'\nmodule.hot = createHotContext(module.url)`;\n    const sourceMap = result.map ? typeof result.map === \"string\" ? JSON.parse(result.map) : { ...result.map } : void 0;\n    if (sourceMap) {\n      const prefixLen = prefixCode.split(\"\\n\").length + 1;\n      sourceMap.mappings = new Array(prefixLen).fill(\";\").join(\"\") + sourceMap.mappings;\n    }\n    return {\n      code: `${prefixCode}\n${result.code}\n\nif (module.hot) {\n  globalThis.$RefreshReg$ = prevRefreshReg;\n  globalThis.$RefreshSig$ = prevRefreshSig;\n  globalThis['lastHmrExports'] = JSON.stringify(Object.keys(exports))\n}\n\n${postfixCode}\n`,\n      map: sourceMap\n    };\n  }\n  result.code += postfixCode;\n  return result;\n}\nconst refreshContentRE = /\\$Refresh(?:Reg|Sig)\\$\\(/;\nfunction shouldSourceMap() {\n  return process.env.VXRN_ENABLE_SOURCE_MAP === \"1\";\n}\nfunction getLang(id, forceJSX = false) {\n  if (id.endsWith(\"one-entry-native\")) {\n    return \"tsx\";\n  }\n  const extension = extname(id);\n  if (extension === \".tsx\") return \"tsx\";\n  if (extension === \".ts\") return \"ts\";\n  if (extension === \".jsx\" || extension === \".mdx\") return \"jsx\";\n  if (extension === \".js\" || extension === \".mjs\" || extension === \".cjs\") {\n    return \"jsx\";\n  }\n  if (!extension) {\n    return forceJSX ? \"jsx\" : \"js\";\n  }\n  return void 0;\n}\nconst transformSWCStripJSX = async (id, code) => {\n  const lang = getLang(id);\n  if (!lang) return;\n  const { transformSync } = await import(\"oxc-transform\");\n  const res = transformSync(id, code, {\n    lang,\n    target: \"es2020\",\n    assumptions: {\n      setPublicClassFields: true\n    },\n    sourcemap: shouldSourceMap(),\n    jsx: {\n      runtime: \"automatic\",\n      development: true,\n      refresh: false\n    }\n  });\n  if (res.errors?.length) {\n    const err = res.errors[0];\n    throw new Error(err.message + (err.codeframe ? `\n${err.codeframe}` : \"\"));\n  }\n  return { code: res.code, map: res.map };\n};\nconst transformOxcStripJSX = transformSWCStripJSX;\nexport {\n  shouldSourceMap,\n  shouldStripFlow,\n  transformOxc,\n  transformOxcStripJSX,\n  transformSWC,\n  transformSWCStripJSX\n};\n//# sourceMappingURL=transformSWC.js.map\n"],"mappings":";;;;;;;AAKA,MAAM,WAAW;AACjB,SAAS,gBAAgB,IAAI,MAAM;CACjC,MAAM,WAAW,GAAG,MAAM,GAAG,CAAC,CAAC;CAC/B,IAAI,CAAC,UAAU,KAAK,QAAQ,GAAG,OAAO;CACtC,MAAM,SAAS,KAAK,MAAM,kEAAkE,CAAC,GAAG,MAAM;CACtG,MAAM,YAAY,8DAA8D,KAAK,QAAQ,KAAK,UAAU,KAAK,MAAM,KAAK,+BAA+B,KAAK,IAAI,KAEpK,mCAAmC,KAAK,IAAI,KAAK,yDAAyD,KAAK,IAAI,KAAK,0GAA0G,KAChO,IACF,KAAK,qDAAqD,KAAK,IAAI;CACnE,OAAO,aAAa,UAAU,UA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