Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ushu/node-addon-loader
A loader for node native addons
https://github.com/ushu/node-addon-loader
nodejs webpack webpack-loader webpack2
Last synced: 4 months ago
JSON representation
A loader for node native addons
- Host: GitHub
- URL: https://github.com/ushu/node-addon-loader
- Owner: ushu
- License: mit
- Archived: true
- Created: 2017-06-01T10:40:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-11T09:58:06.000Z (over 6 years ago)
- Last Synced: 2024-09-25T05:37:34.759Z (4 months ago)
- Topics: nodejs, webpack, webpack-loader, webpack2
- Language: JavaScript
- Homepage: https://github.com/ushu/node-addon-loader
- Size: 9.77 KB
- Stars: 17
- Watchers: 3
- Forks: 26
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A simple loader to embed native addons in [node] or [electron] projects.
This package is a mix between:
- [node-loader] that loads a node module from an _absolute path_
- [file-loader] that loads copy a file into the bundle and return its pathThis one package allows to load a node native addon, binary file will end up copied into the output directory and loaded dynamically from its _relative_ path.
## Install
Add the package to your `package.json`
```bash
$ yarn add --dev node-addon-loader
```## Usage
Add the loader to your `webpack.config.js` for all `.node` native files.
**webpack.config.js**
```js
module.exports = {
module: {
rules: [
{
test: /\.node$/,
use: 'node-addon-loader',
options: [
basePath: resolve(__dirname),
]
}
]
}
}
```Note the `basePath` option: it will instruct the loader of the "base" path, from where the [node] runtime will be started. It will generate loading path relative to this URL.
For example is you develop an [electron] app with a bundle emitted into `dist/` directory, you want all the required paths (emitted `require` statements) to be relative to the current directory of the [electron] runtime (they all will be like `dist/xxxx.node`).**In your application**
You require the file directly
```js
import node from 'relative/path/to/myLib.node';
// or
const node = require("relative/path/to/myLib.node");
```or with the inline syntax:
**Inline**
```js
import node from 'node-addon-loader!./myLib.node';
```**with an alias**
a good option is to keep your modules in some place and create aliases for them, such as:
```js
// webpack config
module.exports = {resolve: {
alias: {
"myLib": "/path/to/myLib.node",
}
},// ...
}
```and then use your alias directly:
```js
import myLib from "myLib";
```## Thanks
big thanks go the the authors of [node-loader] and [file-loader] which I eagerly copied.
## TODO
Fix the issue with `emitFile`, see **TODO** in code.
All contributions are welcome, this is MIT do-whatever-you-want-I-dont-care code.
[node]: https://nodejs.org
[electron]: https://electron.atom.io/
[node-loader]: https://github.com/webpack-contrib/node-loader/blob/master/index.js
[file-loader]: https://github.com/webpack-contrib/file-loader