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