/emscripten-build

Build a C++ WebAssembly program inside your Node.js packaging workflow.

Primary LanguageJavaScriptMIT LicenseMIT

emscripten-build

e2e-test emscripten-build mit-license

Build your C++ WebAssembly project inside your Node.js build workflow.

This package uses the Emscripten SDK to compile your C++ to WebAssembly. It provides a command line and a JS interface that allows you to configure your builds and integrate your WASM project into your packaging scripts.

How to Use

This package assumes that you have a C/C++ project that can build with Emscripten via a Makefile. The recommended usage is to run builds with CMake and Ninja. Support also exists for GNU Make and Autotools.

Building can be made as simple as switching to your project directory and entering in the command line:

npx emscripten --configure --build

or invoking from JavaScript:

const emscripten = import('emscripten-build');

emscripten.configure()
    .then((em) => em.build())
    .then((em) => { /* ... */ });

Builds are configured by placing emscripten.settings.js in your project root. Here is a simple settings file for a CMake project:

module.exports = {
    myProject: {
        type: "cmake",

        configure: {
            path: "./src",
            generator: "Ninja",
            type: "Release",
            definitions: {
                DEFINE1: "Value1"
            }
        },

        build: {
            path: "./build"
        },

        install: {
            path: "./dist"
        }
    }
};

See the end of this document for detailed guides.

Install

npm install --save-dev emscripten-build

Before you install this package, you must install Python 3.6+ on your system. You may download it at python.org or your OS's package manager.

By default, the Emscripten SDK is installed into your node_modules tree. You may specify a custom path by modifying your NPM config via one of the commands below. Do this before you install the package:

Action Command
Save the path to your project .npmrc npm config --userconfig "/your/project/root/.npmrc" set emsdk "/your/absolute/custom/path"
Save the path to your user .npmrc npm config set emsdk "/your/absolute/custom/path"
Set an environment variable set NPM_CONFIG_EMSDK=/your/absolute/custom/path
Use a config argument to NPM temporarily npm [command] --emsdk="/your/absolute/custom/path"

The Emscripten SDK, CMake, and Ninja programs are installed as NPM dependencies.

See Also

For detailed information on how to use this package, see these guides:

Visit this repository to see different examples on how to use this package:

License

MIT License.