Created
November 5, 2025 11:50
-
-
Save nikhaldi/d9093151262e7722f11a48edffa732b4 to your computer and use it in GitHub Desktop.
Vite plugin to make importing the nodehun module work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import path from "path" | |
| const NODEHUN_MODULE_NAME = "nodehun" | |
| // Vite plugin that makes the nodehun module work, at least with vitest | |
| // Inspired by https://github.com/prosopo/captcha/blob/a6ea0bec1ad356b0e5378257dd36e16552b51f9e/dev/config/src/vite/NodejsPolarsNativeFilePlugin.ts | |
| // See also for explanation: https://prosopo.io/articles/vite-node-files | |
| export const nodehunNativePlugin = () => { | |
| const name = "nodehun-native-plugin" | |
| return { | |
| name, | |
| resolveId(source: string, importer: string | undefined, options: any) { | |
| // return the id if this plugin can resolve the import | |
| if (path.basename(source) === NODEHUN_MODULE_NAME) { | |
| return source | |
| } | |
| return null // otherwise return null indicating that this plugin can't handle the import | |
| }, | |
| transform(code: string, id: string) { | |
| // rewrite the code to import the .node file | |
| if (path.basename(id) === NODEHUN_MODULE_NAME) { | |
| // https://stackoverflow.com/questions/66378682/nodejs-loading-es-modules-and-native-addons-in-the-same-project | |
| // this makes the .node file load at runtime from an esm context. .node files aren't native to esm, | |
| // so we have to create a custom require function to load them. The custom require function is | |
| // equivalent to the require function in commonjs, thus allowing the .node file to be loaded. | |
| // See also https://github.com/Wulf/nodehun/issues/104#issuecomment-1186847432 | |
| return ` | |
| // create a custom require function to load .node files | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| import { createRequire } from 'module'; | |
| const customRequire = createRequire(import.meta.url) | |
| // load the .node file directly from within node_modules | |
| let nodeModulePath = path.dirname("${id}"); | |
| // NB: For some weird reason we can't use path.join | |
| while (nodeModulePath && nodeModulePath !== "." && nodeModulePath !== "/" && !fs.existsSync(nodeModulePath + "/node_modules")) { | |
| nodeModulePath = path.dirname(nodeModulePath); | |
| } | |
| if (!nodeModulePath || nodeModulePath === "." || nodeModulePath === "/") { | |
| throw new Error("Could not find node_modules directory for nodehun"); | |
| } | |
| const content = customRequire(nodeModulePath + "/node_modules/nodehun/build/Release/Nodehun.node") | |
| export default { Nodehun: content } | |
| ` | |
| } | |
| return code | |
| }, | |
| load(id: string) { | |
| if (path.basename(id) === NODEHUN_MODULE_NAME) { | |
| // whenever we encounter an import of the .node file, we return an empty string. This makes it | |
| // look like the .node file is empty to the bundler. This is because we're going to copy the | |
| // .node file to the output directory ourselves, so we don't want the bundler to include it in | |
| // the output bundle (also because the bundler can't handle .node files, it tries to read them | |
| // as js and then complains that it's invalid js) | |
| const newCode = `` | |
| return newCode | |
| } | |
| return null | |
| }, | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { defineConfig } from "vitest/config" | |
| import react from "@vitejs/plugin-react" | |
| import { nodehunNativePlugin } from "./nodehunNativePlugin" | |
| export default defineConfig({ | |
| plugins: [react(), nodehunNativePlugin()], | |
| test: {...} | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment