bindings-cpp icon indicating copy to clipboard operation
bindings-cpp copied to clipboard

extract bindings

Open rosek86 opened this issue 2 years ago • 1 comments

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

rosek86 avatar Jul 22 '23 12:07 rosek86

Just a note that for now I solved this with webpack plugin which replaces node-gyp-build with a dummy import file but it would be much more convenient if it is possible to use simple externals as in the example above.

import path from 'node:path';
import { Resolver } from 'webpack';

export const AddonPlugin = {
  apply(resolver: Resolver) {
    resolver.getHook('before-resolve')
      .tapAsync('AddonPlugin', (request, _resolveContext, callback) => {
        if (request.path &&
            request.path?.match(/\@serialport\/bindings-cpp/) &&
            request.request?.match(/node-gyp-build/)) {
          request.request = path.join(__dirname, './serialport.js');
        }
        callback();
      });
  }
};
export default () => require('./addons/serialport.node');

td-krzysiek avatar Jul 27 '23 05:07 td-krzysiek

This seems fine, however I can't get the actions to run as it's been a long time. Would you be able to do an empty commit and push to your branch to trigger it again? eg git commit --allow-empty -m "Trigger Build"

reconbot avatar Oct 18 '24 21:10 reconbot

:tada: This PR is included in version 13.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 23 '24 20:12 github-actions[bot]