Vite build fails with import failure, but works on dev

Viewed 23

Running my Vite application in dev mode works perfectly, but building for production throws the below error from @solana/web3.js/lib/index.browser.esm.js:

4: import BN from 'bn.js';
5: import bs58 from 'bs58';
6: import { serialize, deserialize, deserializeUnchecked } from 'borsh';
            ^
7: import * as BufferLayout from '@solana/buffer-layout';
8: import { coerce, instance, string, tuple, literal, unknown, union, type, optional, any, number, array, nullable, create, boolean, record, assert as assert$7 } from 'superstruct';
error during build:
Error: 'serialize' is not exported by node_modules/@solana/web3.js/node_modules/borsh/lib/index.js

Inspecting borsh/lib/index.js shows that it does in fact export serialize:

/// Serialize given object using schema of the form:
/// { class_name -> [ [field_name, field_type], .. ], .. }
function serialize(schema, obj) {
    const writer = new BinaryWriter();
    serializeStruct(schema, obj, writer);
    return writer.toArray();
}
exports.serialize = serialize;

This is my Vite config:

import inject from '@rollup/plugin-inject'

{
  plugins: [
    vue(),
    inject({
      Buffer: ['buffer', 'Buffer']
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@ui': path.resolve(__dirname, './src/components/ui')
    },
  },
  define: {
    '__COMMIT_HASH__': JSON.stringify(commitHash),
    'process.env.NODE_DEBUG': JSON.stringify("")
  },
  build: {
    sourcemap: true,
    target: 'es2020',
  },
  optimizeDeps: {
    esbuildOptions: {
      target: 'es2020',
      define: {
        global: 'globalThis',
      },
    },
    include: [
      'buffer',
    ],
  },
  server: {
    port: 3000
  },
}

Version information:

"vite": "^3.1.1"
"@rollup/plugin-inject": "^4.0.4"
"@solana/web3.js": "^1.30.2"
"npm": ">=7.0.0"
"node": ">=16.0.0"
"borsh": "^0.7.0"
0 Answers
Related