|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { defineConfig, type RolldownOptions } from "rolldown"; |
| 6 | +import { aliasPlugin, replacePlugin } from "rolldown/experimental"; |
| 7 | + |
| 8 | +function readGypTargetNames(gypFilePath: string): string[] { |
| 9 | + const contents = JSON.parse(fs.readFileSync(gypFilePath, "utf-8")) as unknown; |
| 10 | + assert( |
| 11 | + typeof contents === "object" && contents !== null, |
| 12 | + "Expected gyp file to contain a valid JSON object", |
| 13 | + ); |
| 14 | + assert("targets" in contents, "Expected targets in gyp file"); |
| 15 | + const { targets } = contents; |
| 16 | + assert(Array.isArray(targets), "Expected targets to be an array"); |
| 17 | + return targets.map(({ target_name }) => { |
| 18 | + assert( |
| 19 | + typeof target_name === "string", |
| 20 | + "Expected target_name to be a string", |
| 21 | + ); |
| 22 | + return target_name; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function testSuiteConfig(suitePath: string): RolldownOptions[] { |
| 27 | + const testFiles = fs.globSync("*.js", { |
| 28 | + cwd: suitePath, |
| 29 | + exclude: ["*.bundle.js"], |
| 30 | + }); |
| 31 | + const gypFilePath = path.join(suitePath, "binding.gyp"); |
| 32 | + const targetNames = readGypTargetNames(gypFilePath); |
| 33 | + return testFiles.map((testFile) => ({ |
| 34 | + input: path.join(suitePath, testFile), |
| 35 | + output: { |
| 36 | + file: path.join(suitePath, path.basename(testFile, ".js") + ".bundle.js"), |
| 37 | + }, |
| 38 | + resolve: { |
| 39 | + conditionNames: ["react-native"], |
| 40 | + }, |
| 41 | + polyfillRequire: false, |
| 42 | + plugins: [ |
| 43 | + // Replace dynamic require statements for addon targets to allow the babel plugin to handle them correctly |
| 44 | + replacePlugin( |
| 45 | + Object.fromEntries( |
| 46 | + targetNames.map((targetName) => [ |
| 47 | + `require(\`./build/\${common.buildType}/${targetName}\`)`, |
| 48 | + `require("./build/Release/${targetName}")`, |
| 49 | + ]), |
| 50 | + ), |
| 51 | + { |
| 52 | + delimiters: ["", ""], |
| 53 | + }, |
| 54 | + ), |
| 55 | + replacePlugin( |
| 56 | + Object.fromEntries( |
| 57 | + targetNames.map((targetName) => [ |
| 58 | + // Replace "__require" statement with a regular "require" to allow Metro to resolve it |
| 59 | + `__require("./build/Release/${targetName}")`, |
| 60 | + `require("./build/Release/${targetName}")`, |
| 61 | + ]), |
| 62 | + ), |
| 63 | + { |
| 64 | + delimiters: ["", ""], |
| 65 | + }, |
| 66 | + ), |
| 67 | + aliasPlugin({ |
| 68 | + entries: [ |
| 69 | + { |
| 70 | + find: "../../common", |
| 71 | + replacement: "./common.ts", |
| 72 | + }, |
| 73 | + ], |
| 74 | + }), |
| 75 | + ], |
| 76 | + external: targetNames.map((targetName) => `./build/Release/${targetName}`), |
| 77 | + })); |
| 78 | +} |
| 79 | + |
| 80 | +const suitePaths = fs |
| 81 | + .globSync("tests/*/*", { |
| 82 | + cwd: import.meta.dirname, |
| 83 | + withFileTypes: true, |
| 84 | + }) |
| 85 | + .filter((dirent) => dirent.isDirectory()) |
| 86 | + .map((dirent) => |
| 87 | + path.join( |
| 88 | + path.relative(import.meta.dirname, dirent.parentPath), |
| 89 | + dirent.name, |
| 90 | + ), |
| 91 | + ); |
| 92 | + |
| 93 | +export default defineConfig(suitePaths.flatMap(testSuiteConfig)); |
0 commit comments