| 47 | /** @internal */ |
| 48 | // oxlint-disable-next-line effect/no-unused-internal -- Metro's file calls it. |
| 49 | export const make = (settings: Settings) => { |
| 50 | let upstream: |
| 51 | | { |
| 52 | transform: (args: Args) => unknown |
| 53 | getCacheKey?: (...args: ReadonlyArray<unknown>) => string |
| 54 | } |
| 55 | | undefined |
| 56 | const load = () => upstream ??= Module.createRequire(import.meta.url)(settings.upstream) |
| 57 | return { |
| 58 | transform: (args: Args): unknown => { |
| 59 | const { dev, platform, projectRoot } = args.options |
| 60 | const bundle = platform === null ? undefined : settings.bundles[platform] |
| 61 | return load()!.transform( |
| 62 | !dev && bundle !== undefined && |
| 63 | isFile(Path.resolve(projectRoot, args.filename), bundle.entry) |
| 64 | ? { |
| 65 | ...args, |
| 66 | src: `module.exports = require(${JSON.stringify(bundle.file)})\n` |
| 67 | } |
| 68 | : args |
| 69 | ) |
| 70 | }, |
| 71 | getCacheKey: (...args: ReadonlyArray<unknown>): string => |
| 72 | // Metro hashes only the file that calls this, not this one or the |
| 73 | // upstream. |
| 74 | [ |
| 75 | load()!.getCacheKey?.(...args) ?? "", |
| 76 | Crypto.createHash("sha1") |
| 77 | .update(Fs.readFileSync(fileURLToPath(import.meta.url))) |
| 78 | .update(Fs.readFileSync(settings.upstream)) |
| 79 | .digest("hex") |
| 80 | ].join("$") |
| 81 | } |
| 82 | } |