Skip to content

Commit fd353ae

Browse files
committed
Build and bundle node test addons
1 parent ddbbfbd commit fd353ae

File tree

10 files changed

+327
-99
lines changed

10 files changed

+327
-99
lines changed

package-lock.json

Lines changed: 202 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-tests/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const buildType = "Release";

packages/node-tests/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
"scripts": {
1414
"copy-tests": "tsx scripts/copy-tests.mts",
1515
"gyp-to-cmake": "gyp-to-cmake ./tests",
16-
"build": "tsx scripts/build-tests.mts",
16+
"build-tests": "tsx scripts/build-tests.mts",
1717
"bundle": "rolldown -c rolldown.config.mts",
18-
"copy-and-build": "npm run copy-tests && npm run gyp-to-cmake && npm run build",
19-
"test": "npm run copy-and-build"
18+
"bootstrap": "npm run copy-tests && npm run gyp-to-cmake && npm run build-tests && npm run bundle"
2019
},
2120
"devDependencies": {
21+
"@rollup/plugin-babel": "^6.0.4",
2222
"cmake-js": "^7.3.1",
2323
"cmake-rn": "*",
2424
"gyp-to-cmake": "*",
2525
"prebuildify": "^6.0.1",
26+
"react-native-node-api": "^0.3.2",
2627
"read-pkg": "^9.0.1",
27-
"rolldown": "1.0.0-beta.26"
28+
"rolldown": "1.0.0-beta.29"
2829
}
2930
}
Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,90 @@
1-
import { defineConfig } from "rolldown";
1+
import assert from "node:assert/strict";
2+
import fs from "node:fs";
3+
import path from "node:path";
24

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),
637
output: {
7-
file: "bundle.js",
38+
file: path.join(
39+
testDirectory,
40+
path.basename(testFile, ".js") + ".bundle.js"
41+
),
842
},
943
resolve: {
10-
alias: {
11-
"../../common": "yo.ts",
12-
},
44+
conditionNames: ["react-native"],
1345
},
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"]),
1590
]);
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { execSync } from "node:child_process";
22

3-
import { findCMakeProjects } from "./utils.mts";
3+
import { findCMakeProjects } from "./utils.mjs";
44

55
const projectDirectories = findCMakeProjects();
66

@@ -12,11 +12,4 @@ for (const projectDirectory of projectDirectories) {
1212
cwd: projectDirectory,
1313
stdio: "inherit",
1414
});
15-
16-
console.log(`Running "cmake-js" in ${projectDirectory} to build for Node.js`);
17-
execSync("cmake-js", {
18-
cwd: projectDirectory,
19-
stdio: "inherit",
20-
});
21-
console.log();
2215
}

packages/node-tests/scripts/copy-tests.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import cp from "node:child_process";
44

5-
import { TESTS_DIR } from "./utils.mts";
5+
import { TESTS_DIR } from "./utils.mjs";
66

77
const NODE_REPO_URL = "[email protected]:nodejs/node.git";
88
const NODE_REPO_DIR = path.resolve(import.meta.dirname, "../node");

packages/node-tests/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.tests.json" },
5+
{ "path": "./tsconfig.node-scripts.json" }
6+
]
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@tsconfig/node22/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"emitDeclarationOnly": true,
6+
"outDir": "dist",
7+
"rootDir": "scripts",
8+
"types": ["node", "read-pkg"]
9+
},
10+
"include": ["scripts/**/*.mts"],
11+
"exclude": []
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@tsconfig/react-native",
3+
"compilerOptions": {
4+
"composite": true,
5+
"noEmit": false,
6+
"module": "commonjs",
7+
"outDir": "dist",
8+
"rootDir": "src",
9+
"types": ["react-native"]
10+
},
11+
"include": ["src/*.ts"]
12+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{ "path": "./packages/gyp-to-cmake/tsconfig.json" },
66
{ "path": "./packages/cmake-rn/tsconfig.json" },
77
{ "path": "./packages/ferric/tsconfig.json" },
8-
{ "path": "./packages/node-addon-examples/tsconfig.json" }
8+
{ "path": "./packages/node-addon-examples/tsconfig.json" },
9+
{ "path": "./packages/node-tests/tsconfig.json" }
910
]
1011
}

0 commit comments

Comments
 (0)