Skip to content

Commit fb740bc

Browse files
authored
perf(helper): reduce install size (#464)
1 parent 0f56f35 commit fb740bc

File tree

11 files changed

+26
-94
lines changed

11 files changed

+26
-94
lines changed

packages/helper/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# `@node-rs/helper`
22

3-
> Helper library for node-rs
3+
[![install size](https://packagephobia.com/badge?p=@node-rs/helper)](https://packagephobia.com/result?p=@node-rs/helper)
4+
[![Downloads](https://img.shields.io/npm/dm/@node-rs/helper.svg?sanitize=true)](https://npmcharts.com/compare/@node-rs/helper?minimal=true)
5+
6+
> Helper library for load native package.
47
58
## Usage
69

@@ -9,14 +12,17 @@
912
Load native binding file from `dirname`
1013

1114
```ts
12-
locateBinding(dirname: string): string
15+
loadBinding(dirname: string, filename?: string = 'index', packageName?: string): string
1316
```
1417

1518
- `dirname`, dirname which the **.node** binding file located
16-
- return the full path of the binding file, throw if file not existed or platform not supported
19+
- `filename`, the `napi.name` filed in you `package.json`
20+
- `packageName`, the `name` filed in your `package.json`, `@swc/core` for example.
21+
22+
- return native module
1723

1824
```ts
19-
const { locateBinding } = require('@node-rs/helper')
25+
const { loadBinding } = require('@node-rs/helper')
2026

21-
module.exports = require(locateBinding(__dirname))
27+
module.exports = loadBinding(__dirname, 'swc', '@swc/core')
2228
```

packages/helper/npm/darwin-x64/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/helper/npm/darwin-x64/package.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/helper/npm/linux-x64-gnu/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/helper/npm/linux-x64-gnu/package.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/helper/npm/win32-x64-msvc/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/helper/npm/win32-x64-msvc/package.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/helper/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"build": "exit 0;"
2525
},
2626
"dependencies": {
27-
"@napi-rs/triples": "^1.0.2",
28-
"tslib": "^2.3.0"
27+
"@napi-rs/triples": "^1.0.3"
2928
}
3029
}

packages/helper/src/loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export function loadBinding(dirname: string, filename = 'index', packageName?: s
2727
}
2828
const localFilePath = join(dirname, `${filename}.${triple.platformArchABI}.node`)
2929
if (existsSync(localFilePath)) {
30-
return require(localFilePath)
30+
try {
31+
return require(localFilePath)
32+
} catch (e) {
33+
additionalErrorMsg += `file: ${localFilePath} existed but error occurred while require it: ${e.message ?? e} \n`
34+
}
3135
}
3236
}
3337

packages/helper/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"compilerOptions": {
44
"outDir": "lib",
55
"rootDir": "./src",
6-
"composite": true
6+
"composite": true,
7+
"sourceMap": false,
8+
"inlineSourceMap": false,
9+
"noEmitHelpers": false,
10+
"importHelpers": false
711
},
812
"include": ["./src"]
913
}

0 commit comments

Comments
 (0)