Created
July 7, 2023 07:33
-
-
Save redsuperbat/6d5a6b4395093e103234f63134939ca0 to your computer and use it in GitHub Desktop.
This file contains 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 esbuild from "esbuild"; | |
/** | |
* @type {import("esbuild").Plugin} | |
*/ | |
const nativeNodeModulesPlugin = { | |
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] }), | |
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 path from ${JSON.stringify(args.path)} | |
try { module.exports = require(path) } | |
catch {} | |
`, | |
})); | |
// 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"; | |
}, | |
}; | |
await esbuild.build({ | |
entryPoints: ["src/app.ts"], | |
outfile: "dist/app.js", | |
target: "ESNext", | |
minify: true, | |
bundle: true, | |
loader: { ".ts": "ts" }, | |
platform: "node", | |
external: ["sharp"], | |
format: "esm", | |
plugins: [nativeNodeModulesPlugin], | |
banner: { | |
js: ` | |
// BANNER START | |
const require = (await import("node:module")).createRequire(import.meta.url); | |
const __filename = (await import("node:url")).fileURLToPath(import.meta.url); | |
const __dirname = (await import("node:path")).dirname(__filename); | |
// BANNER END | |
`, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment