@dtkdtk/jsnut

1.2.1 • Public • Published

JSNut :: Tool for building native NodeJS modules

What is "JSNut"? JSNut is a NodeJS NUTive module build tool. It is very easy to use: npx -- @dtkdtk/jsnut build -f ./src/module.cc -o ./dist/ --onlymod will compile module.cc file into a native NodeJS addon (module.node), which you can import into your JS code using require("./module.node")

API

  • command jsnut

Features

  • Auto-generation of package.json and binding.gyp files
  • Configurable output directory and output file name
  • sort_includes tool [experimental]

Usage

Type npx @dtkdtk/jsnut help to get command help.

Type npx -- @dtkdtk/jsnut build <-f filename> <-o outdir> [-n modname] [--clean|--onlymod] to compile C++ file

Example

[!IMPORTANT]
using namespace STH; in any file causes compilation errors. Please use STH::... instead.

src/module.cc

#include <nan.h>
#include <iostream>
#include "otherfile.hh"

#define MODULE_NAME some_code

void do_nothing(const Nan::FunctionCallbackInfo<v8::Value>& info)
{
    std::cout   << "nothing and "
                << otherfile_function()
                << std::endl;
}

void MODULE_INIT(v8::Local<v8::Object> exports)
{
	auto context = exports->GetCreationContextChecked();
	exports->Set(
		context,
		Nan::New("do_nothing").ToLocalChecked(),
		Nan::New<v8::FunctionTemplate>(do_nothing)->GetFunction(context).ToLocalChecked()
	);
}

NODE_MODULE(MODULE_NAME, MODULE_INIT)

src/otherfile.hh

int otherfile_function();

src/otherfile.cc

#include "otherfile.hh"
int otherfile_function()
{
    return 7;
}

./build-native-addons.bash

#!/usr/bin/env bash
#Warning: it calculates the path to the files from package.json
npx -- @dtkdtk/jsnut build  \
    -f "./src/module.cc"    \
    -f "./src/otherfile.hh" \
    -f "./src/otherfile.cc" \
    -o "./dist"             \
    --clean;
sleep 10s

Result: (file tree)

  • src/
    • otherfile.cc
    • otherfile.hh
    • module.cc
  • dist/
    • module.node (first specified source file name)
    • module.sln
    • module.vcxproj

About the '#include's sorting

[!WARNING]
This feature is experimental and uncompleted (and so unperfect). Use at your own risk

To sort, use the jsnut sort_includes <...src_dirs> command

#include directives are sorted as follows:

  1. Your project headers ("ui/app.h")
  2. Extern libraries (must contain / or .) (<node.h>)

[!NOTE]
Standard C headers are into this category

  1. Your libraries (must be in the "lib" directory) ("lib/api.h")
  2. Standart C++ headers (without .h extension) (<iostream>) There is an empty line between categories. The order was borrowed from Google, but with some changes.

[!WARNING]
The tool will remove any not-multiline comments in the '#include's block. Please put them rather (or later) than '#include' directives

Package Sidebar

Install

npm i @dtkdtk/jsnut

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

20.9 kB

Total Files

9

Last publish

Collaborators

  • dtkdtk0