From 730bc619420c1749bff09dce932d6da787cd78bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Rosi=C5=84ski?= Date: Mon, 23 Dec 2024 21:12:57 +0100 Subject: [PATCH] extract bindings (#128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hello, I have a use case where size of an application is important, I'd like to webpack as much as possible. The problematic part is related *.node files, I found a potential solution, example with sqlite: ``` externals: { './sqlite3-binding.js': 'commonjs ./node_sqlite3.node', } ``` I can replace sqlite3-binding.js [1] with *.node, then I copy node_sqlite3.node to the main application folder. I'd like to try something similar with the serialport. However, it is slightly more complicated since serialport doesn't have single import file. This PR is an attempt to fix the problem. [1] https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3-binding.js --------- Co-authored-by: Krzysztof RosiƄski --- lib/load-bindings.ts | 6 +----- lib/poller.ts | 5 ++--- lib/serialport-bindings.ts | 5 +++++ 3 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 lib/serialport-bindings.ts diff --git a/lib/load-bindings.ts b/lib/load-bindings.ts index 56039ee..bee1ada 100644 --- a/lib/load-bindings.ts +++ b/lib/load-bindings.ts @@ -1,9 +1,5 @@ -import nodeGypBuild from 'node-gyp-build' import { promisify } from 'util' -import { join } from 'path' - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -const binding = nodeGypBuild(join(__dirname, '../')) as any +import { binding } from './serialport-bindings' export const asyncClose = binding.close ? promisify(binding.close) : async () => { throw new Error('"binding.close" Method not implemented')} export const asyncDrain = binding.drain ? promisify(binding.drain) : async () => { throw new Error('"binding.drain" Method not implemented')} diff --git a/lib/poller.ts b/lib/poller.ts index a583080..fae9e26 100644 --- a/lib/poller.ts +++ b/lib/poller.ts @@ -1,10 +1,9 @@ import debug from 'debug' import { EventEmitter } from 'events' -import { join } from 'path' -import nodeGypBuild from 'node-gyp-build' import { BindingsError } from './errors' +import { binding } from './serialport-bindings' -const { Poller: PollerBindings } = nodeGypBuild(join(__dirname, '../')) as { Poller: PollerClass } +const { Poller: PollerBindings } = binding as { Poller: PollerClass } const logger = debug('serialport/bindings-cpp/poller') interface PollerClass { diff --git a/lib/serialport-bindings.ts b/lib/serialport-bindings.ts new file mode 100644 index 0000000..616738f --- /dev/null +++ b/lib/serialport-bindings.ts @@ -0,0 +1,5 @@ +import { join } from 'path' +import nodeGypBuild from 'node-gyp-build' + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const binding = nodeGypBuild(join(__dirname, '../')) as any