A webpack loader for node addon modules that handles the recommended require("bindings")("myaddon.node")
pattern. This borrows heavily from awesome-node-loader
(huge credit to its contributors!) with how it handles node addons in your code, but extends the usability to include all dependencies in node_modules.
Add the package to the package.json
file:
$ npm install bindings-loader --save-dev
$ yarn add --dev bindings-loader
Always emits all .node
files directly in the bundle root.
This option chooses in between __dirname
and path.dirname(process.execPath)
. (Default is false
-> __dirname
)
// webpack.config.js
module.exports = {
externals: [
// Don't try to pack referenced .node files
function(context, request, callback) {
if (/\.node$/.test(request)) {
return callback(null, `commonjs ${request}`);
}
return callback();
},
],
module: {
rules: [
{
test: /\.js$/,
loader: "bindings-loader",
options: {
useDirname: !isElectronApp,
}
},
],
},
};
Recognizes require('bindings')('foo.node')
, uses (roughly) require('bindings')({ path: true, bindings: 'foo.node' })
to get the built addon module, re-writes the require to require("path").join(__dirname, 'foo.node')
when useDirname = true
, and emits the referenced file at that bundle destination.
PRs welcome!
This option would allow changing the file name in the output directory and allow usage of all placeholders defined in the loader-utils package.
This option would need to remain undefined
if you are building a package with embedded files, such as with Electron.