#BUFFER IS NOT DEFINED, in built app

1 messages · Page 1 of 1 (latest)

worthy belfry
#

@vagrant hull have you solve this Buffer issue?

vagrant hull
#

@worthy belfry

#

Yes

#
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'


// https://vitejs.dev/config/
export default defineConfig({
  plugins: [svelte()],
  optimizeDeps: {
    esbuildOptions: {
        // Node.js global to browser globalThis
        define: {
            global: 'globalThis',
        },
        // Enable esbuild polyfill plugins
        plugins: [
          NodeGlobalsPolyfillPlugin({
            buffer: true,
          }),
        ]
    }
  },
  build: {
        rollupOptions: {
            plugins: [inject({ Buffer: ['buffer', 'Buffer']}),

    
    ],
        },

    }
})```
#

npm i buffer

#

Then in whichever file I used Buffer:

#

import { Buffer } from 'buffer'

vagrant hull
#

(The vite config is missing the rollup inject plugin import statement)

worthy belfry
#

import { inject } from '@rollup/plugin-inject';

#

?

vagrant hull
#

npm install @rollup/plugin-inject --save-dev

#

import inject from '@rollup/plugin-inject';

worthy belfry
#

hmmmn, interesting. this one is working in local dev but not in production build

import inject from '@rollup/plugin-inject';
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import { defineConfig } from 'vite';
import { qwikVite } from '@builder.io/qwik/optimizer';
import { qwikCity } from '@builder.io/qwik-city/vite';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig(() => {
  return {
    rollupOptions: {
      plugins: [
        inject({ Buffer: ['buffer', 'Buffer']}),
      ],
    },
    plugins: [
      qwikCity(),
      qwikVite(),
      tsconfigPaths(),
    ],
    preview: {
      headers: {
        'Cache-Control': 'public, max-age=600',
      },
    },
    optimizeDeps: {
      esbuildOptions: {
        // Node.js global to browser globalThis
        define: {
            global: 'globalThis',
        },
        // Enable esbuild polyfill plugins
        plugins: [
          NodeGlobalsPolyfillPlugin({
            buffer: true,
          }),
        ]
    }
    },
  };
});

#

@can you send also your tsconfig.json?

worthy belfry
deft gorge
worthy belfry
#

Yup. Thank you so much for your insights