Skip to content

Commit 992084f

Browse files
committed
feat: use prebuildify to create the bundle with native addons
1 parent 11fadec commit 992084f

File tree

9 files changed

+654
-66
lines changed

9 files changed

+654
-66
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ packages/app/.env
125125

126126
.vercel
127127
.DS_Store
128-
#node-gyp builds
129-
packages/core/build
128+
130129
# moon
131130
.moon/cache
132131
.moon/docker

packages/core/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
prebuilds

packages/core/jest.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
transform: {
6+
"^.+\\.tsx?$": [
7+
"ts-jest",
8+
{
9+
tsconfig: "tsconfig.test.json",
10+
},
11+
],
12+
},
13+
};

packages/core/moon.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ tasks:
77
- "build-native-addon"
88

99
build-native-addon:
10-
command: "node-gyp rebuild"
11-
platform: "system"
10+
deps:
11+
- "build-native-addon-linux-x64"
12+
13+
build-native-addon-linux-x64:
14+
command: prebuildify --napi --strip
1215
inputs:
1316
- "src/measurement.cc"
1417
- "binding.gyp"
1518
outputs:
16-
- "build/"
19+
- "prebuilds/linux-x64"

packages/core/package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
"benchmark",
88
"performance"
99
],
10-
"gypfile": true,
10+
"files": [
11+
"dist",
12+
"prebuilds"
13+
],
1114
"main": "dist/index.cjs.js",
1215
"module": "dist/index.es5.js",
1316
"types": "dist/index.d.ts",
14-
"files": [
15-
"dist"
16-
],
17+
"gypfile": true,
1718
"author": "Arthur Pastel <[email protected]>",
1819
"repository": "https://github.com/CodSpeedHQ/codspeed-node/tree/main/packages/core",
1920
"homepage": "https://codspeed.io",
2021
"license": "MIT",
2122
"devDependencies": {
22-
"@types/bindings": "^1.5.1",
23-
"rollup-plugin-natives": "^0.7.6"
23+
"node-addon-api": "^5.0.0",
24+
"node-gyp": "^9.3.1",
25+
"prebuildify": "^5.0.1"
2426
},
2527
"dependencies": {
26-
"bindings": "^1.5.0",
27-
"node-addon-api": "^5.0.0"
28+
"node-gyp-build": "^4.6.0"
2829
}
2930
}

packages/core/rollup.config.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { defineConfig } from "rollup";
2-
import nativePlugin from "rollup-plugin-natives";
32
import { declarationsPlugin, jsPlugins } from "../../rollup.options";
43

54
import pkg from "./package.json" assert { type: "json" };
@@ -28,12 +27,6 @@ export default defineConfig([
2827
},
2928
{ file: pkg.module, format: "es", sourcemap: true },
3029
],
31-
plugins: [
32-
...jsPlugins,
33-
nativePlugin({
34-
copyTo: "dist/lib",
35-
destDir: "./lib",
36-
}),
37-
],
30+
plugins: jsPlugins,
3831
},
3932
]);

packages/core/src/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
import path from "path";
2+
3+
/* eslint-disable @typescript-eslint/no-empty-function */
14
export interface Measurement {
25
isInstrumented(): boolean;
36
startMeasurement(): void;
47
stopMeasurement(at: string): void;
8+
isBound: boolean;
59
}
610

7-
// eslint-disable-next-line @typescript-eslint/no-var-requires
8-
const measurement = require("bindings")("measurement.node") as Measurement;
9-
11+
let m: Measurement;
12+
try {
13+
m = {
14+
// eslint-disable-next-line @typescript-eslint/no-var-requires
15+
...require("node-gyp-build")(path.dirname(__dirname)),
16+
isBound: true,
17+
} as Measurement;
18+
console.warn("@codspeed/core binding found");
19+
} catch (e) {
20+
console.warn("@codspeed/core binding not available on this architecture");
21+
m = {
22+
isInstrumented: () => false,
23+
startMeasurement: () => {},
24+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
25+
stopMeasurement: (at) => {},
26+
isBound: false,
27+
};
28+
}
29+
const measurement = m;
1030
export default measurement;

packages/core/tests/unit.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
import type { Measurement } from "../dist";
3+
4+
beforeEach(() => {
5+
jest.resetModules();
6+
});
7+
8+
describe("with bindings", () => {
9+
it("should be bound", () => {
10+
const measurement = require("..") as Measurement;
11+
expect(measurement.isBound).toBe(true);
12+
});
13+
});
14+
15+
describe("without bindings", () => {
16+
const initialEnv = process.env;
17+
beforeAll(() => {
18+
process.env.npm_config_arch = "unknown";
19+
});
20+
afterAll(() => {
21+
process.env = initialEnv;
22+
});
23+
it("should not be bound", () => {
24+
const measurement = require("..") as Measurement;
25+
expect(measurement.isBound).toBe(false);
26+
});
27+
});

0 commit comments

Comments
 (0)