Fixing cesiumWorkerBootstrapper when using Electron+React+Webpack

221 views
Skip to first unread message

satten...@datalive.com.au

unread,
Feb 5, 2018, 1:32:30 AM2/5/18
to cesium-dev
When using Cesium in React via Webpack in Electron with the BrowserWindow option nodeIntegrationInWorker: true, Cesium throws exceptions in cesiumWorkerBootstrapper as require() becomes the nodejs implementation, not the requirejs implementation (assuming I'm interpreting the errors and execution correctly).

Unfortunately the example Electron app shown in the blog post below didn't have the issue so I'm assuming it's something more like the combination of React and/or Webpack in combination with Electron.

https://cesium.com/blog/2016/04/04/an-introduction-to-cesium-desktop-apps-with-electron/

I need to run my own web worker code with node integration in Electron, but setting the value causes Cesium to fail in our setup (which is obviously complicated). We're using npm: ces...@1.42.1, Win10 x64, Electron 1.8.1 (Chrome 59.0.3071.115).

The fix was to simply set the following line near the start of the minified cesiumWorkerBootstrapper.js from:

var requirejs, require, define;

to

var requirejs = undefined, require = undefined, define = undefined;

Whilst the original code probably exists to avoid multiple redefines of require, in cesium's web worker context it should be fine to explicitly reset the variables to undefined as they're isolated, thus fixing the issue here and not affecting existing consumers.

If I'm on the right track, any chance this could be changed upstream? If I've grossly misinterpreted the situation, please let me know what ramifications this may have :)

Gabby Getz

unread,
Feb 6, 2018, 10:26:05 AM2/6/18
to cesium-dev
Hi there,

Have you seen to Cesium and Webpack tutorial? We walk through getting Cesium up and running with webpack and the recommended configuration.

In particular specifying this in your webpack configuration object should "fix" how requireJS is being built with webpack in Cesium:

amd: {
  // Enable webpack-friendly use of require in Cesium
  toUrlUndefined: true
},
Enter code here...

Thanks, hopefully that helps!
Gabby

satten...@datalive.com.au

unread,
Feb 6, 2018, 10:00:03 PM2/6/18
to cesium-dev
Hi Gabby,

Yep followed everything in that tutorial and I double-checked my webpack config just in case. It definitely didn't work without the config shown in that tutorial so the current issue is something else unfortunately.

Cheers,
Stuart

Gabby Getz

unread,
Feb 7, 2018, 5:00:04 PM2/7/18
to cesium-dev
OK, if you have a fix and want to open a pull request with your changes, we'd be happy to take a look at them!

Thanks,
Gabby

Stuart Attenborrow

unread,
Feb 7, 2018, 5:57:29 PM2/7/18
to cesiu...@googlegroups.com
Thanks Gabby,

I'll try to isolate the exact issue so there is a clearer bug report / pull request for review.

Cheers,
Stuart


Stuart Attenborrow BComp MCP
Software Engineer
T (03) 6323 0029

DATALIVE SOFTWARE PTY LTD
1st Floor, 7-11 The Quadrant (PO Box 388)
Launceston, Tasmania 7250

--
You received this message because you are subscribed to a topic in the Google Groups "cesium-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cesium-dev/jZ8s9UOVKBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cesium-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

william....@gmail.com

unread,
Jul 17, 2018, 10:38:46 AM7/17/18
to cesium-dev
On Wednesday, February 7, 2018 at 3:57:29 PM UTC-7, Stuart Attenborrow wrote:
> Thanks Gabby,
>
>
> I'll try to isolate the exact issue so there is a clearer bug report / pull request for review.
>
>
> Cheers,
> Stuart
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Stuart Attenborrow BComp MCP
> Software Engineer
> T (03) 6323 0029
> E stu...@datalive.com.au
>  linkedin.com/in/stuartattenborrow
>
>
>
> DATALIVE SOFTWARE PTY LTD
> 1st Floor, 7-11 The Quadrant (PO Box 388)
> Launceston, Tasmania 7250
> www.datalive.com.au
>
>
> On 8 February 2018 at 09:00, Gabby Getz <gget...@gmail.com> wrote:
>
> OK, if you have a fix and want to open a pull request with your changes, we'd be happy to take a look at them!
>
>
> Thanks,
> Gabby
>
>
>
> On Tuesday, February 6, 2018 at 10:00:03 PM UTC-5, satten...@datalive.com.au wrote:Hi Gabby,
> Yep followed everything in that tutorial and I double-checked my webpack config just in case. It definitely didn't work without the config shown in that tutorial so the current issue is something else unfortunately.
> Cheers,
> Stuart
>
>
>
>
>
>
>
> --
>
> You received this message because you are subscribed to a topic in the Google Groups "cesium-dev" group.
>
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/cesium-dev/jZ8s9UOVKBM/unsubscribe.
>
> To unsubscribe from this group and all its topics, send an email to cesium-dev+...@googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

We are running into the same issue with cesiumWorkerBootstrapper and Electron with nodeIntegrationInWorker: true, however, we are not using Webpack, but rather the minified build output at node_modules/cesium/Build/Cesium/Cesium.js. We can apply the fix in an ad-hoc fashion as part of our build (hacky). I could not find a PR resulting from this discussion. Is cesiumWorkerBootstrapper ever intended to run in a node context rather than a browser context?

Gabby Getz

unread,
Jul 18, 2018, 11:52:09 AM7/18/18
to cesium-dev
No, the Cesium's use of web workers were designed for use in a browser. Processes that use web workers, like creating geometry, normally have a "asynchronous" option that you can set to false, which allows them to run as part of the main thread without attempting to spin up a we worker.

william....@gmail.com

unread,
Jul 18, 2018, 8:04:17 PM7/18/18
to cesium-dev
I'm not familiar with how that file is generated, but the issue is that requirejs (or whatever sets up requirejs) does not replace functions like 'require' if they already exist (the node context's 'require' function already exists). In cesiumWorkerBootstrapper's case it should, because nothing but Cesium code is running within Cesium's own web worker.

In our app we simply turned off 'nodeIntegrationInWorker' in Electron (because what we were using it for is running into a separate Electron bug) and it works. However, I expect we will want it back on as soon as Electron fixes their issue.

Gabby Getz

unread,
Jul 20, 2018, 1:30:30 PM7/20/18
to cesium-dev
I'm confused what the setup is here exactly. People have had success running Cesium in Electron with and without webpack. Can you provide your configuration and what Cesium files you are including?

William Wall

unread,
Jul 20, 2018, 2:36:25 PM7/20/18
to cesiu...@googlegroups.com
This doesn't necessarily have anything to do with Electron. However, using nodeIntegrationInWorker: true will provide a nodejs context within the Web Worker, meaning that 'require()` and other node items are already present on the global 'self' object when cesiumWorkerBootstrapper.js loads.

Since cesiumWorkerBootstrapper.js only ever needs the browser context, it shouldn't be so careful about replacing functions that already exist. It should just overwrite them and set itself up. This would be a bad idea in the main thread, but in a worker which has nothing running alongside it, it should be fine.

Ideally, Electron would also provide a way to select which workers get node integration, rather than just blanket providing it for everything.
Reply all
Reply to author
Forward
0 new messages