npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-cpp-loader

v0.1.4

Published

A Vite plugin that enables seamless integration of C++ code with WebAssembly

Readme

vite-cpp-loader

A Vite plugin that lets you import C++ files directly into your TypeScript/JavaScript code. Write your C++ functions and use them in your frontend code with full type safety and hot module reloading. Under the hood, it uses Emscripten to compile your C++ code to WebAssembly, but you don't need to worry about that - just import and use your C++ functions as if they were TypeScript functions.

Features

  • 🔄 Automatic C++ to WebAssembly compilation
  • 📝 TypeScript declaration generation
  • 🚀 Seamless integration with Vite
  • 🔌 Zero configuration setup
  • 🧩 Emscripten integration for C++ bindings
  • 📦 Automatic type inference from C++ to TypeScript

Prerequisites

Emscripten Installation

  1. macOS:

    # Using Homebrew
    brew install emscripten
       
    # Or using the official installer
    git clone https://github.com/emscripten-core/emsdk.git
    cd emsdk
    ./emsdk install latest
    ./emsdk activate latest
    source ./emsdk_env.sh
  2. Windows:

    # Using the official installer
    git clone https://github.com/emscripten-core/emsdk.git
    cd emsdk
    emsdk install latest
    emsdk activate latest
    emsdk_env.bat
  3. Linux:

    # Using the official installer
    git clone https://github.com/emscripten-core/emsdk.git
    cd emsdk
    ./emsdk install latest
    ./emsdk activate latest
    source ./emsdk_env.sh

Verify the installation:

emcc --version

VSCode Configuration

To get proper C++ IntelliSense in VSCode, create a .vscode/c_cpp_properties.json file in your project:

{
  "configurations": [
    {
      "name": "Emscripten",
      "includePath": [
        "${workspaceFolder}/**",
        "/usr/local/opt/emscripten/libexec/emscripten/system/include/**",
        "C:/emsdk/upstream/emscripten/system/include/**",
        "~/emsdk/upstream/emscripten/system/include/**",
        "/opt/homebrew/include/emscripten"
      ],
      "macFrameworkPath": [],
      "compilerPath": "/usr/local/opt/emscripten/libexec/emscripten/emcc",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}

Installation

npm install vite-cpp-loader --save-dev
# or
yarn add vite-cpp-loader --dev

Usage

  1. Add the plugin to your vite.config.ts:
import { defineConfig } from 'vite';
import cppLoader from 'vite-cpp-loader';

export default defineConfig({
  plugins: [
    cppLoader()
  ]
});
  1. Create a C++ file (e.g., math.cpp):
#include <emscripten/bind.h>
#include <string>

int add(int a, int b) {
    return a + b;
}

std::string greet(const std::string& name) {
    return "Hello, " + name + "!";
}

EMSCRIPTEN_BINDINGS(my_module) {
    emscripten::function("add", &add);
    emscripten::function("greet", &greet);
}
  1. Import and use in your TypeScript/JavaScript:

Note: Since WebAssembly module initialization is asynchronous, all imported C++ functions return Promises. You need to use await when calling them:

import { add, greet } from './math.cpp';

// Use async/await to handle the Promises
const result = await add(2, 3);
console.log(result); // 5

// Or use Promise chaining
greet("World").then(result => {
  console.log(result); // "Hello, World!"
});

How It Works

The plugin:

  1. Detects .cpp files in your project
  2. Extracts function declarations and their types
  3. Generates TypeScript declarations automatically
  4. Compiles C++ to WebAssembly using Emscripten
  5. Provides type-safe access to your C++ functions

Type Support

The plugin automatically converts C++ types to TypeScript types:

| C++ Type | TypeScript Type | |----------|-----------------| | int | number | | float | number | | double | number | | bool | boolean | | string | string | | void | void | | char | string | | unsigned | number | | long | number | | short | number |

Note: Only simple types can be used as function return types. Complex types (like STL containers, custom classes, etc.) are not currently supported.

Generated TypeScript Declarations

For each C++ module, a TypeScript declaration file (.d.ts) is automatically generated in the same directory as the C++ file. For example, if you have math.cpp, a math.cpp.d.ts file will be generated. These files contain the type definitions that enable TypeScript type checking and IntelliSense.

Important: These generated declaration files should not be modified manually as they are automatically generated and will be overwritten. It's recommended to add them to your .gitignore file:

# Generated TypeScript declarations for C++ modules
*.cpp.d.ts

Troubleshooting

Common Issues

  1. Emscripten not found:

    • Make sure Emscripten is properly installed and in your PATH
    • Try running emcc --version to verify the installation
    • On Windows, you might need to restart your terminal after installation
  2. Missing include files:

    • Verify your VSCode configuration has the correct include paths
    • Make sure Emscripten is properly installed
    • Try running emcc -v to see the include paths Emscripten is using
  3. Compilation errors:

    • Check that your C++ code is valid
    • Make sure you're using the correct Emscripten bindings syntax
    • Verify that all required headers are included

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT