Files
tunarr/server/esbuild/native-node-module.ts
Christian Benincasa 5570631adc Packaging v0: Build and run server in a docker container (#139)
* This is a nightmare

* Checkpointing.... getting closer

* First cut - packaging the server in a docker container

* Remove busted bundles

* Minify build

* Some common commands for building - we're going to look into proper monorepo solutions soon

* Remove dependency on serve-static

* Add web serving, full-stack docker target, and Nvidia container support

* Remove test action graph for now
2024-03-05 13:13:26 -05:00

58 lines
1.9 KiB
TypeScript

import path from 'path';
import { Plugin } from 'esbuild';
// Copied from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
export const nativeNodeModulesPlugin = (): 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) => {
const resolvedId = require.resolve(args.path, {
paths: [args.resolveDir],
});
if (resolvedId.endsWith('.node')) {
return {
path: resolvedId,
namespace: 'node-file',
};
}
return {
path: resolvedId,
};
});
// 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) => {
return {
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`,
resolveDir: path.dirname(args.path),
};
});
// 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',
}),
);
// Tell esbuild's default loading behavior to use the "file" loader for
// these ".node" files.
const opts = build.initialOptions;
opts.loader = opts.loader || {};
opts.loader['.node'] = 'file';
},
};
};