Skip to content

Commit

Permalink
Properly setup node file imports
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Nov 9, 2024
1 parent 55bac1c commit 80bab9c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dev-packages/native-esbuild-plugin/src/native-esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,41 @@ class PluginImpl implements Plugin {
copyNodePtySpawnHelper(outdir);
}
});
this.setupNodeRequires(build);
}

private setupNodeRequires(build: PluginBuild): void {
// By default, ESBuild does not handle `.node` files. We need to handle them ourselves.
// When using the `file` loader directly, the files only get exposed via their paths.
// However, we want to load them directly as native modules via `require`.
build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => {
try {
// Move the resolved path to the `node-file` namespace to load it as a native module.
const resolved = require.resolve(args.path, { paths: [args.resolveDir] });
return {
path: resolved,
namespace: 'node-file',
};
} catch {
// If the module cannot be resolved, mark it as external.
return {
external: true
};
}
});
build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({
// Replace the require statement with a direct require call to the native module.
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch { throw new Error('Could not load native module from "${args.path}"') }
`,
}));
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, args => ({
// Finally, resolve the `.node` file to the local path.
path: args.path,
namespace: 'file',
}));
}
}

Expand Down

0 comments on commit 80bab9c

Please sign in to comment.