egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
9.23k stars 218 forks source link

How to use a polyfill for node.js Buffer? #882

Open arunstan opened 1 year ago

arunstan commented 1 year ago

Hello,

I working on an app handling GIS shape files. To support shape files, I am using a package called shpjs which in turn uses jszip. Both of them depend on nodejs Buffer.

I am trying to use the node-stdlib-browser package to polyfill Buffer but have not been successful.

Please see my tsup config below:

import { defineConfig } from "tsup";
//@ts-ignore
import nodeStdLibBrowswerPlugin from "node-stdlib-browser/helpers/esbuild/plugin";
import { default as stdLibBrowser } from "node-stdlib-browser";

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

export default defineConfig({
  platform: "browser",
  esbuildOptions: (options, context) => {
    options.inject = [require.resolve("node-stdlib-browser/helpers/esbuild/shim")];
    options.define = {
      global: "global",
      process: "process",
      Buffer: "Buffer",
    };
  },
  esbuildPlugins: [nodeStdLibBrowswerPlugin(stdLibBrowser)],
  inject: [require.resolve("node-stdlib-browser/helpers/esbuild/shim")],
  define: {
    global: "global",
    process: "process",
    Buffer: "Buffer",
  },
});

But still I get an error in JSZip.

image

Am I setting up polyfill correctly?

Upvote & Fund

Fund with Polar

bucko13 commented 1 year ago

Just wanted to bump this as well. I'm getting an error on Buffer being undefined. I'm trying to bundle with tsup instead of babel and tsc previously which was working but Buffer isn't getting polyfilled unfortunately.

0xmaayan commented 8 months ago

having the same issue with Buffer being undefined