esbuild-plugin-require-resolve
v0.2.0
Published
Find any `require.resolve` calls in your bundle, copy the resolved files to the output directory, and rewrite the import paths to be relative to the output file.
Readme
esbuild-plugin-require-resolve
Find any require.resolve calls in your bundle, copy the resolved files to the output directory, and rewrite the import paths to be relative to the output file.
This uses esbuild-extra to support chained transforms. (Credit to @chialab/esbuild-plugin-require-resolve for the original implementation.)
pnpm add esbuild-plugin-require-resolveUsage
import requireResolvePlugin from 'esbuild-plugin-require-resolve'
import esbuild from 'esbuild'
await esbuild.build({
plugins: [requireResolvePlugin()],
})Handling Native Node Modules (.node files)
The plugin automatically detects require() calls that target .node files (native Node.js addons). It performs the following actions:
- Copies the referenced
.nodefile to the esbuild output directory. - Rewrites the
require()call in your bundled code to correctly point to the copied file's new location relative to the output file.
This ensures that native modules used by your project are included in the build output and can be loaded correctly at runtime.
Automatic Dependency Copying for .node Files (macOS)
When a .node file is processed on macOS, the plugin goes a step further:
- It uses the
otool -Lcommand to inspect the.nodefile and identify any linked shared libraries (dependencies) that use@loader_path. - It attempts to locate these dependent libraries (e.g.,
.dylibfiles) in common paths like/opt/homebrew/libor paths specified in theDYLD_LIBRARY_PATHenvironment variable. - Found dependencies are also copied to the esbuild output directory, ensuring the native addon can find its own required libraries at runtime.
Note: This automatic dependency discovery currently relies on the otool command and is specific to macOS.
