Search code examples
typescriptviteweb-workerrollupjsgeotiff

Rollup/Vite build error when including GeoTiff.js and Web Worker in build


I am using a Web Worker to do calculation on a GeoTiff file. I call the function getRidgePoints from CalculateRidge.ts, and this function creates a worker, posts the message to the worker, and then awaits a result from the worker, and finally, terminates the worker after the result is gotten and returns the result. The worker is being wrapped in a "await"-function (i have heard about Comlink, but i found this solution and it works great).

The worker has an addEventListener that executes the function CreateRidge_Init that will do the calculations. This function loads a GeoTiff file, reads it and does calculation based on values read from this file.

My problem is:

This code wont build. I get the following error when trying to build the code:

Error [RollupError]: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
    at error (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:2125:30)
    at validateOptionsForMultiChunkOutput (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:17493:16)
    at Bundle.generate (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:17384:17)
    at async file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:25683:27
    at async catchUnfinishedHookActions (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:24770:20)
    at async serialBundleWorkerEntry (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/vite/dist/node/chunks/dep-d305c21f.js:22316:61) {
  code: 'PLUGIN_ERROR',
  url: 'https://rollupjs.org/configuration-options/#output-format',
  pluginCode: 'INVALID_OPTION',
  plugin: 'vite:worker',
  hook: 'transform',
  id: 'C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/src/lib/Functions/CalculateRidgeWorker.ts?worker&url',
  watchFiles: [
    ... basicly all the files used in this project ...
]

I have tested different things, and i notice that it is the combination of using both GeoTiff.js (fromArrayBuffer() and .getImage()) and a Web Worker that causes the error:

  • If i bypass the Web Worker and execute CreateRidge_Init on the main thread, the build works.
  • If i do not use fromArrayBuffer() and .getImage() in the Web Worker, the build works.

I have tried to play around with the build.rollupOptions settings, but with no error change in the build. I want to add, that the code works perfectly fine in development.

CalculateRidge.ts:

import workerUrl from "src/lib/Functions/CalculateRidgeWorker?worker&url";
import type { Pos, Point } from "../Stores";

// The function calling the worker.
export async function getRidgePoints(pos: Pos) {

  // Create a worker
  const worker = new Worker(workerUrl, { type: 'module' })

  // Send a value to the worker.
  worker.postMessage(pos)

  // Wait for the worker to process the sent value and recieve the calculated value.
  const result = await runWorker(worker);

  // Terminate the worker.
  worker.terminate();

  return result
}

// An await function that will wait for a message from the worker
function runWorker(worker: Worker): Promise<Point[]> {
  return new Promise(resolve => {
    worker.onmessage = (e:MessageEvent<Point[]>) => {
      resolve(e.data)
    }
  })
}

CalculateRidgeWorker.ts:

addEventListener('message', async (e:MessageEvent<Pos>) => {
  let pos = e.data;
  let points = await getRidgePoints_Init(pos)
  postMessage(points)
})

// If i bypass the web worker, and execute this function on
// the main thread, the code does build succesfully.
async function getRidgePoints_Init(pos_m: Pos) {

  const DSM_Base = new URL(import.meta.url)
  const DSM_25M = new URL('FO_DSM_2017_FOTM_25M_DEFLATE_UInt16.tif', DSM_Base.origin);
  const DSM_5M = new URL('FO_DSM_2017_FOTM_5M_DEFLATE_UInt16.tif', DSM_Base.origin);

  // Load the 25M resolution map.
  let image_25M = await fetch(url)
    .then(response  => response.arrayBuffer())
    .then(tiff      => fromArrayBuffer(tiff)) // If i comment out these two lines when using a
    .then(result    => result.getImage())     // web worker, the code does build succesfully.

  ... Does alot of calculations based on this file ...

  return result
}

My current vite.config.ts file:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  base: "/solar-analysis-faroe-island/",
  plugins: [svelte()],

  build: {
    rollupOptions: {
      output: {
        format: "esm",
        inlineDynamicImports: true,
      }
    }
  },

  resolve: {
    alias: {
      src: path.resolve('src/'),
    }
  },
})

Solution

  • I tried to change the output format in the rollup options, but i was supposed to change the format for the worker itself (i did not know there was a .worker option). This vite.config.js file solved the error, and now the project is succesfully building and working:

    import { defineConfig } from 'vite'
    import { svelte } from '@sveltejs/vite-plugin-svelte'
    import path, { resolve } from 'path';
    
    // https://vitejs.dev/config/
    export default defineConfig({
      base: "/solar-analysis-faroe-island/",
      plugins: [svelte()],
    
      worker: {
        format: "es"
      },
    
      resolve: {
        alias: {
          src: path.resolve('src/'),
        }
      },
    
    })