/** * The Babel transformer `withEffectUi` gives Metro for a pre-bundled * release. It turns the entry into a module that requires the entry's * pre-bundle, and hands every file to the app's own transformer. Metro * hands the community CLI's entry over as given, without resolving it, so * this is the one place the entry can be swapped for every CLI. * * Metro loads it in its workers from a file `withEffectUi` writes for each * config, which passes the config's settings to {@link make}. * * @internal */ import * as Crypto from "node:crypto" import * as Fs from "node:fs" import * as Module from "node:module" import * as Path from "node:path" import { fileURLToPath } from "node:url" /** @internal */ export interface Settings { /** The app's own Babel transformer. */ readonly upstream: string /** Each platform's entry, and the pre-bundle that replaces it. */ readonly bundles: Readonly< Record > } interface Args { readonly filename: string readonly src: string readonly options: { readonly dev: boolean readonly platform: string | null readonly projectRoot: string } } const isFile = (path: string, file: string) => { try { return path === file || Fs.realpathSync(path) === file } catch { return false } } /** @internal */ // oxlint-disable-next-line effect/no-unused-internal -- Metro's file calls it. export const make = (settings: Settings) => { let upstream: | { transform: (args: Args) => unknown getCacheKey?: (...args: ReadonlyArray) => string } | undefined const load = () => upstream ??= Module.createRequire(import.meta.url)(settings.upstream) return { transform: (args: Args): unknown => { const { dev, platform, projectRoot } = args.options const bundle = platform === null ? undefined : settings.bundles[platform] return load()!.transform( !dev && bundle !== undefined && isFile(Path.resolve(projectRoot, args.filename), bundle.entry) ? { ...args, src: `module.exports = require(${JSON.stringify(bundle.file)})\n` } : args ) }, getCacheKey: (...args: ReadonlyArray): string => // Metro hashes only the file that calls this, not this one or the // upstream. [ load()!.getCacheKey?.(...args) ?? "", Crypto.createHash("sha1") .update(Fs.readFileSync(fileURLToPath(import.meta.url))) .update(Fs.readFileSync(settings.upstream)) .digest("hex") ].join("$") } }