gosling-lang/gosling.js

Bam data fetcher issues in development build

etowahadams opened this issue · 0 comments

The BAM data fetcher is broken in the development server, but works production. It stopped working after #977, when major parts of the build were updated.

The Issue
The BAM data fetcher relies on a worker uses gmod/bam which uses Buffer. However, Buffer is undefined in the worker.

Previously, this was solved using this config plugin (in vite.config.js)

const bundleWebWorker = {
    name: 'bundle-web-worker',
    apply: 'serve', // plugin only applied with dev-server
    async transform(_, id) {
        if (/\?worker_file$/.test(id)) {
            // just use esbuild to bundle the worker dependencies
            const bundle = await esbuild.build({
                entryPoints: [id],
                inject: ['./src/alias/buffer-shim.js'],
                format: 'iife',
                bundle: true,
                write: false
            });
            if (bundle.outputFiles.length !== 1) {
                throw new Error('Worker must be a single module.');
            }
            return bundle.outputFiles[0].text;
        }
    }
};

However, this solution no longer works as I now see a Buffer not defined error.

ReferenceError: Buffer is not defined [@gmod_bam.js:8097:39](http://localhost:5173/node_modules/.vite/deps/@gmod_bam.js?v=81069ddd)

Attempts at solving;

I tried upgrading to gmod/bam@1.1.18 to gmod/bam@2.0 since 2.0 has explicit buffer imports. However, this did not solve the problem either. Interestingly, the other workers that use various gmod libraries don't seem to have problems.