Coder Social home page Coder Social logo

decred / node-addon-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexlyp/node-addon-loader

0.0 4.0 2.0 8 KB

A loader for node native addons

Home Page: https://github.com/ushu/node-addon-loader

License: MIT License

JavaScript 100.00%

node-addon-loader's Introduction

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 path

This 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

$ yarn add --dev node-addon-loader

Usage

Add the loader to your webpack.config.js for all .node native files.

webpack.config.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

import node from 'relative/path/to/myLib.node';
// or
const node = require("relative/path/to/myLib.node");

or with the inline syntax:

Inline

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:

// webpack config
module.exports = {

  resolve: {
    alias: {
    "myLib": "/path/to/myLib.node",
    }
  },

  // ...

}

and then use your alias directly:

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-addon-loader's People

Contributors

alexlyp avatar aurelien-iapp avatar matheusd avatar ushu avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.