-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(remix-dev): add .node
file support for Node based platforms
#3263
Changes from all commits
239d0da
3d09010
33325c3
cfe2a7a
bfa3f4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
- axel-habermaier | ||
- BasixKOR | ||
- BenMcH | ||
- benwis | ||
- bmarvinb | ||
- bmontalvo | ||
- bogas04 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
title: Native Node Modules | ||
toc: false | ||
--- | ||
|
||
# Native Node Modules | ||
|
||
Some node packages contain a binary `.node` file that contains code that is usually written in another language and that interacts with Node through the [Node-API](https://nodejs.org/api/n-api.html) foreign function interface. This can offer substantial benefits in speed, easy concurrency/multithreading, access to native interfaces, and/or interoperability with other languages. A popular example is the `Sharp` image processing library, which uses `libvips`, a C library, to increase performance. | ||
|
||
## Limitations | ||
|
||
Because these files use the Node FFI, they will only run on servers or edge environments that run Node. | ||
|
||
The browser does not run Node, so their use is limited to Loaders and Actions, or other server side code. | ||
|
||
# Setup | ||
|
||
Remix should work with these files out of the box, but you'll want to export the package that contains them from a `.server.{ts,js}` file and then import them from that file in your loaders and actions. This prevents them from being included in the browser bundle and causing errors. | ||
|
||
## Creating Native Node Modules | ||
|
||
If you're interested in creating a package with Node modules, look for a package that interacts with the Node API in your language of choice. | ||
Here are some examples: | ||
|
||
*Rust:* | ||
- [napi-rs](https://napi.rs/) | ||
- [neon](https://neon-bindings.com/) | ||
|
||
*C++* | ||
- [Node Addon API](https://github.com/nodejs/node-addon-api). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type esbuild from "esbuild"; | ||
import * as path from "path"; | ||
// See https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 | ||
export function nativeNodeModulesPlugin(): esbuild.Plugin { | ||
return { | ||
name: 'native-node-modules', | ||
setup(build) { | ||
// If a ".node" file is imported within a module in the "file" namespace, resolve | ||
// it to an absolute path and put it into the "node-file" virtual namespace. | ||
build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({ | ||
path: require.resolve(args.path, { paths: [args.resolveDir] }), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
namespace: 'node-file', | ||
})) | ||
|
||
// Files in the "node-file" virtual namespace call "require()" on the | ||
// path from esbuild of the ".node" file in the output directory. | ||
build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({ | ||
contents: ` | ||
import * as path from "path"; | ||
import modulePath from ${JSON.stringify(args.path)} | ||
|
||
// Esbuild gives a package path, so we want to convert to an absolute one | ||
let projectRoot = "${path.resolve()}"; | ||
let absolutePath = path.join(projectRoot, modulePath) | ||
|
||
try { module.exports = require(absolutePath) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will break node ESM. Is it possible to load via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trouble is getting the absolute path and then loading it. I can't use await import() because I can't have top level await. Unless there's a way to get the absolute path and the compiled path without importing it, I don't see how I resolve this |
||
catch (error){ | ||
console.error(error) | ||
} | ||
`, | ||
})) | ||
|
||
// If a ".node" file is imported within a module in the "node-file" namespace, put | ||
// it in the "file" namespace where esbuild's default loading behavior will handle | ||
// it. It is already an absolute path since we resolved it to one above. | ||
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, args => ({ | ||
path: args.path, | ||
namespace: 'file', | ||
})) | ||
}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use
path.dirname()
instead of split. This will break on windows machines.