ptato
ptatoโ€ข5mo ago

How to polyfill node imports when bundling with the deno version of esbuild?

Hello, I am trying to run esbuild using https://deno.land/x/esbuild to bundle a Deno project into a single file. I am using the denoPlugins from https://deno.land/x/esbuild_deno_loader to resolve npm imports, but I need all node std lib imports to be polyfilled as well (which the plugin doesn't do because it assumes youre running the bundle using Deno). My intended compile target is cloudflare workers so i need the node imports to be polyfilled. I tried using https://www.npmjs.com/package/esbuild-plugin-polyfill-node alongside the denoPlugins but I get the error Cannot find package '@jspm/core'. this is my general setup:
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js";
import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.5/mod.ts";
import { polyfillNode } from "npm:@rivet-gg/esbuild-plugin-polyfill-node@^0.4.0";

await esbuild.build({
entryPoints: [join(project.path, "_gen", "entrypoint.ts")],
outfile: join(project.path, "_gen", "/output.js"),
format: "esm",
platform: "neutral",
plugins: [
...denoPlugins(),
polyfillNode(),
],
external: ["*.wasm", "*.wasm?module"],
bundle: true,
// minify: true,
});

await esbuild.stop();
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js";
import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.5/mod.ts";
import { polyfillNode } from "npm:@rivet-gg/esbuild-plugin-polyfill-node@^0.4.0";

await esbuild.build({
entryPoints: [join(project.path, "_gen", "entrypoint.ts")],
outfile: join(project.path, "_gen", "/output.js"),
format: "esm",
platform: "neutral",
plugins: [
...denoPlugins(),
polyfillNode(),
],
external: ["*.wasm", "*.wasm?module"],
bundle: true,
// minify: true,
});

await esbuild.stop();
npm
esbuild-plugin-polyfill-node
ESBuild plugin for polyfilling Node.js builtins for edge and Deno. Latest version: 0.3.0, last published: 9 months ago. Start using esbuild-plugin-polyfill-node in your project by running npm i esbuild-plugin-polyfill-node. There are 17 other projects in the npm registry using esbuild-plugin-polyfill-node.
3 Replies
ptato
ptatoโ€ข5mo ago
it looks like cf workers do allow some nodejs interoperability, i will try this and see if it works https://developers.cloudflare.com/workers/runtime-apis/nodejs/
Cloudflare Docs
Node.js compatibility ยท Cloudflare Workers docs
Implemented Node.js runtime APIs and enablement instructions for your Worker project.
Deno AI Helper
Deno AI Helperโ€ข5mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided in your message, I noticed that you are trying to import polyfillNode from an npm package using an import specifier that looks like it's intended for use with Node.js or a bundler that supports npm-style imports (npm:@rivet-gg/esbuild-plugin-polyfill-node@^0.4.0). This kind of import specifier is not natively supported in Deno. To fix this issue, you would typically need to import the module using a URL that points to a server that can serve the npm package as an ES module. However, since Deno is designed to work without Node.js-specific APIs and modules, using Node.js polyfills might not be the best approach if you're targeting Deno or environments like Cloudflare Workers. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. If you're targeting Cloudflare Workers, you might want to look for solutions that are specifically designed for that environment, as it has its own set of global APIs and limitations.
Deno AI Helper
Deno AI Helperโ€ข5mo ago
๐Ÿฆ• Oops! Can you pinpoint where I went wrong to help me improve? ๐Ÿ“ˆ