dfinity / examples

Example applications, microservices, and code samples for the Internet Computer
https://dfinity.org
Apache License 2.0
508 stars 324 forks source link

Add NodeGlobalsPolyfillPlugin to vite config on sveltekit example #865

Closed ocluf closed 1 week ago

ocluf commented 1 month ago

Currently the sveltekit example uses the inject plugin to add the Buffer module in vite.config.ts.

 build: {
    target: "esnext",
    rollupOptions: {
      // Polyfill Buffer for production build
      plugins: [
        inject({
          modules: { Buffer: ["buffer", "Buffer"] },
        }),
      ],
    },
  }, 

This makes sure important packages work when building a project, but not when the project is in dev mode. I got it working there by adding

import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";

// rest of the config 

  optimizeDeps: {
    esbuildOptions: {
      // Node.js global to browser globalThis
      define: {
        global: "globalThis",
      },
      plugins: [
        NodeModulesPolyfillPlugin(), //added this
        NodeGlobalsPolyfillPlugin({
          buffer: true,
        }),
      ],
    },
  }, 
github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 week ago

This issue was closed because it has been inactive for 14 days since being marked as stale.