solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.62k stars 2.1k forks source link

Can't build app with vite.js #3337

Closed xrei closed 1 year ago

xrei commented 2 years ago
image

'toBigIntLE' is not exported by node_modules/bigint-buffer/dist/browser.js, imported by node_modules/@solana/buffer-layout-utils/lib/esm/bigint.mjs

mikemaccana commented 1 year ago

Hello!

  1. I think this can be closed, newer versions on SPL seem to use JS's native BigNumber (instead of the third - party BigInt library)
  2. This is how you can build spl or whatever using BigNumber using Vite.
// https://vitejs.dev/config/
import { defineConfig } from "vite";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import nodePolyfills from "rollup-plugin-node-polyfills";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { getGitVersion } from "./build-tools";

const GIT_VERSION = getGitVersion();

const log = console.log;

// Config is based on metaplex + vite example from:
// https://github.com/metaplex-foundation/js-examples/tree/main/getting-started-vite

// es2020 Needed for BigNumbers
// See https://github.com/sveltejs/kit/issues/859

const isMinifiying = Boolean(process.env.ZIP_FILE_NAME);

log(
  `Git version is: '${GIT_VERSION}', ${
    isMinifiying ? "producing minified build to send externally." : ""
  } `
);

export default defineConfig({
  plugins: [svelte()],
  resolve: {
    alias: {
      stream: "rollup-plugin-node-polyfills/polyfills/stream",
      events: "rollup-plugin-node-polyfills/polyfills/events",
      assert: "assert",
      crypto: "crypto-browserify",
      util: "util",
    },
  },
  define: {
    "process.env": process.env ?? {},
  },
  build: {
    target: "es2020",
    rollupOptions: {
      plugins: [nodePolyfills({ crypto: true })],
    },
    // From https://github.com/vitejs/vite/issues/9703#issuecomment-1216662109
    commonjsOptions: {
      include: [],
    },
    minify: isMinifiying,
    // We use tsc/rollup to build the service worker, so don't destroy that built file.
    emptyOutDir: false,
  },
  optimizeDeps: {
    // From https://github.com/vitejs/vite/issues/9703#issuecomment-1216662109
    disabled: false,
    esbuildOptions: {
      define: {
        global: "globalThis",
      },
      plugins: [NodeGlobalsPolyfillPlugin({ buffer: true })],
      target: "es2020",
    },
  },
});