Cant Instantiate Cesium Objects In Web Worker

I am having a problem here that it does not seem like others are running into. I am trying to use Cesium objects in a web worker for data parsing to avoid having the main thread hitch. Even the most simple web worker will immediately give me an error “Uncaught ReferenceError: global is not defined” as soon as any Cesium object is instantiated. For example this.

import { Cartesian3 } from ‘cesium’;

self.onmessage = (event: MessageEvent) => {

const testcoord: Cartesian3 = new Cartesian3;

self.postMessage(testcoord);

};

I believe this is some sort of configuration issue but I was hoping someone else ran into it. Any help would be appreciated.

As a followup. I tried making up a useless class and importing it into the worker.

export class PrimitiveEntity {
  billBoard = 0;
  label = 'test';  
}

I was able to instantiate with no issues. I added this

export class PrimitiveEntity {
  billBoard = 0;
  label = 'test';
  point: Cartesian3 = new Cartesian3;
}

And it throws that error. So its something with the way Cesium is configured, since I know its possible to use workers with it.

As a followup, it appears as though its blowing up in knockout.js which is being used by Cesium widgets.

OK I fixed it and the trick is to change the import to

from '@cesium/engine';

instead of just cesium. That loads in a widgetless version of the library. Hopefully somebody else finds this useful.

2 Likes