{"version":3,"file":"transformBabel.js","names":[],"sources":["esm/transformBabel.js"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { extname, join } from \"node:path\";\nimport { resolvePath } from \"@vxrn/utils\";\nimport { normalizePath } from \"vite\";\nimport { configuration } from \"./configure\";\nimport { asyncGeneratorRegex, debug } from \"./constants\";\nconst USER_BABEL_CONFIG_FILES = [\n  \"babel.config.js\",\n  \"babel.config.cjs\",\n  \"babel.config.mjs\",\n  \"babel.config.json\",\n  \".babelrc\",\n  \".babelrc.js\",\n  \".babelrc.json\"\n];\nconst ONE_GENERATED_MARKER = \"@one/generated bundler-config\";\nfunction findUserBabelConfig(projectRoot) {\n  if (!projectRoot) return null;\n  for (const name of USER_BABEL_CONFIG_FILES) {\n    const fullPath = join(projectRoot, name);\n    if (existsSync(fullPath)) {\n      try {\n        const content = readFileSync(fullPath, \"utf8\");\n        if (content.includes(ONE_GENERATED_MARKER)) {\n          continue;\n        }\n      } catch {\n        continue;\n      }\n      return fullPath;\n    }\n  }\n  return null;\n}\nfunction getBabelOptions(props) {\n  props = { ...props, id: normalizePath(props.id.split(\"?\")[0]) };\n  const isProjectFile = !props.id.includes(\"node_modules\");\n  const userBabelConfig = isProjectFile && props.projectRoot ? findUserBabelConfig(props.projectRoot) : null;\n  if (props.userSetting === \"babel\") {\n    return getOptions(props, true, userBabelConfig);\n  }\n  if (typeof props.userSetting === \"undefined\" || typeof props.userSetting === \"object\" && props.userSetting.transform === \"babel\") {\n    if (props.userSetting?.excludeDefaultPlugins) {\n      return {\n        caller: getBabelCaller(props),\n        ...props.userSetting,\n        ...userBabelConfig ? { configFile: userBabelConfig, babelrc: true } : {}\n      };\n    }\n    return getOptions(props, false, userBabelConfig);\n  }\n  const userSetting = props.userSetting;\n  if (userSetting === \"swc\" || userSetting === \"oxc\" || userSetting === false || typeof userSetting === \"object\" && userSetting !== null && (userSetting.transform === \"swc\" || userSetting.transform === \"oxc\")) {\n    return null;\n  }\n  if (userBabelConfig) {\n    return getOptions(props, false, userBabelConfig);\n  }\n  return null;\n}\nconst getOptions = (props, force = false, userBabelConfig = null) => {\n  let plugins = [];\n  if (force || shouldBabelGenerators(props)) {\n    plugins = getBasePlugins(props);\n  }\n  const enableNativewind = configuration.enableNativewind && (props.environment === \"ios\" || props.environment === \"android\") && // if reanimated gets wrapped in transform it causes circular dep issues\n  !/node_modules/.test(props.id) && // only needed for createElement calls, so be a bit conservative\n  props.code.includes(\"createElement\");\n  if (enableNativewind) {\n    if (!props.id.includes(\"node_modules\")) {\n      plugins.push(resolvePath(\"react-native-css-interop/dist/babel-plugin.js\"));\n    }\n  }\n  if (shouldBabelReactCompiler(props)) {\n    debug?.(`Using babel react compiler on file`);\n    plugins.push(getBabelReactCompilerPlugin(props));\n  }\n  if (plugins.length || userBabelConfig) {\n    return {\n      plugins,\n      caller: getBabelCaller(props),\n      ...userBabelConfig ? { configFile: userBabelConfig, babelrc: true } : {}\n    };\n  }\n  return null;\n};\nfunction getBabelCaller(props) {\n  const caller = {\n    name: \"vxrn\",\n    platform: props.environment === \"ios\" || props.environment === \"android\" ? props.environment : \"web\",\n    isDev: props.development\n  };\n  return caller;\n}\nasync function transformOxcReactCompiler(id, code, optionsOrTarget = \"19\", sourceMap = false) {\n  const { transform } = await import(\"oxc-transform-react\");\n  const compilerOptions = typeof optionsOrTarget === \"string\" ? { target: optionsOrTarget } : optionsOrTarget || {};\n  const target = compilerOptions.target ?? \"19\";\n  const result = await transform(id, code, {\n    jsx: \"preserve\",\n    sourcemap: sourceMap,\n    reactCompiler: {\n      target,\n      ...compilerOptions,\n      environment: {\n        enableCustomTypeDefinitionForReanimated: true,\n        ...compilerOptions.environment\n      }\n    }\n  });\n  if (result.fatal) {\n    throw new Error(\n      `[vxrn:compiler] oxc react compiler failed on ${id}: ${(result.errors ?? []).map((e) => e.message ?? String(e)).join(\", \")}`\n    );\n  }\n  if (result.errors?.length) {\n    debug?.(\n      `[compiler/bailout] ${id}: ${result.errors.map((e) => e.message ?? String(e)).join(\", \")}`\n    );\n  }\n  return {\n    code: result.code,\n    map: sourceMap ? result.map : void 0\n  };\n}\nasync function transformBabel(id, code, options) {\n  const [{ default: babelCore }, { default: hermesParserPlugin }] = await Promise.all([\n    import(\"@babel/core\"),\n    import(\"babel-plugin-syntax-hermes-parser\")\n  ]);\n  const extension = extname(id);\n  const isTSX = extension === \".tsx\";\n  const isTS = isTSX || extension === \".ts\";\n  const babelOptions = {\n    filename: id,\n    compact: false,\n    babelrc: options.babelrc ?? false,\n    configFile: options.configFile ?? false,\n    sourceMaps: false,\n    minified: false,\n    ...options,\n    ...!isTS ? {\n      parserOpts: {\n        ...options.parserOpts,\n        plugins: [...options.parserOpts?.plugins || [], \"jsx\"]\n      }\n    } : {},\n    // vite and rolldown own module syntax and import.meta, so presets written for\n    // metro (babel-preset-expo) must keep esm instead of rewriting it for metro's runtime\n    caller: {\n      name: \"vxrn\",\n      ...options.caller,\n      supportsStaticESM: true,\n      supportsDynamicImport: true\n    },\n    presets: [\n      isTS ? [\n        \"@babel/preset-typescript\",\n        {\n          isTSX,\n          allExtensions: isTSX\n        }\n      ] : \"\",\n      ...options.presets || []\n    ].filter(Boolean),\n    // non-TS files use React Native's Flow dialect. RN's own component specs are\n    // Flow `.js` (`import type`, `codegenNativeComponent<T>(…)`, `(expr: Type)` casts).\n    // enable Hermes Flow parsing + stripping, mirroring the TypeScript preset above\n    // (`@react-native/babel-preset` uses the same parser for JS). without it babel\n    // can't parse current RN Flow syntax such as `call<T>(...) as HostComponent<T>`, so\n    // plugins like\n    // @react-native/babel-plugin-codegen silently fail on RN specs and the runtime\n    // \"Codegen didn't run\" warning fires. TS path is byte-identical (empty slice).\n    plugins: [\n      ...isTS ? [] : [\n        [hermesParserPlugin, { parseLangTypes: \"flow\", reactRuntimeTarget: \"19\" }],\n        \"babel-plugin-transform-flow-enums\"\n      ],\n      ...options.plugins || [],\n      ...isTS ? [] : [\"@babel/plugin-transform-flow-strip-types\"]\n    ]\n  };\n  return await new Promise((res, rej) => {\n    babelCore.transform(code, babelOptions, (err, result) => {\n      if (!result || err) {\n        return rej(err || new Error(`[vxrn:compiler] babel returned no result for ${id}`));\n      }\n      res(result);\n    });\n  });\n}\nconst getBasePlugins = ({ development }) => [\n  [\"@babel/plugin-transform-destructuring\"],\n  [\"@babel/plugin-transform-react-jsx\", { development }],\n  [\"@babel/plugin-transform-async-generator-functions\"],\n  [\"@babel/plugin-transform-async-to-generator\"],\n  [\n    \"@babel/plugin-transform-runtime\",\n    {\n      helpers: true,\n      // NOTE THIS WAS SPELLED WRONG BEFOER THIS COMMIT MAYBE IT WAS UNINTENTIONALLY WORKING\n      regenerator: false\n    }\n  ]\n];\nconst shouldBabelReactCompiler = (props) => {\n  if (props.environment === \"ssr\") {\n    return false;\n  }\n  if (!configuration.enableCompiler) {\n    return false;\n  }\n  const { enableCompiler } = configuration;\n  if (typeof enableCompiler === \"object\" && !Array.isArray(enableCompiler) && !(enableCompiler instanceof RegExp)) {\n    const isNative = props.environment === \"ios\" || props.environment === \"android\";\n    const filter = isNative ? enableCompiler.native : enableCompiler.web;\n    if (!filter) return false;\n    return applyCompilerFilter(filter, props);\n  }\n  if (typeof enableCompiler === \"function\") {\n    return enableCompiler(props.id, props.environment);\n  }\n  if (enableCompiler instanceof RegExp) {\n    return enableCompiler.test(props.id);\n  }\n  if (Array.isArray(enableCompiler)) {\n    if (!enableCompiler.includes(props.environment)) {\n      return false;\n    }\n  }\n  return applyDefaultFilters(props);\n};\nfunction applyCompilerFilter(filter, props) {\n  if (typeof filter === \"function\") {\n    return filter(props.id, props.environment);\n  }\n  if (filter instanceof RegExp) {\n    return filter.test(props.id);\n  }\n  if (filter === true) {\n    return applyDefaultFilters(props);\n  }\n  return false;\n}\nfunction applyDefaultFilters(props) {\n  if (!/(\\.tsx?)$/.test(props.id)) return false;\n  if (props.id.includes(\"node_modules\")) return false;\n  if (props.id.includes(\"+api.\")) return false;\n  if (props.code.startsWith(\"// disable-compiler\")) return false;\n  return true;\n}\nconst getBabelReactCompilerPlugin = (props) => {\n  const target = props.reactForRNVersion === \"18\" && (props.environment === \"ios\" || props.environment === \"android\") ? \"18\" : \"19\";\n  return [\"babel-plugin-react-compiler\", { target }];\n};\nfunction shouldBabelGenerators({ code }) {\n  if (process.env.VXRN_USE_BABEL_FOR_GENERATORS) {\n    return asyncGeneratorRegex.test(code);\n  }\n}\nexport {\n  findUserBabelConfig,\n  getBabelOptions,\n  transformBabel,\n  transformOxcReactCompiler\n};\n//# sourceMappingURL=transformBabel.js.map\n"],"mappings":";;;;;;;;AAMA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AACA,MAAM,uBAAuB;AAC7B,SAAS,oBAAoB,aAAa;CACxC,IAAI,CAAC,aAAa,OAAO;CACzB,KAAK,MAAM,QAAQ,yBAAyB;EAC1C,MAAM,WAAW,KAAK,aAAa,IAAI;EACvC,IAAI,WAAW,QAAQ,GAAG;GACxB,IAAI;IACF,MAAM,UAAU,aAAa,UAAU,MAAM;IAC7C,IAAI,QAAQ,SAAS,oBAAoB,GAAG;KAC1C;IACF;GACF,QAAQ;IACN;GACF;GACA,OAAO;EACT;CACF;CACA,OAAO;AACT;AACA,SAAS,gBAAgB,OAAO;CAC9B,QAAQ;EAAE,GAAG;EAAO,IAAI,cAAc,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE;CAAE;CAC9D,MAAM,gBAAgB,CAAC,MAAM,GAAG,SAAS,cAAc;CACvD,MAAM,kBAAkB,iBAAiB,MAAM,cAAc,oBAAoB,MAAM,WAAW,IAAI;CACtG,IAAI,MAAM,gBAAgB,SAAS;EACjC,OAAO,WAAW,OAAO,MAAM,eAAe;CAChD;CACA,IAAI,OAAO,MAAM,gBAAgB,eAAe,OAAO,MAAM,gBAAgB,YAAY,MAAM,YAAY,cAAc,SAAS;EAChI,IAAI,MAAM,aAAa,uBAAuB;GAC5C,OAAO;IACL,QAAQ,eAAe,KAAK;IAC5B,GAAG,MAAM;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